1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB 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 ##############################################################################
10 # Unittest for yardstick.benchmark.scenarios.networking.iperf3.Iperf
12 from __future__ import absolute_import
18 from oslo_serialization import jsonutils
20 from yardstick.common import utils
21 from yardstick.benchmark.scenarios.networking import iperf3
24 @mock.patch('yardstick.benchmark.scenarios.networking.iperf3.ssh')
25 class IperfTestCase(unittest.TestCase):
26 output_name_tcp = 'iperf3_sample_output.json'
27 output_name_udp = 'iperf3_sample_output_udp.json'
34 'key_filename': 'mykey.key'
39 'key_filename': 'mykey.key',
40 'ipaddr': '172.16.0.138',
44 def test_iperf_successful_setup(self, mock_ssh):
46 p = iperf3.Iperf({}, self.ctx)
47 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
50 self.assertIsNotNone(p.target)
51 self.assertIsNotNone(p.host)
52 mock_ssh.SSH.from_node().execute.assert_called_with("iperf3 -s -D")
54 def test_iperf_unsuccessful_setup(self, mock_ssh):
56 p = iperf3.Iperf({}, self.ctx)
57 mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
58 self.assertRaises(RuntimeError, p.setup)
60 def test_iperf_successful_teardown(self, mock_ssh):
62 p = iperf3.Iperf({}, self.ctx)
63 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
64 p.host = mock_ssh.SSH.from_node()
65 p.target = mock_ssh.SSH.from_node()
68 self.assertTrue(mock_ssh.SSH.from_node().close.called)
69 mock_ssh.SSH.from_node().execute.assert_called_with("pkill iperf3")
71 def test_iperf_successful_no_sla(self, mock_ssh):
74 args = {'options': options}
77 p = iperf3.Iperf(args, self.ctx)
78 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
79 p.host = mock_ssh.SSH.from_node()
81 sample_output = self._read_sample_output(self.output_name_tcp)
82 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
83 expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
85 self.assertEqual(result, expected_result)
87 def test_iperf_successful_sla(self, mock_ssh):
92 'sla': {'bytes_per_second': 15000000}
96 p = iperf3.Iperf(args, self.ctx)
97 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
98 p.host = mock_ssh.SSH.from_node()
100 sample_output = self._read_sample_output(self.output_name_tcp)
101 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
102 expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
104 self.assertEqual(result, expected_result)
106 def test_iperf_unsuccessful_sla(self, mock_ssh):
111 'sla': {'bytes_per_second': 25000000}
115 p = iperf3.Iperf(args, self.ctx)
116 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
117 p.host = mock_ssh.SSH.from_node()
119 sample_output = self._read_sample_output(self.output_name_tcp)
120 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
121 self.assertRaises(AssertionError, p.run, result)
123 def test_iperf_successful_sla_jitter(self, mock_ssh):
124 options = {"protocol": "udp", "bandwidth": "20m"}
127 'sla': {'jitter': 10}
131 p = iperf3.Iperf(args, self.ctx)
132 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
133 p.host = mock_ssh.SSH.from_node()
135 sample_output = self._read_sample_output(self.output_name_udp)
136 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
137 expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
139 self.assertEqual(result, expected_result)
141 def test_iperf_unsuccessful_sla_jitter(self, mock_ssh):
142 options = {"protocol": "udp", "bandwidth": "20m"}
145 'sla': {'jitter': 0.0001}
149 p = iperf3.Iperf(args, self.ctx)
150 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
151 p.host = mock_ssh.SSH.from_node()
153 sample_output = self._read_sample_output(self.output_name_udp)
154 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
155 self.assertRaises(AssertionError, p.run, result)
157 def test_iperf_successful_tcp_protocal(self, mock_ssh):
158 options = {"protocol": "tcp", "nodelay": "yes"}
161 'sla': {'bytes_per_second': 15000000}
165 p = iperf3.Iperf(args, self.ctx)
166 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
167 p.host = mock_ssh.SSH.from_node()
169 sample_output = self._read_sample_output(self.output_name_tcp)
170 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
171 expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
173 self.assertEqual(result, expected_result)
175 def test_iperf_unsuccessful_script_error(self, mock_ssh):
178 args = {'options': options}
181 p = iperf3.Iperf(args, self.ctx)
182 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
183 p.host = mock_ssh.SSH.from_node()
185 mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
186 self.assertRaises(RuntimeError, p.run, result)
188 def _read_sample_output(self, filename):
189 curr_path = os.path.dirname(os.path.abspath(__file__))
190 output = os.path.join(curr_path, filename)
191 with open(output) as f:
192 sample_output = f.read()