3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # pylint: disable=missing-docstring
18 from functest.core import testcase
19 from functest.opnfv_tests.openstack.refstack_client.refstack_client import \
20 RefstackClient, RefstackClientParser
21 from functest.utils.constants import CONST
23 from snaps.openstack.os_credentials import OSCreds
25 __author__ = ("Matthew Li <matthew.lijun@huawei.com>,"
26 "Linda Wang <wangwulin@huawei.com>")
29 class OSRefstackClientTesting(unittest.TestCase):
30 """The class testing RefstackClient """
31 # pylint: disable=missing-docstring, too-many-public-methods
33 _config = pkg_resources.resource_filename(
35 'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
36 _testlist = pkg_resources.resource_filename(
37 'functest', 'opnfv_tests/openstack/refstack_client/defcore.txt')
40 self.default_args = {'config': self._config,
41 'testlist': self._testlist}
42 CONST.__setattr__('OS_AUTH_URL', 'https://ip:5000/v3')
43 CONST.__setattr__('OS_INSECURE', 'true')
44 self.case_name = 'refstack_defcore'
46 self.os_creds = OSCreds(
47 username='user', password='pass',
48 auth_url='http://foo.com:5000/v3', project_name='bar')
49 self.details = {"tests": 3,
51 "success": ['tempest.api.compute [18.464988s]'],
52 "errors": ['tempest.api.volume [0.230334s]'],
53 "skipped": ['tempest.api.network [1.265828s]']}
55 def _create_client(self):
56 with mock.patch('snaps.openstack.tests.openstack_tests.'
57 'get_credentials', return_value=self.os_creds):
58 return RefstackClient()
60 def test_run_defcore_insecure(self):
62 config = 'tempest.conf'
64 client = self._create_client()
65 with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
66 'refstack_client.ft_utils.execute_command') as m_cmd:
67 cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
68 .format(insecure, config, testlist))
69 client.run_defcore(config, testlist)
70 m_cmd.assert_any_call(cmd)
72 def test_run_defcore(self):
73 CONST.__setattr__('OS_AUTH_URL', 'http://ip:5000/v3')
75 config = 'tempest.conf'
77 client = self._create_client()
78 with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
79 'refstack_client.ft_utils.execute_command') as m_cmd:
80 cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
81 .format(insecure, config, testlist))
82 client.run_defcore(config, testlist)
83 m_cmd.assert_any_call(cmd)
85 @mock.patch('functest.opnfv_tests.openstack.refstack_client.'
86 'refstack_client.LOGGER.info')
87 @mock.patch('__builtin__.open', side_effect=Exception)
88 def test_parse_refstack_result_fail(self, *args):
89 self._create_client().parse_refstack_result()
90 args[1].assert_called_once_with(
91 "Testcase %s success_rate is %s%%",
92 self.case_name, self.result)
94 def test_parse_refstack_result_ok(self):
96 {0} tempest.api.compute [18.464988s] ... ok
97 {0} tempest.api.volume [0.230334s] ... FAILED
98 {0} tempest.api.network [1.265828s] ... SKIPPED:
99 Ran: 3 tests in 1259.0000 sec.
104 client = self._create_client()
105 with mock.patch('__builtin__.open',
106 mock.mock_open(read_data=log_file)):
107 client.parse_refstack_result()
108 self.assertEqual(client.details, self.details)
110 def _get_main_kwargs(self, key=None):
111 kwargs = {'config': self._config,
112 'testlist': self._testlist}
117 def _test_main_missing_keyword(self, key):
118 kwargs = self._get_main_kwargs(key)
119 client = self._create_client()
120 self.assertEqual(client.main(**kwargs),
121 testcase.TestCase.EX_RUN_ERROR)
123 def test_main_missing_conf(self):
124 self._test_main_missing_keyword('config')
126 def test_main_missing_testlist(self):
127 self._test_main_missing_keyword('testlist')
129 def _test_argparser(self, arg, value):
130 self.default_args[arg] = value
131 parser = RefstackClientParser()
132 self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]),
135 def test_argparser_conf(self):
136 self._test_argparser('config', self._config)
138 def test_argparser_testlist(self):
139 self._test_argparser('testlist', self._testlist)
141 def test_argparser_multiple_args(self):
142 self.default_args['config'] = self._config
143 self.default_args['testlist'] = self._testlist
144 parser = RefstackClientParser()
145 self.assertEqual(parser.parse_args(
146 ["--config={}".format(self._config),
147 "--testlist={}".format(self._testlist)]), self.default_args)
150 if __name__ == "__main__":
151 logging.disable(logging.CRITICAL)
152 unittest.main(verbosity=2)