1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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.netperf.Netperf
12 from __future__ import absolute_import
18 from oslo_serialization import jsonutils
20 from yardstick.benchmark.scenarios.networking import netperf
23 @mock.patch('yardstick.benchmark.scenarios.networking.netperf.ssh')
24 class NetperfTestCase(unittest.TestCase):
31 'key_filename': 'mykey.key'
36 'key_filename': 'mykey.key',
37 'ipaddr': '172.16.0.138'
41 def test_netperf_successful_setup(self, mock_ssh):
43 p = netperf.Netperf({}, self.ctx)
44 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
47 self.assertIsNotNone(p.server)
48 self.assertIsNotNone(p.client)
49 self.assertTrue(p.setup_done)
51 def test_netperf_successful_no_sla(self, mock_ssh):
54 args = {'options': options}
57 p = netperf.Netperf(args, self.ctx)
58 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
59 p.host = mock_ssh.SSH.from_node()
61 sample_output = self._read_sample_output()
62 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
63 expected_result = jsonutils.loads(sample_output)
65 self.assertEqual(result, expected_result)
67 def test_netperf_successful_sla(self, mock_ssh):
72 'sla': {'mean_latency': 100}
76 p = netperf.Netperf(args, self.ctx)
77 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
78 p.host = mock_ssh.SSH.from_node()
80 sample_output = self._read_sample_output()
81 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
82 expected_result = jsonutils.loads(sample_output)
84 self.assertEqual(result, expected_result)
86 def test_netperf_unsuccessful_sla(self, mock_ssh):
91 'sla': {'mean_latency': 5}
95 p = netperf.Netperf(args, self.ctx)
96 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
97 p.host = mock_ssh.SSH.from_node()
99 sample_output = self._read_sample_output()
100 mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
101 self.assertRaises(AssertionError, p.run, result)
103 def test_netperf_unsuccessful_script_error(self, mock_ssh):
106 args = {'options': options}
109 p = netperf.Netperf(args, self.ctx)
110 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
111 p.host = mock_ssh.SSH.from_node()
113 mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
114 self.assertRaises(RuntimeError, p.run, result)
116 def _read_sample_output(self):
117 curr_path = os.path.dirname(os.path.abspath(__file__))
118 output = os.path.join(curr_path, 'netperf_sample_output.json')
119 with open(output) as f:
120 sample_output = f.read()