3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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.netperf.Netperf
19 from yardstick.benchmark.scenarios.networking import netperf
22 @mock.patch('yardstick.benchmark.scenarios.networking.netperf.ssh')
23 class NetperfTestCase(unittest.TestCase):
27 'host': '172.16.0.137',
28 'target': '172.16.0.138',
30 'key_filename': "mykey.key"
33 def test_netperf_successful_setup(self, mock_ssh):
35 p = netperf.Netperf(self.ctx)
36 mock_ssh.SSH().execute.return_value = (0, '', '')
39 self.assertIsNotNone(p.server)
40 self.assertIsNotNone(p.client)
41 self.assertEqual(p.setup_done, True)
43 def test_netperf_successful_no_sla(self, mock_ssh):
45 p = netperf.Netperf(self.ctx)
46 mock_ssh.SSH().execute.return_value = (0, '', '')
47 p.host = mock_ssh.SSH()
50 args = {'options': options}
53 sample_output = self._read_sample_output()
54 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
55 expected_result = json.loads(sample_output)
57 self.assertEqual(result, expected_result)
59 def test_netperf_successful_sla(self, mock_ssh):
61 p = netperf.Netperf(self.ctx)
62 mock_ssh.SSH().execute.return_value = (0, '', '')
63 p.host = mock_ssh.SSH()
68 'sla': {'mean_latency': 100}
72 sample_output = self._read_sample_output()
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_netperf_unsuccessful_sla(self, mock_ssh):
80 p = netperf.Netperf(self.ctx)
81 mock_ssh.SSH().execute.return_value = (0, '', '')
82 p.host = mock_ssh.SSH()
87 'sla': {'mean_latency': 5}
91 sample_output = self._read_sample_output()
92 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
93 self.assertRaises(AssertionError, p.run, args, result)
95 def test_netperf_unsuccessful_script_error(self, mock_ssh):
97 p = netperf.Netperf(self.ctx)
98 mock_ssh.SSH().execute.return_value = (0, '', '')
99 p.host = mock_ssh.SSH()
102 args = {'options': options}
105 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
106 self.assertRaises(RuntimeError, p.run, args, result)
108 def _read_sample_output(self):
109 curr_path = os.path.dirname(os.path.abspath(__file__))
110 output = os.path.join(curr_path, 'netperf_sample_output.json')
111 with open(output) as f:
112 sample_output = f.read()
119 if __name__ == '__main__':