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
18 from yardstick.benchmark.core import task
21 class TaskTestCase(unittest.TestCase):
23 @mock.patch('yardstick.benchmark.core.task.Context')
24 def test_parse_nodes_host_target_same_context(self, mock_context):
29 scenario_cfg = {"nodes": nodes}
33 "key_filename": "/root/.ssh/id_rsa"
35 mock_context.get_server.return_value = server_info
36 context_cfg = task.parse_nodes_with_context(scenario_cfg)
38 self.assertEqual(context_cfg["host"], server_info)
39 self.assertEqual(context_cfg["target"], server_info)
41 @mock.patch('yardstick.benchmark.core.task.Context')
42 @mock.patch('yardstick.benchmark.core.task.base_runner')
43 def test_run(self, mock_base_runner, mock_ctx):
45 'host': 'athena.demo',
46 'target': 'ares.demo',
57 runner.join.return_value = 0
58 mock_base_runner.Runner.get.return_value = runner
59 t._run([scenario], False, "yardstick.out")
60 self.assertTrue(runner.run.called)
62 @mock.patch('yardstick.benchmark.core.task.os')
63 def test_check_precondition(self, mock_os):
66 'installer_type': 'compass',
67 'deploy_scenarios': 'os-nosdn',
68 'pod_name': 'huawei-pod1'
72 t = task.TaskParser('/opt')
73 mock_os.environ.get.side_effect = ['compass',
76 result = t._check_precondition(cfg)
77 self.assertTrue(result)
79 @mock.patch('yardstick.benchmark.core.task.os.environ')
80 def test_parse_suite_no_constraint_no_args(self, mock_environ):
81 SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
82 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
83 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
84 task_files, task_args, task_args_fnames = t.parse_suite()
85 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
87 self.assertEqual(task_files[0],
88 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
89 self.assertEqual(task_files[1],
90 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
91 self.assertEqual(task_args[0], None)
92 self.assertEqual(task_args[1], None)
93 self.assertEqual(task_args_fnames[0], None)
94 self.assertEqual(task_args_fnames[1], None)
96 @mock.patch('yardstick.benchmark.core.task.os.environ')
97 def test_parse_suite_no_constraint_with_args(self, mock_environ):
98 SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
99 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
100 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
101 task_files, task_args, task_args_fnames = t.parse_suite()
102 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
104 self.assertEqual(task_files[0],
105 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
106 self.assertEqual(task_files[1],
107 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
108 self.assertEqual(task_args[0], None)
109 self.assertEqual(task_args[1],
110 '{"host": "node1.LF","target": "node2.LF"}')
111 self.assertEqual(task_args_fnames[0], None)
112 self.assertEqual(task_args_fnames[1], None)
114 @mock.patch('yardstick.benchmark.core.task.os.environ')
115 def test_parse_suite_with_constraint_no_args(self, mock_environ):
116 SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
117 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
118 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
119 task_files, task_args, task_args_fnames = t.parse_suite()
120 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
122 self.assertEqual(task_files[0],
123 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
124 self.assertEqual(task_files[1],
125 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
126 self.assertEqual(task_args[0], None)
127 self.assertEqual(task_args[1], None)
128 self.assertEqual(task_args_fnames[0], None)
129 self.assertEqual(task_args_fnames[1], None)
131 @mock.patch('yardstick.benchmark.core.task.os.environ')
132 def test_parse_suite_with_constraint_with_args(self, mock_environ):
133 SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
134 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
135 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
136 task_files, task_args, task_args_fnames = t.parse_suite()
137 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
139 self.assertEqual(task_files[0],
140 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
141 self.assertEqual(task_files[1],
142 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
143 self.assertEqual(task_args[0], None)
144 self.assertEqual(task_args[1],
145 '{"host": "node1.LF","target": "node2.LF"}')
146 self.assertEqual(task_args_fnames[0], None)
147 self.assertEqual(task_args_fnames[1], None)
149 def _get_file_abspath(self, filename):
150 curr_path = os.path.dirname(os.path.abspath(__file__))
151 file_path = os.path.join(curr_path, filename)
159 if __name__ == '__main__':