Merge "Adding vPE VNF class aligned with IETS per-deploy senarios"
[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 from yardstick.common import constants as consts
28
29
30 class TaskTestCase(unittest.TestCase):
31
32     @mock.patch('yardstick.benchmark.core.task.Context')
33     def test_parse_nodes_host_target_same_context(self, mock_context):
34         nodes = {
35             "host": "node1.LF",
36             "target": "node2.LF"
37         }
38         scenario_cfg = {"nodes": nodes}
39         server_info = {
40             "ip": "10.20.0.3",
41             "user": "root",
42             "key_filename": "/root/.ssh/id_rsa"
43         }
44         mock_context.get_server.return_value = server_info
45         context_cfg = task.parse_nodes_with_context(scenario_cfg)
46
47         self.assertEqual(context_cfg["host"], server_info)
48         self.assertEqual(context_cfg["target"], server_info)
49
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):
53         scenario = {
54             'host': 'athena.demo',
55             'target': 'ares.demo',
56             'runner': {
57                 'duration': 60,
58                 'interval': 1,
59                 'type': 'Duration'
60             },
61             'type': 'Ping'
62         }
63
64         t = task.Task()
65         runner = mock.Mock()
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)
70
71     @mock.patch('yardstick.benchmark.core.task.os')
72     def test_check_precondition(self, mock_os):
73         cfg = {
74             'precondition': {
75                 'installer_type': 'compass',
76                 'deploy_scenarios': 'os-nosdn',
77                 'pod_name': 'huawei-pod1'
78             }
79         }
80
81         t = task.TaskParser('/opt')
82         mock_os.environ.get.side_effect = ['compass',
83                                            'os-nosdn',
84                                            'huawei-pod1']
85         result = t._check_precondition(cfg)
86         self.assertTrue(result)
87
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,
95                                                 task_args_fnames))
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)
104
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,
112                                                 task_args_fnames))
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)
122
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,
130                                                 task_args_fnames))
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)
139
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,
147                                                 task_args_fnames))
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)
157
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)
161         return file_path
162
163     def change_to_abspath(self, filepath):
164         return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
165
166
167 def main():
168     unittest.main()
169
170
171 if __name__ == '__main__':
172     main()