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
14 from __future__ import absolute_import
20 from oslo_serialization import jsonutils
22 from yardstick.benchmark.scenarios.networking import netperf
25 @mock.patch('yardstick.benchmark.scenarios.networking.netperf.ssh')
26 class NetperfTestCase(unittest.TestCase):
33 'key_filename': 'mykey.key'
38 'key_filename': 'mykey.key',
39 'ipaddr': '172.16.0.138'
43 def test_netperf_successful_setup(self, mock_ssh):
45 p = netperf.Netperf({}, self.ctx)
46 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
49 self.assertIsNotNone(p.server)
50 self.assertIsNotNone(p.client)
51 self.assertEqual(p.setup_done, True)
53 def test_netperf_successful_no_sla(self, mock_ssh):
56 args = {'options': options}
59 p = netperf.Netperf(args, self.ctx)
60 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
61 p.host = mock_ssh.SSH.from_node()
63 sample_output = self._read_sample_output()
64 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
65 expected_result = jsonutils.loads(sample_output)
67 self.assertEqual(result, expected_result)
69 def test_netperf_successful_sla(self, mock_ssh):
74 'sla': {'mean_latency': 100}
78 p = netperf.Netperf(args, self.ctx)
79 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
80 p.host = mock_ssh.SSH.from_node()
82 sample_output = self._read_sample_output()
83 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
84 expected_result = jsonutils.loads(sample_output)
86 self.assertEqual(result, expected_result)
88 def test_netperf_unsuccessful_sla(self, mock_ssh):
93 'sla': {'mean_latency': 5}
97 p = netperf.Netperf(args, self.ctx)
98 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
99 p.host = mock_ssh.SSH.from_node()
101 sample_output = self._read_sample_output()
102 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
103 self.assertRaises(AssertionError, p.run, result)
105 def test_netperf_unsuccessful_script_error(self, mock_ssh):
108 args = {'options': options}
111 p = netperf.Netperf(args, self.ctx)
112 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
113 p.host = mock_ssh.SSH.from_node()
115 mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
116 self.assertRaises(RuntimeError, p.run, result)
118 def _read_sample_output(self):
119 curr_path = os.path.dirname(os.path.abspath(__file__))
120 output = os.path.join(curr_path, 'netperf_sample_output.json')
121 with open(output) as f:
122 sample_output = f.read()
129 if __name__ == '__main__':