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