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 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)
73 @mock.patch('yardstick.benchmark.core.task.os')
74 def test_check_precondition(self, mock_os):
77 'installer_type': 'compass',
78 'deploy_scenarios': 'os-nosdn',
79 'pod_name': 'huawei-pod1'
83 t = task.TaskParser('/opt')
84 mock_os.environ.get.side_effect = ['compass',
87 result = t._check_precondition(cfg)
88 self.assertTrue(result)
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,
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)
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,
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)
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,
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)
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,
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)
160 def test_parse_options(self):
163 'EXTERNAL_NETWORK': '$network'
165 'ndoes': ['node1', '$node'],
171 'network': 'ext-net',
173 'host': 'server.yardstick'
178 'EXTERNAL_NETWORK': 'ext-net'
180 'ndoes': ['node1', 'node2'],
181 'host': 'server.yardstick'
184 actual_result = t._parse_options(options)
185 self.assertEqual(idle_result, actual_result)
187 def test_change_server_name_host_str(self):
188 scenario = {'host': 'demo'}
190 task.change_server_name(scenario, suffix)
191 self.assertTrue(scenario['host'], 'demo-8')
193 def test_change_server_name_host_dict(self):
194 scenario = {'host': {'name': 'demo'}}
196 task.change_server_name(scenario, suffix)
197 self.assertTrue(scenario['host']['name'], 'demo-8')
199 def test_change_server_name_target_str(self):
200 scenario = {'target': 'demo'}
202 task.change_server_name(scenario, suffix)
203 self.assertTrue(scenario['target'], 'demo-8')
205 def test_change_server_name_target_dict(self):
206 scenario = {'target': {'name': 'demo'}}
208 task.change_server_name(scenario, suffix)
209 self.assertTrue(scenario['target']['name'], 'demo-8')
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)
216 def change_to_abspath(self, filepath):
217 return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
224 if __name__ == '__main__':