Merge "testcase start service script fail on centos env"
[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 from yardstick.benchmark.scenarios import base
17 from yardstick.tests.unit import base as ut_base
18
19
20 class ScenarioTestCase(ut_base.BaseUnitTestCase):
21
22     def test_get_scenario_type(self):
23         scenario_type = 'dummy scenario'
24
25         class DummyScenario(base.Scenario):
26             __scenario_type__ = scenario_type
27
28         self.assertEqual(scenario_type, DummyScenario.get_scenario_type())
29
30     def test_get_scenario_type_not_defined(self):
31         class DummyScenario(base.Scenario):
32             pass
33
34         self.assertEqual(str(None), DummyScenario.get_scenario_type())
35
36     def test_get_description(self):
37         docstring = """First line
38             Second line
39             Third line
40         """
41
42         class DummyScenario(base.Scenario):
43             __doc__ = docstring
44
45         self.assertEqual(docstring.splitlines()[0],
46                          DummyScenario.get_description())
47
48     def test_get_description_empty(self):
49         class DummyScenario(base.Scenario):
50             pass
51
52         self.assertEqual(str(None), DummyScenario.get_description())
53
54     def test_get_types(self):
55         scenario_names = set(
56             scenario.__scenario_type__ for scenario in
57             base.Scenario.get_types() if hasattr(scenario,
58                                                  '__scenario_type__'))
59         existing_scenario_class_names = {
60             'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser'}
61         self.assertTrue(existing_scenario_class_names.issubset(scenario_names))
62
63     def test_get_cls_existing_scenario(self):
64         scenario_name = 'NSPerf'
65         scenario = base.Scenario.get_cls(scenario_name)
66         self.assertEqual(scenario_name, scenario.__scenario_type__)
67
68     def test_get_cls_non_existing_scenario(self):
69         wrong_scenario_name = 'Non-existing-scenario'
70         with self.assertRaises(RuntimeError) as exc:
71             base.Scenario.get_cls(wrong_scenario_name)
72         self.assertEqual('No such scenario type %s' % wrong_scenario_name,
73                          str(exc.exception))
74
75     def test_get_existing_scenario(self):
76         scenario_name = 'NSPerf'
77         scenario_module = ('yardstick.benchmark.scenarios.networking.'
78                            'vnf_generic.NetworkServiceTestCase')
79         self.assertEqual(scenario_module, base.Scenario.get(scenario_name))
80
81     def test_get_non_existing_scenario(self):
82         wrong_scenario_name = 'Non-existing-scenario'
83         with self.assertRaises(RuntimeError) as exc:
84             base.Scenario.get(wrong_scenario_name)
85         self.assertEqual('No such scenario type %s' % wrong_scenario_name,
86                          str(exc.exception))
87
88
89 class IterScenarioClassesTestCase(ut_base.BaseUnitTestCase):
90
91     def test_no_scenario_type_defined(self):
92         some_existing_scenario_class_names = [
93             'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser']
94         scenario_types = [scenario.__scenario_type__ for scenario
95                           in base._iter_scenario_classes()]
96         for class_name in some_existing_scenario_class_names:
97             self.assertIn(class_name, scenario_types)
98
99     def test_scenario_type_defined(self):
100         some_existing_scenario_class_names = [
101             'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser']
102         for class_name in some_existing_scenario_class_names:
103             scenario_class = next(base._iter_scenario_classes(
104                 scenario_type=class_name))
105             self.assertEqual(class_name, scenario_class.__scenario_type__)