Merge "Allow VMs to access internet"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / networking / test_iperf3.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
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 ##############################################################################
9
10 # Unittest for yardstick.benchmark.scenarios.networking.iperf3.Iperf
11
12 from __future__ import absolute_import
13
14 import os
15 import unittest
16
17 import mock
18 from oslo_serialization import jsonutils
19
20 from yardstick.common import utils
21 from yardstick.benchmark.scenarios.networking import iperf3
22
23
24 @mock.patch('yardstick.benchmark.scenarios.networking.iperf3.ssh')
25 class IperfTestCase(unittest.TestCase):
26     output_name_tcp = 'iperf3_sample_output.json'
27     output_name_udp = 'iperf3_sample_output_udp.json'
28
29     def setUp(self):
30         self.ctx = {
31             'host': {
32                 'ip': '172.16.0.137',
33                 'user': 'root',
34                 'key_filename': 'mykey.key'
35             },
36             'target': {
37                 'ip': '172.16.0.138',
38                 'user': 'root',
39                 'key_filename': 'mykey.key',
40                 'ipaddr': '172.16.0.138',
41             }
42         }
43
44     def test_iperf_successful_setup(self, mock_ssh):
45
46         p = iperf3.Iperf({}, self.ctx)
47         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
48
49         p.setup()
50         self.assertIsNotNone(p.target)
51         self.assertIsNotNone(p.host)
52         mock_ssh.SSH.from_node().execute.assert_called_with("iperf3 -s -D")
53
54     def test_iperf_unsuccessful_setup(self, mock_ssh):
55
56         p = iperf3.Iperf({}, self.ctx)
57         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
58         self.assertRaises(RuntimeError, p.setup)
59
60     def test_iperf_successful_teardown(self, mock_ssh):
61
62         p = iperf3.Iperf({}, self.ctx)
63         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
64         p.host = mock_ssh.SSH.from_node()
65         p.target = mock_ssh.SSH.from_node()
66
67         p.teardown()
68         self.assertTrue(mock_ssh.SSH.from_node().close.called)
69         mock_ssh.SSH.from_node().execute.assert_called_with("pkill iperf3")
70
71     def test_iperf_successful_no_sla(self, mock_ssh):
72
73         options = {}
74         args = {'options': options}
75         result = {}
76
77         p = iperf3.Iperf(args, self.ctx)
78         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
79         p.host = mock_ssh.SSH.from_node()
80
81         sample_output = self._read_sample_output(self.output_name_tcp)
82         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
83         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
84         p.run(result)
85         self.assertEqual(result, expected_result)
86
87     def test_iperf_successful_sla(self, mock_ssh):
88
89         options = {}
90         args = {
91             'options': options,
92             'sla': {'bytes_per_second': 15000000}
93         }
94         result = {}
95
96         p = iperf3.Iperf(args, self.ctx)
97         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
98         p.host = mock_ssh.SSH.from_node()
99
100         sample_output = self._read_sample_output(self.output_name_tcp)
101         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
102         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
103         p.run(result)
104         self.assertEqual(result, expected_result)
105
106     def test_iperf_unsuccessful_sla(self, mock_ssh):
107
108         options = {}
109         args = {
110             'options': options,
111             'sla': {'bytes_per_second': 25000000}
112         }
113         result = {}
114
115         p = iperf3.Iperf(args, self.ctx)
116         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
117         p.host = mock_ssh.SSH.from_node()
118
119         sample_output = self._read_sample_output(self.output_name_tcp)
120         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
121         self.assertRaises(AssertionError, p.run, result)
122
123     def test_iperf_successful_sla_jitter(self, mock_ssh):
124         options = {"protocol": "udp", "bandwidth": "20m"}
125         args = {
126             'options': options,
127             'sla': {'jitter': 10}
128         }
129         result = {}
130
131         p = iperf3.Iperf(args, self.ctx)
132         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
133         p.host = mock_ssh.SSH.from_node()
134
135         sample_output = self._read_sample_output(self.output_name_udp)
136         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
137         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
138         p.run(result)
139         self.assertEqual(result, expected_result)
140
141     def test_iperf_unsuccessful_sla_jitter(self, mock_ssh):
142         options = {"protocol": "udp", "bandwidth": "20m"}
143         args = {
144             'options': options,
145             'sla': {'jitter': 0.0001}
146         }
147         result = {}
148
149         p = iperf3.Iperf(args, self.ctx)
150         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
151         p.host = mock_ssh.SSH.from_node()
152
153         sample_output = self._read_sample_output(self.output_name_udp)
154         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
155         self.assertRaises(AssertionError, p.run, result)
156
157     def test_iperf_successful_tcp_protocal(self, mock_ssh):
158         options = {"protocol": "tcp", "nodelay": "yes"}
159         args = {
160             'options': options,
161             'sla': {'bytes_per_second': 15000000}
162         }
163         result = {}
164
165         p = iperf3.Iperf(args, self.ctx)
166         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
167         p.host = mock_ssh.SSH.from_node()
168
169         sample_output = self._read_sample_output(self.output_name_tcp)
170         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
171         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
172         p.run(result)
173         self.assertEqual(result, expected_result)
174
175     def test_iperf_unsuccessful_script_error(self, mock_ssh):
176
177         options = {}
178         args = {'options': options}
179         result = {}
180
181         p = iperf3.Iperf(args, self.ctx)
182         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
183         p.host = mock_ssh.SSH.from_node()
184
185         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
186         self.assertRaises(RuntimeError, p.run, result)
187
188     def _read_sample_output(self, filename):
189         curr_path = os.path.dirname(os.path.abspath(__file__))
190         output = os.path.join(curr_path, filename)
191         with open(output) as f:
192             sample_output = f.read()
193         return sample_output