Merge "apiserver: mock socket.gethostbyname"
[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     @mock.patch('yardstick.benchmark.core.task.os.environ')
169     def test_parse_suite_no_constraint_no_args(self, mock_environ):
170         SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
171         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
172         mock_environ.get.side_effect = ['huawei-pod1', '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         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
190         task_files, task_args, task_args_fnames = t.parse_suite()
191         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
192                                                 task_args_fnames))
193         self.assertEqual(task_files[0], self.change_to_abspath(
194                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
195         self.assertEqual(task_files[1], self.change_to_abspath(
196                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
197         self.assertEqual(task_args[0], None)
198         self.assertEqual(task_args[1],
199                          '{"host": "node1.LF","target": "node2.LF"}')
200         self.assertEqual(task_args_fnames[0], None)
201         self.assertEqual(task_args_fnames[1], None)
202
203     @mock.patch('yardstick.benchmark.core.task.os.environ')
204     def test_parse_suite_with_constraint_no_args(self, mock_environ):
205         SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
206         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
207         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
208         task_files, task_args, task_args_fnames = t.parse_suite()
209         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
210                                                 task_args_fnames))
211         self.assertEqual(task_files[0], self.change_to_abspath(
212                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
213         self.assertEqual(task_files[1], self.change_to_abspath(
214                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
215         self.assertEqual(task_args[0], None)
216         self.assertEqual(task_args[1], None)
217         self.assertEqual(task_args_fnames[0], None)
218         self.assertEqual(task_args_fnames[1], None)
219
220     @mock.patch('yardstick.benchmark.core.task.os.environ')
221     def test_parse_suite_with_constraint_with_args(self, mock_environ):
222         SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
223         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
224         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
225         task_files, task_args, task_args_fnames = t.parse_suite()
226         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
227                                                 task_args_fnames))
228         self.assertEqual(task_files[0], self.change_to_abspath(
229                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
230         self.assertEqual(task_files[1], self.change_to_abspath(
231                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
232         self.assertEqual(task_args[0], None)
233         self.assertEqual(task_args[1],
234                          '{"host": "node1.LF","target": "node2.LF"}')
235         self.assertEqual(task_args_fnames[0], None)
236         self.assertEqual(task_args_fnames[1], None)
237
238     def test_parse_options(self):
239         options = {
240             'openstack': {
241                 'EXTERNAL_NETWORK': '$network'
242             },
243             'ndoes': ['node1', '$node'],
244             'host': '$host'
245         }
246
247         t = task.Task()
248         t.outputs = {
249             'network': 'ext-net',
250             'node': 'node2',
251             'host': 'server.yardstick'
252         }
253
254         idle_result = {
255             'openstack': {
256                 'EXTERNAL_NETWORK': 'ext-net'
257             },
258             'ndoes': ['node1', 'node2'],
259             'host': 'server.yardstick'
260         }
261
262         actual_result = t._parse_options(options)
263         self.assertEqual(idle_result, actual_result)
264
265     def test_change_server_name_host_str(self):
266         scenario = {'host': 'demo'}
267         suffix = '-8'
268         task.change_server_name(scenario, suffix)
269         self.assertTrue(scenario['host'], 'demo-8')
270
271     def test_change_server_name_host_dict(self):
272         scenario = {'host': {'name': 'demo'}}
273         suffix = '-8'
274         task.change_server_name(scenario, suffix)
275         self.assertTrue(scenario['host']['name'], 'demo-8')
276
277     def test_change_server_name_target_str(self):
278         scenario = {'target': 'demo'}
279         suffix = '-8'
280         task.change_server_name(scenario, suffix)
281         self.assertTrue(scenario['target'], 'demo-8')
282
283     def test_change_server_name_target_dict(self):
284         scenario = {'target': {'name': 'demo'}}
285         suffix = '-8'
286         task.change_server_name(scenario, suffix)
287         self.assertTrue(scenario['target']['name'], 'demo-8')
288
289     @mock.patch('yardstick.benchmark.core.task.utils')
290     @mock.patch('yardstick.benchmark.core.task.logging')
291     def test_set_log(self, mock_logging, mock_utils):
292         task_obj = task.Task()
293         task_obj.task_id = 'task_id'
294         task_obj._set_log()
295         self.assertTrue(mock_logging.root.addHandler.called)
296
297     def _get_file_abspath(self, filename):
298         curr_path = os.path.dirname(os.path.abspath(__file__))
299         file_path = os.path.join(curr_path, filename)
300         return file_path
301
302     def change_to_abspath(self, filepath):
303         return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
304
305
306 def main():
307     unittest.main()
308
309
310 if __name__ == '__main__':
311     main()