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