1 # Copyright 2017: Intel Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
20 from yardstick.benchmark.scenarios import base
21 from yardstick.tests.unit import base as ut_base
24 class _TestScenario(base.Scenario):
25 __scenario_type__ = 'Test Scenario'
31 class ScenarioTestCase(ut_base.BaseUnitTestCase):
33 def test_get_scenario_type(self):
34 scenario_type = 'dummy scenario'
36 class DummyScenario(base.Scenario):
37 __scenario_type__ = scenario_type
39 self.assertEqual(scenario_type, DummyScenario.get_scenario_type())
41 def test_get_scenario_type_not_defined(self):
42 class DummyScenario(base.Scenario):
45 self.assertEqual(str(None), DummyScenario.get_scenario_type())
47 def test_get_description(self):
48 docstring = """First line
53 class DummyScenario(base.Scenario):
56 self.assertEqual(docstring.splitlines()[0],
57 DummyScenario.get_description())
59 def test_get_description_empty(self):
60 class DummyScenario(base.Scenario):
63 self.assertEqual(str(None), DummyScenario.get_description())
65 def test_get_types(self):
67 scenario.__scenario_type__ for scenario in
68 base.Scenario.get_types() if hasattr(scenario,
70 existing_scenario_class_names = {
71 'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser'}
72 self.assertTrue(existing_scenario_class_names.issubset(scenario_names))
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__)
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,
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))
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,
99 def test_scenario_abstract_class(self):
100 # pylint: disable=abstract-class-instantiated
101 with self.assertRaises(TypeError):
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()
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)
119 class IterScenarioClassesTestCase(ut_base.BaseUnitTestCase):
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)
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__)