Merge "Record test case names when run a task using API"
[yardstick.git] / tests / unit / benchmark / core / test_task.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
5 #
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 ##############################################################################
11
12 # Unittest for yardstick.benchmark.core.task
13
14 import os
15 import mock
16 import unittest
17
18 from yardstick.benchmark.core import task
19
20
21 class TaskTestCase(unittest.TestCase):
22
23     @mock.patch('yardstick.benchmark.core.task.Context')
24     def test_parse_nodes_host_target_same_context(self, mock_context):
25         nodes = {
26             "host": "node1.LF",
27             "target": "node2.LF"
28         }
29         scenario_cfg = {"nodes": nodes}
30         server_info = {
31            "ip": "10.20.0.3",
32            "user": "root",
33            "key_filename": "/root/.ssh/id_rsa"
34         }
35         mock_context.get_server.return_value = server_info
36         context_cfg = task.parse_nodes_with_context(scenario_cfg)
37
38         self.assertEqual(context_cfg["host"], server_info)
39         self.assertEqual(context_cfg["target"], server_info)
40
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):
44         scenario = {
45             'host': 'athena.demo',
46             'target': 'ares.demo',
47             'runner': {
48                 'duration': 60,
49                 'interval': 1,
50                 'type': 'Duration'
51              },
52             'type': 'Ping'
53         }
54
55         t = task.Task()
56         runner = mock.Mock()
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)
61
62     @mock.patch('yardstick.benchmark.core.task.os')
63     def test_check_precondition(self, mock_os):
64         cfg = {
65             'precondition': {
66                 'installer_type': 'compass',
67                 'deploy_scenarios': 'os-nosdn',
68                 'pod_name': 'huawei-pod1'
69             }
70         }
71
72         t = task.TaskParser('/opt')
73         mock_os.environ.get.side_effect = ['compass',
74                                            'os-nosdn',
75                                            'huawei-pod1']
76         result = t._check_precondition(cfg)
77         self.assertTrue(result)
78
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,
86                task_args_fnames))
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)
95
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,
103                task_args_fnames))
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)
113
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,
121                task_args_fnames))
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)
130
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,
138                task_args_fnames))
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)
148
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)
152         return file_path
153
154
155 def main():
156     unittest.main()
157
158
159 if __name__ == '__main__':
160     main()