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