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 mock_base_runner.Runner.get.return_value = runner
68 t._run([scenario], False, "yardstick.out")
69 self.assertTrue(runner.run.called)
71 @mock.patch('yardstick.benchmark.core.task.os')
72 def test_check_precondition(self, mock_os):
75 'installer_type': 'compass',
76 'deploy_scenarios': 'os-nosdn',
77 'pod_name': 'huawei-pod1'
81 t = task.TaskParser('/opt')
82 mock_os.environ.get.side_effect = ['compass',
85 result = t._check_precondition(cfg)
86 self.assertTrue(result)
88 @mock.patch('yardstick.benchmark.core.task.os.environ')
89 def test_parse_suite_no_constraint_no_args(self, mock_environ):
90 SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
91 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
92 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
93 task_files, task_args, task_args_fnames = t.parse_suite()
94 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
96 self.assertEqual(task_files[0], self.change_to_abspath(
97 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
98 self.assertEqual(task_files[1], self.change_to_abspath(
99 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
100 self.assertEqual(task_args[0], None)
101 self.assertEqual(task_args[1], None)
102 self.assertEqual(task_args_fnames[0], None)
103 self.assertEqual(task_args_fnames[1], None)
105 @mock.patch('yardstick.benchmark.core.task.os.environ')
106 def test_parse_suite_no_constraint_with_args(self, mock_environ):
107 SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
108 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
109 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
110 task_files, task_args, task_args_fnames = t.parse_suite()
111 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
113 self.assertEqual(task_files[0], self.change_to_abspath(
114 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
115 self.assertEqual(task_files[1], self.change_to_abspath(
116 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
117 self.assertEqual(task_args[0], None)
118 self.assertEqual(task_args[1],
119 '{"host": "node1.LF","target": "node2.LF"}')
120 self.assertEqual(task_args_fnames[0], None)
121 self.assertEqual(task_args_fnames[1], None)
123 @mock.patch('yardstick.benchmark.core.task.os.environ')
124 def test_parse_suite_with_constraint_no_args(self, mock_environ):
125 SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
126 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
127 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
128 task_files, task_args, task_args_fnames = t.parse_suite()
129 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
131 self.assertEqual(task_files[0], self.change_to_abspath(
132 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
133 self.assertEqual(task_files[1], self.change_to_abspath(
134 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
135 self.assertEqual(task_args[0], None)
136 self.assertEqual(task_args[1], None)
137 self.assertEqual(task_args_fnames[0], None)
138 self.assertEqual(task_args_fnames[1], None)
140 @mock.patch('yardstick.benchmark.core.task.os.environ')
141 def test_parse_suite_with_constraint_with_args(self, mock_environ):
142 SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
143 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
144 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
145 task_files, task_args, task_args_fnames = t.parse_suite()
146 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
148 self.assertEqual(task_files[0], self.change_to_abspath(
149 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
150 self.assertEqual(task_files[1], self.change_to_abspath(
151 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
152 self.assertEqual(task_args[0], None)
153 self.assertEqual(task_args[1],
154 '{"host": "node1.LF","target": "node2.LF"}')
155 self.assertEqual(task_args_fnames[0], None)
156 self.assertEqual(task_args_fnames[1], None)
158 def _get_file_abspath(self, filename):
159 curr_path = os.path.dirname(os.path.abspath(__file__))
160 file_path = os.path.join(curr_path, filename)
163 def change_to_abspath(self, filepath):
164 return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
171 if __name__ == '__main__':