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
29 class TestDpdkBindHelper(unittest.TestCase):
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=
37 Network devices using kernel driver
38 ===================================
39 0000:00:03.0 'Virtio network device' if=ens3 drv=virtio-pci unused=igb_uio *Active*
45 Crypto devices using DPDK-compatible driver
46 ===========================================
49 Crypto devices using kernel driver
50 ==================================
61 'dev_type': 'Virtio network device',
65 'vpci': '0000:00:04.0',
68 'dev_type': 'Virtio network device',
72 'vpci': '0000:00:05.0',
77 'dev_type': 'Virtio network device',
78 'driver': 'virtio-pci',
81 'vpci': '0000:00:03.0',
99 ONE_INPUT_LINE = ("0000:00:03.0 'Virtio network device' if=ens3 "
100 "drv=virtio-pci unused=igb_uio *Active*")
102 ONE_INPUT_LINE_PARSED = [{
103 'vpci': '0000:00:03.0',
104 'dev_type': 'Virtio network device',
106 'driver': 'virtio-pci',
111 def test___init__(self):
113 conn.provision_tool = mock.Mock(return_value='path_to_tool')
115 dpdk_bind_helper = DpdkBindHelper(conn)
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_devbind)
121 self.assertIsNone(dpdk_bind_helper._status_cmd_attr)
123 def test__dpdk_execute(self):
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'))
131 def test__dpdk_execute_failure(self):
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')
139 def test__addline(self):
142 dpdk_bind_helper = DpdkBindHelper(conn)
144 dpdk_bind_helper._addline(NETWORK_KERNEL, self.ONE_INPUT_LINE)
146 self.assertIsNotNone(dpdk_bind_helper.dpdk_status)
147 self.assertEqual(self.ONE_INPUT_LINE_PARSED, dpdk_bind_helper.dpdk_status[NETWORK_KERNEL])
149 def test__switch_active_dict_by_header(self):
150 line = "Crypto devices using DPDK-compatible driver"
152 self.assertEqual(CRYPTO_DPDK, DpdkBindHelper._switch_active_dict(line, olddict))
154 def test__switch_active_dict_by_header_empty(self):
157 self.assertEqual(olddict, DpdkBindHelper._switch_active_dict(line, olddict))
159 def test_parse_dpdk_status_output(self):
162 dpdk_bind_helper = DpdkBindHelper(conn)
164 dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
167 self.assertEqual(self.PARSED_EXAMPLE, dpdk_bind_helper.dpdk_status)
169 def test_read_status(self):
171 conn.execute = mock.Mock(return_value=(0, self.EXAMPLE_OUTPUT, ''))
172 conn.provision_tool = mock.Mock(return_value='path_to_tool')
174 dpdk_bind_helper = DpdkBindHelper(conn)
176 self.assertEqual(self.PARSED_EXAMPLE, dpdk_bind_helper.read_status())
178 def test__get_bound_pci_addresses(self):
181 dpdk_bind_helper = DpdkBindHelper(conn)
183 dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
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))
190 def test_interface_driver_map(self):
193 dpdk_bind_helper = DpdkBindHelper(conn)
195 dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
197 self.assertEqual({'0000:00:04.0': 'igb_uio',
198 '0000:00:03.0': 'virtio-pci',
199 '0000:00:05.0': 'igb_uio',
201 dpdk_bind_helper.interface_driver_map)
205 conn.execute = mock.Mock(return_value=(0, '', ''))
206 conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py')
208 dpdk_bind_helper = DpdkBindHelper(conn)
209 dpdk_bind_helper.read_status = mock.Mock()
211 dpdk_bind_helper.bind(['0000:00:03.0', '0000:00:04.0'], 'my_driver')
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()
217 def test_bind_single_pci(self):
219 conn.execute = mock.Mock(return_value=(0, '', ''))
220 conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk-devbind.py')
222 dpdk_bind_helper = DpdkBindHelper(conn)
223 dpdk_bind_helper.read_status = mock.Mock()
225 dpdk_bind_helper.bind('0000:00:03.0', 'my_driver')
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()
231 def test_rebind_drivers(self):
234 dpdk_bind_helper = DpdkBindHelper(conn)
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'],
242 dpdk_bind_helper.rebind_drivers()
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)
247 def test_save_used_drivers(self):
249 dpdk_bind_helper = DpdkBindHelper(conn)
250 dpdk_bind_helper.dpdk_status = self.PARSED_EXAMPLE
252 dpdk_bind_helper.save_used_drivers()
255 'igb_uio': ['0000:00:04.0', '0000:00:05.0'],
256 'virtio-pci': ['0000:00:03.0'],
259 self.assertDictEqual(expected, dpdk_bind_helper.used_drivers)