3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
12 # Unittest for yardstick.benchmark.core.task
14 from __future__ import print_function
16 from __future__ import absolute_import
21 from unittest import mock
26 from yardstick.benchmark.core import task
27 from yardstick.common import constants as consts
30 class TaskTestCase(unittest.TestCase):
32 @mock.patch('yardstick.benchmark.core.task.Context')
33 def test_parse_nodes_host_target_same_context(self, mock_context):
38 scenario_cfg = {"nodes": nodes}
42 "key_filename": "/root/.ssh/id_rsa"
44 mock_context.get_server.return_value = server_info
45 context_cfg = task.parse_nodes_with_context(scenario_cfg)
47 self.assertEqual(context_cfg["host"], server_info)
48 self.assertEqual(context_cfg["target"], server_info)
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):
54 'host': 'athena.demo',
55 'target': 'ares.demo',
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)
72 @mock.patch('yardstick.benchmark.core.task.os')
73 def test_check_precondition(self, mock_os):
76 'installer_type': 'compass',
77 'deploy_scenarios': 'os-nosdn',
78 'pod_name': 'huawei-pod1'
82 t = task.TaskParser('/opt')
83 mock_os.environ.get.side_effect = ['compass',
86 result = t._check_precondition(cfg)
87 self.assertTrue(result)
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,
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)
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,
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)
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,
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)
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,
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)
159 def test_parse_options(self):
162 'EXTERNAL_NETWORK': '$network'
164 'ndoes': ['node1', '$node'],
170 'network': 'ext-net',
172 'host': 'server.yardstick'
177 'EXTERNAL_NETWORK': 'ext-net'
179 'ndoes': ['node1', 'node2'],
180 'host': 'server.yardstick'
183 actual_result = t._parse_options(options)
184 self.assertEqual(idle_result, actual_result)
186 def test_change_server_name_host_str(self):
187 scenario = {'host': 'demo'}
189 task.change_server_name(scenario, suffix)
190 self.assertTrue(scenario['host'], 'demo-8')
192 def test_change_server_name_host_dict(self):
193 scenario = {'host': {'name': 'demo'}}
195 task.change_server_name(scenario, suffix)
196 self.assertTrue(scenario['host']['name'], 'demo-8')
198 def test_change_server_name_target_str(self):
199 scenario = {'target': 'demo'}
201 task.change_server_name(scenario, suffix)
202 self.assertTrue(scenario['target'], 'demo-8')
204 def test_change_server_name_target_dict(self):
205 scenario = {'target': {'name': 'demo'}}
207 task.change_server_name(scenario, suffix)
208 self.assertTrue(scenario['target']['name'], 'demo-8')
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)
215 def change_to_abspath(self, filepath):
216 return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
223 if __name__ == '__main__':