Remove the useless opnfv_tests dir in tests
[functest-xtesting.git] / functest / tests / unit / openstack / refstack_client / test_refstack_client.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8 import logging
9 import mock
10 import os
11 import unittest
12
13 from functest.core import testcase
14 from functest.opnfv_tests.openstack.refstack_client import refstack_client
15 from functest.utils.constants import CONST
16
17
18 class OSRefstackClientTesting(unittest.TestCase):
19
20     logging.disable(logging.CRITICAL)
21     _config = \
22         os.path.join(CONST.dir_functest_test, CONST.refstack_tempest_conf_path)
23     _testlist = \
24         os.path.join(CONST.dir_functest_test, CONST.refstack_defcore_list)
25
26     def setUp(self):
27         self.defaultargs = {'config': self._config,
28                             'testlist': self._testlist}
29         self.refstackclient = refstack_client.RefstackClient()
30
31     def test_source_venv(self):
32         CONST.dir_refstack_client = 'test_repo_dir'
33         with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
34                         'refstack_client.ft_utils.execute_command') as m:
35             cmd = ("cd {0};"
36                    ". .venv/bin/activate;"
37                    "cd -;".format(CONST.dir_refstack_client))
38             self.refstackclient.source_venv()
39             m.assert_any_call(cmd)
40
41     def test_run_defcore(self):
42         config = 'tempest.conf'
43         testlist = 'testlist'
44         with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
45                         'refstack_client.ft_utils.execute_command') as m:
46             cmd = ("cd {0};"
47                    "./refstack-client test -c {1} -v --test-list {2};"
48                    "cd -;".format(CONST.dir_refstack_client,
49                                   config,
50                                   testlist))
51             self.refstackclient.run_defcore(config, testlist)
52             m.assert_any_call(cmd)
53
54     def _get_main_kwargs(self, key=None):
55         kwargs = {'config': self._config,
56                   'testlist': self._testlist}
57         if key:
58             del kwargs[key]
59         return kwargs
60
61     def _test_main(self, status, *args):
62         kwargs = self._get_main_kwargs()
63         self.assertEqual(self.refstackclient.main(**kwargs), status)
64         if len(args) > 0:
65             args[0].assert_called_once_with(
66                  refstack_client.RefstackClient.result_dir)
67         if len(args) > 1:
68             args
69
70     def _test_main_missing_keyword(self, key):
71         kwargs = self._get_main_kwargs(key)
72         self.assertEqual(self.refstackclient.main(**kwargs),
73                          testcase.TestCase.EX_RUN_ERROR)
74
75     def test_main_missing_conf(self):
76         self._test_main_missing_keyword('config')
77
78     def test_main_missing_testlist(self):
79         self._test_main_missing_keyword('testlist')
80
81     def _test_argparser(self, arg, value):
82         self.defaultargs[arg] = value
83         parser = refstack_client.RefstackClientParser()
84         self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]),
85                          self.defaultargs)
86
87     def test_argparser_conf(self):
88         self._test_argparser('config', self._config)
89
90     def test_argparser_testlist(self):
91         self._test_argparser('testlist', self._testlist)
92
93     def test_argparser_multiple_args(self):
94         self.defaultargs['config'] = self._config
95         self.defaultargs['testlist'] = self._testlist
96         parser = refstack_client.RefstackClientParser()
97         self.assertEqual(parser.parse_args(
98             ["--config={}".format(self._config),
99              "--testlist={}".format(self._testlist)
100              ]), self.defaultargs)
101
102
103 if __name__ == "__main__":
104     unittest.main(verbosity=2)