Make files pep8 compliant before using assertTrue|False
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_pktgen_dpdk.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 ZTE 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.pktgen.Pktgen
13
14 from __future__ import absolute_import
15 import unittest
16
17 import mock
18
19 import yardstick.common.utils as utils
20 from yardstick.benchmark.scenarios.networking import pktgen_dpdk
21
22
23 class PktgenDPDKLatencyTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self.ctx = {
27             'host': {
28                 'ip': '172.16.0.137',
29                 'user': 'root',
30                 'key_filename': 'mykey.key'
31             },
32             'target': {
33                 'ip': '172.16.0.138',
34                 'user': 'root',
35                 'key_filename': 'mykey.key',
36                 'ipaddr': '172.16.0.138'
37             }
38         }
39
40         self._mock_ssh = mock.patch(
41             'yardstick.benchmark.scenarios.networking.pktgen_dpdk.ssh')
42         self.mock_ssh = self._mock_ssh.start()
43         self._mock_time = mock.patch(
44             'yardstick.benchmark.scenarios.networking.pktgen_dpdk.time')
45         self.mock_time = self._mock_time.start()
46
47         self.addCleanup(self._stop_mock)
48
49     def _stop_mock(self):
50         self._mock_ssh.stop()
51         self._mock_time.stop()
52
53     def test_pktgen_dpdk_successful_setup(self):
54
55         args = {
56             'options': {'packetsize': 60},
57         }
58         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
59         p.setup()
60
61         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
62         self.assertIsNotNone(p.server)
63         self.assertIsNotNone(p.client)
64         self.assertEqual(p.setup_done, True)
65
66     def test_pktgen_dpdk_successful_get_port_ip(self):
67
68         args = {
69             'options': {'packetsize': 60},
70         }
71         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
72         p.server = self.mock_ssh.SSH.from_node()
73
74         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
75
76         utils.get_port_ip(p.server, "eth1")
77
78         self.mock_ssh.SSH.from_node().execute.assert_called_with(
79             "ifconfig eth1 |grep 'inet addr' |awk '{print $2}' |cut -d ':' -f2 ")
80
81     def test_pktgen_dpdk_unsuccessful_get_port_ip(self):
82
83         args = {
84             'options': {'packetsize': 60},
85         }
86
87         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
88         p.server = self.mock_ssh.SSH.from_node()
89
90         self.mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
91         self.assertRaises(RuntimeError, utils.get_port_ip, p.server, "eth1")
92
93     def test_pktgen_dpdk_successful_get_port_mac(self):
94
95         args = {
96             'options': {'packetsize': 60},
97         }
98         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
99         p.server = self.mock_ssh.SSH.from_node()
100
101         self.mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
102
103         utils.get_port_mac(p.server, "eth1")
104
105         self.mock_ssh.SSH.from_node().execute.assert_called_with(
106             "ifconfig |grep HWaddr |grep eth1 |awk '{print $5}' ")
107
108     def test_pktgen_dpdk_unsuccessful_get_port_mac(self):
109
110         args = {
111             'options': {'packetsize': 60},
112         }
113
114         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
115         p.server = self.mock_ssh.SSH.from_node()
116
117         self.mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
118         self.assertRaises(RuntimeError, utils.get_port_mac, p.server, "eth1")
119
120     def test_pktgen_dpdk_successful_no_sla(self):
121
122         args = {
123             'options': {'packetsize': 60},
124         }
125
126         result = {}
127         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
128
129         sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n'
130         self.mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
131
132         p.run(result)
133         # with python 3 we get float, might be due python division changes
134         # AssertionError: {'avg_latency': 132.33333333333334} != {
135         # 'avg_latency': 132}
136         delta = result['avg_latency'] - 132
137         self.assertLessEqual(delta, 1)
138
139     def test_pktgen_dpdk_successful_sla(self):
140
141         args = {
142             'options': {'packetsize': 60},
143             'sla': {'max_latency': 100}
144         }
145         result = {}
146
147         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
148
149         sample_output = '100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n'
150         self.mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
151
152         p.run(result)
153
154         self.assertEqual(result, {"avg_latency": 100})
155
156     def test_pktgen_dpdk_unsuccessful_sla(self):
157
158         args = {
159             'options': {'packetsize': 60},
160             'sla': {'max_latency': 100}
161         }
162         result = {}
163
164         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
165
166         p.server = self.mock_ssh.SSH.from_node()
167         p.client = self.mock_ssh.SSH.from_node()
168
169         sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n'
170         self.mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
171         self.assertRaises(AssertionError, p.run, result)
172
173     def test_pktgen_dpdk_unsuccessful_script_error(self):
174
175         args = {
176             'options': {'packetsize': 60},
177             'sla': {'max_latency': 100}
178         }
179         result = {}
180
181         p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx)
182
183         self.mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
184         self.assertRaises(RuntimeError, p.run, result)
185
186
187 def main():
188     unittest.main()
189
190
191 if __name__ == '__main__':
192     main()