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
19 from yardstick.benchmark.scenarios.networking import iperf3
22 @mock.patch('yardstick.benchmark.scenarios.networking.iperf3.ssh')
23 class IperfTestCase(unittest.TestCase):
24 output_name_tcp = 'iperf3_sample_output.json'
25 output_name_udp = 'iperf3_sample_output_udp.json'
29 'host': '172.16.0.137',
30 'target': '172.16.0.138',
32 'key_filename': "mykey.key"
35 def test_iperf_successful_setup(self, mock_ssh):
37 p = iperf3.Iperf(self.ctx)
38 mock_ssh.SSH().execute.return_value = (0, '', '')
41 self.assertIsNotNone(p.target)
42 self.assertIsNotNone(p.host)
43 mock_ssh.SSH().execute.assert_called_with("iperf3 -s -D")
45 def test_iperf_unsuccessful_setup(self, mock_ssh):
47 p = iperf3.Iperf(self.ctx)
48 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
49 self.assertRaises(RuntimeError, p.setup)
51 def test_iperf_successful_teardown(self, mock_ssh):
53 p = iperf3.Iperf(self.ctx)
54 mock_ssh.SSH().execute.return_value = (0, '', '')
55 p.host = mock_ssh.SSH()
56 p.target = mock_ssh.SSH()
59 self.assertTrue(mock_ssh.SSH().close.called)
60 mock_ssh.SSH().execute.assert_called_with("pkill iperf3")
62 def test_iperf_successful_no_sla(self, mock_ssh):
64 p = iperf3.Iperf(self.ctx)
65 mock_ssh.SSH().execute.return_value = (0, '', '')
66 p.host = mock_ssh.SSH()
69 args = {'options': options}
71 sample_output = self._read_sample_output(self.output_name_tcp)
72 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
73 expected_result = json.loads(sample_output)
75 self.assertEqual(result, expected_result)
77 def test_iperf_successful_sla(self, mock_ssh):
79 p = iperf3.Iperf(self.ctx)
80 mock_ssh.SSH().execute.return_value = (0, '', '')
81 p.host = mock_ssh.SSH()
86 'sla': {'bytes_per_second': 15000000}
89 sample_output = self._read_sample_output(self.output_name_tcp)
90 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
91 expected_result = json.loads(sample_output)
93 self.assertEqual(result, expected_result)
95 def test_iperf_unsuccessful_sla(self, mock_ssh):
97 p = iperf3.Iperf(self.ctx)
98 mock_ssh.SSH().execute.return_value = (0, '', '')
99 p.host = mock_ssh.SSH()
104 'sla': {'bytes_per_second': 25000000}
107 sample_output = self._read_sample_output(self.output_name_tcp)
108 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
109 self.assertRaises(AssertionError, p.run, args)
111 def test_iperf_successful_sla_jitter(self, mock_ssh):
113 p = iperf3.Iperf(self.ctx)
114 mock_ssh.SSH().execute.return_value = (0, '', '')
115 p.host = mock_ssh.SSH()
117 options = {"udp":"udp","bandwidth":"20m"}
120 'sla': {'jitter': 10}
123 sample_output = self._read_sample_output(self.output_name_udp)
124 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
125 expected_result = json.loads(sample_output)
127 self.assertEqual(result, expected_result)
129 def test_iperf_unsuccessful_sla_jitter(self, mock_ssh):
131 p = iperf3.Iperf(self.ctx)
132 mock_ssh.SSH().execute.return_value = (0, '', '')
133 p.host = mock_ssh.SSH()
135 options = {"udp":"udp","bandwidth":"20m"}
138 'sla': {'jitter': 0.0001}
141 sample_output = self._read_sample_output(self.output_name_udp)
142 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
143 self.assertRaises(AssertionError, p.run, args)
145 def test_iperf_unsuccessful_script_error(self, mock_ssh):
147 p = iperf3.Iperf(self.ctx)
148 mock_ssh.SSH().execute.return_value = (0, '', '')
149 p.host = mock_ssh.SSH()
152 args = {'options': options}
154 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
155 self.assertRaises(RuntimeError, p.run, args)
157 def _read_sample_output(self,filename):
158 curr_path = os.path.dirname(os.path.abspath(__file__))
159 output = os.path.join(curr_path, filename)
160 with open(output) as f:
161 sample_output = f.read()
168 if __name__ == '__main__':