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 # pylint: disable=unused-argument
25 # disable this for now because I keep forgetting mock patch arg ordering
28 class NodeContextTestCase(unittest.TestCase):
30 PREFIX = 'yardstick.benchmark.contexts.node'
32 NODES_SAMPLE = "nodes_sample.yaml"
33 NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample.yaml"
36 self.test_context = node.NodeContext()
37 self.os_path_join = os.path.join
39 def _get_file_abspath(self, filename):
40 curr_path = os.path.dirname(os.path.abspath(__file__))
41 file_path = self.os_path_join(curr_path, filename)
44 def test___init__(self):
45 self.assertIsNone(self.test_context.name)
46 self.assertIsNone(self.test_context.file_path)
47 self.assertEqual(self.test_context.nodes, [])
48 self.assertEqual(self.test_context.controllers, [])
49 self.assertEqual(self.test_context.computes, [])
50 self.assertEqual(self.test_context.baremetals, [])
51 self.assertEqual(self.test_context.env, {})
52 self.assertEqual(self.test_context.attrs, {})
54 @mock.patch('{}.os.path.join'.format(PREFIX))
55 def test_init_negative(self, mock_path_join):
56 special_path = '/foo/bar/error_file'
57 error_path = self._get_file_abspath("error_file")
60 if args == (consts.YARDSTICK_ROOT_PATH, error_path):
62 return self.os_path_join(*args)
64 # we can't count mock_path_join calls because
65 # it can catch join calls for .pyc files.
66 mock_path_join.side_effect = path_join
67 self.test_context.read_config_file = read_mock = mock.Mock()
70 with self.assertRaises(KeyError):
71 self.test_context.init({})
73 self.assertEqual(read_mock.call_count, read_calls)
79 read_mock.side_effect = IOError(errno.EBUSY, 'busy')
80 with self.assertRaises(IOError) as raised:
81 self.test_context.init(attrs)
84 self.assertEqual(read_mock.called, read_calls)
85 self.assertIn(attrs['file'], self.test_context.file_path)
86 self.assertEqual(raised.exception.errno, errno.EBUSY)
87 self.assertEqual(str(raised.exception), str(read_mock.side_effect))
89 read_mock.side_effect = IOError(errno.ENOENT, 'not found')
90 with self.assertRaises(IOError) as raised:
91 self.test_context.init(attrs)
94 self.assertEqual(read_mock.call_count, read_calls)
95 self.assertEqual(self.test_context.file_path, special_path)
96 self.assertEqual(raised.exception.errno, errno.ENOENT)
97 self.assertEqual(str(raised.exception), str(read_mock.side_effect))
99 def test_read_config_file(self):
103 'file': self._get_file_abspath(self.NODES_SAMPLE)
106 self.test_context.init(attrs)
108 self.assertIsNotNone(self.test_context.read_config_file())
110 def test__dispatch_script(self):
114 'file': self._get_file_abspath(self.NODES_SAMPLE)
117 self.test_context.init(attrs)
119 self.test_context.env = {'bash': [{'script': 'dummy'}]}
120 self.test_context._execute_script = mock.Mock()
121 self.assertEqual(self.test_context._dispatch_script('bash'), None)
123 def test__dispatch_ansible(self):
127 'file': self._get_file_abspath(self.NODES_SAMPLE)
130 self.test_context.init(attrs)
132 self.test_context.env = {'ansible': [{'script': 'dummy'}]}
133 self.test_context._do_ansible_job = mock.Mock()
134 self.assertEqual(self.test_context._dispatch_ansible('ansible'), None)
135 self.test_context.env = {}
136 self.assertEqual(self.test_context._dispatch_ansible('ansible'), None)
138 @mock.patch("{}.AnsibleCommon".format(PREFIX))
139 def test__do_ansible_job(self, mock_ansible):
140 self.assertEqual(None, self.test_context._do_ansible_job('dummy'))
142 def test_successful_init(self):
146 'file': self._get_file_abspath(self.NODES_SAMPLE)
149 self.test_context.init(attrs)
151 self.assertEqual(self.test_context.name, "foo")
152 self.assertEqual(len(self.test_context.nodes), 4)
153 self.assertEqual(len(self.test_context.controllers), 2)
154 self.assertEqual(len(self.test_context.computes), 1)
155 self.assertEqual(self.test_context.computes[0]["name"], "node3")
156 self.assertEqual(len(self.test_context.baremetals), 1)
157 self.assertEqual(self.test_context.baremetals[0]["name"], "node4")
159 def test__get_server_with_dic_attr_name(self):
163 'file': self._get_file_abspath(self.NODES_SAMPLE)
166 self.test_context.init(attrs)
168 attr_name = {'name': 'foo.bar'}
169 result = self.test_context._get_server(attr_name)
171 self.assertEqual(result, None)
173 def test__get_server_not_found(self):
177 'file': self._get_file_abspath(self.NODES_SAMPLE)
180 self.test_context.init(attrs)
182 attr_name = 'bar.foo'
183 result = self.test_context._get_server(attr_name)
185 self.assertEqual(result, None)
187 def test__get_server_mismatch(self):
191 'file': self._get_file_abspath(self.NODES_SAMPLE)
194 self.test_context.init(attrs)
196 attr_name = 'bar.foo1'
197 result = self.test_context._get_server(attr_name)
199 self.assertEqual(result, None)
201 def test__get_server_duplicate(self):
205 'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE)
208 self.test_context.init(attrs)
210 attr_name = 'node1.foo'
211 with self.assertRaises(ValueError):
212 self.test_context._get_server(attr_name)
214 def test__get_server_found(self):
218 'file': self._get_file_abspath(self.NODES_SAMPLE)
221 self.test_context.init(attrs)
223 attr_name = 'node1.foo'
224 result = self.test_context._get_server(attr_name)
226 self.assertEqual(result['ip'], '10.229.47.137')
227 self.assertEqual(result['name'], 'node1.foo')
228 self.assertEqual(result['user'], 'root')
229 self.assertEqual(result['key_filename'], '/root/.yardstick_key')
231 @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
232 def test_deploy(self, dispatch_script_mock):
233 obj = node.NodeContext()
238 self.assertTrue(dispatch_script_mock.called)
240 @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
241 def test_deploy_anisible(self, dispatch_ansible_mock):
242 obj = node.NodeContext()
247 self.assertTrue(dispatch_ansible_mock.called)
249 @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
250 def test_undeploy(self, dispatch_script_mock):
251 obj = node.NodeContext()
256 self.assertTrue(dispatch_script_mock.called)
258 @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
259 def test_undeploy_anisble(self, dispatch_ansible_mock):
260 obj = node.NodeContext()
265 self.assertTrue(dispatch_ansible_mock.called)
267 @mock.patch('{}.ssh.SSH._put_file_shell'.format(PREFIX))
268 @mock.patch('{}.ssh.SSH.execute'.format(PREFIX))
269 def test_execute_remote_script(self, execute_mock, put_file_mock):
270 obj = node.NodeContext()
271 obj.env = {'prefix': 'yardstick.benchmark.scenarios.compute'}
272 node_name_args = 'node5'
274 'name': node_name_args,
280 info = {'script': 'computecapacity.bash'}
281 execute_mock.return_value = (0, '', '')
282 obj._execute_remote_script('node5', info)
284 self.assertTrue(put_file_mock.called)
285 self.assertTrue(execute_mock.called)
287 @mock.patch('{}.NodeContext._execute_local_script'.format(PREFIX))
288 def test_execute_script_local(self, local_execute_mock):
291 node.NodeContext()._execute_script(node_name, info)
292 self.assertTrue(local_execute_mock.called)
294 @mock.patch('{}.NodeContext._execute_remote_script'.format(PREFIX))
295 def test_execute_script_remote(self, remote_execute_mock):
298 node.NodeContext()._execute_script(node_name, info)
299 self.assertTrue(remote_execute_mock.called)
301 def test_get_script(self):
302 script_args = 'hello.bash'
304 'script': script_args
306 script, options = node.NodeContext()._get_script(info_args)
307 self.assertEqual(script_args, script)
308 self.assertEqual('', options)
310 def test_node_info(self):
311 node_name_args = 'node5'
312 obj = node.NodeContext()
313 obj.nodes = [{'name': node_name_args, 'check': node_name_args}]
314 node_info = obj._get_node_info(node_name_args)
315 self.assertEqual(node_info.get('check'), node_name_args)
317 @mock.patch('{}.ssh.SSH.wait'.format(PREFIX))
318 def test_get_client(self, wait_mock):
319 node_name_args = 'node5'
320 obj = node.NodeContext()
322 'name': node_name_args,
327 obj._get_client(node_name_args)
328 self.assertTrue(wait_mock.called)
330 def test_get_server(self):
331 self.test_context.name = 'vnf1'
332 self.test_context.nodes = [{'name': 'my', 'value': 100}]
334 with self.assertRaises(ValueError):
335 self.test_context.get_server('my.vnf2')
337 expected = {'name': 'my.vnf1', 'value': 100, 'interfaces': {}}
338 result = self.test_context.get_server('my.vnf1')
339 self.assertDictEqual(result, expected)
341 def test_get_context_from_server(self):
342 self.test_context.name = 'vnf1'
343 self.test_context.nodes = [{'name': 'my', 'value': 100}]
344 self.test_context.attrs = {'attr1': 200}
346 with self.assertRaises(ValueError):
347 self.test_context.get_context_from_server('my.vnf2')
349 result = self.test_context.get_context_from_server('my.vnf1')
350 self.assertIs(result, self.test_context)
352 # TODO: Split this into more granular tests
353 def test__get_network(self):
357 'segmentation_id': 'seg54',
358 'network_type': 'type_a',
359 'physical_network': 'phys',
365 self.test_context.networks = {
371 self.assertIsNone(self.test_context._get_network(attr_name))
373 attr_name = {'vld_id': 'vld777'}
374 self.assertIsNone(self.test_context._get_network(attr_name))
376 self.assertIsNone(self.test_context._get_network(None))
379 self.assertIsNone(self.test_context._get_network(attr_name))
381 attr_name = {'vld_id': 'vld999'}
385 "segmentation_id": None,
386 "network_type": None,
387 "physical_network": None,
389 result = self.test_context._get_network(attr_name)
390 self.assertDictEqual(result, expected)
394 result = self.test_context._get_network(attr_name)
395 self.assertDictEqual(result, expected)
402 if __name__ == '__main__':