3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 # Unittest for yardstick.benchmark.scenarios.networking.pktgen.Pktgen
14 from __future__ import absolute_import
19 from oslo_serialization import jsonutils
21 from yardstick.benchmark.scenarios.networking import pktgen
24 @mock.patch('yardstick.benchmark.scenarios.networking.pktgen.ssh')
25 class PktgenTestCase(unittest.TestCase):
32 'key_filename': 'mykey.key'
37 'key_filename': 'mykey.key',
38 'ipaddr': '172.16.0.138'
42 def test_pktgen_successful_setup(self, mock_ssh):
45 'options': {'packetsize': 60},
47 p = pktgen.Pktgen(args, self.ctx)
50 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
51 self.assertIsNotNone(p.server)
52 self.assertIsNotNone(p.client)
53 self.assertEqual(p.setup_done, True)
55 def test_pktgen_successful_iptables_setup(self, mock_ssh):
58 'options': {'packetsize': 60, 'number_of_ports': 10},
60 p = pktgen.Pktgen(args, self.ctx)
61 p.server = mock_ssh.SSH.from_node()
62 p.number_of_ports = args['options']['number_of_ports']
64 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
68 mock_ssh.SSH.from_node().execute.assert_called_with(
70 "sudo iptables -A INPUT -p udp --dport 1000:%s -j DROP"
73 def test_pktgen_unsuccessful_iptables_setup(self, mock_ssh):
76 'options': {'packetsize': 60, 'number_of_ports': 10},
79 p = pktgen.Pktgen(args, self.ctx)
80 p.server = mock_ssh.SSH.from_node()
81 p.number_of_ports = args['options']['number_of_ports']
83 mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
84 self.assertRaises(RuntimeError, p._iptables_setup)
86 def test_pktgen_successful_iptables_get_result(self, mock_ssh):
89 'options': {'packetsize': 60, 'number_of_ports': 10},
92 p = pktgen.Pktgen(args, self.ctx)
93 p.server = mock_ssh.SSH.from_node()
94 p.number_of_ports = args['options']['number_of_ports']
96 mock_ssh.SSH.from_node().execute.return_value = (0, '150000', '')
97 p._iptables_get_result()
99 mock_ssh.SSH.from_node().execute.assert_called_with(
100 "sudo iptables -L INPUT -vnx |"
101 "awk '/dpts:1000:%s/ {{printf \"%%s\", $1}}'"
104 def test_pktgen_unsuccessful_iptables_get_result(self, mock_ssh):
107 'options': {'packetsize': 60, 'number_of_ports': 10},
110 p = pktgen.Pktgen(args, self.ctx)
112 p.server = mock_ssh.SSH.from_node()
113 p.number_of_ports = args['options']['number_of_ports']
115 mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
116 self.assertRaises(RuntimeError, p._iptables_get_result)
118 def test_pktgen_successful_no_sla(self, mock_ssh):
121 'options': {'packetsize': 60, 'number_of_ports': 10},
125 p = pktgen.Pktgen(args, self.ctx)
127 p.server = mock_ssh.SSH.from_node()
128 p.client = mock_ssh.SSH.from_node()
130 mock_iptables_result = mock.Mock()
131 mock_iptables_result.return_value = 149300
132 p._iptables_get_result = mock_iptables_result
134 sample_output = '{"packets_per_second": 9753, "errors": 0, \
135 "packets_sent": 149776, "packetsize": 60, "flows": 110}'
136 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
139 expected_result = jsonutils.loads(sample_output)
140 expected_result["packets_received"] = 149300
141 self.assertEqual(result, expected_result)
143 def test_pktgen_successful_sla(self, mock_ssh):
146 'options': {'packetsize': 60, 'number_of_ports': 10},
147 'sla': {'max_ppm': 10000}
151 p = pktgen.Pktgen(args, self.ctx)
153 p.server = mock_ssh.SSH.from_node()
154 p.client = mock_ssh.SSH.from_node()
156 mock_iptables_result = mock.Mock()
157 mock_iptables_result.return_value = 149300
158 p._iptables_get_result = mock_iptables_result
160 sample_output = '{"packets_per_second": 9753, "errors": 0, \
161 "packets_sent": 149776, "packetsize": 60, "flows": 110}'
162 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
165 expected_result = jsonutils.loads(sample_output)
166 expected_result["packets_received"] = 149300
167 self.assertEqual(result, expected_result)
169 def test_pktgen_unsuccessful_sla(self, mock_ssh):
172 'options': {'packetsize': 60, 'number_of_ports': 10},
173 'sla': {'max_ppm': 1000}
177 p = pktgen.Pktgen(args, self.ctx)
179 p.server = mock_ssh.SSH.from_node()
180 p.client = mock_ssh.SSH.from_node()
182 mock_iptables_result = mock.Mock()
183 mock_iptables_result.return_value = 149300
184 p._iptables_get_result = mock_iptables_result
186 sample_output = '{"packets_per_second": 9753, "errors": 0, \
187 "packets_sent": 149776, "packetsize": 60, "flows": 110}'
188 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
189 self.assertRaises(AssertionError, p.run, result)
191 def test_pktgen_unsuccessful_script_error(self, mock_ssh):
194 'options': {'packetsize': 60, 'number_of_ports': 10},
195 'sla': {'max_ppm': 1000}
199 p = pktgen.Pktgen(args, self.ctx)
201 p.server = mock_ssh.SSH.from_node()
202 p.client = mock_ssh.SSH.from_node()
204 mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
205 self.assertRaises(RuntimeError, p.run, result)
211 if __name__ == '__main__':