1 ##############################################################################
2 # Copyright (c) 2017 Nokia and others.
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 ##############################################################################
11 # Unittest for yardstick.benchmark.scenarios.networking.pktgen.PktgenDPDK
13 from __future__ import absolute_import
16 from oslo_serialization import jsonutils
19 from yardstick.benchmark.scenarios.networking import pktgen_dpdk_throughput
22 @mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.ssh')
23 class PktgenDPDKTestCase(unittest.TestCase):
30 'key_filename': 'mykey.key'
35 'key_filename': 'mykey.key',
39 self._mock_time = mock.patch(
40 'yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.time')
41 self.mock_time = self._mock_time.start()
43 self.addCleanup(self._cleanup)
46 self._mock_time.stop()
48 def test_pktgen_dpdk_throughput_successful_setup(self, mock_ssh):
50 'options': {'packetsize': 60},
52 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
55 mock_ssh.SSH().execute.return_value = (0, '', '')
56 self.assertIsNotNone(p.server)
57 self.assertIsNotNone(p.client)
58 self.assertTrue(p.setup_done)
60 def test_pktgen_dpdk_throughput_successful_no_sla(self, mock_ssh):
62 'options': {'packetsize': 60, 'number_of_ports': 10},
67 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
69 p.server = mock_ssh.SSH()
70 p.client = mock_ssh.SSH()
72 mock_dpdk_result = mock.Mock()
73 mock_dpdk_result.return_value = 149300
74 p._dpdk_get_result = mock_dpdk_result
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, '')
81 expected_result = jsonutils.loads(sample_output)
82 expected_result["packets_received"] = 149300
83 expected_result["packetsize"] = 60
84 self.assertEqual(result, expected_result)
86 def test_pktgen_dpdk_throughput_successful_sla(self, mock_ssh):
88 'options': {'packetsize': 60, 'number_of_ports': 10},
89 'sla': {'max_ppm': 10000}
93 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
95 p.server = mock_ssh.SSH()
96 p.client = mock_ssh.SSH()
98 mock_dpdk_result = mock.Mock()
99 mock_dpdk_result.return_value = 149300
100 p._dpdk_get_result = mock_dpdk_result
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, '')
107 expected_result = jsonutils.loads(sample_output)
108 expected_result["packets_received"] = 149300
109 expected_result["packetsize"] = 60
110 self.assertEqual(result, expected_result)
112 def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock_ssh):
114 'options': {'packetsize': 60, 'number_of_ports': 10},
115 'sla': {'max_ppm': 1000}
119 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
121 p.server = mock_ssh.SSH()
122 p.client = mock_ssh.SSH()
124 mock_dpdk_result = mock.Mock()
125 mock_dpdk_result.return_value = 149300
126 p._dpdk_get_result = mock_dpdk_result
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)
133 def test_pktgen_dpdk_throughput_unsuccessful_script_error(
136 'options': {'packetsize': 60, 'number_of_ports': 10},
137 'sla': {'max_ppm': 1000}
141 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
143 p.server = mock_ssh.SSH()
144 p.client = mock_ssh.SSH()
146 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
147 self.assertRaises(RuntimeError, p.run, result)
149 def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock_ssh):
151 'options': {'packetsize': 60},
153 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
154 p.server = mock_ssh.SSH()
156 mock_ssh.SSH().execute.return_value = (0, '', '')
158 p._is_dpdk_setup("server")
160 mock_ssh.SSH().execute.assert_called_with(
161 "ip a | grep eth1 2>/dev/null")
163 def test_pktgen_dpdk_throughput_dpdk_setup(self, mock_ssh):
165 'options': {'packetsize': 60},
167 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
168 p.server = mock_ssh.SSH()
169 p.client = mock_ssh.SSH()
171 mock_ssh.SSH().execute.return_value = (0, '', '')
175 self.assertTrue(p.dpdk_setup_done)
177 def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock_ssh):
179 'options': {'packetsize': 60},
181 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
182 p.server = mock_ssh.SSH()
183 p.client = mock_ssh.SSH()
185 mock_ssh.SSH().execute.return_value = (0, '10000', '')
189 mock_ssh.SSH().execute.assert_called_with(
190 "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset > /dev/null 2>&1")
197 if __name__ == '__main__':