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
8 # pylint: disable=missing-docstring
16 from functest.opnfv_tests.openstack.tempest import conf_utils
17 from functest.utils import config
20 class OSTempestConfUtilsTesting(unittest.TestCase):
21 # pylint: disable=too-many-public-methods
23 @mock.patch('subprocess.check_output')
24 def test_create_rally_deployment(self, mock_exec):
25 self.assertEqual(conf_utils.create_rally_deployment(), None)
27 mock.call(['rally', 'deployment', 'destroy', '--deployment',
28 str(getattr(config.CONF, 'rally_deployment_name'))]),
29 mock.call(['rally', 'deployment', 'create', '--fromenv', '--name',
30 str(getattr(config.CONF, 'rally_deployment_name'))]),
31 mock.call(['rally', 'deployment', 'check'])]
32 mock_exec.assert_has_calls(calls)
34 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
36 def test_create_verifier(self, mock_logger_debug):
37 mock_popen = mock.Mock()
38 attrs = {'poll.return_value': None,
39 'stdout.readline.return_value': '0'}
40 mock_popen.configure_mock(**attrs)
42 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
43 with mock.patch('subprocess.Popen', side_effect=Exception), \
44 self.assertRaises(Exception):
45 conf_utils.create_verifier()
46 mock_logger_debug.assert_any_call("Tempest test_verifier_name"
49 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
50 'create_verifier', return_value=mock.Mock())
51 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
52 'create_rally_deployment', return_value=mock.Mock())
53 def test_get_verif_id_missing_verif(self, mock_rally, mock_tempest):
54 # pylint: disable=unused-argument
55 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
56 with mock.patch('functest.opnfv_tests.openstack.tempest.'
57 'conf_utils.subprocess.Popen') as mock_popen, \
58 self.assertRaises(Exception):
59 mock_stdout = mock.Mock()
60 attrs = {'stdout.readline.return_value': ''}
61 mock_stdout.configure_mock(**attrs)
62 mock_popen.return_value = mock_stdout
63 conf_utils.get_verifier_id()
65 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
66 'create_verifier', return_value=mock.Mock())
67 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
68 'create_rally_deployment', return_value=mock.Mock())
69 def test_get_verifier_id_default(self, mock_rally, mock_tempest):
70 # pylint: disable=unused-argument
71 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
72 with mock.patch('functest.opnfv_tests.openstack.tempest.'
73 'conf_utils.subprocess.Popen') as mock_popen:
74 mock_stdout = mock.Mock()
75 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
76 mock_stdout.configure_mock(**attrs)
77 mock_popen.return_value = mock_stdout
79 self.assertEqual(conf_utils.get_verifier_id(),
82 def test_get_depl_id_missing_rally(self):
83 setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
84 with mock.patch('functest.opnfv_tests.openstack.tempest.'
85 'conf_utils.subprocess.Popen') as mock_popen, \
86 self.assertRaises(Exception):
87 mock_stdout = mock.Mock()
88 attrs = {'stdout.readline.return_value': ''}
89 mock_stdout.configure_mock(**attrs)
90 mock_popen.return_value = mock_stdout
91 conf_utils.get_verifier_deployment_id()
93 def test_get_depl_id_default(self):
94 setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
95 with mock.patch('functest.opnfv_tests.openstack.tempest.'
96 'conf_utils.subprocess.Popen') as mock_popen:
97 mock_stdout = mock.Mock()
98 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
99 mock_stdout.configure_mock(**attrs)
100 mock_popen.return_value = mock_stdout
102 self.assertEqual(conf_utils.get_verifier_deployment_id(),
105 def test_get_verif_repo_dir_default(self):
106 with mock.patch('functest.opnfv_tests.openstack.tempest.'
107 'conf_utils.os.path.join',
108 return_value='test_verifier_repo_dir'), \
109 mock.patch('functest.opnfv_tests.openstack.tempest.'
110 'conf_utils.get_verifier_id') as mock_get_id:
111 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
112 'test_verifier_repo_dir')
113 self.assertTrue(mock_get_id.called)
115 def test_get_depl_dir_default(self):
116 with mock.patch('functest.opnfv_tests.openstack.tempest.'
117 'conf_utils.os.path.join',
118 return_value='test_verifier_repo_dir'), \
119 mock.patch('functest.opnfv_tests.openstack.tempest.'
120 'conf_utils.get_verifier_id') as mock_get_vid, \
121 mock.patch('functest.opnfv_tests.openstack.tempest.'
122 'conf_utils.get_verifier_deployment_id') \
124 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
125 'test_verifier_repo_dir')
126 self.assertTrue(mock_get_vid.called)
127 self.assertTrue(mock_get_did.called)
129 def _test_missing_param(self, params, image_id, flavor_id, alt=False):
130 with mock.patch('six.moves.configparser.RawConfigParser.'
132 mock.patch('six.moves.configparser.RawConfigParser.'
134 mock.patch('six.moves.configparser.RawConfigParser.'
135 'write') as mwrite, \
136 mock.patch('six.moves.builtins.open', mock.mock_open()), \
137 mock.patch('functest.utils.functest_utils.yaml.safe_load',
138 return_value={'validation': {'ssh_timeout': 300}}):
139 os.environ['OS_INTERFACE'] = ''
141 conf_utils.configure_tempest_update_params(
142 'test_conf_file', image_id=image_id,
144 mset.assert_any_call(params[0], params[1], params[2])
146 conf_utils.configure_tempest_update_params(
147 'test_conf_file', image_alt_id=image_id,
148 flavor_alt_id=flavor_id)
149 mset.assert_any_call(params[0], params[1], params[2])
150 self.assertTrue(mread.called)
151 self.assertTrue(mwrite.called)
153 def test_upd_missing_image_id(self):
154 self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
155 'test_image_id', None)
157 def test_upd_missing_image_id_alt(self):
158 self._test_missing_param(
159 ('compute', 'image_ref_alt', 'test_image_id_alt'),
160 'test_image_id_alt', None, alt=True)
162 def test_upd_missing_flavor_id(self):
163 self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
164 None, 'test_flavor_id')
166 def test_upd_missing_flavor_id_alt(self):
167 self._test_missing_param(
168 ('compute', 'flavor_ref_alt', 'test_flavor_id_alt'),
169 None, 'test_flavor_id_alt', alt=True)
171 def test_verif_missing_conf_file(self):
172 with mock.patch('functest.opnfv_tests.openstack.tempest.'
173 'conf_utils.os.path.isfile',
174 return_value=False), \
175 mock.patch('subprocess.check_output') as mexe, \
176 self.assertRaises(Exception) as context:
177 conf_utils.configure_verifier('test_dep_dir')
178 mexe.assert_called_once_with("rally verify configure-verifier")
179 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
181 self.assertTrue(msg in context.exception)
183 def test_configure_verifier_default(self):
184 with mock.patch('functest.opnfv_tests.openstack.tempest.'
185 'conf_utils.os.path.isfile',
186 return_value=True), \
187 mock.patch('subprocess.check_output') as mexe:
188 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
189 'test_dep_dir/tempest.conf')
190 mexe.assert_called_once_with(
191 ['rally', 'verify', 'configure-verifier', '--reconfigure',
192 '--id', str(getattr(config.CONF, 'tempest_verifier_name'))])
195 if __name__ == "__main__":
196 logging.disable(logging.CRITICAL)
197 unittest.main(verbosity=2)