Merge "Increase Ping scenario ssh timeout limit to 600 seconds"
[yardstick.git] / tests / unit / cmd / commands / 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.cmd.commands.task
13
14 import os
15 import mock
16 import unittest
17
18 from yardstick.cmd.commands import task
19
20
21 class TaskCommandsTestCase(unittest.TestCase):
22
23     @mock.patch('yardstick.cmd.commands.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.cmd.commands.task.Context')
42     @mock.patch('yardstick.cmd.commands.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         t = task.TaskCommands()
55         runner = mock.Mock()
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)
60
61     @mock.patch('yardstick.cmd.commands.task.os')
62     def test_check_precondition(self, mock_os):
63         cfg = \
64             {'precondition':
65                  {'installer_type': 'compass',
66                   'deploy_scenarios': 'os-nosdn',
67                   'pod_name': 'huawei-pod1'
68                  }
69             }
70
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)
75
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,
83                task_args_fnames))
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)
92
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,
100                task_args_fnames))
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)
110
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,
118                task_args_fnames))
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)
127
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,
135                task_args_fnames))
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)
145
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)
149         return file_path
150