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 @mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.time')
24 class PktgenDPDKTestCase(unittest.TestCase):
31 'key_filename': 'mykey.key'
36 'key_filename': 'mykey.key',
40 def test_pktgen_dpdk_throughput_successful_setup(self, mock__time, mock_ssh):
42 'options': {'packetsize': 60},
44 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
47 mock_ssh.SSH().execute.return_value = (0, '', '')
48 self.assertIsNotNone(p.server)
49 self.assertIsNotNone(p.client)
50 self.assertEqual(p.setup_done, True)
52 def test_pktgen_dpdk_throughput_successful_no_sla(self, mock__time, mock_ssh):
54 'options': {'packetsize': 60, 'number_of_ports': 10},
59 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
61 p.server = mock_ssh.SSH()
62 p.client = mock_ssh.SSH()
64 mock_dpdk_result = mock.Mock()
65 mock_dpdk_result.return_value = 149300
66 p._dpdk_get_result = mock_dpdk_result
68 sample_output = '{"packets_per_second": 9753, "errors": 0, \
69 "packets_sent": 149776, "flows": 110}'
70 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
73 expected_result = jsonutils.loads(sample_output)
74 expected_result["packets_received"] = 149300
75 expected_result["packetsize"] = 60
76 self.assertEqual(result, expected_result)
78 def test_pktgen_dpdk_throughput_successful_sla(self, mock__time, mock_ssh):
80 'options': {'packetsize': 60, 'number_of_ports': 10},
81 'sla': {'max_ppm': 10000}
85 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
87 p.server = mock_ssh.SSH()
88 p.client = mock_ssh.SSH()
90 mock_dpdk_result = mock.Mock()
91 mock_dpdk_result.return_value = 149300
92 p._dpdk_get_result = mock_dpdk_result
94 sample_output = '{"packets_per_second": 9753, "errors": 0, \
95 "packets_sent": 149776, "flows": 110}'
96 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
99 expected_result = jsonutils.loads(sample_output)
100 expected_result["packets_received"] = 149300
101 expected_result["packetsize"] = 60
102 self.assertEqual(result, expected_result)
104 def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock__time, mock_ssh):
106 'options': {'packetsize': 60, 'number_of_ports': 10},
107 'sla': {'max_ppm': 1000}
111 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
113 p.server = mock_ssh.SSH()
114 p.client = mock_ssh.SSH()
116 mock_dpdk_result = mock.Mock()
117 mock_dpdk_result.return_value = 149300
118 p._dpdk_get_result = mock_dpdk_result
120 sample_output = '{"packets_per_second": 9753, "errors": 0, \
121 "packets_sent": 149776, "flows": 110}'
122 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
123 self.assertRaises(AssertionError, p.run, result)
125 def test_pktgen_dpdk_throughput_unsuccessful_script_error(self, mock__time, mock_ssh):
127 'options': {'packetsize': 60, 'number_of_ports': 10},
128 'sla': {'max_ppm': 1000}
132 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
134 p.server = mock_ssh.SSH()
135 p.client = mock_ssh.SSH()
137 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
138 self.assertRaises(RuntimeError, p.run, result)
140 def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock__time, mock_ssh):
142 'options': {'packetsize': 60},
144 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
145 p.server = mock_ssh.SSH()
147 mock_ssh.SSH().execute.return_value = (0, '', '')
149 p._is_dpdk_setup("server")
151 mock_ssh.SSH().execute.assert_called_with(
152 "ip a | grep eth1 2>/dev/null")
154 def test_pktgen_dpdk_throughput_dpdk_setup(self, mock__time, mock_ssh):
156 'options': {'packetsize': 60},
158 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
159 p.server = mock_ssh.SSH()
160 p.client = mock_ssh.SSH()
162 mock_ssh.SSH().execute.return_value = (0, '', '')
166 self.assertEqual(p.dpdk_setup_done, True)
168 def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock__time, mock_ssh):
170 'options': {'packetsize': 60},
172 p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
173 p.server = mock_ssh.SSH()
174 p.client = mock_ssh.SSH()
176 mock_ssh.SSH().execute.return_value = (0, '10000', '')
180 mock_ssh.SSH().execute.assert_called_with(
181 "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset > /dev/null 2>&1")
186 if __name__ == '__main__':