Merge "Bump pip packages installed in "nsb_setup.sh" script"
[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.benchmark.scenarios.networking import vsperf_dpdk
22
23
24 class VsperfDPDKTestCase(unittest.TestCase):
25
26     def setUp(self):
27         self.ctx = {
28             "host": {
29                 "ip": "10.229.47.137",
30                 "user": "ubuntu",
31                 "password": "ubuntu",
32             },
33         }
34         self.args = {
35             'task_id': "1234-5678",
36             'options': {
37                 'testname': 'pvp_tput',
38                 'traffic_type': 'rfc2544_throughput',
39                 'frame_size': '64',
40                 'test_params': 'TRAFFICGEN_DURATION=30;',
41                 'trafficgen_port1': 'ens4',
42                 'trafficgen_port2': 'ens5',
43                 'conf_file': 'vsperf-yardstick.conf',
44                 'setup_script': 'setup_yardstick.sh',
45                 'moongen_helper_file': '~/moongen.py',
46                 'moongen_host_ip': '10.5.201.151',
47                 'moongen_port1_mac': '8c:dc:d4:ae:7c:5c',
48                 'moongen_port2_mac': '8c:dc:d4:ae:7c:5d',
49                 'trafficgen_port1_nw': 'test2',
50                 'trafficgen_port2_nw': 'test3',
51             },
52             'sla': {
53                 'metrics': 'throughput_rx_fps',
54                 'throughput_rx_fps': 500000,
55                 'action': 'monitor',
56             }
57         }
58
59         self.scenario = vsperf_dpdk.VsperfDPDK(self.args, self.ctx)
60
61         self._mock_ssh = mock.patch(
62             'yardstick.benchmark.scenarios.networking.vsperf_dpdk.ssh')
63         self.mock_ssh = self._mock_ssh.start()
64         self._mock_subprocess_call = mock.patch.object(subprocess, 'call')
65         self.mock_subprocess_call = self._mock_subprocess_call.start()
66
67         self.addCleanup(self._cleanup)
68
69     def _cleanup(self):
70         self._mock_ssh.stop()
71         self._mock_subprocess_call.stop()
72
73     def test_setup(self):
74         # setup() specific mocks
75         self.mock_subprocess_call().execute.return_value = None
76
77         self.scenario.setup()
78         self.assertIsNotNone(self.scenario.client)
79         self.assertTrue(self.scenario.setup_done)
80
81     def test_teardown(self):
82         # setup() specific mocks
83         self.mock_subprocess_call().execute.return_value = None
84
85         self.scenario.setup()
86         self.assertIsNotNone(self.scenario.client)
87         self.assertTrue(self.scenario.setup_done)
88
89         self.scenario.teardown()
90         self.assertFalse(self.scenario.setup_done)
91
92     def test_is_dpdk_setup_no(self):
93         # setup() specific mocks
94         self.mock_subprocess_call().execute.return_value = None
95
96         self.scenario.setup()
97         self.assertIsNotNone(self.scenario.client)
98         self.assertTrue(self.scenario.setup_done)
99
100         # is_dpdk_setup() specific mocks
101         self.mock_ssh.SSH.from_node().execute.return_value = (0, 'dummy', '')
102
103         result = self.scenario._is_dpdk_setup()
104         self.assertFalse(result)
105
106     def test_is_dpdk_setup_yes(self):
107         # setup() specific mocks
108         self.mock_subprocess_call().execute.return_value = None
109
110         self.scenario.setup()
111         self.assertIsNotNone(self.scenario.client)
112         self.assertTrue(self.scenario.setup_done)
113
114         # is_dpdk_setup() specific mocks
115         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
116
117         result = self.scenario._is_dpdk_setup()
118         self.assertTrue(result)
119
120     @mock.patch.object(time, 'sleep')
121     def test_dpdk_setup_first(self, *args):
122         # setup() specific mocks
123         self.mock_subprocess_call().execute.return_value = None
124
125         self.scenario.setup()
126         self.assertIsNotNone(self.scenario.client)
127         self.assertTrue(self.scenario.setup_done)
128
129         # is_dpdk_setup() specific mocks
130         self.mock_ssh.SSH.from_node().execute.return_value = (0, 'dummy', '')
131
132         self.scenario.dpdk_setup()
133         self.assertFalse(self.scenario._is_dpdk_setup())
134         self.assertTrue(self.scenario.dpdk_setup_done)
135
136     @mock.patch.object(time, 'sleep')
137     def test_dpdk_setup_next(self, *args):
138         # setup() specific mocks
139         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
140         self.mock_subprocess_call().execute.return_value = None
141
142         self.scenario.setup()
143         self.assertIsNotNone(self.scenario.client)
144         self.assertTrue(self.scenario.setup_done)
145
146         self.scenario.dpdk_setup()
147         self.assertTrue(self.scenario._is_dpdk_setup())
148         self.assertTrue(self.scenario.dpdk_setup_done)
149
150     @mock.patch.object(time, 'sleep')
151     def test_dpdk_setup_runtime_error(self, *args):
152
153         # setup specific mocks
154         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
155         self.mock_subprocess_call().execute.return_value = None
156
157         self.scenario.setup()
158         self.assertIsNotNone(self.scenario.client)
159         self.mock_ssh.SSH.from_node().execute.return_value = (1, '', '')
160         self.assertTrue(self.scenario.setup_done)
161
162         self.assertRaises(RuntimeError, self.scenario.dpdk_setup)
163
164     @mock.patch.object(subprocess, 'check_output')
165     @mock.patch('time.sleep')
166     def test_run_ok(self, *args):
167         # setup() specific mocks
168         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
169         self.mock_subprocess_call().execute.return_value = None
170
171         self.scenario.setup()
172         self.assertIsNotNone(self.scenario.client)
173         self.assertTrue(self.scenario.setup_done)
174
175         # run() specific mocks
176         self.mock_subprocess_call().execute.return_value = None
177         self.mock_ssh.SSH.from_node().execute.return_value = (
178             0, 'throughput_rx_fps\r\n14797660.000\r\n', '')
179
180         result = {}
181         self.scenario.run(result)
182
183         self.assertEqual(result['throughput_rx_fps'], '14797660.000')
184
185     def test_run_failed_vsperf_execution(self):
186         # setup() specific mocks
187         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
188         self.mock_subprocess_call().execute.return_value = None
189
190         self.scenario.setup()
191         self.assertIsNotNone(self.scenario.client)
192         self.assertTrue(self.scenario.setup_done)
193
194         self.mock_ssh.SSH.from_node().execute.return_value = (1, '', '')
195
196         result = {}
197         self.assertRaises(RuntimeError, self.scenario.run, result)
198
199     def test_run_falied_csv_report(self):
200         # setup() specific mocks
201         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
202         self.mock_subprocess_call().execute.return_value = None
203
204         self.scenario.setup()
205         self.assertIsNotNone(self.scenario.client)
206         self.assertTrue(self.scenario.setup_done)
207
208         # run() specific mocks
209         self.mock_subprocess_call().execute.return_value = None
210         self.mock_ssh.SSH.from_node().execute.return_value = (1, '', '')
211
212         result = {}
213         self.assertRaises(RuntimeError, self.scenario.run, result)
214
215
216 def main():
217     unittest.main()
218
219
220 if __name__ == '__main__':
221     main()