Merge "Bug fix for Ansible destroy VM script"
[yardstick.git] / yardstick / tests / unit / benchmark / core / test_task.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 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.core.task
13
14 from __future__ import print_function
15
16 from __future__ import absolute_import
17 import os
18 import unittest
19
20 try:
21     from unittest import mock
22 except ImportError:
23     import mock
24
25
26 from yardstick.benchmark.core import task
27 from yardstick.common import constants as consts
28
29
30 # pylint: disable=unused-argument
31 # disable this for now because I keep forgetting mock patch arg ordering
32
33
34 class TaskTestCase(unittest.TestCase):
35
36     @mock.patch('yardstick.benchmark.core.task.Context')
37     def test_parse_nodes_host_target_same_context(self, mock_context):
38         nodes = {
39             "host": "node1.LF",
40             "target": "node2.LF"
41         }
42         scenario_cfg = {"nodes": nodes}
43         server_info = {
44             "ip": "10.20.0.3",
45             "user": "root",
46             "key_filename": "/root/.ssh/id_rsa"
47         }
48         mock_context.get_server.return_value = server_info
49         context_cfg = task.parse_nodes_with_context(scenario_cfg)
50
51         self.assertEqual(context_cfg["host"], server_info)
52         self.assertEqual(context_cfg["target"], server_info)
53
54     def test_set_dispatchers(self):
55         t = task.Task()
56         output_config = {"DEFAULT": {"dispatcher": "file, http"}}
57         t._set_dispatchers(output_config)
58         self.assertEqual(output_config, output_config)
59
60     @mock.patch('yardstick.benchmark.core.task.DispatcherBase')
61     def test__do_output(self, mock_dispatcher):
62         t = task.Task()
63         output_config = {"DEFAULT": {"dispatcher": "file, http"}}
64         mock_dispatcher.get = mock.MagicMock(return_value=[mock.MagicMock(),
65                                                            mock.MagicMock()])
66         self.assertEqual(None, t._do_output(output_config, {}))
67
68     @mock.patch('yardstick.benchmark.core.task.Context')
69     def test_parse_networks_from_nodes(self, mock_context):
70         nodes = {
71             'node1': {
72                 'interfaces': {
73                     'mgmt': {
74                         'network_name': 'mgmt',
75                     },
76                     'xe0': {
77                         'network_name': 'uplink_0',
78                     },
79                     'xe1': {
80                         'network_name': 'downlink_0',
81                     },
82                 },
83             },
84             'node2': {
85                 'interfaces': {
86                     'mgmt': {
87                         'network_name': 'mgmt',
88                     },
89                     'uplink_0': {
90                         'network_name': 'uplink_0',
91                     },
92                     'downlink_0': {
93                         'network_name': 'downlink_0',
94                     },
95                 },
96             },
97         }
98
99         mock_context.get_network.side_effect = iter([
100             None,
101             {
102                 'name': 'mgmt',
103                 'network_type': 'flat',
104             },
105             {},
106             {
107                 'name': 'uplink_0',
108                 'subnet_cidr': '10.20.0.0/16',
109             },
110             {
111                 'name': 'downlink_0',
112                 'segmentation_id': '1001',
113             },
114             {
115                 'name': 'uplink_1',
116             },
117         ])
118
119         # one for each interface
120         expected_get_network_calls = 6
121         expected = {
122             'mgmt': {'name': 'mgmt', 'network_type': 'flat'},
123             'uplink_0': {'name': 'uplink_0', 'subnet_cidr': '10.20.0.0/16'},
124             'uplink_1': {'name': 'uplink_1'},
125             'downlink_0': {'name': 'downlink_0', 'segmentation_id': '1001'},
126         }
127
128         networks = task.get_networks_from_nodes(nodes)
129         self.assertEqual(mock_context.get_network.call_count, expected_get_network_calls)
130         self.assertDictEqual(networks, expected)
131
132     @mock.patch('yardstick.benchmark.core.task.Context')
133     @mock.patch('yardstick.benchmark.core.task.base_runner')
134     def test_run(self, mock_base_runner, mock_ctx):
135         scenario = {
136             'host': 'athena.demo',
137             'target': 'ares.demo',
138             'runner': {
139                 'duration': 60,
140                 'interval': 1,
141                 'type': 'Duration'
142             },
143             'type': 'Ping'
144         }
145
146         t = task.Task()
147         runner = mock.Mock()
148         runner.join.return_value = 0
149         runner.get_output.return_value = {}
150         runner.get_result.return_value = []
151         mock_base_runner.Runner.get.return_value = runner
152         t._run([scenario], False, "yardstick.out")
153         self.assertTrue(runner.run.called)
154
155     @mock.patch('yardstick.benchmark.core.task.os')
156     def test_check_precondition(self, mock_os):
157         cfg = {
158             'precondition': {
159                 'installer_type': 'compass',
160                 'deploy_scenarios': 'os-nosdn',
161                 'pod_name': 'huawei-pod1'
162             }
163         }
164
165         t = task.TaskParser('/opt')
166         mock_os.environ.get.side_effect = ['compass',
167                                            'os-nosdn',
168                                            'huawei-pod1']
169         result = t._check_precondition(cfg)
170         self.assertTrue(result)
171
172     def test_parse_suite_no_constraint_no_args(self):
173         SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
174         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
175         with mock.patch('yardstick.benchmark.core.task.os.environ',
176                         new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
177             task_files, task_args, task_args_fnames = t.parse_suite()
178         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
179                                                 task_args_fnames))
180         self.assertEqual(task_files[0], self.change_to_abspath(
181                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
182         self.assertEqual(task_files[1], self.change_to_abspath(
183                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
184         self.assertEqual(task_args[0], None)
185         self.assertEqual(task_args[1], None)
186         self.assertEqual(task_args_fnames[0], None)
187         self.assertEqual(task_args_fnames[1], None)
188
189     @mock.patch('yardstick.benchmark.core.task.os.environ')
190     def test_parse_suite_no_constraint_with_args(self, mock_environ):
191         SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
192         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
193         with mock.patch('yardstick.benchmark.core.task.os.environ',
194                         new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
195             task_files, task_args, task_args_fnames = t.parse_suite()
196         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
197                                                 task_args_fnames))
198         self.assertEqual(task_files[0], self.change_to_abspath(
199                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
200         self.assertEqual(task_files[1], self.change_to_abspath(
201                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
202         self.assertEqual(task_args[0], None)
203         self.assertEqual(task_args[1],
204                          '{"host": "node1.LF","target": "node2.LF"}')
205         self.assertEqual(task_args_fnames[0], None)
206         self.assertEqual(task_args_fnames[1], None)
207
208     @mock.patch('yardstick.benchmark.core.task.os.environ')
209     def test_parse_suite_with_constraint_no_args(self, mock_environ):
210         SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
211         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
212         with mock.patch('yardstick.benchmark.core.task.os.environ',
213                         new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
214             task_files, task_args, task_args_fnames = t.parse_suite()
215         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
216                                                 task_args_fnames))
217         self.assertEqual(task_files[0], self.change_to_abspath(
218                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
219         self.assertEqual(task_files[1], self.change_to_abspath(
220                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
221         self.assertEqual(task_args[0], None)
222         self.assertEqual(task_args[1], None)
223         self.assertEqual(task_args_fnames[0], None)
224         self.assertEqual(task_args_fnames[1], None)
225
226     @mock.patch('yardstick.benchmark.core.task.os.environ')
227     def test_parse_suite_with_constraint_with_args(self, mock_environ):
228         SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
229         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
230         with mock.patch('yardstick.benchmark.core.task.os.environ',
231                         new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
232             task_files, task_args, task_args_fnames = t.parse_suite()
233         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
234                                                 task_args_fnames))
235         self.assertEqual(task_files[0], self.change_to_abspath(
236                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
237         self.assertEqual(task_files[1], self.change_to_abspath(
238                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
239         self.assertEqual(task_args[0], None)
240         self.assertEqual(task_args[1],
241                          '{"host": "node1.LF","target": "node2.LF"}')
242         self.assertEqual(task_args_fnames[0], None)
243         self.assertEqual(task_args_fnames[1], None)
244
245     def test_parse_options(self):
246         options = {
247             'openstack': {
248                 'EXTERNAL_NETWORK': '$network'
249             },
250             'ndoes': ['node1', '$node'],
251             'host': '$host'
252         }
253
254         t = task.Task()
255         t.outputs = {
256             'network': 'ext-net',
257             'node': 'node2',
258             'host': 'server.yardstick'
259         }
260
261         idle_result = {
262             'openstack': {
263                 'EXTERNAL_NETWORK': 'ext-net'
264             },
265             'ndoes': ['node1', 'node2'],
266             'host': 'server.yardstick'
267         }
268
269         actual_result = t._parse_options(options)
270         self.assertEqual(idle_result, actual_result)
271
272     def test_change_server_name_host_str(self):
273         scenario = {'host': 'demo'}
274         suffix = '-8'
275         task.change_server_name(scenario, suffix)
276         self.assertTrue(scenario['host'], 'demo-8')
277
278     def test_change_server_name_host_dict(self):
279         scenario = {'host': {'name': 'demo'}}
280         suffix = '-8'
281         task.change_server_name(scenario, suffix)
282         self.assertTrue(scenario['host']['name'], 'demo-8')
283
284     def test_change_server_name_target_str(self):
285         scenario = {'target': 'demo'}
286         suffix = '-8'
287         task.change_server_name(scenario, suffix)
288         self.assertTrue(scenario['target'], 'demo-8')
289
290     def test_change_server_name_target_dict(self):
291         scenario = {'target': {'name': 'demo'}}
292         suffix = '-8'
293         task.change_server_name(scenario, suffix)
294         self.assertTrue(scenario['target']['name'], 'demo-8')
295
296     @mock.patch('yardstick.benchmark.core.task.utils')
297     @mock.patch('yardstick.benchmark.core.task.logging')
298     def test_set_log(self, mock_logging, mock_utils):
299         task_obj = task.Task()
300         task_obj.task_id = 'task_id'
301         task_obj._set_log()
302         self.assertTrue(mock_logging.root.addHandler.called)
303
304     def _get_file_abspath(self, filename):
305         curr_path = os.path.dirname(os.path.abspath(__file__))
306         file_path = os.path.join(curr_path, filename)
307         return file_path
308
309     def change_to_abspath(self, filepath):
310         return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
311
312
313 def main():
314     unittest.main()
315
316
317 if __name__ == '__main__':
318     main()