Merge "Add a new monitor type: MultiMonitor that can run any number of other monitors...
[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 class PktgenDPDKTestCase(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             }
37         }
38
39     def test_pktgen_dpdk_throughput_successful_setup(self, mock_ssh):
40         args = {
41             'options': {'packetsize': 60},
42         }
43         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
44         p.setup()
45
46         mock_ssh.SSH().execute.return_value = (0, '', '')
47         self.assertIsNotNone(p.server)
48         self.assertIsNotNone(p.client)
49         self.assertEqual(p.setup_done, True)
50
51     def test_pktgen_dpdk_throughput_successful_no_sla(self, mock_ssh):
52         args = {
53             'options': {'packetsize': 60, 'number_of_ports': 10},
54         }
55
56         result = {}
57
58         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
59
60         p.server = mock_ssh.SSH()
61         p.client = mock_ssh.SSH()
62
63         mock_dpdk_result = mock.Mock()
64         mock_dpdk_result.return_value = 149300
65         p._dpdk_get_result = mock_dpdk_result
66
67         sample_output = '{"packets_per_second": 9753, "errors": 0, \
68             "packets_sent": 149776, "flows": 110}'
69         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
70
71         p.run(result)
72         expected_result = jsonutils.loads(sample_output)
73         expected_result["packets_received"] = 149300
74         expected_result["packetsize"] = 60
75         self.assertEqual(result, expected_result)
76
77     def test_pktgen_dpdk_throughput_successful_sla(self, mock_ssh):
78         args = {
79             'options': {'packetsize': 60, 'number_of_ports': 10},
80             'sla': {'max_ppm': 10000}
81         }
82         result = {}
83
84         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
85
86         p.server = mock_ssh.SSH()
87         p.client = mock_ssh.SSH()
88
89         mock_dpdk_result = mock.Mock()
90         mock_dpdk_result.return_value = 149300
91         p._dpdk_get_result = mock_dpdk_result
92
93         sample_output = '{"packets_per_second": 9753, "errors": 0, \
94             "packets_sent": 149776, "flows": 110}'
95         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
96
97         p.run(result)
98         expected_result = jsonutils.loads(sample_output)
99         expected_result["packets_received"] = 149300
100         expected_result["packetsize"] = 60
101         self.assertEqual(result, expected_result)
102
103     def test_pktgen_dpdk_throughput_unsuccessful_sla(self, mock_ssh):
104         args = {
105             'options': {'packetsize': 60, 'number_of_ports': 10},
106             'sla': {'max_ppm': 1000}
107         }
108         result = {}
109
110         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
111
112         p.server = mock_ssh.SSH()
113         p.client = mock_ssh.SSH()
114
115         mock_dpdk_result = mock.Mock()
116         mock_dpdk_result.return_value = 149300
117         p._dpdk_get_result = mock_dpdk_result
118
119         sample_output = '{"packets_per_second": 9753, "errors": 0, \
120             "packets_sent": 149776, "flows": 110}'
121         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
122         self.assertRaises(AssertionError, p.run, result)
123
124     def test_pktgen_dpdk_throughput_unsuccessful_script_error(self, mock_ssh):
125         args = {
126             'options': {'packetsize': 60, 'number_of_ports': 10},
127             'sla': {'max_ppm': 1000}
128         }
129         result = {}
130
131         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
132
133         p.server = mock_ssh.SSH()
134         p.client = mock_ssh.SSH()
135
136         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
137         self.assertRaises(RuntimeError, p.run, result)
138
139     def test_pktgen_dpdk_throughput_is_dpdk_setup(self, mock_ssh):
140         args = {
141             'options': {'packetsize': 60},
142         }
143         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
144         p.server = mock_ssh.SSH()
145
146         mock_ssh.SSH().execute.return_value = (0, '', '')
147
148         p._is_dpdk_setup("server")
149
150         mock_ssh.SSH().execute.assert_called_with(
151             "ip a | grep eth1 2>/dev/null")
152
153     def test_pktgen_dpdk_throughput_dpdk_setup(self, mock_ssh):
154         args = {
155             'options': {'packetsize': 60},
156         }
157         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
158         p.server = mock_ssh.SSH()
159         p.client = mock_ssh.SSH()
160
161         mock_ssh.SSH().execute.return_value = (0, '', '')
162
163         p.dpdk_setup()
164
165         self.assertEqual(p.dpdk_setup_done, True)
166
167     def test_pktgen_dpdk_throughput_dpdk_get_result(self, mock_ssh):
168         args = {
169             'options': {'packetsize': 60},
170         }
171         p = pktgen_dpdk_throughput.PktgenDPDK(args, self.ctx)
172         p.server = mock_ssh.SSH()
173         p.client = mock_ssh.SSH()
174
175         mock_ssh.SSH().execute.return_value = (0, '10000', '')
176
177         p._dpdk_get_result()
178
179         mock_ssh.SSH().execute.assert_called_with(
180             "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset > /dev/null 2>&1")
181
182 def main():
183     unittest.main()
184
185 if __name__ == '__main__':
186     main()