Merge "Fix adding right deb repo based on the distro we are running on"
[yardstick.git] / tests / unit / benchmark / contexts / test_sriov.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from __future__ import absolute_import
16 import os
17 import mock
18 import unittest
19
20 from yardstick.benchmark.contexts import sriov
21
22 NIC_INPUT = {
23     'interface': {},
24     'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
25     'pci': ['0000:06:00.0', '0000:06:00.1'],
26     'phy_driver': 'i40e'}
27 DRIVER = "i40e"
28 NIC_DETAILS = {
29     'interface': {0: 'enp6s0f0', 1: 'enp6s0f1'},
30     'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
31     'pci': ['0000:06:00.0', '0000:06:00.1'],
32     'phy_driver': 'i40e'}
33
34 CORRECT_FILE_PATH = "/etc/yardstick/nodes/pod_sriov.yaml"
35 WRONG_FILE_PATH = "/etc/yardstick/wrong.yaml"
36 SAMPLE_FILE = "sriov_sample_write_to_file.txt"
37
38 SRIOV = [{
39     'auth_type': 'ssh_key',
40     'name': 'sriov',
41     'ssh_port': 22,
42     'ip': '10.10.10.11',
43     'key_filename': '/root/.ssh/id_rsa',
44     'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
45     'role': 'Sriov',
46     'user': 'root',
47     'images': '/var/lib/libvirt/images/ubuntu1.img',
48     'phy_driver': 'i40e',
49     'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
50
51 SRIOV_PASSWORD = [{
52     'auth_type': 'password',
53     'name': 'sriov',
54     'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
55     'ip': '10.10.10.11',
56     'role': 'Sriov',
57     'user': 'root',
58     'images': '/var/lib/libvirt/images/ubuntu1.img',
59     'phy_driver': 'i40e',
60     'password': 'password',
61     'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
62
63 vfnic = "i40evf"
64 PCIS = ['0000:06:00.0', '0000:06:00.1']
65
66
67 class SriovTestCase(unittest.TestCase):
68
69     NODES_SAMPLE_SSH = "sriov_sample_ssh_key.yaml"
70     NODES_SAMPLE_PASSWORD = "sriov_sample_password.yaml"
71
72     def setUp(self):
73         self.test_context = sriov.Sriov()
74
75     def test_construct(self):
76         self.assertIsNone(self.test_context.name)
77         self.assertIsNone(self.test_context.file_path)
78         self.assertEqual(self.test_context.nodes, [])
79         self.assertEqual(self.test_context.sriov, [])
80         self.assertFalse(self.test_context.vm_deploy)
81         self.assertTrue(self.test_context.first_run)
82         self.assertEqual(self.test_context.user, "")
83         self.assertEqual(self.test_context.ssh_ip, "")
84         self.assertEqual(self.test_context.passwd, "")
85         self.assertEqual(self.test_context.ssh_port, "")
86         self.assertEqual(self.test_context.auth_type, "")
87
88     def test_init(self):
89         self.test_context.parse_pod_and_get_data = mock.Mock()
90         self.test_context.file_path = CORRECT_FILE_PATH
91         self.test_context.init()
92         self.assertIsNone(self.test_context.init())
93
94     def test_successful_init_with_ssh(self):
95         CORRECT_FILE_PATH = self._get_file_abspath(self.NODES_SAMPLE_SSH)
96         self.test_context.parse_pod_and_get_data(CORRECT_FILE_PATH)
97
98     def test_successful_init_with_password(self):
99         CORRECT_FILE_PATH = self._get_file_abspath(self.NODES_SAMPLE_PASSWORD)
100         self.test_context.parse_pod_and_get_data(CORRECT_FILE_PATH)
101
102     def test_unsuccessful_init(self):
103         self.assertRaises(
104             IOError,
105             lambda: self.test_context.parse_pod_and_get_data(WRONG_FILE_PATH))
106
107     @mock.patch('yardstick.network_services.utils.provision_tool', return_value="a")
108     def test_ssh_connection(self, mock_prov):
109         with mock.patch("yardstick.ssh.SSH") as ssh:
110             ssh_mock = mock.Mock(autospec=ssh.SSH)
111             ssh_mock.execute = \
112                 mock.Mock(return_value=(1, "a", ""))
113             ssh.return_value = ssh_mock
114             mock_prov.provision_tool = mock.Mock()
115             sriov_obj = sriov.Sriov()
116             sriov_obj.connection = ssh_mock
117             sriov_obj.sriov = SRIOV_PASSWORD
118             self.assertIsNone(sriov_obj.ssh_remote_machine())
119
120     @mock.patch('yardstick.network_services.utils.provision_tool', return_value="a")
121     def test_ssh_connection_ssh_key(self, mock_prov):
122         with mock.patch("yardstick.ssh.SSH") as ssh:
123             ssh_mock = mock.Mock(autospec=ssh.SSH)
124             ssh_mock.execute = \
125                 mock.Mock(return_value=(1, "a", ""))
126             ssh.return_value = ssh_mock
127             mock_prov.provision_tool = mock.Mock()
128             sriov_obj = sriov.Sriov()
129             sriov_obj.connection = ssh_mock
130             sriov_obj.sriov = SRIOV
131             sriov_obj.key_filename = '/root/.ssh/id_rsa'
132             self.assertIsNone(sriov_obj.ssh_remote_machine())
133
134     def test_get_nic_details(self):
135         with mock.patch("yardstick.ssh.SSH") as ssh:
136             ssh_mock = mock.Mock(autospec=ssh.SSH)
137             ssh_mock.execute = \
138                 mock.Mock(return_value=(0, "eth0 eth1", ""))
139             ssh.return_value = ssh_mock
140             sriov_obj = sriov.Sriov()
141             sriov_obj.sriov = SRIOV
142             sriov_obj.connection = ssh_mock
143             self.assertIsNotNone(sriov_obj.get_nic_details())
144
145     def test_install_req_libs(self):
146         with mock.patch("yardstick.ssh.SSH") as ssh:
147             ssh_mock = mock.Mock(autospec=ssh.SSH)
148             ssh_mock.execute = \
149                 mock.Mock(return_value=(0, {}, ""))
150             ssh.return_value = ssh_mock
151             sriov_obj = sriov.Sriov()
152             sriov_obj.first_run = True
153             sriov_obj.connection = ssh_mock
154             self.assertIsNone(sriov_obj.install_req_libs())
155
156     def test_configure_nics_for_sriov(self):
157         with mock.patch("yardstick.ssh.SSH") as ssh:
158             nic_details = {
159                 'interface': {0: 'enp6s0f0', 1: 'enp6s0f1'},
160                 'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
161                 'pci': ['0000:06:00.0', '0000:06:00.1'],
162                 'phy_driver': 'i40e',
163                 'vf_pci': [{}, {}]}
164             ssh_mock = mock.Mock(autospec=ssh.SSH)
165             ssh_mock.execute = \
166                 mock.Mock((DRIVER), return_value=(0, "0 driver", ""))
167             ssh.return_value = ssh_mock
168             ssh_mock.execute = \
169                 mock.Mock(return_value=(0, {}, ""))
170             ssh.return_value = ssh_mock
171             for i in range(len(NIC_DETAILS['pci'])):
172                 ssh_mock.execute = \
173                     mock.Mock(return_value=(0, {}, ""))
174                 ssh_mock.execute = \
175                     mock.Mock(return_value=(0, {}, ""))
176                 sriov_obj = sriov.Sriov()
177                 sriov_obj.connection = ssh_mock
178                 ssh_mock.execute = \
179                     mock.Mock(return_value=(
180                         0,
181                         "{'0':'06:02:00','1':'06:06:00'}",
182                         ""))
183                 sriov_obj.get_vf_datas = mock.Mock(return_value={
184                     '0000:06:00.0': '0000:06:02.0'})
185                 nic_details['vf_pci'][i] = sriov_obj.get_vf_datas.return_value
186                 vf_pci = [[], []]
187                 vf_pci[i] = sriov_obj.get_vf_datas.return_value
188             with mock.patch("yardstick.benchmark.contexts.sriov.time"):
189                 self.assertIsNotNone(sriov_obj.configure_nics_for_sriov(DRIVER, NIC_DETAILS))
190
191     def test_setup_sriov_context(self):
192         with mock.patch("yardstick.ssh.SSH") as ssh:
193             nic_details = {
194                 'interface': {0: 'enp6s0f0', 1: 'enp6s0f1'},
195                 'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
196                 'pci': ['0000:06:00.0', '0000:06:00.1'],
197                 'phy_driver': 'i40e',
198                 'vf_pci': [{'vf_pci': '06:02.00'}, {'vf_pci': '06:06.00'}]}
199             vf = [{'vf_pci': '06:02.00'}, {'vf_pci': '06:06.00'}]
200             ssh_mock = mock.Mock(autospec=ssh.SSH)
201             ssh_mock.execute = \
202                 mock.Mock(return_value=(0, {}, ""))
203             ssh.return_value = ssh_mock
204             sriov_obj = sriov.Sriov()
205             sriov_obj.connection = ssh_mock
206             sriov_obj.sriov = SRIOV
207             blacklist = "/etc/modprobe.d/blacklist.conf"
208             self.assertEqual(vfnic, "i40evf")
209             mock_sriov = mock.Mock()
210             mock_sriov.sriov_obj.read_from_file(blacklist)
211             sriov_obj.read_from_file = mock.Mock(
212                 return_value="some random text")
213             ssh_mock.execute = \
214                 mock.Mock(return_value=(0, {}, ""))
215             sriov_obj.configure_nics_for_sriov = mock.Mock(
216                 return_value=nic_details)
217             nic_details = sriov_obj.configure_nics_for_sriov.return_value
218             self.assertEqual(vf, nic_details['vf_pci'])
219             vf = [
220                 {'vf_pci': '06:02.00', 'mac': '00:00:00:00:00:0a'},
221                 {'vf_pci': '06:06.00', 'mac': '00:00:00:00:00:0b'}]
222             sriov_obj.add_sriov_interface = mock.Mock()
223             ssh_mock.execute = \
224                 mock.Mock(return_value=(0, {}, ""))
225             ssh_mock.put = mock.Mock()
226             sriov_obj.check_output = mock.Mock(return_value=(1, {}))
227             with mock.patch("yardstick.benchmark.contexts.sriov.time"):
228                 self.assertIsNone(sriov_obj.setup_sriov_context(PCIS, nic_details, DRIVER))
229
230     def test_setup_sriov_context_vm_already_present(self):
231         with mock.patch("yardstick.ssh.SSH") as ssh:
232             nic_details = {
233                 'interface': {0: 'enp6s0f0', 1: 'enp6s0f1'},
234                 'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
235                 'pci': ['0000:06:00.0', '0000:06:00.1'],
236                 'phy_driver': 'i40e',
237                 'vf_pci': [{'vf_pci': '06:02.00'}, {'vf_pci': '06:06.00'}]}
238             vf = [{'vf_pci': '06:02.00'}, {'vf_pci': '06:06.00'}]
239             ssh_mock = mock.Mock(autospec=ssh.SSH)
240             ssh_mock.execute = \
241                 mock.Mock(return_value=(0, {}, ""))
242             ssh.return_value = ssh_mock
243             sriov_obj = sriov.Sriov()
244             sriov_obj.connection = ssh_mock
245             sriov_obj.sriov = SRIOV
246             blacklist = "/etc/modprobe.d/blacklist.conf"
247             self.assertEqual(vfnic, "i40evf")
248             mock_sriov = mock.Mock()
249             mock_sriov.sriov_obj.read_from_file(blacklist)
250             sriov_obj.read_from_file = mock.Mock(
251                 return_value="some random text")
252             ssh_mock.execute = \
253                 mock.Mock(return_value=(0, {}, ""))
254             sriov_obj.configure_nics_for_sriov = mock.Mock(
255                 return_value=nic_details)
256             nic_details = sriov_obj.configure_nics_for_sriov.return_value
257             self.assertEqual(vf, nic_details['vf_pci'])
258             vf = [
259                 {'vf_pci': '06:02.00', 'mac': '00:00:00:00:00:0a'},
260                 {'vf_pci': '06:06.00', 'mac': '00:00:00:00:00:0b'}]
261             sriov_obj.add_sriov_interface = mock.Mock()
262             ssh_mock.execute = \
263                 mock.Mock(return_value=(0, {}, ""))
264             ssh_mock.put = mock.Mock()
265             sriov_obj.check_output = mock.Mock(return_value=(0, "vm1"))
266             with mock.patch("yardstick.benchmark.contexts.sriov.time"):
267                 self.assertIsNone(sriov_obj.setup_sriov_context(PCIS, nic_details, DRIVER))
268
269     @mock.patch(
270         'yardstick.benchmark.contexts.sriov',
271         return_value="Domain vm1 created from /tmp/vm_sriov.xml")
272     def test_is_vm_created(self, NIC_INPUT):
273         with mock.patch("yardstick.ssh.SSH") as ssh:
274             ssh_mock = mock.Mock(autospec=ssh.SSH)
275             ssh_mock.execute = \
276                 mock.Mock(return_value=(0, {}, ""))
277             ssh.return_value = ssh_mock
278             mock_sriov = mock.Mock()
279             pcis = NIC_DETAILS['pci']
280             driver = NIC_DETAILS['phy_driver']
281             self.assertIsNotNone(
282                 mock_sriov.sriov_obj.setup_sriov_context(
283                     pcis,
284                     NIC_DETAILS,
285                     driver))
286
287     def test_add_sriov_interface(self):
288         with mock.patch("yardstick.ssh.SSH") as ssh:
289             ssh_mock = mock.Mock(autospec=ssh.SSH)
290             ssh_mock.execute = \
291                 mock.Mock(return_value=(0, {}, ""))
292             ssh.return_value = ssh_mock
293             sriov_obj = sriov.Sriov()
294             sriov_obj.connection = ssh_mock
295             with mock.patch("xml.etree.ElementTree.parse") as parse:
296                 with mock.patch("re.search") as re:
297                     with mock.patch("xml.etree.ElementTree.SubElement") \
298                         as elem:
299                             parse = mock.Mock(return_value="root")
300                             re = mock.Mock()
301                             elem = mock.Mock()
302                             print("{0} {1} {2}".format(parse, re, elem))
303                             self.assertIsNone(sriov_obj.add_sriov_interface(
304                                 0,
305                                 "0000:06:02.0",
306                                 "00:00:00:00:00:0a",
307                                 "/tmp/vm_sriov.xml"))
308
309     def test_get_virtual_devices(self):
310         with mock.patch("yardstick.ssh.SSH") as ssh:
311             ssh_mock = mock.Mock(autospec=ssh.SSH)
312             ssh_mock.execute = \
313                 mock.Mock(return_value=(0, {}, ""))
314             ssh.return_value = ssh_mock
315             sriov_obj = sriov.Sriov()
316             sriov_obj.connection = ssh_mock
317             pci_out = " \
318             PCI_CLASS=20000 \
319             PCI_ID=8086:154C \
320             PCI_SUBSYS_ID=8086:0000 \
321             PCI_SLOT_NAME=0000:06:02.0 \
322             MODALIAS= \
323             pci:v00008086d0000154Csv00008086sd00000000bc02sc00i00"
324             pci = "0000:06:00.0"
325             sriov_obj.check_output = mock.Mock(return_value=(0, pci_out))
326             with mock.patch("re.search") as re:
327                 re = mock.Mock(return_value="a")
328                 print("{0}".format(re))
329                 self.assertIsNotNone(sriov_obj.get_virtual_devices(pci))
330
331     def test_get_vf_datas(self):
332         with mock.patch("yardstick.ssh.SSH") as ssh:
333             ssh_mock = mock.Mock(autospec=ssh.SSH)
334             ssh_mock.execute = \
335                 mock.Mock(return_value=(0, {}, ""))
336             ssh.return_value = ssh_mock
337             sriov_obj = sriov.Sriov()
338             sriov_obj.connection = ssh_mock
339             sriov_obj.get_virtual_devices = mock.Mock(
340                 return_value={'0000:06:00.0': '0000:06:02.0'})
341             with mock.patch("re.search") as re:
342                 re = mock.Mock()
343                 print("{0}".format(re))
344                 self.assertIsNotNone(sriov_obj.get_vf_datas(
345                     'vf_pci',
346                     {'0000:06:00.0': '0000:06:02.0'},
347                     "00:00:00:00:00:0a"))
348
349     def test_check_output(self):
350         with mock.patch("yardstick.ssh.SSH") as ssh:
351             cmd = "command"
352             ssh_mock = mock.Mock(autospec=ssh.SSH)
353             ssh_mock.execute = \
354                 mock.Mock(return_value=(0, {}, ""))
355             ssh.return_value = ssh_mock
356             sriov_obj = sriov.Sriov()
357             sriov_obj.connection = ssh_mock
358             self.assertIsNotNone(sriov_obj.check_output(cmd, None))
359
360     def test_split_cpu_list_available(self):
361         with mock.patch("itertools.chain") as iter1:
362             iter1 = mock.Mock()
363             print("{0}".format(iter1))
364             sriov_obj = sriov.Sriov()
365             self.assertIsNotNone(sriov_obj.split_cpu_list('0,5'))
366
367     def test_split_cpu_list_null(self):
368         with mock.patch("itertools.chain") as iter1:
369             iter1 = mock.Mock()
370             print("{0}".format(iter1))
371             sriov_obj = sriov.Sriov()
372             self.assertEqual(sriov_obj.split_cpu_list([]), [])
373
374     def test_destroy_vm_successful(self):
375         with mock.patch("yardstick.ssh.SSH") as ssh:
376             ssh_mock = mock.Mock(autospec=ssh.SSH)
377             ssh_mock.execute = \
378                 mock.Mock(return_value=(0, {}, ""))
379             ssh.return_value = ssh_mock
380             sriov_obj = sriov.Sriov()
381             sriov_obj.connection = ssh_mock
382             sriov_obj.sriov = SRIOV
383             sriov_obj.check_output = mock.Mock(return_value=(0, "vm1"))
384             ssh_mock.execute = \
385                 mock.Mock(return_value=(0, {}, ""))
386             ssh_mock.execute = \
387                 mock.Mock(return_value=(0, {}, ""))
388             ssh_mock.execute = \
389                 mock.Mock(return_value=(0, "0 i40e"))
390             ssh_mock.execute = \
391                 mock.Mock(return_value=(0, "0 i40e"))
392             self.assertIsNone(sriov_obj.destroy_vm())
393
394     def test_destroy_vm_unsuccessful(self):
395         with mock.patch("yardstick.ssh.SSH") as ssh:
396             ssh_mock = mock.Mock(autospec=ssh.SSH)
397             ssh_mock.execute = \
398                 mock.Mock(return_value=(0, {}, ""))
399             ssh.return_value = ssh_mock
400             sriov_obj = sriov.Sriov()
401             sriov_obj.connection = ssh_mock
402             sriov_obj.sriov = SRIOV
403             sriov_obj.check_output = mock.Mock(return_value=(1, {}))
404             self.assertIsNone(sriov_obj.destroy_vm())
405
406     def test_read_from_file(self):
407         CORRECT_FILE_PATH = self._get_file_abspath(self.NODES_SAMPLE_PASSWORD)
408         sriov_obj = sriov.Sriov()
409         self.assertIsNotNone(sriov_obj.read_from_file(CORRECT_FILE_PATH))
410
411     def test_write_to_file(self):
412         sriov_obj = sriov.Sriov()
413         self.assertIsNone(sriov_obj.write_to_file(SAMPLE_FILE, "some content"))
414
415     def _get_file_abspath(self, filename):
416         curr_path = os.path.dirname(os.path.abspath(__file__))
417         file_path = os.path.join(curr_path, filename)
418         return file_path
419
420 if __name__ == '__main__':
421     unittest.main()