2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 # matthew.lijun@huawei.com wangwulin@huawei.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
14 from functest.core import testcase
15 from functest.opnfv_tests.openstack.refstack_client.refstack_client import \
16 RefstackClient, RefstackClientParser
17 from functest.utils.constants import CONST
19 from snaps.openstack.os_credentials import OSCreds
22 class OSRefstackClientTesting(unittest.TestCase):
24 _config = pkg_resources.resource_filename(
26 'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
27 _testlist = pkg_resources.resource_filename(
28 'functest', 'opnfv_tests/openstack/refstack_client/defcore.txt')
31 self.default_args = {'config': self._config,
32 'testlist': self._testlist}
33 CONST.__setattr__('OS_AUTH_URL', 'https://ip:5000/v3')
34 CONST.__setattr__('OS_INSECURE', 'true')
35 self.os_creds = OSCreds(
36 username='user', password='pass',
37 auth_url='http://foo.com:5000/v3', project_name='bar')
39 @mock.patch('functest.opnfv_tests.openstack.refstack_client.tempest_conf.'
40 'TempestConf', return_value=mock.Mock())
41 def _create_client(self, mock_conf):
42 with mock.patch('snaps.openstack.tests.openstack_tests.'
43 'get_credentials', return_value=self.os_creds):
44 return RefstackClient()
46 def test_run_defcore_insecure(self):
48 config = 'tempest.conf'
50 client = self._create_client()
51 with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
52 'refstack_client.ft_utils.execute_command') as m:
53 cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
54 .format(insecure, config, testlist))
55 client.run_defcore(config, testlist)
56 m.assert_any_call(cmd)
58 def test_run_defcore(self):
59 CONST.__setattr__('OS_AUTH_URL', 'http://ip:5000/v3')
61 config = 'tempest.conf'
63 client = self._create_client()
64 with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
65 'refstack_client.ft_utils.execute_command') as m:
66 cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
67 .format(insecure, config, testlist))
68 client.run_defcore(config, testlist)
69 m.assert_any_call(cmd)
71 @mock.patch('functest.opnfv_tests.openstack.refstack_client.'
72 'refstack_client.LOGGER.info')
73 @mock.patch('__builtin__.open', side_effect=Exception)
74 def test_parse_refstack_result_missing_log_file(self, mock_open,
76 self.case_name = 'refstack_defcore'
78 self._create_client().parse_refstack_result()
79 mock_logger_info.assert_called_once_with(
80 "Testcase %s success_rate is %s%%",
81 self.case_name, self.result)
83 def test_parse_refstack_result_default(self):
85 {0} tempest.api.compute [18.464988s] ... ok
86 {0} tempest.api.volume [0.230334s] ... FAILED
87 {0} tempest.api.network [1.265828s] ... SKIPPED:
88 Ran: 3 tests in 1259.0000 sec.
93 self.details = {"tests": 3,
95 "success": ['tempest.api.compute [18.464988s]'],
96 "errors": ['tempest.api.volume [0.230334s]'],
97 "skipped": ['tempest.api.network [1.265828s]']}
98 client = self._create_client()
99 with mock.patch('__builtin__.open',
100 mock.mock_open(read_data=log_file)):
101 client.parse_refstack_result()
102 self.assertEqual(client.details, self.details)
104 def _get_main_kwargs(self, key=None):
105 kwargs = {'config': self._config,
106 'testlist': self._testlist}
111 def _test_main(self, status, *args):
112 kwargs = self._get_main_kwargs()
113 client = self._create_client()
114 self.assertEqual(client.main(**kwargs), status)
116 args[0].assert_called_once_with(
117 RefstackClient.result_dir)
121 def _test_main_missing_keyword(self, key):
122 kwargs = self._get_main_kwargs(key)
123 client = self._create_client()
124 self.assertEqual(client.main(**kwargs),
125 testcase.TestCase.EX_RUN_ERROR)
127 def test_main_missing_conf(self):
128 self._test_main_missing_keyword('config')
130 def test_main_missing_testlist(self):
131 self._test_main_missing_keyword('testlist')
133 def _test_argparser(self, arg, value):
134 self.default_args[arg] = value
135 parser = RefstackClientParser()
136 self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]),
139 def test_argparser_conf(self):
140 self._test_argparser('config', self._config)
142 def test_argparser_testlist(self):
143 self._test_argparser('testlist', self._testlist)
145 def test_argparser_multiple_args(self):
146 self.default_args['config'] = self._config
147 self.default_args['testlist'] = self._testlist
148 parser = RefstackClientParser()
149 self.assertEqual(parser.parse_args(
150 ["--config={}".format(self._config),
151 "--testlist={}".format(self._testlist)
152 ]), self.default_args)
155 if __name__ == "__main__":
156 logging.disable(logging.CRITICAL)
157 unittest.main(verbosity=2)