Merge "Add l2fwd module in Tgen mode"
[yardstick.git] / yardstick / 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
10 # Unittest for yardstick.benchmark.scenarios.networking.pktgen.PktgenDPDK
11
12 from __future__ import absolute_import
13 import unittest
14
15 from oslo_serialization import jsonutils
16 import mock
17
18 from yardstick.benchmark.scenarios.networking import pktgen_dpdk_throughput
19
20
21 # pylint: disable=unused-argument
22 # disable this for now because I keep forgetting mock patch arg ordering
23
24
25 @mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.ssh')
26 class PktgenDPDKTestCase(unittest.TestCase):
27
28     def setUp(self):
29         self.ctx = {
30             'host': {
31                 'ip': '172.16.0.137',
32                 'user': 'root',
33                 'key_filename': 'mykey.key'
34             },
35             'target': {
36                 'ip': '172.16.0.138',
37                 'user': 'root',
38                 'key_filename': 'mykey.key',
39             }
40         }
41
42         self._mock_time = mock.patch(
43             'yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.time')
44         self.mock_time = self._mock_time.start()
45
46         self.addCleanup(self._cleanup)
47
48     def _cleanup(self):
49         self._mock_time.stop()
50
51     def test_pktgen_dpdk_throughput_successful_setup(self, mock_ssh):
52         args = {
53             'options': {'packetsize': 60},
54         }
55         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
56         p.setup()
57
58         mock_ssh.SSH().execute.return_value = (0, '', '')
59         self.assertIsNotNone(p.server)
60         self.assertIsNotNone(p.client)
61         self.assertTrue(p.setup_done)
62
63     def test_pktgen_dpdk_throughput_successful_no_sla(self, mock_ssh):
64         args = {
65             'options': {'packetsize': 60, 'number_of_ports': 10},
66         }
67
68         result = {}
69
70         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
71
72         p.server = mock_ssh.SSH()
73         p.client = mock_ssh.SSH()
74
75         mock_dpdk_result = mock.Mock()
76         mock_dpdk_result.return_value = 149300
77         p._dpdk_get_result = mock_dpdk_result
78
79         sample_output = '{"packets_per_second": 9753, "errors": 0, \
80             "packets_sent": 149776, "flows": 110}'
81         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
82
83         p.run(result)
84         expected_result = jsonutils.loads(sample_output)
85         expected_result["packets_received"] = 149300
86         expected_result["packetsize"] = 60
87         self.assertEqual(result, expected_result)
88
89     def test_pktgen_dpdk_throughput_successful_sla(self, mock_ssh):
90         args = {
91             'options': {'packetsize': 60, 'number_of_ports': 10},
92             'sla': {'max_ppm': 10000}
93         }
94         result = {}
95
96         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
97
98         p.server = mock_ssh.SSH()
99         p.client = mock_ssh.SSH()
100
101         mock_dpdk_result = mock.Mock()
102         mock_dpdk_result.return_value = 149300
103         p._dpdk_get_result = mock_dpdk_result
104
105         sample_output = '{"packets_per_second": 9753, "errors": 0, \
106             "packets_sent": 149776, "flows": 110}'
107         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
108
109         p.run(result)
110         expected_result = jsonutils.loads(sample_output)
111         expected_result["packets_received"] = 149300
112         expected_result["packetsize"] = 60
113         self.assertEqual(result, expected_result)
114
115     def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock_ssh):
116         args = {
117             'options': {'packetsize': 60, 'number_of_ports': 10},
118             'sla': {'max_ppm': 1000}
119         }
120         result = {}
121
122         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
123
124         p.server = mock_ssh.SSH()
125         p.client = mock_ssh.SSH()
126
127         mock_dpdk_result = mock.Mock()
128         mock_dpdk_result.return_value = 149300
129         p._dpdk_get_result = mock_dpdk_result
130
131         sample_output = '{"packets_per_second": 9753, "errors": 0, \
132             "packets_sent": 149776, "flows": 110}'
133         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
134         self.assertRaises(AssertionError, p.run, result)
135
136     def test_pktgen_dpdk_throughput_unsuccessful_script_error(
137             self, mock_ssh):
138         args = {
139             'options': {'packetsize': 60, 'number_of_ports': 10},
140             'sla': {'max_ppm': 1000}
141         }
142         result = {}
143
144         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
145
146         p.server = mock_ssh.SSH()
147         p.client = mock_ssh.SSH()
148
149         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
150         self.assertRaises(RuntimeError, p.run, result)
151
152     def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock_ssh):
153         args = {
154             'options': {'packetsize': 60},
155         }
156         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
157         p.server = mock_ssh.SSH()
158
159         mock_ssh.SSH().execute.return_value = (0, '', '')
160
161         p._is_dpdk_setup("server")
162
163         mock_ssh.SSH().execute.assert_called_with(
164             "ip a | grep eth1 2>/dev/null")
165
166     def test_pktgen_dpdk_throughput_dpdk_setup(self, mock_ssh):
167         args = {
168             'options': {'packetsize': 60},
169         }
170         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
171         p.server = mock_ssh.SSH()
172         p.client = mock_ssh.SSH()
173
174         mock_ssh.SSH().execute.return_value = (0, '', '')
175
176         p.dpdk_setup()
177
178         self.assertTrue(p.dpdk_setup_done)
179
180     def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock_ssh):
181         args = {
182             'options': {'packetsize': 60},
183         }
184         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
185         p.server = mock_ssh.SSH()
186         p.client = mock_ssh.SSH()
187
188         mock_ssh.SSH().execute.return_value = (0, '10000', '')
189
190         p._dpdk_get_result()
191
192         mock_ssh.SSH().execute.assert_called_with(
193             "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset > /dev/null 2>&1")