Merge "Replace assertEqual(x, True|False) with assert[True|False](x)"
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_pktgen_dpdk_throughput.py
1 ##############################################################################
2 # Copyright (c) 2017 Nokia and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 #!/usr/bin/env python
10
11 # Unittest for yardstick.benchmark.scenarios.networking.pktgen.PktgenDPDK
12
13 from __future__ import absolute_import
14 import unittest
15
16 from oslo_serialization import jsonutils
17 import mock
18
19 from yardstick.benchmark.scenarios.networking import pktgen_dpdk_throughput
20
21
22 @mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.ssh')
23 class PktgenDPDKTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self.ctx = {
27             'host': {
28                 'ip': '172.16.0.137',
29                 'user': 'root',
30                 'key_filename': 'mykey.key'
31             },
32             'target': {
33                 'ip': '172.16.0.138',
34                 'user': 'root',
35                 'key_filename': 'mykey.key',
36             }
37         }
38
39         self._mock_time = mock.patch(
40             'yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.time')
41         self.mock_time = self._mock_time.start()
42
43         self.addCleanup(self._cleanup)
44
45     def _cleanup(self):
46         self._mock_time.stop()
47
48     def test_pktgen_dpdk_throughput_successful_setup(self, mock_ssh):
49         args = {
50             'options': {'packetsize': 60},
51         }
52         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
53         p.setup()
54
55         mock_ssh.SSH().execute.return_value = (0, '', '')
56         self.assertIsNotNone(p.server)
57         self.assertIsNotNone(p.client)
58         self.assertTrue(p.setup_done)
59
60     def test_pktgen_dpdk_throughput_successful_no_sla(self, mock_ssh):
61         args = {
62             'options': {'packetsize': 60, 'number_of_ports': 10},
63         }
64
65         result = {}
66
67         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
68
69         p.server = mock_ssh.SSH()
70         p.client = mock_ssh.SSH()
71
72         mock_dpdk_result = mock.Mock()
73         mock_dpdk_result.return_value = 149300
74         p._dpdk_get_result = mock_dpdk_result
75
76         sample_output = '{"packets_per_second": 9753, "errors": 0, \
77             "packets_sent": 149776, "flows": 110}'
78         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
79
80         p.run(result)
81         expected_result = jsonutils.loads(sample_output)
82         expected_result["packets_received"] = 149300
83         expected_result["packetsize"] = 60
84         self.assertEqual(result, expected_result)
85
86     def test_pktgen_dpdk_throughput_successful_sla(self, mock_ssh):
87         args = {
88             'options': {'packetsize': 60, 'number_of_ports': 10},
89             'sla': {'max_ppm': 10000}
90         }
91         result = {}
92
93         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
94
95         p.server = mock_ssh.SSH()
96         p.client = mock_ssh.SSH()
97
98         mock_dpdk_result = mock.Mock()
99         mock_dpdk_result.return_value = 149300
100         p._dpdk_get_result = mock_dpdk_result
101
102         sample_output = '{"packets_per_second": 9753, "errors": 0, \
103             "packets_sent": 149776, "flows": 110}'
104         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
105
106         p.run(result)
107         expected_result = jsonutils.loads(sample_output)
108         expected_result["packets_received"] = 149300
109         expected_result["packetsize"] = 60
110         self.assertEqual(result, expected_result)
111
112     def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock_ssh):
113         args = {
114             'options': {'packetsize': 60, 'number_of_ports': 10},
115             'sla': {'max_ppm': 1000}
116         }
117         result = {}
118
119         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
120
121         p.server = mock_ssh.SSH()
122         p.client = mock_ssh.SSH()
123
124         mock_dpdk_result = mock.Mock()
125         mock_dpdk_result.return_value = 149300
126         p._dpdk_get_result = mock_dpdk_result
127
128         sample_output = '{"packets_per_second": 9753, "errors": 0, \
129             "packets_sent": 149776, "flows": 110}'
130         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
131         self.assertRaises(AssertionError, p.run, result)
132
133     def test_pktgen_dpdk_throughput_unsuccessful_script_error(
134             self, mock_ssh):
135         args = {
136             'options': {'packetsize': 60, 'number_of_ports': 10},
137             'sla': {'max_ppm': 1000}
138         }
139         result = {}
140
141         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
142
143         p.server = mock_ssh.SSH()
144         p.client = mock_ssh.SSH()
145
146         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
147         self.assertRaises(RuntimeError, p.run, result)
148
149     def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock_ssh):
150         args = {
151             'options': {'packetsize': 60},
152         }
153         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
154         p.server = mock_ssh.SSH()
155
156         mock_ssh.SSH().execute.return_value = (0, '', '')
157
158         p._is_dpdk_setup("server")
159
160         mock_ssh.SSH().execute.assert_called_with(
161             "ip a | grep eth1 2>/dev/null")
162
163     def test_pktgen_dpdk_throughput_dpdk_setup(self, mock_ssh):
164         args = {
165             'options': {'packetsize': 60},
166         }
167         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
168         p.server = mock_ssh.SSH()
169         p.client = mock_ssh.SSH()
170
171         mock_ssh.SSH().execute.return_value = (0, '', '')
172
173         p.dpdk_setup()
174
175         self.assertTrue(p.dpdk_setup_done)
176
177     def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock_ssh):
178         args = {
179             'options': {'packetsize': 60},
180         }
181         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
182         p.server = mock_ssh.SSH()
183         p.client = mock_ssh.SSH()
184
185         mock_ssh.SSH().execute.return_value = (0, '10000', '')
186
187         p._dpdk_get_result()
188
189         mock_ssh.SSH().execute.assert_called_with(
190             "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset > /dev/null 2>&1")
191
192
193 def main():
194     unittest.main()
195
196
197 if __name__ == '__main__':
198     main()