Merge "HA testcase containerized Compass support"
[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     @mock.patch('yardstick.benchmark.core.task.Context')
51     def test_parse_networks_from_nodes(self, mock_context):
52         nodes = {
53             'node1': {
54                 'interfaces': {
55                     'eth0': {
56                         'name': 'mgmt',
57                     },
58                     'eth1': {
59                         'name': 'external',
60                         'vld_id': '23',
61                     },
62                     'eth10': {
63                         'name': 'internal',
64                         'vld_id': '55',
65                     },
66                 },
67             },
68             'node2': {
69                 'interfaces': {
70                     'eth4': {
71                         'name': 'mgmt',
72                     },
73                     'eth2': {
74                         'name': 'external',
75                         'vld_id': '32',
76                     },
77                     'eth11': {
78                         'name': 'internal',
79                         'vld_id': '55',
80                     },
81                 },
82             },
83         }
84
85         mock_context.get_network.side_effect = iter([
86             None,
87             {
88                 'name': 'a',
89                 'network_type': 'private',
90             },
91             {},
92             {
93                 'name': 'b',
94                 'vld_id': 'y',
95                 'subnet_cidr': '10.20.0.0/16',
96             },
97             {
98                 'name': 'c',
99                 'vld_id': 'x',
100             },
101             {
102                 'name': 'd',
103                 'vld_id': 'w',
104             },
105         ])
106
107         expected_get_network_calls = 4 # once for each vld_id in the nodes dict
108         expected = {
109             'a': {'name': 'a', 'network_type': 'private'},
110             'b': {'name': 'b', 'vld_id': 'y', 'subnet_cidr': '10.20.0.0/16'},
111         }
112
113         networks = task.get_networks_from_nodes(nodes)
114         self.assertEqual(mock_context.get_network.call_count, expected_get_network_calls)
115         self.assertDictEqual(networks, expected)
116
117     @mock.patch('yardstick.benchmark.core.task.Context')
118     @mock.patch('yardstick.benchmark.core.task.base_runner')
119     def test_run(self, mock_base_runner, mock_ctx):
120         scenario = {
121             'host': 'athena.demo',
122             'target': 'ares.demo',
123             'runner': {
124                 'duration': 60,
125                 'interval': 1,
126                 'type': 'Duration'
127             },
128             'type': 'Ping'
129         }
130
131         t = task.Task()
132         runner = mock.Mock()
133         runner.join.return_value = 0
134         runner.get_output.return_value = {}
135         runner.get_result.return_value = []
136         mock_base_runner.Runner.get.return_value = runner
137         t._run([scenario], False, "yardstick.out")
138         self.assertTrue(runner.run.called)
139
140     @mock.patch('yardstick.benchmark.core.task.os')
141     def test_check_precondition(self, mock_os):
142         cfg = {
143             'precondition': {
144                 'installer_type': 'compass',
145                 'deploy_scenarios': 'os-nosdn',
146                 'pod_name': 'huawei-pod1'
147             }
148         }
149
150         t = task.TaskParser('/opt')
151         mock_os.environ.get.side_effect = ['compass',
152                                            'os-nosdn',
153                                            'huawei-pod1']
154         result = t._check_precondition(cfg)
155         self.assertTrue(result)
156
157     @mock.patch('yardstick.benchmark.core.task.os.environ')
158     def test_parse_suite_no_constraint_no_args(self, mock_environ):
159         SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
160         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
161         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
162         task_files, task_args, task_args_fnames = t.parse_suite()
163         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
164                                                 task_args_fnames))
165         self.assertEqual(task_files[0], self.change_to_abspath(
166                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
167         self.assertEqual(task_files[1], self.change_to_abspath(
168                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
169         self.assertEqual(task_args[0], None)
170         self.assertEqual(task_args[1], None)
171         self.assertEqual(task_args_fnames[0], None)
172         self.assertEqual(task_args_fnames[1], None)
173
174     @mock.patch('yardstick.benchmark.core.task.os.environ')
175     def test_parse_suite_no_constraint_with_args(self, mock_environ):
176         SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
177         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
178         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
179         task_files, task_args, task_args_fnames = t.parse_suite()
180         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
181                                                 task_args_fnames))
182         self.assertEqual(task_files[0], self.change_to_abspath(
183                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
184         self.assertEqual(task_files[1], self.change_to_abspath(
185                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
186         self.assertEqual(task_args[0], None)
187         self.assertEqual(task_args[1],
188                          '{"host": "node1.LF","target": "node2.LF"}')
189         self.assertEqual(task_args_fnames[0], None)
190         self.assertEqual(task_args_fnames[1], None)
191
192     @mock.patch('yardstick.benchmark.core.task.os.environ')
193     def test_parse_suite_with_constraint_no_args(self, mock_environ):
194         SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
195         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
196         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
197         task_files, task_args, task_args_fnames = t.parse_suite()
198         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
199                                                 task_args_fnames))
200         self.assertEqual(task_files[0], self.change_to_abspath(
201                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
202         self.assertEqual(task_files[1], self.change_to_abspath(
203                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
204         self.assertEqual(task_args[0], None)
205         self.assertEqual(task_args[1], None)
206         self.assertEqual(task_args_fnames[0], None)
207         self.assertEqual(task_args_fnames[1], None)
208
209     @mock.patch('yardstick.benchmark.core.task.os.environ')
210     def test_parse_suite_with_constraint_with_args(self, mock_environ):
211         SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
212         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
213         mock_environ.get.side_effect = ['huawei-pod1', '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],
223                          '{"host": "node1.LF","target": "node2.LF"}')
224         self.assertEqual(task_args_fnames[0], None)
225         self.assertEqual(task_args_fnames[1], None)
226
227     def test_parse_options(self):
228         options = {
229             'openstack': {
230                 'EXTERNAL_NETWORK': '$network'
231             },
232             'ndoes': ['node1', '$node'],
233             'host': '$host'
234         }
235
236         t = task.Task()
237         t.outputs = {
238             'network': 'ext-net',
239             'node': 'node2',
240             'host': 'server.yardstick'
241         }
242
243         idle_result = {
244             'openstack': {
245                 'EXTERNAL_NETWORK': 'ext-net'
246             },
247             'ndoes': ['node1', 'node2'],
248             'host': 'server.yardstick'
249         }
250
251         actual_result = t._parse_options(options)
252         self.assertEqual(idle_result, actual_result)
253
254     def test_change_server_name_host_str(self):
255         scenario = {'host': 'demo'}
256         suffix = '-8'
257         task.change_server_name(scenario, suffix)
258         self.assertTrue(scenario['host'], 'demo-8')
259
260     def test_change_server_name_host_dict(self):
261         scenario = {'host': {'name': 'demo'}}
262         suffix = '-8'
263         task.change_server_name(scenario, suffix)
264         self.assertTrue(scenario['host']['name'], 'demo-8')
265
266     def test_change_server_name_target_str(self):
267         scenario = {'target': 'demo'}
268         suffix = '-8'
269         task.change_server_name(scenario, suffix)
270         self.assertTrue(scenario['target'], 'demo-8')
271
272     def test_change_server_name_target_dict(self):
273         scenario = {'target': {'name': 'demo'}}
274         suffix = '-8'
275         task.change_server_name(scenario, suffix)
276         self.assertTrue(scenario['target']['name'], 'demo-8')
277
278     def _get_file_abspath(self, filename):
279         curr_path = os.path.dirname(os.path.abspath(__file__))
280         file_path = os.path.join(curr_path, filename)
281         return file_path
282
283     def change_to_abspath(self, filepath):
284         return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
285
286
287 def main():
288     unittest.main()
289
290
291 if __name__ == '__main__':
292     main()