aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/benchmark/scenarios/networking/test_vsperf.py
blob: a606543e5b9d06d5221be7d8f1ff821e0304aa4a (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright 2016 Intel Corporation.
#
# 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 mock
import unittest
import subprocess
import yardstick.ssh as ssh

from yardstick.benchmark.scenarios.networking import vsperf
from yardstick import exceptions as y_exc


class VsperfTestCase(unittest.TestCase):

    def setUp(self):
        self.context_cfg = {
            "host": {
                "ip": "10.229.47.137",
                "user": "ubuntu",
                "password": "ubuntu",
            },
        }
        self.scenario_cfg = {
            'options': {
                'testname': 'p2p_rfc2544_continuous',
                'traffic_type': 'continuous',
                'frame_size': '64',
                'bidirectional': 'True',
                'iload': 100,
                'trafficgen_port1': 'eth1',
                'trafficgen_port2': 'eth3',
                'external_bridge': 'br-ex',
                'conf_file': 'vsperf-yardstick.conf',
                'setup_script': 'setup_yardstick.sh',
                'test_params': 'TRAFFICGEN_DURATION=30;',
            },
            'sla': {
                'metrics': 'throughput_rx_fps',
                'throughput_rx_fps': 500000,
                'action': 'monitor',
            }
        }

        self._mock_SSH = mock.patch.object(ssh, 'SSH')
        self.mock_SSH = self._mock_SSH.start()
        self.mock_SSH.from_node().execute.return_value = (0, '', '')

        self._mock_subprocess_call = mock.patch.object(subprocess, 'call')
        self.mock_subprocess_call = self._mock_subprocess_call.start()
        self.mock_subprocess_call.return_value = None

        self.addCleanup(self._stop_mock)

        self.scenario = vsperf.Vsperf(self.scenario_cfg, self.context_cfg)

    def _stop_mock(self):
        self._mock_SSH.stop()
        self._mock_subprocess_call.stop()

    def test_setup(self):
        self.scenario.setup()
        self.assertIsNotNone(self.scenario.client)
        self.assertTrue(self.scenario.setup_done)

    def test_setup_tg_port_not_set(self):
        del self.scenario_cfg['options']['trafficgen_port1']
        del self.scenario_cfg['options']['trafficgen_port2']
        scenario = vsperf.Vsperf(self.scenario_cfg, self.context_cfg)
        scenario.setup()

        self.mock_subprocess_call.assert_called_once_with(
            'setup_yardstick.sh setup', shell=True)
        self.assertIsNone(scenario.tg_port1)
        self.assertIsNone(scenario.tg_port2)
        self.assertIsNotNone(scenario.client)
        self.assertTrue(scenario.setup_done)

    def test_setup_no_setup_script(self):
        del self.scenario_cfg['options']['setup_script']
        scenario = vsperf.Vsperf(self.scenario_cfg, self.context_cfg)
        scenario.setup()

        self.mock_subprocess_call.assert_has_calls(
            (mock.call('sudo bash -c "ovs-vsctl add-port br-ex eth1"',
                       shell=True),
             mock.call('sudo bash -c "ovs-vsctl add-port br-ex eth3"',
                       shell=True)))
        self.assertEqual(2, self.mock_subprocess_call.call_count)
        self.assertIsNone(scenario.setup_script)
        self.assertIsNotNone(scenario.client)
        self.assertTrue(scenario.setup_done)

    def test_run_ok(self):
        self.scenario.setup()

        self.mock_SSH.from_node().execute.return_value = (
            0, 'throughput_rx_fps\r\n14797660.000\r\n', '')

        result = {}
        self.scenario.run(result)

        self.assertEqual(result['throughput_rx_fps'], '14797660.000')

    def test_run_ok_setup_not_done(self):
        self.mock_SSH.from_node().execute.return_value = (
            0, 'throughput_rx_fps\r\n14797660.000\r\n', '')

        result = {}
        self.scenario.run(result)

        self.assertTrue(self.scenario.setup_done)
        self.assertEqual(result['throughput_rx_fps'], '14797660.000')

    def test_run_failed_vsperf_execution(self):
        self.mock_SSH.from_node().execute.side_effect = ((0, '', ''),
                                                         (1, '', ''))

        with self.assertRaises(RuntimeError):
            self.scenario.run({})
        self.assertEqual(self.mock_SSH.from_node().execute.call_count, 2)

    def test_run_failed_csv_report(self):
        self.mock_SSH.from_node().execute.side_effect = ((0, '', ''),
                                                         (0, '', ''),
                                                         (1, '', ''))

        with self.assertRaises(RuntimeError):
            self.scenario.run({})
        self.assertEqual(self.mock_SSH.from_node().execute.call_count, 3)

    def test_run_sla_fail(self):
        self.mock_SSH.from_node().execute.return_value = (
            0, 'throughput_rx_fps\r\n123456.000\r\n', '')

        with self.assertRaises(y_exc.SLAValidationError) as raised:
            self.scenario.run({})

        self.assertTrue('VSPERF_throughput_rx_fps(123456.000000) < '
                        'SLA_throughput_rx_fps(500000.000000)'
                        in str(raised.exception))

    def test_run_sla_fail_metric_not_collected(self):
        self.mock_SSH.from_node().execute.return_value = (
            0, 'nonexisting_metric\r\n14797660.000\r\n', '')

        with self.assertRaises(y_exc.SLAValidationError) as raised:
            self.scenario.run({})

        self.assertTrue('throughput_rx_fps was not collected by VSPERF'
                        in str(raised.exception))

    def test_run_sla_fail_metric_not_defined_in_sla(self):
        del self.scenario_cfg['sla']['throughput_rx_fps']
        scenario = vsperf.Vsperf(self.scenario_cfg, self.context_cfg)
        scenario.setup()

        self.mock_SSH.from_node().execute.return_value = (
            0, 'throughput_rx_fps\r\n14797660.000\r\n', '')

        with self.assertRaises(y_exc.SLAValidationError) as raised:
            scenario.run({})
        self.assertTrue('throughput_rx_fps is not defined in SLA'
                        in str(raised.exception))

    def test_teardown(self):
        self.scenario.setup()
        self.assertIsNotNone(self.scenario.client)
        self.assertTrue(self.scenario.setup_done)

        self.scenario.teardown()
        self.assertFalse(self.scenario.setup_done)

    def test_teardown_tg_port_not_set(self):
        del self.scenario_cfg['options']['trafficgen_port1']
        del self.scenario_cfg['options']['trafficgen_port2']
        scenario = vsperf.Vsperf(self.scenario_cfg, self.context_cfg)
        scenario.teardown()

        self.mock_subprocess_call.assert_called_once_with(
            'setup_yardstick.sh teardown', shell=True)
        self.assertFalse(scenario.setup_done)

    def test_teardown_no_setup_script(self):
        del self.scenario_cfg['options']['setup_script']
        scenario = vsperf.Vsperf(self.scenario_cfg, self.context_cfg)
        scenario.teardown()

        self.mock_subprocess_call.assert_has_calls(
            (mock.call('sudo bash -c "ovs-vsctl del-port br-ex eth1"',
                       shell=True),
             mock.call('sudo bash -c "ovs-vsctl del-port br-ex eth3"',
                       shell=True)))
        self.assertEqual(2, self.mock_subprocess_call.call_count)
        self.assertFalse(scenario.setup_done)