Support for netperf
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_netperf.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
5 #
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 ##############################################################################
11
12 # Unittest for yardstick.benchmark.scenarios.networking.netperf.Netperf
13
14 import mock
15 import unittest
16 import os
17 import json
18
19 from yardstick.benchmark.scenarios.networking import netperf
20
21
22 @mock.patch('yardstick.benchmark.scenarios.networking.netperf.ssh')
23 class NetperfTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self.ctx = {
27             'host': '172.16.0.137',
28             'target': '172.16.0.138',
29             'user': 'cirros',
30             'key_filename': "mykey.key"
31         }
32
33     def test_netperf_successful_setup(self, mock_ssh):
34
35         p = netperf.Netperf(self.ctx)
36         mock_ssh.SSH().execute.return_value = (0, '', '')
37
38         p.setup()
39         self.assertIsNotNone(p.server)
40         self.assertIsNotNone(p.client)
41         self.assertEqual(p.setup_done, True)
42
43     def test_netperf_successful_no_sla(self, mock_ssh):
44
45         p = netperf.Netperf(self.ctx)
46         mock_ssh.SSH().execute.return_value = (0, '', '')
47         p.host = mock_ssh.SSH()
48
49         options = {}
50         args = {'options': options}
51
52         sample_output = self._read_sample_output()
53         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
54         expected_result = json.loads(sample_output)
55         result = p.run(args)
56         self.assertEqual(result, expected_result)
57
58     def test_netperf_successful_sla(self, mock_ssh):
59
60         p = netperf.Netperf(self.ctx)
61         mock_ssh.SSH().execute.return_value = (0, '', '')
62         p.host = mock_ssh.SSH()
63
64         options = {}
65         args = {
66             'options': options,
67             'sla': {'mean_latency': 100}
68         }
69
70         sample_output = self._read_sample_output()
71         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
72         expected_result = json.loads(sample_output)
73         result = p.run(args)
74         self.assertEqual(result, expected_result)
75
76     def test_netperf_unsuccessful_sla(self, mock_ssh):
77
78         p = netperf.Netperf(self.ctx)
79         mock_ssh.SSH().execute.return_value = (0, '', '')
80         p.host = mock_ssh.SSH()
81
82         options = {}
83         args = {
84             'options': options,
85             'sla': {'mean_latency': 5}
86         }
87
88         sample_output = self._read_sample_output()
89         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
90         self.assertRaises(AssertionError, p.run, args)
91
92     def test_netperf_unsuccessful_script_error(self, mock_ssh):
93
94         p = netperf.Netperf(self.ctx)
95         mock_ssh.SSH().execute.return_value = (0, '', '')
96         p.host = mock_ssh.SSH()
97
98         options = {}
99         args = {'options': options}
100
101         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
102         self.assertRaises(RuntimeError, p.run, args)
103
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()
109         return sample_output
110
111
112 def main():
113     unittest.main()
114
115 if __name__ == '__main__':
116     main()