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