3 # Copyright (c) 2016-2017 Intel Corporation
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
31 class TestDpdkBindHelper(unittest.TestCase):
34 Network devices using DPDK-compatible driver
35 ============================================
36 0000:00:04.0 'Virtio network device' drv=igb_uio unused=
37 0000:00:05.0 'Virtio network device' drv=igb_uio unused=
39 Network devices using kernel driver
40 ===================================
41 0000:00:03.0 'Virtio network device' if=ens3 drv=virtio-pci unused=igb_uio *Active*
47 Crypto devices using DPDK-compatible driver
48 ===========================================
51 Crypto devices using kernel driver
52 ==================================
63 'dev_type': 'Virtio network device',
67 'vpci': '0000:00:04.0',
70 'dev_type': 'Virtio network device',
74 'vpci': '0000:00:05.0',
79 'dev_type': 'Virtio network device',
80 'driver': 'virtio-pci',
83 'vpci': '0000:00:03.0',
101 ONE_INPUT_LINE = ("0000:00:03.0 'Virtio network device' if=ens3 "
102 "drv=virtio-pci unused=igb_uio *Active*")
104 ONE_INPUT_LINE_PARSED = [{
105 'vpci': '0000:00:03.0',
106 'dev_type': 'Virtio network device',
108 'driver': 'virtio-pci',
113 def test___init__(self):
115 conn.provision_tool = mock.Mock(return_value='path_to_tool')
117 dpdk_bind_helper = DpdkBindHelper(conn)
119 self.assertEquals(conn, dpdk_bind_helper.ssh_helper)
120 self.assertEquals(self.CLEAN_STATUS, dpdk_bind_helper.dpdk_status)
121 self.assertIsNone(dpdk_bind_helper.status_nic_row_re)
122 self.assertIsNone(dpdk_bind_helper._dpdk_nic_bind_attr)
123 self.assertIsNone(dpdk_bind_helper._status_cmd_attr)
125 def test__dpdk_execute(self):
127 conn.execute = mock.Mock(return_value=(0, 'output', 'error'))
128 conn.provision_tool = mock.Mock(return_value='tool_path')
129 dpdk_bind_helper = DpdkBindHelper(conn)
130 self.assertEquals((0, 'output', 'error'), dpdk_bind_helper._dpdk_execute('command'))
132 def test__dpdk_execute_failure(self):
134 conn.execute = mock.Mock(return_value=(1, 'output', 'error'))
135 conn.provision_tool = mock.Mock(return_value='tool_path')
136 dpdk_bind_helper = DpdkBindHelper(conn)
137 with self.assertRaises(DpdkBindHelperException):
138 dpdk_bind_helper._dpdk_execute('command')
140 def test__addline(self):
143 dpdk_bind_helper = DpdkBindHelper(conn)
145 dpdk_bind_helper._addline(NETWORK_KERNEL, self.ONE_INPUT_LINE)
147 self.assertIsNotNone(dpdk_bind_helper.dpdk_status)
148 self.assertEquals(self.ONE_INPUT_LINE_PARSED, dpdk_bind_helper.dpdk_status[NETWORK_KERNEL])
150 def test__switch_active_dict_by_header(self):
151 line = "Crypto devices using DPDK-compatible driver"
153 self.assertEqual(CRYPTO_DPDK, DpdkBindHelper._switch_active_dict(line, olddict))
155 def test__switch_active_dict_by_header_empty(self):
158 self.assertEqual(olddict, DpdkBindHelper._switch_active_dict(line, olddict))
160 def test_parse_dpdk_status_output(self):
163 dpdk_bind_helper = DpdkBindHelper(conn)
165 dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
168 self.assertEquals(self.PARSED_EXAMPLE, dpdk_bind_helper.dpdk_status)
170 def test_read_status(self):
172 conn.execute = mock.Mock(return_value=(0, self.EXAMPLE_OUTPUT, ''))
173 conn.provision_tool = mock.Mock(return_value='path_to_tool')
175 dpdk_bind_helper = DpdkBindHelper(conn)
177 self.assertEquals(self.PARSED_EXAMPLE, dpdk_bind_helper.read_status())
179 def test__get_bound_pci_addresses(self):
182 dpdk_bind_helper = DpdkBindHelper(conn)
184 dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
186 self.assertEquals(['0000:00:04.0', '0000:00:05.0'],
187 dpdk_bind_helper._get_bound_pci_addresses(NETWORK_DPDK))
188 self.assertEquals(['0000:00:03.0'],
189 dpdk_bind_helper._get_bound_pci_addresses(NETWORK_KERNEL))
191 def test_interface_driver_map(self):
194 dpdk_bind_helper = DpdkBindHelper(conn)
196 dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
198 self.assertEquals({'0000:00:04.0': 'igb_uio',
199 '0000:00:03.0': 'virtio-pci',
200 '0000:00:05.0': 'igb_uio',
202 dpdk_bind_helper.interface_driver_map)
206 conn.execute = mock.Mock(return_value=(0, '', ''))
207 conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py')
209 dpdk_bind_helper = DpdkBindHelper(conn)
210 dpdk_bind_helper.read_status = mock.Mock()
212 dpdk_bind_helper.bind(['0000:00:03.0', '0000:00:04.0'], 'my_driver')
214 conn.execute.assert_called_with('sudo /opt/nsb_bin/dpdk-devbind.py --force '
215 '-b my_driver 0000:00:03.0 0000:00:04.0')
216 dpdk_bind_helper.read_status.assert_called_once()
218 def test_bind_single_pci(self):
220 conn.execute = mock.Mock(return_value=(0, '', ''))
221 conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py')
223 dpdk_bind_helper = DpdkBindHelper(conn)
224 dpdk_bind_helper.read_status = mock.Mock()
226 dpdk_bind_helper.bind('0000:00:03.0', 'my_driver')
228 conn.execute.assert_called_with('sudo /opt/nsb_bin/dpdk-devbind.py --force '
229 '-b my_driver 0000:00:03.0')
230 dpdk_bind_helper.read_status.assert_called_once()
232 def test_rebind_drivers(self):
235 dpdk_bind_helper = DpdkBindHelper(conn)
237 dpdk_bind_helper.bind = mock.Mock()
238 dpdk_bind_helper.used_drivers = {
239 'd1': ['0000:05:00.0'],
240 'd3': ['0000:05:01.0', '0000:05:02.0'],
243 dpdk_bind_helper.rebind_drivers()
245 dpdk_bind_helper.bind.assert_any_call(['0000:05:00.0'], 'd1', True)
246 dpdk_bind_helper.bind.assert_any_call(['0000:05:01.0', '0000:05:02.0'], 'd3', True)
248 def test_save_used_drivers(self):
250 dpdk_bind_helper = DpdkBindHelper(conn)
251 dpdk_bind_helper.dpdk_status = self.PARSED_EXAMPLE
253 dpdk_bind_helper.save_used_drivers()
256 'igb_uio': ['0000:00:04.0', '0000:00:05.0'],
257 'virtio-pci': ['0000:00:03.0'],
260 self.assertDictEqual(expected, dpdk_bind_helper.used_drivers)