3 ##############################################################################
4 # Copyright (c) 2015-2017 Huawei Technologies Co.,Ltd and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 # Unittest for yardstick.benchmark.contexts.node
14 from __future__ import absolute_import
20 from yardstick.common import constants as consts
21 from yardstick.benchmark.contexts import node
24 class NodeContextTestCase(unittest.TestCase):
26 PREFIX = 'yardstick.benchmark.contexts.node'
28 NODES_SAMPLE = "nodes_sample.yaml"
29 NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample.yaml"
32 self.test_context = node.NodeContext()
33 self.os_path_join = os.path.join
35 def _get_file_abspath(self, filename):
36 curr_path = os.path.dirname(os.path.abspath(__file__))
37 file_path = self.os_path_join(curr_path, filename)
40 def test___init__(self):
41 self.assertIsNone(self.test_context.name)
42 self.assertIsNone(self.test_context.file_path)
43 self.assertEqual(self.test_context.nodes, [])
44 self.assertEqual(self.test_context.controllers, [])
45 self.assertEqual(self.test_context.computes, [])
46 self.assertEqual(self.test_context.baremetals, [])
47 self.assertEqual(self.test_context.env, {})
48 self.assertEqual(self.test_context.attrs, {})
50 @mock.patch('{}.os.path.join'.format(PREFIX))
51 def test_init_negative(self, mock_path_join):
52 special_path = '/foo/bar/error_file'
53 error_path = self._get_file_abspath("error_file")
56 if args == (consts.YARDSTICK_ROOT_PATH, error_path):
58 return self.os_path_join(*args)
60 # we can't count mock_path_join calls because
61 # it can catch join calls for .pyc files.
62 mock_path_join.side_effect = path_join
63 self.test_context.read_config_file = read_mock = mock.Mock()
66 with self.assertRaises(KeyError):
67 self.test_context.init({})
69 self.assertEqual(read_mock.call_count, read_calls)
75 read_mock.side_effect = IOError(errno.EBUSY, 'busy')
76 with self.assertRaises(IOError) as raised:
77 self.test_context.init(attrs)
80 self.assertEqual(read_mock.called, read_calls)
81 self.assertIn(attrs['file'], self.test_context.file_path)
82 self.assertEqual(raised.exception.errno, errno.EBUSY)
83 self.assertEqual(str(raised.exception), str(read_mock.side_effect))
85 read_mock.side_effect = IOError(errno.ENOENT, 'not found')
86 with self.assertRaises(IOError) as raised:
87 self.test_context.init(attrs)
90 self.assertEqual(read_mock.call_count, read_calls)
91 self.assertEqual(self.test_context.file_path, special_path)
92 self.assertEqual(raised.exception.errno, errno.ENOENT)
93 self.assertEqual(str(raised.exception), str(read_mock.side_effect))
95 def test_read_config_file(self):
99 'file': self._get_file_abspath(self.NODES_SAMPLE)
102 self.test_context.init(attrs)
104 self.assertIsNotNone(self.test_context.read_config_file())
106 def test__dispatch_script(self):
110 'file': self._get_file_abspath(self.NODES_SAMPLE)
113 self.test_context.init(attrs)
115 self.test_context.env = {'bash': [{'script': 'dummy'}]}
116 self.test_context._execute_script = mock.Mock()
117 self.assertEqual(self.test_context._dispatch_script('bash'), None)
119 def test__dispatch_ansible(self):
123 'file': self._get_file_abspath(self.NODES_SAMPLE)
126 self.test_context.init(attrs)
128 self.test_context.env = {'ansible': [{'script': 'dummy'}]}
129 self.test_context._do_ansible_job = mock.Mock()
130 self.assertEqual(self.test_context._dispatch_ansible('ansible'), None)
131 self.test_context.env = {}
132 self.assertEqual(self.test_context._dispatch_ansible('ansible'), None)
134 @mock.patch("{}.AnsibleCommon".format(PREFIX))
135 def test__do_ansible_job(self, mock_ansible):
136 self.assertEqual(None, self.test_context._do_ansible_job('dummy'))
138 def test_successful_init(self):
142 'file': self._get_file_abspath(self.NODES_SAMPLE)
145 self.test_context.init(attrs)
147 self.assertEqual(self.test_context.name, "foo")
148 self.assertEqual(len(self.test_context.nodes), 4)
149 self.assertEqual(len(self.test_context.controllers), 2)
150 self.assertEqual(len(self.test_context.computes), 1)
151 self.assertEqual(self.test_context.computes[0]["name"], "node3")
152 self.assertEqual(len(self.test_context.baremetals), 1)
153 self.assertEqual(self.test_context.baremetals[0]["name"], "node4")
155 def test__get_server_with_dic_attr_name(self):
159 'file': self._get_file_abspath(self.NODES_SAMPLE)
162 self.test_context.init(attrs)
164 attr_name = {'name': 'foo.bar'}
165 result = self.test_context._get_server(attr_name)
167 self.assertEqual(result, None)
169 def test__get_server_not_found(self):
173 'file': self._get_file_abspath(self.NODES_SAMPLE)
176 self.test_context.init(attrs)
178 attr_name = 'bar.foo'
179 result = self.test_context._get_server(attr_name)
181 self.assertEqual(result, None)
183 def test__get_server_mismatch(self):
187 'file': self._get_file_abspath(self.NODES_SAMPLE)
190 self.test_context.init(attrs)
192 attr_name = 'bar.foo1'
193 result = self.test_context._get_server(attr_name)
195 self.assertEqual(result, None)
197 def test__get_server_duplicate(self):
201 'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE)
204 self.test_context.init(attrs)
206 attr_name = 'node1.foo'
207 with self.assertRaises(ValueError):
208 self.test_context._get_server(attr_name)
210 def test__get_server_found(self):
214 'file': self._get_file_abspath(self.NODES_SAMPLE)
217 self.test_context.init(attrs)
219 attr_name = 'node1.foo'
220 result = self.test_context._get_server(attr_name)
222 self.assertEqual(result['ip'], '10.229.47.137')
223 self.assertEqual(result['name'], 'node1.foo')
224 self.assertEqual(result['user'], 'root')
225 self.assertEqual(result['key_filename'], '/root/.yardstick_key')
227 @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
228 def test_deploy(self, dispatch_script_mock):
229 obj = node.NodeContext()
234 self.assertTrue(dispatch_script_mock.called)
236 @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
237 def test_deploy_anisible(self, dispatch_ansible_mock):
238 obj = node.NodeContext()
243 self.assertTrue(dispatch_ansible_mock.called)
245 @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
246 def test_undeploy(self, dispatch_script_mock):
247 obj = node.NodeContext()
252 self.assertTrue(dispatch_script_mock.called)
254 @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
255 def test_undeploy_anisble(self, dispatch_ansible_mock):
256 obj = node.NodeContext()
261 self.assertTrue(dispatch_ansible_mock.called)
263 @mock.patch('{}.ssh.SSH._put_file_shell'.format(PREFIX))
264 @mock.patch('{}.ssh.SSH.execute'.format(PREFIX))
265 def test_execute_remote_script(self, execute_mock, put_file_mock):
266 obj = node.NodeContext()
267 obj.env = {'prefix': 'yardstick.benchmark.scenarios.compute'}
268 node_name_args = 'node5'
270 'name': node_name_args,
276 info = {'script': 'computecapacity.bash'}
277 execute_mock.return_value = (0, '', '')
278 obj._execute_remote_script('node5', info)
280 self.assertTrue(put_file_mock.called)
281 self.assertTrue(execute_mock.called)
283 @mock.patch('{}.NodeContext._execute_local_script'.format(PREFIX))
284 def test_execute_script_local(self, local_execute_mock):
287 node.NodeContext()._execute_script(node_name, info)
288 self.assertTrue(local_execute_mock.called)
290 @mock.patch('{}.NodeContext._execute_remote_script'.format(PREFIX))
291 def test_execute_script_remote(self, remote_execute_mock):
294 node.NodeContext()._execute_script(node_name, info)
295 self.assertTrue(remote_execute_mock.called)
297 def test_get_script(self):
298 script_args = 'hello.bash'
300 'script': script_args
302 script, options = node.NodeContext()._get_script(info_args)
303 self.assertEqual(script_args, script)
304 self.assertEqual('', options)
306 def test_node_info(self):
307 node_name_args = 'node5'
308 obj = node.NodeContext()
309 obj.nodes = [{'name': node_name_args, 'check': node_name_args}]
310 node_info = obj._get_node_info(node_name_args)
311 self.assertEqual(node_info.get('check'), node_name_args)
313 @mock.patch('{}.ssh.SSH.wait'.format(PREFIX))
314 def test_get_client(self, wait_mock):
315 node_name_args = 'node5'
316 obj = node.NodeContext()
318 'name': node_name_args,
323 obj._get_client(node_name_args)
324 self.assertTrue(wait_mock.called)
326 def test_get_server(self):
327 self.test_context.name = 'vnf1'
328 self.test_context.nodes = [{'name': 'my', 'value': 100}]
330 with self.assertRaises(ValueError):
331 self.test_context.get_server('my.vnf2')
333 expected = {'name': 'my.vnf1', 'value': 100, 'interfaces': {}}
334 result = self.test_context.get_server('my.vnf1')
335 self.assertDictEqual(result, expected)
337 def test_get_context_from_server(self):
338 self.test_context.name = 'vnf1'
339 self.test_context.nodes = [{'name': 'my', 'value': 100}]
340 self.test_context.attrs = {'attr1': 200}
342 with self.assertRaises(ValueError):
343 self.test_context.get_context_from_server('my.vnf2')
345 result = self.test_context.get_context_from_server('my.vnf1')
346 self.assertIs(result, self.test_context)
348 def test__get_network(self):
352 'segmentation_id': 'seg54',
353 'network_type': 'type_a',
354 'physical_network': 'phys',
360 self.test_context.networks = {
366 self.assertIsNone(self.test_context._get_network(attr_name))
368 attr_name = {'vld_id': 'vld777'}
369 self.assertIsNone(self.test_context._get_network(attr_name))
371 self.assertIsNone(self.test_context._get_network(None))
374 self.assertIsNone(self.test_context._get_network(attr_name))
376 attr_name = {'vld_id': 'vld999'}
380 "segmentation_id": None,
381 "network_type": None,
382 "physical_network": None,
384 result = self.test_context._get_network(attr_name)
385 self.assertDictEqual(result, expected)
389 result = self.test_context._get_network(attr_name)
390 self.assertDictEqual(result, expected)
397 if __name__ == '__main__':