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