Merge "requirements: bump Ansible version to 2.3"
[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.dpdknicbind_helper import DpdkBindHelper
20 from yardstick.network_services.helpers.dpdknicbind_helper import DpdkBindHelperException
21 from yardstick.network_services.helpers.dpdknicbind_helper import NETWORK_KERNEL
22 from yardstick.network_services.helpers.dpdknicbind_helper import NETWORK_DPDK
23 from yardstick.network_services.helpers.dpdknicbind_helper import CRYPTO_KERNEL
24 from yardstick.network_services.helpers.dpdknicbind_helper import CRYPTO_DPDK
25 from yardstick.network_services.helpers.dpdknicbind_helper import NETWORK_OTHER
26 from yardstick.network_services.helpers.dpdknicbind_helper import CRYPTO_OTHER
27
28 pass
29
30
31 class MyTestDpdkBindHelper(unittest.TestCase):
32     EXAMPLE_OUTPUT = """
33
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=
38
39 Network devices using kernel driver
40 ===================================
41 0000:00:03.0 'Virtio network device' if=ens3 drv=virtio-pci unused=igb_uio *Active*
42
43 Other network devices
44 =====================
45 <none>
46
47 Crypto devices using DPDK-compatible driver
48 ===========================================
49 <none>
50
51 Crypto devices using kernel driver
52 ==================================
53 <none>
54
55 Other crypto devices
56 ====================
57 <none>
58 """
59
60     PARSED_EXAMPLE = {
61         NETWORK_DPDK: [
62             {'active': False,
63              'dev_type': 'Virtio network device',
64              'driver': 'igb_uio',
65              'iface': None,
66              'unused': '',
67              'vpci': '0000:00:04.0',
68              },
69             {'active': False,
70              'dev_type': 'Virtio network device',
71              'driver': 'igb_uio',
72              'iface': None,
73              'unused': '',
74              'vpci': '0000:00:05.0',
75              }
76         ],
77         NETWORK_KERNEL: [
78             {'active': True,
79              'dev_type': 'Virtio network device',
80              'driver': 'virtio-pci',
81              'iface': 'ens3',
82              'unused': 'igb_uio',
83              'vpci': '0000:00:03.0',
84              }
85         ],
86         CRYPTO_KERNEL: [],
87         CRYPTO_DPDK: [],
88         NETWORK_OTHER: [],
89         CRYPTO_OTHER: [],
90     }
91
92     CLEAN_STATUS = {
93         NETWORK_KERNEL: [],
94         NETWORK_DPDK: [],
95         CRYPTO_KERNEL: [],
96         CRYPTO_DPDK: [],
97         NETWORK_OTHER: [],
98         CRYPTO_OTHER: [],
99     }
100
101     ONE_INPUT_LINE = ("0000:00:03.0 'Virtio network device' if=ens3 "
102                       "drv=virtio-pci unused=igb_uio *Active*")
103
104     ONE_INPUT_LINE_PARSED = [{
105         'vpci': '0000:00:03.0',
106         'dev_type': 'Virtio network device',
107         'iface': 'ens3',
108         'driver': 'virtio-pci',
109         'unused': 'igb_uio',
110         'active': True,
111     }]
112
113     def test___init__(self):
114         conn = mock.Mock()
115         conn.provision_tool = mock.Mock(return_value='path_to_tool')
116
117         dpdk_bind_helper = DpdkBindHelper(conn)
118
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)
124
125     def test__dpdk_execute(self):
126         conn = mock.Mock()
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'))
131
132     def test__dpdk_execute_failure(self):
133         conn = mock.Mock()
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')
139
140     def test__addline(self):
141         conn = mock.Mock()
142
143         dpdk_bind_helper = DpdkBindHelper(conn)
144
145         dpdk_bind_helper._addline(NETWORK_KERNEL, self.ONE_INPUT_LINE)
146
147         self.assertIsNotNone(dpdk_bind_helper.dpdk_status)
148         self.assertEquals(self.ONE_INPUT_LINE_PARSED, dpdk_bind_helper.dpdk_status[NETWORK_KERNEL])
149
150     def test__switch_active_dict_by_header(self):
151         line = "Crypto devices using DPDK-compatible driver"
152         olddict = 'olddict'
153         self.assertEqual(CRYPTO_DPDK, DpdkBindHelper._switch_active_dict(line, olddict))
154
155     def test__switch_active_dict_by_header_empty(self):
156         line = "<none>"
157         olddict = 'olddict'
158         self.assertEqual(olddict, DpdkBindHelper._switch_active_dict(line, olddict))
159
160     def test_parse_dpdk_status_output(self):
161         conn = mock.Mock()
162
163         dpdk_bind_helper = DpdkBindHelper(conn)
164
165         dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
166
167         self.maxDiff = None
168         self.assertEquals(self.PARSED_EXAMPLE, dpdk_bind_helper.dpdk_status)
169
170     def test_read_status(self):
171         conn = mock.Mock()
172         conn.execute = mock.Mock(return_value=(0, self.EXAMPLE_OUTPUT, ''))
173         conn.provision_tool = mock.Mock(return_value='path_to_tool')
174
175         dpdk_bind_helper = DpdkBindHelper(conn)
176
177         self.assertEquals(self.PARSED_EXAMPLE, dpdk_bind_helper.read_status())
178
179     def test__get_bound_pci_addresses(self):
180         conn = mock.Mock()
181
182         dpdk_bind_helper = DpdkBindHelper(conn)
183
184         dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
185
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))
190
191     def test_interface_driver_map(self):
192         conn = mock.Mock()
193
194         dpdk_bind_helper = DpdkBindHelper(conn)
195
196         dpdk_bind_helper.parse_dpdk_status_output(self.EXAMPLE_OUTPUT)
197
198         self.assertEquals({'0000:00:04.0': 'igb_uio',
199                            '0000:00:03.0': 'virtio-pci',
200                            '0000:00:05.0': 'igb_uio',
201                            },
202                           dpdk_bind_helper.interface_driver_map)
203
204     def test_bind(self):
205         conn = mock.Mock()
206         conn.execute = mock.Mock(return_value=(0, '', ''))
207         conn.provision_tool = mock.Mock(return_value='/opt/nsb_bin/dpdk_nic_bind.py')
208
209         dpdk_bind_helper = DpdkBindHelper(conn)
210         dpdk_bind_helper.read_status = mock.Mock()
211
212         dpdk_bind_helper.bind(['0000:00:03.0', '0000:00:04.0'], 'my_driver')
213
214         conn.execute.assert_called_with('sudo /opt/nsb_bin/dpdk_nic_bind.py --force '
215                                         '-b my_driver 0000:00:03.0 0000:00:04.0')
216         dpdk_bind_helper.read_status.assert_called_once()
217
218     def test_rebind_drivers(self):
219         conn = mock.Mock()
220
221         dpdk_bind_helper = DpdkBindHelper(conn)
222
223         dpdk_bind_helper.bind = mock.Mock()
224         dpdk_bind_helper.used_drivers = {
225             '0000:05:00.0': 'd1',
226             '0000:05:01.0': 'd3',
227         }
228
229         dpdk_bind_helper.rebind_drivers()
230
231         dpdk_bind_helper.bind.assert_any_call('0000:05:00.0', 'd1', True)
232         dpdk_bind_helper.bind.assert_any_call('0000:05:01.0', 'd3', True)
233
234     def test_save_used_drivers(self):
235         conn = mock.Mock()
236         dpdk_bind_helper = DpdkBindHelper(conn)
237         dpdk_bind_helper.dpdk_status = self.PARSED_EXAMPLE
238
239         dpdk_bind_helper.save_used_drivers()
240
241         expected = {
242             '0000:00:04.0': 'igb_uio',
243             '0000:00:05.0': 'igb_uio',
244             '0000:00:03.0': 'virtio-pci',
245         }
246
247         self.assertEqual(expected, dpdk_bind_helper.used_drivers)