aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/benchmark/scenarios/test_base.py
blob: 284a71cc8781bdd5c4d1fdf318593badb0d8fc04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright 2017: Intel Ltd.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import time

import mock

from yardstick.benchmark.scenarios import base
from yardstick.tests.unit import base as ut_base


class _TestScenario(base.Scenario):
    __scenario_type__ = 'Test Scenario'

    def run(self):
        pass


class ScenarioTestCase(ut_base.BaseUnitTestCase):

    def test_get_scenario_type(self):
        scenario_type = 'dummy scenario'

        class DummyScenario(base.Scenario):
            __scenario_type__ = scenario_type

        self.assertEqual(scenario_type, DummyScenario.get_scenario_type())

    def test_get_scenario_type_not_defined(self):
        class DummyScenario(base.Scenario):
            pass

        self.assertEqual(str(None), DummyScenario.get_scenario_type())

    def test_get_description(self):
        docstring = """First line
            Second line
            Third line
        """

        class DummyScenario(base.Scenario):
            __doc__ = docstring

        self.assertEqual(docstring.splitlines()[0],
                         DummyScenario.get_description())

    def test_get_description_empty(self):
        class DummyScenario(base.Scenario):
            pass

        self.assertEqual(str(None), DummyScenario.get_description())

    def test_get_types(self):
        scenario_names = set(
            scenario.__scenario_type__ for scenario in
            base.Scenario.get_types() if hasattr(scenario,
                                                 '__scenario_type__'))
        existing_scenario_class_names = {
            'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser'}
        self.assertTrue(existing_scenario_class_names.issubset(scenario_names))

    def test_get_cls_existing_scenario(self):
        scenario_name = 'NSPerf'
        scenario = base.Scenario.get_cls(scenario_name)
        self.assertEqual(scenario_name, scenario.__scenario_type__)

    def test_get_cls_non_existing_scenario(self):
        wrong_scenario_name = 'Non-existing-scenario'
        with self.assertRaises(RuntimeError) as exc:
            base.Scenario.get_cls(wrong_scenario_name)
        self.assertEqual('No such scenario type %s' % wrong_scenario_name,
                         str(exc.exception))

    def test_get_existing_scenario(self):
        scenario_name = 'NSPerf'
        scenario_module = ('yardstick.benchmark.scenarios.networking.'
                           'vnf_generic.NetworkServiceTestCase')
        self.assertEqual(scenario_module, base.Scenario.get(scenario_name))

    def test_get_non_existing_scenario(self):
        wrong_scenario_name = 'Non-existing-scenario'
        with self.assertRaises(RuntimeError) as exc:
            base.Scenario.get(wrong_scenario_name)
        self.assertEqual('No such scenario type %s' % wrong_scenario_name,
                         str(exc.exception))

    def test_scenario_abstract_class(self):
        # pylint: disable=abstract-class-instantiated
        with self.assertRaises(TypeError):
            base.Scenario()

    @mock.patch.object(time, 'sleep')
    def test_pre_run_wait_time(self, mock_sleep):
        """Ensure default behaviour (backwards compatibility): no wait time"""
        test_scenario = _TestScenario()
        test_scenario.pre_run_wait_time(mock.ANY)
        mock_sleep.assert_not_called()

    @mock.patch.object(time, 'sleep')
    def test_post_run_wait_time(self, mock_sleep):
        """Ensure default behaviour (backwards compatibility): wait time"""
        test_scenario = _TestScenario()
        test_scenario.post_run_wait_time(100)
        mock_sleep.assert_called_once_with(100)


class IterScenarioClassesTestCase(ut_base.BaseUnitTestCase):

    def test_no_scenario_type_defined(self):
        some_existing_scenario_class_names = [
            'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser']
        scenario_types = [scenario.__scenario_type__ for scenario
                          in base._iter_scenario_classes()]
        for class_name in some_existing_scenario_class_names:
            self.assertIn(class_name, scenario_types)

    def test_scenario_type_defined(self):
        some_existing_scenario_class_names = [
            'Iperf3', 'CACHEstat', 'SpecCPU2006', 'Dummy', 'NSPerf', 'Parser']
        for class_name in some_existing_scenario_class_names:
            scenario_class = next(base._iter_scenario_classes(
                scenario_type=class_name))
            self.assertEqual(class_name, scenario_class.__scenario_type__)