Adding cpu set to enable affinity for given vcpu
[yardstick.git] / tests / unit / benchmark / contexts / standalone / test_model.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 # Unittest for yardstick.benchmark.contexts.standalone.model
18
19
20 from __future__ import absolute_import
21 import os
22 import unittest
23 import errno
24 import mock
25
26 from yardstick.common import constants as consts
27 from yardstick.benchmark.contexts.standalone.model import Libvirt
28 from yardstick.benchmark.contexts.standalone.model import StandaloneContextHelper
29 from yardstick.benchmark.contexts.standalone import model
30 from yardstick.network_services.utils import PciAddress
31
32
33 class ModelLibvirtTestCase(unittest.TestCase):
34
35     def test_check_if_vm_exists_and_delete(self):
36         with mock.patch("yardstick.ssh.SSH") as ssh:
37             ssh_mock = mock.Mock(autospec=ssh.SSH)
38             ssh_mock.execute = \
39                 mock.Mock(return_value=(0, "a", ""))
40             ssh.return_value = ssh_mock
41         result = Libvirt.check_if_vm_exists_and_delete("vm_0", ssh_mock)
42         self.assertIsNone(result)
43
44     def test_virsh_create_vm(self):
45         with mock.patch("yardstick.ssh.SSH") as ssh:
46             ssh_mock = mock.Mock(autospec=ssh.SSH)
47             ssh_mock.execute = \
48                 mock.Mock(return_value=(0, "a", ""))
49             ssh.return_value = ssh_mock
50         result = Libvirt.virsh_create_vm(ssh_mock, "vm_0")
51         self.assertIsNone(result)
52
53     def test_virsh_destroy_vm(self):
54         with mock.patch("yardstick.ssh.SSH") as ssh:
55             ssh_mock = mock.Mock(autospec=ssh.SSH)
56             ssh_mock.execute = \
57                 mock.Mock(return_value=(0, "a", ""))
58             ssh.return_value = ssh_mock
59         result = Libvirt.virsh_destroy_vm("vm_0", ssh_mock)
60         self.assertIsNone(result)
61
62     @mock.patch('yardstick.benchmark.contexts.standalone.model.ET')
63     def test_add_interface_address(self, mock_et):
64         pci_address = PciAddress.parse_address("0000:00:04.0", multi_line=True)
65         result = Libvirt.add_interface_address("<interface/>", pci_address)
66         self.assertIsNotNone(result)
67
68     @mock.patch('yardstick.benchmark.contexts.standalone.model.Libvirt.add_interface_address')
69     @mock.patch('yardstick.benchmark.contexts.standalone.model.ET')
70     def test_add_ovs_interfaces(self, mock_et, mock_add_interface_address):
71         pci_address = PciAddress.parse_address("0000:00:04.0", multi_line=True)
72         result = Libvirt.add_ovs_interface("/usr/local", 0, "0000:00:04.0",
73                                                 "00:00:00:00:00:01", "xml")
74         self.assertIsNone(result)
75
76     @mock.patch('yardstick.benchmark.contexts.standalone.model.Libvirt.add_interface_address')
77     @mock.patch('yardstick.benchmark.contexts.standalone.model.ET')
78     def test_add_sriov_interfaces(self, mock_et, mock_add_interface_address):
79         pci_address = PciAddress.parse_address("0000:00:04.0", multi_line=True)
80         result = Libvirt.add_sriov_interfaces("0000:00:05.0", "0000:00:04.0",
81                                               "00:00:00:00:00:01", "xml")
82         self.assertIsNone(result)
83
84     def test_create_snapshot_qemu(self):
85         result = "/var/lib/libvirt/images/0.qcow2"
86         with mock.patch("yardstick.ssh.SSH") as ssh:
87             ssh_mock = mock.Mock(autospec=ssh.SSH)
88             ssh_mock.execute = \
89                 mock.Mock(return_value=(0, "a", ""))
90             ssh.return_value = ssh_mock
91         image = Libvirt.create_snapshot_qemu(ssh_mock, "0", "ubuntu.img")
92         self.assertEqual(image, result)
93
94     @mock.patch("yardstick.benchmark.contexts.standalone.model.Libvirt.pin_vcpu_for_perf")
95     @mock.patch("yardstick.benchmark.contexts.standalone.model.Libvirt.create_snapshot_qemu")
96     @mock.patch('yardstick.benchmark.contexts.standalone.model.open')
97     @mock.patch('yardstick.benchmark.contexts.standalone.model.write_file')
98     def test_build_vm_xml(self, mock_open, mock_write_file, mock_create_snapshot_qemu,
99                           mock_pin_vcpu_for_perf):
100         result = [4]
101         with mock.patch("yardstick.ssh.SSH") as ssh:
102             ssh_mock = mock.Mock(autospec=ssh.SSH)
103             ssh_mock.execute = \
104                 mock.Mock(return_value=(0, "a", ""))
105             ssh.return_value = ssh_mock
106         mock_create_snapshot_qemu.return_value = "0.img"
107
108         status = Libvirt.build_vm_xml(ssh_mock, {}, "test", "vm_0", 0)
109         self.assertEqual(status[0], result[0])
110
111     def test_update_interrupts_hugepages_perf(self):
112         with mock.patch("yardstick.ssh.SSH") as ssh:
113             ssh_mock = mock.Mock(autospec=ssh.SSH)
114             ssh_mock.execute = \
115                 mock.Mock(return_value=(0, "a", ""))
116             ssh.return_value = ssh_mock
117         status = Libvirt.update_interrupts_hugepages_perf(ssh_mock)
118         self.assertIsNone(status)
119
120     @mock.patch("yardstick.benchmark.contexts.standalone.model.CpuSysCores")
121     @mock.patch("yardstick.benchmark.contexts.standalone.model.Libvirt.update_interrupts_hugepages_perf")
122     def test_pin_vcpu_for_perf(self, mock_update_interrupts_hugepages_perf, mock_CpuSysCores):
123         with mock.patch("yardstick.ssh.SSH") as ssh:
124             ssh_mock = mock.Mock(autospec=ssh.SSH)
125             ssh_mock.execute = \
126                 mock.Mock(return_value=(0, "a", ""))
127             ssh.return_value = ssh_mock
128         status = Libvirt.pin_vcpu_for_perf(ssh_mock, "vm_0", 4)
129         self.assertIsNotNone(status)
130
131 class StandaloneContextHelperTestCase(unittest.TestCase):
132
133     NODE_SAMPLE = "nodes_sample.yaml"
134     NODE_SRIOV_SAMPLE = "nodes_sriov_sample.yaml"
135
136     NETWORKS = {
137         'mgmt': {'cidr': '152.16.100.10/24'},
138         'private_0': {
139          'phy_port': "0000:05:00.0",
140          'vpci': "0000:00:07.0",
141          'cidr': '152.16.100.10/24',
142          'gateway_ip': '152.16.100.20'},
143         'public_0': {
144          'phy_port': "0000:05:00.1",
145          'vpci': "0000:00:08.0",
146          'cidr': '152.16.40.10/24',
147          'gateway_ip': '152.16.100.20'}
148     }
149
150     def setUp(self):
151         self.helper = StandaloneContextHelper()
152
153     def test___init__(self):
154         self.assertIsNone(self.helper.file_path)
155
156     def test_install_req_libs(self):
157         with mock.patch("yardstick.ssh.SSH") as ssh:
158             ssh_mock = mock.Mock(autospec=ssh.SSH)
159             ssh_mock.execute = \
160                 mock.Mock(return_value=(1, "a", ""))
161             ssh.return_value = ssh_mock
162         status = StandaloneContextHelper.install_req_libs(ssh_mock)
163         self.assertIsNone(status)
164
165     def test_get_kernel_module(self):
166         with mock.patch("yardstick.ssh.SSH") as ssh:
167             ssh_mock = mock.Mock(autospec=ssh.SSH)
168             ssh_mock.execute = \
169                 mock.Mock(return_value=(1, "i40e", ""))
170             ssh.return_value = ssh_mock
171         status = StandaloneContextHelper.get_kernel_module(ssh_mock, "05:00.0", None)
172         self.assertEqual(status, "i40e")
173
174     @mock.patch('yardstick.benchmark.contexts.standalone.model.StandaloneContextHelper.get_kernel_module')
175     def test_get_nic_details(self, mock_get_kernel_module):
176         with mock.patch("yardstick.ssh.SSH") as ssh:
177             ssh_mock = mock.Mock(autospec=ssh.SSH)
178             ssh_mock.execute = \
179                 mock.Mock(return_value=(1, "i40e ixgbe", ""))
180             ssh.return_value = ssh_mock
181         mock_get_kernel_module.return_value = "i40e"
182         status = StandaloneContextHelper.get_nic_details(ssh_mock, self.NETWORKS, "dpdk-devbind.py")
183         self.assertIsNotNone(status)
184
185     def test_get_virtual_devices(self):
186         pattern = "PCI_SLOT_NAME=0000:05:00.0"
187         with mock.patch("yardstick.ssh.SSH") as ssh:
188             ssh_mock = mock.Mock(autospec=ssh.SSH)
189             ssh_mock.execute = \
190                     mock.Mock(return_value=(1, pattern, ""))
191             ssh.return_value = ssh_mock
192         status = StandaloneContextHelper.get_virtual_devices(ssh_mock, "0000:00:05.0")
193         self.assertIsNotNone(status)
194
195     def _get_file_abspath(self, filename):
196         curr_path = os.path.dirname(os.path.abspath(__file__))
197         file_path = os.path.join(curr_path, filename)
198         return file_path
199
200     def test_read_config_file(self):
201         self.helper.file_path = self._get_file_abspath(self.NODE_SAMPLE)
202         status = self.helper.read_config_file()
203         self.assertIsNotNone(status)
204
205     def test_parse_pod_file(self):
206         self.helper.file_path = self._get_file_abspath("dummy")
207         self.assertRaises(IOError, self.helper.parse_pod_file, self.helper.file_path)
208
209         self.helper.file_path = self._get_file_abspath(self.NODE_SAMPLE)
210         self.assertRaises(TypeError, self.helper.parse_pod_file, self.helper.file_path)
211
212         self.helper.file_path = self._get_file_abspath(self.NODE_SRIOV_SAMPLE)
213         self.assertIsNotNone(self.helper.parse_pod_file(self.helper.file_path))
214
215     def test_get_mac_address(self):
216         status = StandaloneContextHelper.get_mac_address()
217         self.assertIsNotNone(status)
218
219     @mock.patch('yardstick.ssh.SSH')
220     def test_get_mgmt_ip(self, mock_ssh):
221         with mock.patch("yardstick.ssh.SSH") as ssh:
222             ssh_mock = mock.Mock(autospec=ssh.SSH)
223             ssh_mock.execute = \
224                     mock.Mock(return_value=(1, "1.2.3.4 00:00:00:00:00:01", ""))
225             ssh.return_value = ssh_mock
226         status = StandaloneContextHelper.get_mgmt_ip(ssh_mock, "00:00:00:00:00:01", "1.1.1.1/24", {})
227         self.assertIsNotNone(status)
228
229     @mock.patch('yardstick.ssh.SSH')
230     def test_get_mgmt_ip_no(self, mock_ssh):
231         with mock.patch("yardstick.ssh.SSH") as ssh:
232             ssh_mock = mock.Mock(autospec=ssh.SSH)
233             ssh_mock.execute = \
234                     mock.Mock(return_value=(1, "", ""))
235             ssh.return_value = ssh_mock
236
237         model.WAIT_FOR_BOOT = 0
238         status = StandaloneContextHelper.get_mgmt_ip(ssh_mock, "99", "1.1.1.1/24", {})
239         self.assertIsNone(status)
240
241 class ServerTestCase(unittest.TestCase):
242
243     NETWORKS = {
244         'mgmt': {'cidr': '152.16.100.10/24'},
245         'private_0': {
246          'phy_port': "0000:05:00.0",
247          'vpci': "0000:00:07.0",
248          'driver': 'i40e',
249          'mac': '',
250          'cidr': '152.16.100.10/24',
251          'gateway_ip': '152.16.100.20'},
252         'public_0': {
253          'phy_port': "0000:05:00.1",
254          'vpci': "0000:00:08.0",
255          'driver': 'i40e',
256          'mac': '',
257          'cidr': '152.16.40.10/24',
258          'gateway_ip': '152.16.100.20'}
259     }
260     def setUp(self):
261         self.server = model.Server()
262
263     def test___init__(self):
264         self.assertIsNotNone(self.server)
265
266     def test_build_vnf_interfaces(self):
267         vnf = {
268             "network_ports": {
269                 'mgmt': {'cidr': '152.16.100.10/24'},
270                 'xe0': ['private_0'],
271                 'xe1': ['public_0'],
272             }
273         }
274         status = model.Server.build_vnf_interfaces(vnf, self.NETWORKS)
275         self.assertIsNotNone(status)
276
277     def test_generate_vnf_instance(self):
278         vnf = {
279             "network_ports": {
280                 'mgmt': {'cidr': '152.16.100.10/24'},
281                 'xe0': ['private_0'],
282                 'xe1': ['public_0'],
283             }
284         }
285         status = self.server.generate_vnf_instance({}, self.NETWORKS, "1.1.1.1/24", 'vm_0', vnf, '00:00:00:00:00:01')
286         self.assertIsNotNone(status)
287
288 class OvsDeployTestCase(unittest.TestCase):
289
290     NETWORKS = {
291         'mgmt': {'cidr': '152.16.100.10/24'},
292         'private_0': {
293          'phy_port': "0000:05:00.0",
294          'vpci': "0000:00:07.0",
295          'driver': 'i40e',
296          'mac': '',
297          'cidr': '152.16.100.10/24',
298          'gateway_ip': '152.16.100.20'},
299         'public_0': {
300          'phy_port': "0000:05:00.1",
301          'vpci': "0000:00:08.0",
302          'driver': 'i40e',
303          'mac': '',
304          'cidr': '152.16.40.10/24',
305          'gateway_ip': '152.16.100.20'}
306     }
307     @mock.patch('yardstick.ssh.SSH')
308     def setUp(self, mock_ssh):
309         self.ovs_deploy = model.OvsDeploy(mock_ssh, '/tmp/dpdk-devbind.py', {})
310
311     def test___init__(self):
312         self.assertIsNotNone(self.ovs_deploy.connection)
313
314     @mock.patch('yardstick.benchmark.contexts.standalone.model.os')
315     def test_prerequisite(self, mock_ssh):
316         self.ovs_deploy.helper = mock.Mock()
317         self.assertIsNone(self.ovs_deploy.prerequisite())
318
319     @mock.patch('yardstick.benchmark.contexts.standalone.model.os')
320     def test_prerequisite(self, mock_ssh):
321         self.ovs_deploy.helper = mock.Mock()
322         self.ovs_deploy.connection.execute = \
323                     mock.Mock(return_value=(1, "1.2.3.4 00:00:00:00:00:01", ""))
324         self.ovs_deploy.prerequisite = mock.Mock()
325         self.assertIsNone(self.ovs_deploy.ovs_deploy())