Merge "Replace assertEqual(x, True|False) with assert[True|False](x)"
[yardstick.git] / tests / unit / network_services / helpers / test_dpdkbindnic_helper.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import mock
18 import unittest
19 from yardstick.network_services.helpers.dpdkbindnic_helper import DpdkBindHelper
20 from yardstick.network_services.helpers.dpdkbindnic_helper import DpdkBindHelperException
21 from yardstick.network_services.helpers.dpdkbindnic_helper import NETWORK_KERNEL
22 from yardstick.network_services.helpers.dpdkbindnic_helper import NETWORK_DPDK
23 from yardstick.network_services.helpers.dpdkbindnic_helper import CRYPTO_KERNEL
24 from yardstick.network_services.helpers.dpdkbindnic_helper import CRYPTO_DPDK
25 from yardstick.network_services.helpers.dpdkbindnic_helper import NETWORK_OTHER
26 from yardstick.network_services.helpers.dpdkbindnic_helper import CRYPTO_OTHER
27
28
29 class TestDpdkBindHelper(unittest.TestCase):
30     EXAMPLE_OUTPUT = """
31
32 Network devices using DPDK-compatible driver
33 ============================================
34 0000:00:04.0 'Virtio network device' drv=igb_uio unused=
35 0000:00:05.0 'Virtio network device' drv=igb_uio unused=
36
37 Network devices using kernel driver
38 ===================================
39 0000:00:03.0 'Virtio network device' if=ens3 drv=virtio-pci unused=igb_uio *Active*
40
41 Other network devices
42 =====================
43 <none>
44
45 Crypto devices using DPDK-compatible driver
46 ===========================================
47 <none>
48
49 Crypto devices using kernel driver
50 ==================================
51 <none>
52
53 Other crypto devices
54 ====================
55 <none>
56 """
57
58     PARSED_EXAMPLE = {
59         NETWORK_DPDK: [
60             {'active': False,
61              'dev_type': 'Virtio network device',
62              'driver': 'igb_uio',
63              'iface': None,
64              'unused': '',
65              'vpci': '0000:00:04.0',
66              },
67             {'active': False,
68              'dev_type': 'Virtio network device',
69              'driver': 'igb_uio',
70              'iface': None,
71              'unused': '',
72              'vpci': '0000:00:05.0',
73              }
74         ],
75         NETWORK_KERNEL: [
76             {'active': True,
77              'dev_type': 'Virtio network device',
78              'driver': 'virtio-pci',
79              'iface': 'ens3',
80              'unused': 'igb_uio',
81              'vpci': '0000:00:03.0',
82              }
83         ],
84         CRYPTO_KERNEL: [],
85         CRYPTO_DPDK: [],
86         NETWORK_OTHER: [],
87         CRYPTO_OTHER: [],
88     }
89
90     CLEAN_STATUS = {
91         NETWORK_KERNEL: [],
92         NETWORK_DPDK: [],
93         CRYPTO_KERNEL: [],
94         CRYPTO_DPDK: [],
95         NETWORK_OTHER: [],
96         CRYPTO_OTHER: [],
97     }
98
99     ONE_INPUT_LINE = ("0000:00:03.0 'Virtio network device' if=ens3 "
100                       "drv=virtio-pci unused=igb_uio *Active*")
101
102     ONE_INPUT_LINE_PARSED = [{
103         'vpci': '0000:00:03.0',
104         'dev_type': 'Virtio network device',
105         'iface': 'ens3',
106         'driver': 'virtio-pci',
107         'unused': 'igb_uio',
108         'active': True,
109     }]
110
111     def test___init__(self):
112         conn = mock.Mock()
113         conn.provision_tool = mock.Mock(return_value='path_to_tool')
114
115         dpdk_bind_helper = DpdkBindHelper(conn)
116
117         self.assertEqual(conn, dpdk_bind_helper.ssh_helper)
118         self.assertEqual(self.CLEAN_STATUS, dpdk_bind_helper.dpdk_status)
119         self.assertIsNone(dpdk_bind_helper.status_nic_row_re)
120         self.assertIsNone(dpdk_bind_helper._dpdk_nic_bind_attr)
121         self.assertIsNone(dpdk_bind_helper._status_cmd_attr)
122
123     def test__dpdk_execute(self):
124         conn = mock.Mock()
125         conn.execute = mock.Mock(return_value=(0, 'output', 'error'))
126         conn.provision_tool = mock.Mock(return_value='tool_path')
127         dpdk_bind_helper = DpdkBindHelper(conn)
128         self.assertEqual((0, 'output', 'error'),
129                          dpdk_bind_helper._dpdk_execute('command'))
130
131     def test__dpdk_execute_failure(self):
132         conn = mock.Mock()
133         conn.execute = mock.Mock(return_value=(1, 'output', 'error'))
134         conn.provision_tool = mock.Mock(return_value='tool_path')
135         dpdk_bind_helper = DpdkBindHelper(conn)
136         with self.assertRaises(DpdkBindHelperException):
137             dpdk_bind_helper._dpdk_execute('command')
138
139     def test__addline(self):
140         conn = mock.Mock()
141
142         dpdk_bind_helper = DpdkBindHelper(conn)
143
144         dpdk_bind_helper._addline(NETWORK_KERNEL, self.ONE_INPUT_LINE)
145
146         self.assertIsNotNone(dpdk_bind_helper.dpdk_status)
147         self.assertEqual(self.ONE_INPUT_LINE_PARSED, dpdk_bind_helper.dpdk_status[NETWORK_KERNEL])
148
149     def test__switch_active_dict_by_header(self):
150         line = "Crypto devices using DPDK-compatible driver"
151         olddict = 'olddict'
152         self.assertEqual(CRYPTO_DPDK, DpdkBindHelper._switch_active_dict(line, olddict))
153
154     def test__switch_active_dict_by_header_empty(self):
155         line = "<none>"
156         olddict = 'olddict'
157         self.assertEqual(olddict, DpdkBindHelper._switch_active_dict(line, olddict))
158
159     def test_parse_dpdk_status_output(self):
160         conn = mock.Mock()
161
162         dpdk_bind_helper = DpdkBindHelper(conn)
163
164         dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
165
166         self.maxDiff = None
167         self.assertEqual(self.PARSED_EXAMPLE, dpdk_bind_helper.dpdk_status)
168
169     def test_read_status(self):
170         conn = mock.Mock()
171         conn.execute = mock.Mock(return_value=(0, self.EXAMPLE_OUTPUT, ''))
172         conn.provision_tool = mock.Mock(return_value='path_to_tool')
173
174         dpdk_bind_helper = DpdkBindHelper(conn)
175
176         self.assertEqual(self.PARSED_EXAMPLE, dpdk_bind_helper.read_status())
177
178     def test__get_bound_pci_addresses(self):
179         conn = mock.Mock()
180
181         dpdk_bind_helper = DpdkBindHelper(conn)
182
183         dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
184
185         self.assertEqual(['0000:00:04.0', '0000:00:05.0'],
186                           dpdk_bind_helper._get_bound_pci_addresses(NETWORK_DPDK))
187         self.assertEqual(['0000:00:03.0'],
188                           dpdk_bind_helper._get_bound_pci_addresses(NETWORK_KERNEL))
189
190     def test_interface_driver_map(self):
191         conn = mock.Mock()
192
193         dpdk_bind_helper = DpdkBindHelper(conn)
194
195         dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
196
197         self.assertEqual({'0000:00:04.0': 'igb_uio',
198                            '0000:00:03.0': 'virtio-pci',
199                            '0000:00:05.0': 'igb_uio',
200                            },
201                           dpdk_bind_helper.interface_driver_map)
202
203     def test_bind(self):
204         conn = mock.Mock()
205         conn.execute = mock.Mock(return_value=(0, '', ''))
206         conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py')
207
208         dpdk_bind_helper = DpdkBindHelper(conn)
209         dpdk_bind_helper.read_status = mock.Mock()
210
211         dpdk_bind_helper.bind(['0000:00:03.0', '0000:00:04.0'], 'my_driver')
212
213         conn.execute.assert_called_with('sudo /opt/nsb_bin/dpdk-devbind.py --force '
214                                         '-b my_driver 0000:00:03.0 0000:00:04.0')
215         dpdk_bind_helper.read_status.assert_called_once()
216
217     def test_bind_single_pci(self):
218         conn = mock.Mock()
219         conn.execute = mock.Mock(return_value=(0, '', ''))
220         conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py')
221
222         dpdk_bind_helper = DpdkBindHelper(conn)
223         dpdk_bind_helper.read_status = mock.Mock()
224
225         dpdk_bind_helper.bind('0000:00:03.0', 'my_driver')
226
227         conn.execute.assert_called_with('sudo /opt/nsb_bin/dpdk-devbind.py --force '
228                                         '-b my_driver 0000:00:03.0')
229         dpdk_bind_helper.read_status.assert_called_once()
230
231     def test_rebind_drivers(self):
232         conn = mock.Mock()
233
234         dpdk_bind_helper = DpdkBindHelper(conn)
235
236         dpdk_bind_helper.bind = mock.Mock()
237         dpdk_bind_helper.used_drivers = {
238             'd1': ['0000:05:00.0'],
239             'd3': ['0000:05:01.0', '0000:05:02.0'],
240         }
241
242         dpdk_bind_helper.rebind_drivers()
243
244         dpdk_bind_helper.bind.assert_any_call(['0000:05:00.0'], 'd1', True)
245         dpdk_bind_helper.bind.assert_any_call(['0000:05:01.0', '0000:05:02.0'], 'd3', True)
246
247     def test_save_used_drivers(self):
248         conn = mock.Mock()
249         dpdk_bind_helper = DpdkBindHelper(conn)
250         dpdk_bind_helper.dpdk_status = self.PARSED_EXAMPLE
251
252         dpdk_bind_helper.save_used_drivers()
253
254         expected = {
255             'igb_uio': ['0000:00:04.0', '0000:00:05.0'],
256             'virtio-pci': ['0000:00:03.0'],
257         }
258
259         self.assertDictEqual(expected, dpdk_bind_helper.used_drivers)