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}
52 sample_output = self._read_sample_output()
53 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
54 expected_result = json.loads(sample_output)
56 self.assertEqual(result, expected_result)
58 def test_netperf_successful_sla(self, mock_ssh):
60 p = netperf.Netperf(self.ctx)
61 mock_ssh.SSH().execute.return_value = (0, '', '')
62 p.host = mock_ssh.SSH()
67 'sla': {'mean_latency': 100}
70 sample_output = self._read_sample_output()
71 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
72 expected_result = json.loads(sample_output)
74 self.assertEqual(result, expected_result)
76 def test_netperf_unsuccessful_sla(self, mock_ssh):
78 p = netperf.Netperf(self.ctx)
79 mock_ssh.SSH().execute.return_value = (0, '', '')
80 p.host = mock_ssh.SSH()
85 'sla': {'mean_latency': 5}
88 sample_output = self._read_sample_output()
89 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
90 self.assertRaises(AssertionError, p.run, args)
92 def test_netperf_unsuccessful_script_error(self, mock_ssh):
94 p = netperf.Netperf(self.ctx)
95 mock_ssh.SSH().execute.return_value = (0, '', '')
96 p.host = mock_ssh.SSH()
99 args = {'options': options}
101 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
102 self.assertRaises(RuntimeError, p.run, args)
104 def _read_sample_output(self):
105 curr_path = os.path.dirname(os.path.abspath(__file__))
106 output = os.path.join(curr_path, 'netperf_sample_output.json')
107 with open(output) as f:
108 sample_output = f.read()
115 if __name__ == '__main__':