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