Merge "Add test case file, document and related scripts of yardstick tc058(HA_TC015)"
[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                     'eth0': {
70                         'name': 'mgmt',
71                     },
72                     'eth1': {
73                         'name': 'external',
74                         'vld_id': '23',
75                     },
76                     'eth10': {
77                         'name': 'internal',
78                         'vld_id': '55',
79                     },
80                 },
81             },
82             'node2': {
83                 'interfaces': {
84                     'eth4': {
85                         'name': 'mgmt',
86                     },
87                     'eth2': {
88                         'name': 'external',
89                         'vld_id': '32',
90                     },
91                     'eth11': {
92                         'name': 'internal',
93                         'vld_id': '55',
94                     },
95                 },
96             },
97         }
98
99         mock_context.get_network.side_effect = iter([
100             None,
101             {
102                 'name': 'a',
103                 'network_type': 'private',
104             },
105             {},
106             {
107                 'name': 'b',
108                 'vld_id': 'y',
109                 'subnet_cidr': '10.20.0.0/16',
110             },
111             {
112                 'name': 'c',
113                 'vld_id': 'x',
114             },
115             {
116                 'name': 'd',
117                 'vld_id': 'w',
118             },
119         ])
120
121         # once for each vld_id in the nodes dict
122         expected_get_network_calls = 4
123         expected = {
124             'a': {'name': 'a', 'network_type': 'private'},
125             'b': {'name': 'b', 'vld_id': 'y', 'subnet_cidr': '10.20.0.0/16'},
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     @mock.patch('yardstick.benchmark.core.task.os.environ')
173     def test_parse_suite_no_constraint_no_args(self, mock_environ):
174         SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
175         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
176         mock_environ.get.side_effect = ['huawei-pod1', '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         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
194         task_files, task_args, task_args_fnames = t.parse_suite()
195         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
196                                                 task_args_fnames))
197         self.assertEqual(task_files[0], self.change_to_abspath(
198                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
199         self.assertEqual(task_files[1], self.change_to_abspath(
200                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
201         self.assertEqual(task_args[0], None)
202         self.assertEqual(task_args[1],
203                          '{"host": "node1.LF","target": "node2.LF"}')
204         self.assertEqual(task_args_fnames[0], None)
205         self.assertEqual(task_args_fnames[1], None)
206
207     @mock.patch('yardstick.benchmark.core.task.os.environ')
208     def test_parse_suite_with_constraint_no_args(self, mock_environ):
209         SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
210         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
211         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
212         task_files, task_args, task_args_fnames = t.parse_suite()
213         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
214                                                 task_args_fnames))
215         self.assertEqual(task_files[0], self.change_to_abspath(
216                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
217         self.assertEqual(task_files[1], self.change_to_abspath(
218                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
219         self.assertEqual(task_args[0], None)
220         self.assertEqual(task_args[1], None)
221         self.assertEqual(task_args_fnames[0], None)
222         self.assertEqual(task_args_fnames[1], None)
223
224     @mock.patch('yardstick.benchmark.core.task.os.environ')
225     def test_parse_suite_with_constraint_with_args(self, mock_environ):
226         SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
227         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
228         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
229         task_files, task_args, task_args_fnames = t.parse_suite()
230         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
231                                                 task_args_fnames))
232         self.assertEqual(task_files[0], self.change_to_abspath(
233                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
234         self.assertEqual(task_files[1], self.change_to_abspath(
235                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
236         self.assertEqual(task_args[0], None)
237         self.assertEqual(task_args[1],
238                          '{"host": "node1.LF","target": "node2.LF"}')
239         self.assertEqual(task_args_fnames[0], None)
240         self.assertEqual(task_args_fnames[1], None)
241
242     def test_parse_options(self):
243         options = {
244             'openstack': {
245                 'EXTERNAL_NETWORK': '$network'
246             },
247             'ndoes': ['node1', '$node'],
248             'host': '$host'
249         }
250
251         t = task.Task()
252         t.outputs = {
253             'network': 'ext-net',
254             'node': 'node2',
255             'host': 'server.yardstick'
256         }
257
258         idle_result = {
259             'openstack': {
260                 'EXTERNAL_NETWORK': 'ext-net'
261             },
262             'ndoes': ['node1', 'node2'],
263             'host': 'server.yardstick'
264         }
265
266         actual_result = t._parse_options(options)
267         self.assertEqual(idle_result, actual_result)
268
269     def test_change_server_name_host_str(self):
270         scenario = {'host': 'demo'}
271         suffix = '-8'
272         task.change_server_name(scenario, suffix)
273         self.assertTrue(scenario['host'], 'demo-8')
274
275     def test_change_server_name_host_dict(self):
276         scenario = {'host': {'name': 'demo'}}
277         suffix = '-8'
278         task.change_server_name(scenario, suffix)
279         self.assertTrue(scenario['host']['name'], 'demo-8')
280
281     def test_change_server_name_target_str(self):
282         scenario = {'target': 'demo'}
283         suffix = '-8'
284         task.change_server_name(scenario, suffix)
285         self.assertTrue(scenario['target'], 'demo-8')
286
287     def test_change_server_name_target_dict(self):
288         scenario = {'target': {'name': 'demo'}}
289         suffix = '-8'
290         task.change_server_name(scenario, suffix)
291         self.assertTrue(scenario['target']['name'], 'demo-8')
292
293     @mock.patch('yardstick.benchmark.core.task.utils')
294     @mock.patch('yardstick.benchmark.core.task.logging')
295     def test_set_log(self, mock_logging, mock_utils):
296         task_obj = task.Task()
297         task_obj.task_id = 'task_id'
298         task_obj._set_log()
299         self.assertTrue(mock_logging.root.addHandler.called)
300
301     def _get_file_abspath(self, filename):
302         curr_path = os.path.dirname(os.path.abspath(__file__))
303         file_path = os.path.join(curr_path, filename)
304         return file_path
305
306     def change_to_abspath(self, filepath):
307         return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
308
309
310 def main():
311     unittest.main()
312
313
314 if __name__ == '__main__':
315     main()