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