Pass parameters between scenarios
[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     @mock.patch('yardstick.benchmark.core.task.base_runner')
52     def test_run(self, mock_base_runner, mock_ctx):
53         scenario = {
54             'host': 'athena.demo',
55             'target': 'ares.demo',
56             'runner': {
57                 'duration': 60,
58                 'interval': 1,
59                 'type': 'Duration'
60             },
61             'type': 'Ping'
62         }
63
64         t = task.Task()
65         runner = mock.Mock()
66         runner.join.return_value = 0
67         runner.get_output.return_value = {}
68         mock_base_runner.Runner.get.return_value = runner
69         t._run([scenario], False, "yardstick.out")
70         self.assertTrue(runner.run.called)
71
72     @mock.patch('yardstick.benchmark.core.task.os')
73     def test_check_precondition(self, mock_os):
74         cfg = {
75             'precondition': {
76                 'installer_type': 'compass',
77                 'deploy_scenarios': 'os-nosdn',
78                 'pod_name': 'huawei-pod1'
79             }
80         }
81
82         t = task.TaskParser('/opt')
83         mock_os.environ.get.side_effect = ['compass',
84                                            'os-nosdn',
85                                            'huawei-pod1']
86         result = t._check_precondition(cfg)
87         self.assertTrue(result)
88
89     @mock.patch('yardstick.benchmark.core.task.os.environ')
90     def test_parse_suite_no_constraint_no_args(self, mock_environ):
91         SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
92         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
93         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
94         task_files, task_args, task_args_fnames = t.parse_suite()
95         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
96                                                 task_args_fnames))
97         self.assertEqual(task_files[0], self.change_to_abspath(
98                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
99         self.assertEqual(task_files[1], self.change_to_abspath(
100                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
101         self.assertEqual(task_args[0], None)
102         self.assertEqual(task_args[1], None)
103         self.assertEqual(task_args_fnames[0], None)
104         self.assertEqual(task_args_fnames[1], None)
105
106     @mock.patch('yardstick.benchmark.core.task.os.environ')
107     def test_parse_suite_no_constraint_with_args(self, mock_environ):
108         SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
109         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
110         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
111         task_files, task_args, task_args_fnames = t.parse_suite()
112         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
113                                                 task_args_fnames))
114         self.assertEqual(task_files[0], self.change_to_abspath(
115                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
116         self.assertEqual(task_files[1], self.change_to_abspath(
117                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
118         self.assertEqual(task_args[0], None)
119         self.assertEqual(task_args[1],
120                          '{"host": "node1.LF","target": "node2.LF"}')
121         self.assertEqual(task_args_fnames[0], None)
122         self.assertEqual(task_args_fnames[1], None)
123
124     @mock.patch('yardstick.benchmark.core.task.os.environ')
125     def test_parse_suite_with_constraint_no_args(self, mock_environ):
126         SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
127         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
128         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
129         task_files, task_args, task_args_fnames = t.parse_suite()
130         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
131                                                 task_args_fnames))
132         self.assertEqual(task_files[0], self.change_to_abspath(
133                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
134         self.assertEqual(task_files[1], self.change_to_abspath(
135                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
136         self.assertEqual(task_args[0], None)
137         self.assertEqual(task_args[1], None)
138         self.assertEqual(task_args_fnames[0], None)
139         self.assertEqual(task_args_fnames[1], None)
140
141     @mock.patch('yardstick.benchmark.core.task.os.environ')
142     def test_parse_suite_with_constraint_with_args(self, mock_environ):
143         SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
144         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
145         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
146         task_files, task_args, task_args_fnames = t.parse_suite()
147         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
148                                                 task_args_fnames))
149         self.assertEqual(task_files[0], self.change_to_abspath(
150                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
151         self.assertEqual(task_files[1], self.change_to_abspath(
152                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
153         self.assertEqual(task_args[0], None)
154         self.assertEqual(task_args[1],
155                          '{"host": "node1.LF","target": "node2.LF"}')
156         self.assertEqual(task_args_fnames[0], None)
157         self.assertEqual(task_args_fnames[1], None)
158
159     def test_parse_options(self):
160         options = {
161             'openstack': {
162                 'EXTERNAL_NETWORK': '$network'
163             },
164             'ndoes': ['node1', '$node'],
165             'host': '$host'
166         }
167
168         t = task.Task()
169         t.outputs = {
170             'network': 'ext-net',
171             'node': 'node2',
172             'host': 'server.yardstick'
173         }
174
175         idle_result = {
176             'openstack': {
177                 'EXTERNAL_NETWORK': 'ext-net'
178             },
179             'ndoes': ['node1', 'node2'],
180             'host': 'server.yardstick'
181         }
182
183         actual_result = t._parse_options(options)
184         self.assertEqual(idle_result, actual_result)
185
186     def test_change_server_name_host_str(self):
187         scenario = {'host': 'demo'}
188         suffix = '-8'
189         task.change_server_name(scenario, suffix)
190         self.assertTrue(scenario['host'], 'demo-8')
191
192     def test_change_server_name_host_dict(self):
193         scenario = {'host': {'name': 'demo'}}
194         suffix = '-8'
195         task.change_server_name(scenario, suffix)
196         self.assertTrue(scenario['host']['name'], 'demo-8')
197
198     def test_change_server_name_target_str(self):
199         scenario = {'target': 'demo'}
200         suffix = '-8'
201         task.change_server_name(scenario, suffix)
202         self.assertTrue(scenario['target'], 'demo-8')
203
204     def test_change_server_name_target_dict(self):
205         scenario = {'target': {'name': 'demo'}}
206         suffix = '-8'
207         task.change_server_name(scenario, suffix)
208         self.assertTrue(scenario['target']['name'], 'demo-8')
209
210     def _get_file_abspath(self, filename):
211         curr_path = os.path.dirname(os.path.abspath(__file__))
212         file_path = os.path.join(curr_path, filename)
213         return file_path
214
215     def change_to_abspath(self, filepath):
216         return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
217
218
219 def main():
220     unittest.main()
221
222
223 if __name__ == '__main__':
224     main()