Merge "HA testcase containerized Compass support"
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_pktgen_dpdk_throughput.py
1 ##############################################################################
2 # Copyright (c) 2017 Nokia 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 #!/usr/bin/env python
10
11 # Unittest for yardstick.benchmark.scenarios.networking.pktgen.PktgenDPDK
12
13 from __future__ import absolute_import
14 import unittest
15
16 from oslo_serialization import jsonutils
17 import mock
18
19 from yardstick.benchmark.scenarios.networking import pktgen_dpdk_throughput
20
21
22 @mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.ssh')
23 @mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk_throughput.time')
24 class PktgenDPDKTestCase(unittest.TestCase):
25
26     def setUp(self):
27         self.ctx = {
28             'host': {
29                 'ip': '172.16.0.137',
30                 'user': 'root',
31                 'key_filename': 'mykey.key'
32             },
33             'target': {
34                 'ip': '172.16.0.138',
35                 'user': 'root',
36                 'key_filename': 'mykey.key',
37             }
38         }
39
40     def test_pktgen_dpdk_throughput_successful_setup(self, mock__time, mock_ssh):
41         args = {
42             'options': {'packetsize': 60},
43         }
44         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
45         p.setup()
46
47         mock_ssh.SSH().execute.return_value = (0, '', '')
48         self.assertIsNotNone(p.server)
49         self.assertIsNotNone(p.client)
50         self.assertEqual(p.setup_done, True)
51
52     def test_pktgen_dpdk_throughput_successful_no_sla(self, mock__time, mock_ssh):
53         args = {
54             'options': {'packetsize': 60, 'number_of_ports': 10},
55         }
56
57         result = {}
58
59         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
60
61         p.server = mock_ssh.SSH()
62         p.client = mock_ssh.SSH()
63
64         mock_dpdk_result = mock.Mock()
65         mock_dpdk_result.return_value = 149300
66         p._dpdk_get_result = mock_dpdk_result
67
68         sample_output = '{"packets_per_second": 9753, "errors": 0, \
69             "packets_sent": 149776, "flows": 110}'
70         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
71
72         p.run(result)
73         expected_result = jsonutils.loads(sample_output)
74         expected_result["packets_received"] = 149300
75         expected_result["packetsize"] = 60
76         self.assertEqual(result, expected_result)
77
78     def test_pktgen_dpdk_throughput_successful_sla(self, mock__time, mock_ssh):
79         args = {
80             'options': {'packetsize': 60, 'number_of_ports': 10},
81             'sla': {'max_ppm': 10000}
82         }
83         result = {}
84
85         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
86
87         p.server = mock_ssh.SSH()
88         p.client = mock_ssh.SSH()
89
90         mock_dpdk_result = mock.Mock()
91         mock_dpdk_result.return_value = 149300
92         p._dpdk_get_result = mock_dpdk_result
93
94         sample_output = '{"packets_per_second": 9753, "errors": 0, \
95             "packets_sent": 149776, "flows": 110}'
96         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
97
98         p.run(result)
99         expected_result = jsonutils.loads(sample_output)
100         expected_result["packets_received"] = 149300
101         expected_result["packetsize"] = 60
102         self.assertEqual(result, expected_result)
103
104     def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock__time, mock_ssh):
105         args = {
106             'options': {'packetsize': 60, 'number_of_ports': 10},
107             'sla': {'max_ppm': 1000}
108         }
109         result = {}
110
111         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
112
113         p.server = mock_ssh.SSH()
114         p.client = mock_ssh.SSH()
115
116         mock_dpdk_result = mock.Mock()
117         mock_dpdk_result.return_value = 149300
118         p._dpdk_get_result = mock_dpdk_result
119
120         sample_output = '{"packets_per_second": 9753, "errors": 0, \
121             "packets_sent": 149776, "flows": 110}'
122         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
123         self.assertRaises(AssertionError, p.run, result)
124
125     def test_pktgen_dpdk_throughput_unsuccessful_script_error(self, mock__time, mock_ssh):
126         args = {
127             'options': {'packetsize': 60, 'number_of_ports': 10},
128             'sla': {'max_ppm': 1000}
129         }
130         result = {}
131
132         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
133
134         p.server = mock_ssh.SSH()
135         p.client = mock_ssh.SSH()
136
137         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
138         self.assertRaises(RuntimeError, p.run, result)
139
140     def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock__time, mock_ssh):
141         args = {
142             'options': {'packetsize': 60},
143         }
144         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
145         p.server = mock_ssh.SSH()
146
147         mock_ssh.SSH().execute.return_value = (0, '', '')
148
149         p._is_dpdk_setup("server")
150
151         mock_ssh.SSH().execute.assert_called_with(
152             "ip a | grep eth1 2>/dev/null")
153
154     def test_pktgen_dpdk_throughput_dpdk_setup(self, mock__time, mock_ssh):
155         args = {
156             'options': {'packetsize': 60},
157         }
158         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
159         p.server = mock_ssh.SSH()
160         p.client = mock_ssh.SSH()
161
162         mock_ssh.SSH().execute.return_value = (0, '', '')
163
164         p.dpdk_setup()
165
166         self.assertEqual(p.dpdk_setup_done, True)
167
168     def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock__time, mock_ssh):
169         args = {
170             'options': {'packetsize': 60},
171         }
172         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
173         p.server = mock_ssh.SSH()
174         p.client = mock_ssh.SSH()
175
176         mock_ssh.SSH().execute.return_value = (0, '10000', '')
177
178         p._dpdk_get_result()
179
180         mock_ssh.SSH().execute.assert_called_with(
181             "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset > /dev/null 2>&1")
182
183 def main():
184     unittest.main()
185
186 if __name__ == '__main__':
187     main()