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.iperf3.Iperf
14 from __future__ import absolute_import
20 from oslo_serialization import jsonutils
22 from yardstick.benchmark.scenarios.networking import iperf3
25 @mock.patch('yardstick.benchmark.scenarios.networking.iperf3.ssh')
26 class IperfTestCase(unittest.TestCase):
27 output_name_tcp = 'iperf3_sample_output.json'
28 output_name_udp = 'iperf3_sample_output_udp.json'
35 'key_filename': 'mykey.key'
40 'key_filename': 'mykey.key',
41 'ipaddr': '172.16.0.138',
45 def test_iperf_successful_setup(self, mock_ssh):
47 p = iperf3.Iperf({}, self.ctx)
48 mock_ssh.SSH().execute.return_value = (0, '', '')
51 self.assertIsNotNone(p.target)
52 self.assertIsNotNone(p.host)
53 mock_ssh.SSH().execute.assert_called_with("iperf3 -s -D")
55 def test_iperf_unsuccessful_setup(self, mock_ssh):
57 p = iperf3.Iperf({}, self.ctx)
58 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
59 self.assertRaises(RuntimeError, p.setup)
61 def test_iperf_successful_teardown(self, mock_ssh):
63 p = iperf3.Iperf({}, self.ctx)
64 mock_ssh.SSH().execute.return_value = (0, '', '')
65 p.host = mock_ssh.SSH()
66 p.target = mock_ssh.SSH()
69 self.assertTrue(mock_ssh.SSH().close.called)
70 mock_ssh.SSH().execute.assert_called_with("pkill iperf3")
72 def test_iperf_successful_no_sla(self, mock_ssh):
75 args = {'options': options}
78 p = iperf3.Iperf(args, self.ctx)
79 mock_ssh.SSH().execute.return_value = (0, '', '')
80 p.host = mock_ssh.SSH()
82 sample_output = self._read_sample_output(self.output_name_tcp)
83 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
84 expected_result = jsonutils.loads(sample_output)
86 self.assertEqual(result, expected_result)
88 def test_iperf_successful_sla(self, mock_ssh):
93 'sla': {'bytes_per_second': 15000000}
97 p = iperf3.Iperf(args, self.ctx)
98 mock_ssh.SSH().execute.return_value = (0, '', '')
99 p.host = mock_ssh.SSH()
101 sample_output = self._read_sample_output(self.output_name_tcp)
102 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
103 expected_result = jsonutils.loads(sample_output)
105 self.assertEqual(result, expected_result)
107 def test_iperf_unsuccessful_sla(self, mock_ssh):
112 'sla': {'bytes_per_second': 25000000}
116 p = iperf3.Iperf(args, self.ctx)
117 mock_ssh.SSH().execute.return_value = (0, '', '')
118 p.host = mock_ssh.SSH()
120 sample_output = self._read_sample_output(self.output_name_tcp)
121 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
122 self.assertRaises(AssertionError, p.run, result)
124 def test_iperf_successful_sla_jitter(self, mock_ssh):
125 options = {"udp": "udp", "bandwidth": "20m"}
128 'sla': {'jitter': 10}
132 p = iperf3.Iperf(args, self.ctx)
133 mock_ssh.SSH().execute.return_value = (0, '', '')
134 p.host = mock_ssh.SSH()
136 sample_output = self._read_sample_output(self.output_name_udp)
137 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
138 expected_result = jsonutils.loads(sample_output)
140 self.assertEqual(result, expected_result)
142 def test_iperf_unsuccessful_sla_jitter(self, mock_ssh):
143 options = {"udp": "udp", "bandwidth": "20m"}
146 'sla': {'jitter': 0.0001}
150 p = iperf3.Iperf(args, self.ctx)
151 mock_ssh.SSH().execute.return_value = (0, '', '')
152 p.host = mock_ssh.SSH()
154 sample_output = self._read_sample_output(self.output_name_udp)
155 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
156 self.assertRaises(AssertionError, p.run, result)
158 def test_iperf_unsuccessful_script_error(self, mock_ssh):
161 args = {'options': options}
164 p = iperf3.Iperf(args, self.ctx)
165 mock_ssh.SSH().execute.return_value = (0, '', '')
166 p.host = mock_ssh.SSH()
168 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
169 self.assertRaises(RuntimeError, p.run, result)
171 def _read_sample_output(self, filename):
172 curr_path = os.path.dirname(os.path.abspath(__file__))
173 output = os.path.join(curr_path, filename)
174 with open(output) as f:
175 sample_output = f.read()
182 if __name__ == '__main__':