add yardstick iruya 9.0.0 release notes
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / networking / test_vsperf_dpdk.py
1 # Copyright 2017 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import subprocess
16 import time
17
18 import mock
19 import unittest
20
21 from yardstick import exceptions as y_exc
22 from yardstick.benchmark.scenarios.networking import vsperf_dpdk
23 from yardstick.common import exceptions as y_exc
24 from yardstick import ssh
25
26
27 class VsperfDPDKTestCase(unittest.TestCase):
28
29     def setUp(self):
30         self.ctx = {
31             "host": {
32                 "ip": "10.229.47.137",
33                 "user": "ubuntu",
34                 "password": "ubuntu",
35             },
36         }
37         self.args = {
38             'task_id': "1234-5678",
39             'options': {
40                 'testname': 'pvp_tput',
41                 'traffic_type': 'rfc2544_throughput',
42                 'frame_size': '64',
43                 'test_params': 'TRAFFICGEN_DURATION=30;',
44                 'trafficgen_port1': 'ens4',
45                 'trafficgen_port2': 'ens5',
46                 'conf_file': 'vsperf-yardstick.conf',
47                 'setup_script': 'setup_yardstick.sh',
48                 'moongen_helper_file': '~/moongen.py',
49                 'moongen_host_ip': '10.5.201.151',
50                 'moongen_port1_mac': '8c:dc:d4:ae:7c:5c',
51                 'moongen_port2_mac': '8c:dc:d4:ae:7c:5d',
52                 'trafficgen_port1_nw': 'test2',
53                 'trafficgen_port2_nw': 'test3',
54             },
55             'sla': {
56                 'metrics': 'throughput_rx_fps',
57                 'throughput_rx_fps': 500000,
58                 'action': 'monitor',
59             }
60         }
61         self._mock_ssh = mock.patch.object(ssh, 'SSH')
62         self.mock_ssh = self._mock_ssh.start()
63         self._mock_subprocess_call = mock.patch.object(subprocess, 'call')
64         self.mock_subprocess_call = self._mock_subprocess_call.start()
65         mock_call_obj = mock.Mock()
66         mock_call_obj.execute.return_value = None
67         self.mock_subprocess_call.return_value = mock_call_obj
68
69         self._mock_log_info = mock.patch.object(vsperf_dpdk.LOG, 'info')
70         self.mock_log_info = self._mock_log_info.start()
71
72         self.addCleanup(self._cleanup)
73
74         self.scenario = vsperf_dpdk.VsperfDPDK(self.args, self.ctx)
75         self.scenario.setup()
76
77     def _cleanup(self):
78         self._mock_ssh.stop()
79         self._mock_subprocess_call.stop()
80         self._mock_log_info.stop()
81
82     def test_setup(self):
83         self.assertIsNotNone(self.scenario.client)
84         self.assertTrue(self.scenario.setup_done)
85
86     def test_teardown(self):
87         self.scenario.teardown()
88         self.assertFalse(self.scenario.setup_done)
89
90     def test_is_dpdk_setup_no(self):
91         # is_dpdk_setup() specific mocks
92         self.mock_ssh.from_node().execute.return_value = (0, 'dummy', '')
93
94         self.assertFalse(self.scenario._is_dpdk_setup())
95
96     def test_is_dpdk_setup_yes(self):
97         # is_dpdk_setup() specific mocks
98         self.mock_ssh.from_node().execute.return_value = (0, '', '')
99
100         self.assertTrue(self.scenario._is_dpdk_setup())
101
102     @mock.patch.object(time, 'sleep')
103     def test_dpdk_setup_first(self, *args):
104         # is_dpdk_setup() specific mocks
105         self.mock_ssh.from_node().execute.return_value = (0, 'dummy', '')
106
107         self.scenario.dpdk_setup()
108         self.assertFalse(self.scenario._is_dpdk_setup())
109         self.assertTrue(self.scenario.dpdk_setup_done)
110
111     @mock.patch.object(time, 'sleep')
112     def test_dpdk_setup_next(self, *args):
113         self.mock_ssh.from_node().execute.return_value = (0, '', '')
114
115         self.scenario.dpdk_setup()
116         self.assertTrue(self.scenario._is_dpdk_setup())
117         self.assertTrue(self.scenario.dpdk_setup_done)
118
119     @mock.patch.object(subprocess, 'check_output')
120     def test_run_ok(self, *args):
121         # run() specific mocks
122         self.mock_ssh.from_node().execute.return_value = (
123             0, 'throughput_rx_fps\r\n14797660.000\r\n', '')
124
125         result = {}
126         self.scenario.run(result)
127         self.assertEqual(result['throughput_rx_fps'], '14797660.000')
128
129     @mock.patch.object(time, 'sleep')
130     @mock.patch.object(subprocess, 'check_output')
131     def test_vsperf_run_sla_fail(self, *args):
132         self.mock_ssh.from_node().execute.return_value = (
133             0, 'throughput_rx_fps\r\n123456.000\r\n', '')
134
135         with self.assertRaises(y_exc.SLAValidationError) as raised:
136             self.scenario.run({})
137
138         self.assertIn('VSPERF_throughput_rx_fps(123456.000000) < '
139                       'SLA_throughput_rx_fps(500000.000000)',
140                       str(raised.exception))
141
142     @mock.patch.object(time, 'sleep')
143     @mock.patch.object(subprocess, 'check_output')
144     def test_vsperf_run_sla_fail_metric_not_collected(self, *args):
145         self.mock_ssh.from_node().execute.return_value = (
146             0, 'nonexisting_metric\r\n123456.000\r\n', '')
147
148         with self.assertRaises(y_exc.SLAValidationError) as raised:
149             self.scenario.run({})
150
151         self.assertIn('throughput_rx_fps was not collected by VSPERF',
152                       str(raised.exception))
153
154     @mock.patch.object(time, 'sleep')
155     @mock.patch.object(subprocess, 'check_output')
156     def test_vsperf_run_sla_fail_metric_not_collected_faulty_csv(self, *args):
157         self.scenario.setup()
158
159         self.mock_ssh.from_node().execute.return_value = (
160             0, 'faulty output not csv', '')
161
162         with self.assertRaises(y_exc.SLAValidationError) as raised:
163             self.scenario.run({})
164
165         self.assertIn('throughput_rx_fps was not collected by VSPERF',
166                       str(raised.exception))
167
168     @mock.patch.object(time, 'sleep')
169     @mock.patch.object(subprocess, 'check_output')
170     def test_vsperf_run_sla_fail_sla_not_defined(self, *args):
171         del self.scenario.scenario_cfg['sla']['throughput_rx_fps']
172         self.scenario.setup()
173
174         self.mock_ssh.from_node().execute.return_value = (
175             0, 'throughput_rx_fps\r\n14797660.000\r\n', '')
176
177         with self.assertRaises(y_exc.SLAValidationError) as raised:
178             self.scenario.run({})
179
180         self.assertIn('throughput_rx_fps is not defined in SLA',
181                       str(raised.exception))