Merge "Bugfix: task_id parameter from API can not pass to yardstick core"
[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 from __future__ import print_function
15
16 from __future__ import absolute_import
17 import os
18 import unittest
19
20 try:
21     from unittest import mock
22 except ImportError:
23     import mock
24
25
26 from yardstick.benchmark.core import task
27
28
29 class TaskTestCase(unittest.TestCase):
30
31     @mock.patch('yardstick.benchmark.core.task.Context')
32     def test_parse_nodes_host_target_same_context(self, mock_context):
33         nodes = {
34             "host": "node1.LF",
35             "target": "node2.LF"
36         }
37         scenario_cfg = {"nodes": nodes}
38         server_info = {
39             "ip": "10.20.0.3",
40             "user": "root",
41             "key_filename": "/root/.ssh/id_rsa"
42         }
43         mock_context.get_server.return_value = server_info
44         context_cfg = task.parse_nodes_with_context(scenario_cfg)
45
46         self.assertEqual(context_cfg["host"], server_info)
47         self.assertEqual(context_cfg["target"], server_info)
48
49     @mock.patch('yardstick.benchmark.core.task.Context')
50     @mock.patch('yardstick.benchmark.core.task.base_runner')
51     def test_run(self, mock_base_runner, mock_ctx):
52         scenario = {
53             'host': 'athena.demo',
54             'target': 'ares.demo',
55             'runner': {
56                 'duration': 60,
57                 'interval': 1,
58                 'type': 'Duration'
59             },
60             'type': 'Ping'
61         }
62
63         t = task.Task()
64         runner = mock.Mock()
65         runner.join.return_value = 0
66         mock_base_runner.Runner.get.return_value = runner
67         t._run([scenario], False, "yardstick.out")
68         self.assertTrue(runner.run.called)
69
70     @mock.patch('yardstick.benchmark.core.task.os')
71     def test_check_precondition(self, mock_os):
72         cfg = {
73             'precondition': {
74                 'installer_type': 'compass',
75                 'deploy_scenarios': 'os-nosdn',
76                 'pod_name': 'huawei-pod1'
77             }
78         }
79
80         t = task.TaskParser('/opt')
81         mock_os.environ.get.side_effect = ['compass',
82                                            'os-nosdn',
83                                            'huawei-pod1']
84         result = t._check_precondition(cfg)
85         self.assertTrue(result)
86
87     @mock.patch('yardstick.benchmark.core.task.os.environ')
88     def test_parse_suite_no_constraint_no_args(self, mock_environ):
89         SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
90         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
91         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
92         task_files, task_args, task_args_fnames = t.parse_suite()
93         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
94                                                 task_args_fnames))
95         self.assertEqual(task_files[0],
96                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
97         self.assertEqual(task_files[1],
98                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
99         self.assertEqual(task_args[0], None)
100         self.assertEqual(task_args[1], None)
101         self.assertEqual(task_args_fnames[0], None)
102         self.assertEqual(task_args_fnames[1], None)
103
104     @mock.patch('yardstick.benchmark.core.task.os.environ')
105     def test_parse_suite_no_constraint_with_args(self, mock_environ):
106         SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
107         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
108         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
109         task_files, task_args, task_args_fnames = t.parse_suite()
110         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
111                                                 task_args_fnames))
112         self.assertEqual(task_files[0],
113                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
114         self.assertEqual(task_files[1],
115                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
116         self.assertEqual(task_args[0], None)
117         self.assertEqual(task_args[1],
118                          '{"host": "node1.LF","target": "node2.LF"}')
119         self.assertEqual(task_args_fnames[0], None)
120         self.assertEqual(task_args_fnames[1], None)
121
122     @mock.patch('yardstick.benchmark.core.task.os.environ')
123     def test_parse_suite_with_constraint_no_args(self, mock_environ):
124         SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
125         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
126         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
127         task_files, task_args, task_args_fnames = t.parse_suite()
128         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
129                                                 task_args_fnames))
130         self.assertEqual(task_files[0],
131                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
132         self.assertEqual(task_files[1],
133                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
134         self.assertEqual(task_args[0], None)
135         self.assertEqual(task_args[1], None)
136         self.assertEqual(task_args_fnames[0], None)
137         self.assertEqual(task_args_fnames[1], None)
138
139     @mock.patch('yardstick.benchmark.core.task.os.environ')
140     def test_parse_suite_with_constraint_with_args(self, mock_environ):
141         SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
142         t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
143         mock_environ.get.side_effect = ['huawei-pod1', 'compass']
144         task_files, task_args, task_args_fnames = t.parse_suite()
145         print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
146                                                 task_args_fnames))
147         self.assertEqual(task_files[0],
148                          'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml')
149         self.assertEqual(task_files[1],
150                          'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml')
151         self.assertEqual(task_args[0], None)
152         self.assertEqual(task_args[1],
153                          '{"host": "node1.LF","target": "node2.LF"}')
154         self.assertEqual(task_args_fnames[0], None)
155         self.assertEqual(task_args_fnames[1], None)
156
157     def _get_file_abspath(self, filename):
158         curr_path = os.path.dirname(os.path.abspath(__file__))
159         file_path = os.path.join(curr_path, filename)
160         return file_path
161
162
163 def main():
164     unittest.main()
165
166
167 if __name__ == '__main__':
168     main()