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}
72 sample_output = self._read_sample_output(self.output_name_tcp)
73 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
74 expected_result = json.loads(sample_output)
76 self.assertEqual(result, expected_result)
78 def test_iperf_successful_sla(self, mock_ssh):
80 p = iperf3.Iperf(self.ctx)
81 mock_ssh.SSH().execute.return_value = (0, '', '')
82 p.host = mock_ssh.SSH()
87 'sla': {'bytes_per_second': 15000000}
91 sample_output = self._read_sample_output(self.output_name_tcp)
92 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
93 expected_result = json.loads(sample_output)
95 self.assertEqual(result, expected_result)
97 def test_iperf_unsuccessful_sla(self, mock_ssh):
99 p = iperf3.Iperf(self.ctx)
100 mock_ssh.SSH().execute.return_value = (0, '', '')
101 p.host = mock_ssh.SSH()
106 'sla': {'bytes_per_second': 25000000}
110 sample_output = self._read_sample_output(self.output_name_tcp)
111 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
112 self.assertRaises(AssertionError, p.run, args, result)
114 def test_iperf_successful_sla_jitter(self, mock_ssh):
116 p = iperf3.Iperf(self.ctx)
117 mock_ssh.SSH().execute.return_value = (0, '', '')
118 p.host = mock_ssh.SSH()
120 options = {"udp":"udp","bandwidth":"20m"}
123 'sla': {'jitter': 10}
127 sample_output = self._read_sample_output(self.output_name_udp)
128 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
129 expected_result = json.loads(sample_output)
131 self.assertEqual(result, expected_result)
133 def test_iperf_unsuccessful_sla_jitter(self, mock_ssh):
135 p = iperf3.Iperf(self.ctx)
136 mock_ssh.SSH().execute.return_value = (0, '', '')
137 p.host = mock_ssh.SSH()
139 options = {"udp":"udp","bandwidth":"20m"}
142 'sla': {'jitter': 0.0001}
146 sample_output = self._read_sample_output(self.output_name_udp)
147 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
148 self.assertRaises(AssertionError, p.run, args, result)
150 def test_iperf_unsuccessful_script_error(self, mock_ssh):
152 p = iperf3.Iperf(self.ctx)
153 mock_ssh.SSH().execute.return_value = (0, '', '')
154 p.host = mock_ssh.SSH()
157 args = {'options': options}
160 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
161 self.assertRaises(RuntimeError, p.run, args, result)
163 def _read_sample_output(self,filename):
164 curr_path = os.path.dirname(os.path.abspath(__file__))
165 output = os.path.join(curr_path, filename)
166 with open(output) as f:
167 sample_output = f.read()
174 if __name__ == '__main__':