Fixed document for Grafana Port usage
[yardstick.git] / tests / unit / benchmark / contexts / test_node.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015-2017 Huawei Technologies Co.,Ltd and others.
5 #
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 ##############################################################################
11
12 # Unittest for yardstick.benchmark.contexts.node
13
14 from __future__ import absolute_import
15 import os
16 import unittest
17 import errno
18 import mock
19
20 from yardstick.common import constants as consts
21 from yardstick.benchmark.contexts import node
22
23
24 class NodeContextTestCase(unittest.TestCase):
25
26     PREFIX = 'yardstick.benchmark.contexts.node'
27
28     NODES_SAMPLE = "nodes_sample.yaml"
29     NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample.yaml"
30
31     def setUp(self):
32         self.test_context = node.NodeContext()
33         self.os_path_join = os.path.join
34
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)
38         return file_path
39
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, {})
49
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")
54
55         def path_join(*args):
56             if args == (consts.YARDSTICK_ROOT_PATH, error_path):
57                 return special_path
58             return self.os_path_join(*args)
59
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()
64         read_calls = 0
65
66         with self.assertRaises(KeyError):
67             self.test_context.init({})
68
69         self.assertEqual(read_mock.call_count, read_calls)
70
71         attrs = {
72             'name': 'foo',
73             'file': error_path,
74         }
75         read_mock.side_effect = IOError(errno.EBUSY, 'busy')
76         with self.assertRaises(IOError) as raised:
77             self.test_context.init(attrs)
78
79         read_calls += 1
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))
84
85         read_mock.side_effect = IOError(errno.ENOENT, 'not found')
86         with self.assertRaises(IOError) as raised:
87             self.test_context.init(attrs)
88
89         read_calls += 2
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))
94
95     def test_read_config_file(self):
96
97         attrs = {
98             'name': 'foo',
99             'file': self._get_file_abspath(self.NODES_SAMPLE)
100         }
101
102         self.test_context.init(attrs)
103
104         self.assertIsNotNone(self.test_context.read_config_file())
105
106     def test__dispatch_script(self):
107
108         attrs = {
109             'name': 'foo',
110             'file': self._get_file_abspath(self.NODES_SAMPLE)
111         }
112
113         self.test_context.init(attrs)
114
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)
118
119     def test__dispatch_ansible(self):
120
121         attrs = {
122             'name': 'foo',
123             'file': self._get_file_abspath(self.NODES_SAMPLE)
124         }
125
126         self.test_context.init(attrs)
127
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)
133
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'))
137
138     def test_successful_init(self):
139
140         attrs = {
141             'name': 'foo',
142             'file': self._get_file_abspath(self.NODES_SAMPLE)
143         }
144
145         self.test_context.init(attrs)
146
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")
154
155     def test__get_server_with_dic_attr_name(self):
156
157         attrs = {
158             'name': 'foo',
159             'file': self._get_file_abspath(self.NODES_SAMPLE)
160         }
161
162         self.test_context.init(attrs)
163
164         attr_name = {'name': 'foo.bar'}
165         result = self.test_context._get_server(attr_name)
166
167         self.assertEqual(result, None)
168
169     def test__get_server_not_found(self):
170
171         attrs = {
172             'name': 'foo',
173             'file': self._get_file_abspath(self.NODES_SAMPLE)
174         }
175
176         self.test_context.init(attrs)
177
178         attr_name = 'bar.foo'
179         result = self.test_context._get_server(attr_name)
180
181         self.assertEqual(result, None)
182
183     def test__get_server_mismatch(self):
184
185         attrs = {
186             'name': 'foo',
187             'file': self._get_file_abspath(self.NODES_SAMPLE)
188         }
189
190         self.test_context.init(attrs)
191
192         attr_name = 'bar.foo1'
193         result = self.test_context._get_server(attr_name)
194
195         self.assertEqual(result, None)
196
197     def test__get_server_duplicate(self):
198
199         attrs = {
200             'name': 'foo',
201             'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE)
202         }
203
204         self.test_context.init(attrs)
205
206         attr_name = 'node1.foo'
207         with self.assertRaises(ValueError):
208             self.test_context._get_server(attr_name)
209
210     def test__get_server_found(self):
211
212         attrs = {
213             'name': 'foo',
214             'file': self._get_file_abspath(self.NODES_SAMPLE)
215         }
216
217         self.test_context.init(attrs)
218
219         attr_name = 'node1.foo'
220         result = self.test_context._get_server(attr_name)
221
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')
226
227     @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
228     def test_deploy(self, dispatch_script_mock):
229         obj = node.NodeContext()
230         obj.env = {
231             'type': 'script'
232         }
233         obj.deploy()
234         self.assertTrue(dispatch_script_mock.called)
235
236     @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
237     def test_deploy_anisible(self, dispatch_ansible_mock):
238         obj = node.NodeContext()
239         obj.env = {
240             'type': 'ansible'
241         }
242         obj.deploy()
243         self.assertTrue(dispatch_ansible_mock.called)
244
245     @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
246     def test_undeploy(self, dispatch_script_mock):
247         obj = node.NodeContext()
248         obj.env = {
249             'type': 'script'
250         }
251         obj.undeploy()
252         self.assertTrue(dispatch_script_mock.called)
253
254     @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
255     def test_undeploy_anisble(self, dispatch_ansible_mock):
256         obj = node.NodeContext()
257         obj.env = {
258             'type': 'ansible'
259         }
260         obj.undeploy()
261         self.assertTrue(dispatch_ansible_mock.called)
262
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'
269         obj.nodes = [{
270             'name': node_name_args,
271             'user': 'ubuntu',
272             'ip': '10.10.10.10',
273             'pwd': 'ubuntu',
274         }]
275
276         info = {'script': 'computecapacity.bash'}
277         execute_mock.return_value = (0, '', '')
278         obj._execute_remote_script('node5', info)
279
280         self.assertTrue(put_file_mock.called)
281         self.assertTrue(execute_mock.called)
282
283     @mock.patch('{}.NodeContext._execute_local_script'.format(PREFIX))
284     def test_execute_script_local(self, local_execute_mock):
285         node_name = 'local'
286         info = {}
287         node.NodeContext()._execute_script(node_name, info)
288         self.assertTrue(local_execute_mock.called)
289
290     @mock.patch('{}.NodeContext._execute_remote_script'.format(PREFIX))
291     def test_execute_script_remote(self, remote_execute_mock):
292         node_name = 'node5'
293         info = {}
294         node.NodeContext()._execute_script(node_name, info)
295         self.assertTrue(remote_execute_mock.called)
296
297     def test_get_script(self):
298         script_args = 'hello.bash'
299         info_args = {
300             'script': script_args
301         }
302         script, options = node.NodeContext()._get_script(info_args)
303         self.assertEqual(script_args, script)
304         self.assertEqual('', options)
305
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)
312
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()
317         obj.nodes = [{
318             'name': node_name_args,
319             'user': 'ubuntu',
320             'ip': '10.10.10.10',
321             'pwd': 'ubuntu',
322         }]
323         obj._get_client(node_name_args)
324         self.assertTrue(wait_mock.called)
325
326     def test_get_server(self):
327         self.test_context.name = 'vnf1'
328         self.test_context.nodes = [{'name': 'my', 'value': 100}]
329
330         with self.assertRaises(ValueError):
331             self.test_context.get_server('my.vnf2')
332
333         expected = {'name': 'my.vnf1', 'value': 100, 'interfaces': {}}
334         result = self.test_context.get_server('my.vnf1')
335         self.assertDictEqual(result, expected)
336
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}
341
342         with self.assertRaises(ValueError):
343             self.test_context.get_context_from_server('my.vnf2')
344
345         result = self.test_context.get_context_from_server('my.vnf1')
346         self.assertIs(result, self.test_context)
347
348     def test__get_network(self):
349         network1 = {
350             'name': 'net_1',
351             'vld_id': 'vld111',
352             'segmentation_id': 'seg54',
353             'network_type': 'type_a',
354             'physical_network': 'phys',
355         }
356         network2 = {
357             'name': 'net_2',
358             'vld_id': 'vld999',
359         }
360         self.test_context.networks = {
361             'a': network1,
362             'b': network2,
363         }
364
365         attr_name = {}
366         self.assertIsNone(self.test_context._get_network(attr_name))
367
368         attr_name = {'vld_id': 'vld777'}
369         self.assertIsNone(self.test_context._get_network(attr_name))
370
371         self.assertIsNone(self.test_context._get_network(None))
372
373         attr_name = 'vld777'
374         self.assertIsNone(self.test_context._get_network(attr_name))
375
376         attr_name = {'vld_id': 'vld999'}
377         expected = {
378             "name": 'net_2',
379             "vld_id": 'vld999',
380             "segmentation_id": None,
381             "network_type": None,
382             "physical_network": None,
383         }
384         result = self.test_context._get_network(attr_name)
385         self.assertDictEqual(result, expected)
386
387         attr_name = 'a'
388         expected = network1
389         result = self.test_context._get_network(attr_name)
390         self.assertDictEqual(result, expected)
391
392
393 def main():
394     unittest.main()
395
396
397 if __name__ == '__main__':
398     main()