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