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.cmd.commands.task
18 from yardstick.cmd.commands import task
21 class TaskCommandsTestCase(unittest.TestCase):
23 @mock.patch('yardstick.cmd.commands.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.cmd.commands.task.Context')
42 @mock.patch('yardstick.cmd.commands.task.base_runner')
43 def test_run(self, mock_base_runner, mock_ctx):
45 {'host': 'athena.demo',
46 'target': 'ares.demo',
54 t = task.TaskCommands()
56 runner.join.return_value = 0
57 mock_base_runner.Runner.get.return_value = runner
58 t._run([scenario], False, "yardstick.out")
59 self.assertTrue(runner.run.called)
61 @mock.patch('yardstick.cmd.commands.task.os')
62 def test_check_precondition(self, mock_os):
65 {'installer_type': 'compass',
66 'deploy_scenarios': 'os-nosdn',
67 'pod_name': 'huawei-pod1'
71 t = task.TaskParser('/opt')
72 mock_os.environ.get.side_effect = ['compass', 'os-nosdn', 'huawei-pod1']
73 result = t._check_precondition(cfg)
74 self.assertTrue(result)
76 @mock.patch('yardstick.cmd.commands.task.os.environ')
77 def test_parse_suite_no_constraint_no_args(self, mock_environ):
78 SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
79 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
80 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
81 task_files, task_args, task_args_fnames = t.parse_suite()
82 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
84 self.assertEqual(task_files[0],
85 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
86 self.assertEqual(task_files[1],
87 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
88 self.assertEqual(task_args[0], None)
89 self.assertEqual(task_args[1], None)
90 self.assertEqual(task_args_fnames[0], None)
91 self.assertEqual(task_args_fnames[1], None)
93 @mock.patch('yardstick.cmd.commands.task.os.environ')
94 def test_parse_suite_no_constraint_with_args(self, mock_environ):
95 SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
96 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
97 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
98 task_files, task_args, task_args_fnames = t.parse_suite()
99 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
101 self.assertEqual(task_files[0],
102 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
103 self.assertEqual(task_files[1],
104 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
105 self.assertEqual(task_args[0], None)
106 self.assertEqual(task_args[1],
107 '{"host": "node1.LF","target": "node2.LF"}')
108 self.assertEqual(task_args_fnames[0], None)
109 self.assertEqual(task_args_fnames[1], None)
111 @mock.patch('yardstick.cmd.commands.task.os.environ')
112 def test_parse_suite_with_constraint_no_args(self, mock_environ):
113 SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
114 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
115 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
116 task_files, task_args, task_args_fnames = t.parse_suite()
117 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
119 self.assertEqual(task_files[0],
120 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
121 self.assertEqual(task_files[1],
122 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
123 self.assertEqual(task_args[0], None)
124 self.assertEqual(task_args[1], None)
125 self.assertEqual(task_args_fnames[0], None)
126 self.assertEqual(task_args_fnames[1], None)
128 @mock.patch('yardstick.cmd.commands.task.os.environ')
129 def test_parse_suite_with_constraint_with_args(self, mock_environ):
130 SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
131 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
132 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
133 task_files, task_args, task_args_fnames = t.parse_suite()
134 print ("files=%s, args=%s, fnames=%s" % (task_files, task_args,
136 self.assertEqual(task_files[0],
137 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
138 self.assertEqual(task_files[1],
139 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
140 self.assertEqual(task_args[0], None)
141 self.assertEqual(task_args[1],
142 '{"host": "node1.LF","target": "node2.LF"}')
143 self.assertEqual(task_args_fnames[0], None)
144 self.assertEqual(task_args_fnames[1], None)
146 def _get_file_abspath(self, filename):
147 curr_path = os.path.dirname(os.path.abspath(__file__))
148 file_path = os.path.join(curr_path, filename)