Heat context code refactor part 2
[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': {
28                 'ip': '172.16.0.137',
29                 'user': 'cirros',
30                 'key_filename': 'mykey.key'
31             },
32             'target': {
33                 'ip': '172.16.0.138',
34                 'user': 'cirros',
35                 'key_filename': 'mykey.key',
36                 'ipaddr': '172.16.0.138'
37             }
38         }
39
40     def test_netperf_successful_setup(self, mock_ssh):
41
42         p = netperf.Netperf({}, self.ctx)
43         mock_ssh.SSH().execute.return_value = (0, '', '')
44
45         p.setup()
46         self.assertIsNotNone(p.server)
47         self.assertIsNotNone(p.client)
48         self.assertEqual(p.setup_done, True)
49
50     def test_netperf_successful_no_sla(self, mock_ssh):
51
52         options = {}
53         args = {'options': options}
54         result = {}
55
56         p = netperf.Netperf(args, self.ctx)
57         mock_ssh.SSH().execute.return_value = (0, '', '')
58         p.host = mock_ssh.SSH()
59
60         sample_output = self._read_sample_output()
61         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
62         expected_result = json.loads(sample_output)
63         p.run(result)
64         self.assertEqual(result, expected_result)
65
66     def test_netperf_successful_sla(self, mock_ssh):
67
68         options = {}
69         args = {
70             'options': options,
71             'sla': {'mean_latency': 100}
72         }
73         result = {}
74
75         p = netperf.Netperf(args, self.ctx)
76         mock_ssh.SSH().execute.return_value = (0, '', '')
77         p.host = mock_ssh.SSH()
78
79         sample_output = self._read_sample_output()
80         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
81         expected_result = json.loads(sample_output)
82         p.run(result)
83         self.assertEqual(result, expected_result)
84
85     def test_netperf_unsuccessful_sla(self, mock_ssh):
86
87         options = {}
88         args = {
89             'options': options,
90             'sla': {'mean_latency': 5}
91         }
92         result = {}
93
94         p = netperf.Netperf(args, self.ctx)
95         mock_ssh.SSH().execute.return_value = (0, '', '')
96         p.host = mock_ssh.SSH()
97
98         sample_output = self._read_sample_output()
99         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
100         self.assertRaises(AssertionError, p.run, result)
101
102     def test_netperf_unsuccessful_script_error(self, mock_ssh):
103
104         options = {}
105         args = {'options': options}
106         result = {}
107
108         p = netperf.Netperf(args, self.ctx)
109         mock_ssh.SSH().execute.return_value = (0, '', '')
110         p.host = mock_ssh.SSH()
111
112         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
113         self.assertRaises(RuntimeError, p.run, result)
114
115     def _read_sample_output(self):
116         curr_path = os.path.dirname(os.path.abspath(__file__))
117         output = os.path.join(curr_path, 'netperf_sample_output.json')
118         with open(output) as f:
119             sample_output = f.read()
120         return sample_output
121
122
123 def main():
124     unittest.main()
125
126 if __name__ == '__main__':
127     main()