Merge "Improve "get_server" function in Kubernetes context"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / test_base.py
1 # Copyright 2017: Intel Ltd.
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 import time
17
18 import mock
19
20 from yardstick.benchmark.scenarios import base
21 from yardstick.tests.unit import base as ut_base
22
23
24 class _TestScenario(base.Scenario):
25     __scenario_type__ = 'Test Scenario'
26
27     def run(self):
28         pass
29
30
31 class ScenarioTestCase(ut_base.BaseUnitTestCase):
32
33     def test_get_scenario_type(self):
34         scenario_type = 'dummy scenario'
35
36         class DummyScenario(base.Scenario):
37             __scenario_type__ = scenario_type
38
39         self.assertEqual(scenario_type, DummyScenario.get_scenario_type())
40
41     def test_get_scenario_type_not_defined(self):
42         class DummyScenario(base.Scenario):
43             pass
44
45         self.assertEqual(str(None), DummyScenario.get_scenario_type())
46
47     def test_get_description(self):
48         docstring = """First line
49             Second line
50             Third line
51         """
52
53         class DummyScenario(base.Scenario):
54             __doc__ = docstring
55
56         self.assertEqual(docstring.splitlines()[0],
57                          DummyScenario.get_description())
58
59     def test_get_description_empty(self):
60         class DummyScenario(base.Scenario):
61             pass
62
63         self.assertEqual(str(None), DummyScenario.get_description())
64
65     def test_get_types(self):
66         scenario_names = set(
67             scenario.__scenario_type__ for scenario in
68             base.Scenario.get_types() if hasattr(scenario,
69                                                  '__scenario_type__'))
70         existing_scenario_class_names = {
71             'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser'}
72         self.assertTrue(existing_scenario_class_names.issubset(scenario_names))
73
74     def test_get_cls_existing_scenario(self):
75         scenario_name = 'NSPerf'
76         scenario = base.Scenario.get_cls(scenario_name)
77         self.assertEqual(scenario_name, scenario.__scenario_type__)
78
79     def test_get_cls_non_existing_scenario(self):
80         wrong_scenario_name = 'Non-existing-scenario'
81         with self.assertRaises(RuntimeError) as exc:
82             base.Scenario.get_cls(wrong_scenario_name)
83         self.assertEqual('No such scenario type %s' % wrong_scenario_name,
84                          str(exc.exception))
85
86     def test_get_existing_scenario(self):
87         scenario_name = 'NSPerf'
88         scenario_module = ('yardstick.benchmark.scenarios.networking.'
89                            'vnf_generic.NetworkServiceTestCase')
90         self.assertEqual(scenario_module, base.Scenario.get(scenario_name))
91
92     def test_get_non_existing_scenario(self):
93         wrong_scenario_name = 'Non-existing-scenario'
94         with self.assertRaises(RuntimeError) as exc:
95             base.Scenario.get(wrong_scenario_name)
96         self.assertEqual('No such scenario type %s' % wrong_scenario_name,
97                          str(exc.exception))
98
99     def test_scenario_abstract_class(self):
100         # pylint: disable=abstract-class-instantiated
101         with self.assertRaises(TypeError):
102             base.Scenario()
103
104     @mock.patch.object(time, 'sleep')
105     def test_pre_run_wait_time(self, mock_sleep):
106         """Ensure default behaviour (backwards compatibility): no wait time"""
107         test_scenario = _TestScenario()
108         test_scenario.pre_run_wait_time(mock.ANY)
109         mock_sleep.assert_not_called()
110
111     @mock.patch.object(time, 'sleep')
112     def test_post_run_wait_time(self, mock_sleep):
113         """Ensure default behaviour (backwards compatibility): wait time"""
114         test_scenario = _TestScenario()
115         test_scenario.post_run_wait_time(100)
116         mock_sleep.assert_called_once_with(100)
117
118
119 class IterScenarioClassesTestCase(ut_base.BaseUnitTestCase):
120
121     def test_no_scenario_type_defined(self):
122         some_existing_scenario_class_names = [
123             'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser']
124         scenario_types = [scenario.__scenario_type__ for scenario
125                           in base._iter_scenario_classes()]
126         for class_name in some_existing_scenario_class_names:
127             self.assertIn(class_name, scenario_types)
128
129     def test_scenario_type_defined(self):
130         some_existing_scenario_class_names = [
131             'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser']
132         for class_name in some_existing_scenario_class_names:
133             scenario_class = next(base._iter_scenario_classes(
134                 scenario_type=class_name))
135             self.assertEqual(class_name, scenario_class.__scenario_type__)