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