Merge "bugfix: remove pod_name in host and unify host parameter"
[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.create_snapshot_qemu")
95     @mock.patch('yardstick.benchmark.contexts.standalone.model.open')
96     @mock.patch('yardstick.benchmark.contexts.standalone.model.write_file')
97     def test_build_vm_xml(self, mock_open, mock_write_file, mock_create_snapshot_qemu):
98         result = [4]
99         with mock.patch("yardstick.ssh.SSH") as ssh:
100             ssh_mock = mock.Mock(autospec=ssh.SSH)
101             ssh_mock.execute = \
102                 mock.Mock(return_value=(0, "a", ""))
103             ssh.return_value = ssh_mock
104         mock_create_snapshot_qemu.return_value = "0.img"
105         status = Libvirt.build_vm_xml(ssh_mock, {}, "test", "vm_0", 0)
106         self.assertEqual(status[0], result[0])
107
108     def test_split_cpu_list(self):
109         result = Libvirt.split_cpu_list("1,2,3")
110         self.assertEqual(result, [1, 2, 3])
111
112     def test_get_numa_nodes(self):
113         result = Libvirt.get_numa_nodes()
114         self.assertIsNotNone(result)
115
116     def test_update_interrupts_hugepages_perf(self):
117         with mock.patch("yardstick.ssh.SSH") as ssh:
118             ssh_mock = mock.Mock(autospec=ssh.SSH)
119             ssh_mock.execute = \
120                 mock.Mock(return_value=(0, "a", ""))
121             ssh.return_value = ssh_mock
122         status = Libvirt.update_interrupts_hugepages_perf(ssh_mock)
123         self.assertIsNone(status)
124
125     @mock.patch("yardstick.benchmark.contexts.standalone.model.Libvirt.get_numa_nodes")
126     @mock.patch("yardstick.benchmark.contexts.standalone.model.Libvirt.update_interrupts_hugepages_perf")
127     def test_pin_vcpu_for_perf(self, mock_update_interrupts_hugepages_perf, mock_get_numa_nodes):
128         with mock.patch("yardstick.ssh.SSH") as ssh:
129             ssh_mock = mock.Mock(autospec=ssh.SSH)
130             ssh_mock.execute = \
131                 mock.Mock(return_value=(0, "a", ""))
132             ssh.return_value = ssh_mock
133         mock_get_numa_nodes.return_value = {'1': [18, 19, 20, 21], '0': [0, 1, 2, 3]}
134         status = Libvirt.pin_vcpu_for_perf(ssh_mock, "vm_0", 4)
135         self.assertIsNone(status)
136
137 class StandaloneContextHelperTestCase(unittest.TestCase):
138
139     NODE_SAMPLE = "nodes_sample.yaml"
140     NODE_SRIOV_SAMPLE = "nodes_sriov_sample.yaml"
141
142     NETWORKS = {
143         'mgmt': {'cidr': '152.16.100.10/24'},
144         'private_0': {
145          'phy_port': "0000:05:00.0",
146          'vpci': "0000:00:07.0",
147          'cidr': '152.16.100.10/24',
148          'gateway_ip': '152.16.100.20'},
149         'public_0': {
150          'phy_port': "0000:05:00.1",
151          'vpci': "0000:00:08.0",
152          'cidr': '152.16.40.10/24',
153          'gateway_ip': '152.16.100.20'}
154     }
155
156     def setUp(self):
157         self.helper = StandaloneContextHelper()
158
159     def test___init__(self):
160         self.assertIsNone(self.helper.file_path)
161
162     def test_install_req_libs(self):
163         with mock.patch("yardstick.ssh.SSH") as ssh:
164             ssh_mock = mock.Mock(autospec=ssh.SSH)
165             ssh_mock.execute = \
166                 mock.Mock(return_value=(1, "a", ""))
167             ssh.return_value = ssh_mock
168         status = StandaloneContextHelper.install_req_libs(ssh_mock)
169         self.assertIsNone(status)
170
171     def test_get_kernel_module(self):
172         with mock.patch("yardstick.ssh.SSH") as ssh:
173             ssh_mock = mock.Mock(autospec=ssh.SSH)
174             ssh_mock.execute = \
175                 mock.Mock(return_value=(1, "i40e", ""))
176             ssh.return_value = ssh_mock
177         status = StandaloneContextHelper.get_kernel_module(ssh_mock, "05:00.0", None)
178         self.assertEqual(status, "i40e")
179
180     @mock.patch('yardstick.benchmark.contexts.standalone.model.StandaloneContextHelper.get_kernel_module')
181     def test_get_nic_details(self, mock_get_kernel_module):
182         with mock.patch("yardstick.ssh.SSH") as ssh:
183             ssh_mock = mock.Mock(autospec=ssh.SSH)
184             ssh_mock.execute = \
185                 mock.Mock(return_value=(1, "i40e ixgbe", ""))
186             ssh.return_value = ssh_mock
187         mock_get_kernel_module.return_value = "i40e"
188         status = StandaloneContextHelper.get_nic_details(ssh_mock, self.NETWORKS, "dpdk-devbind.py")
189         self.assertIsNotNone(status)
190
191     def test_get_virtual_devices(self):
192         pattern = "PCI_SLOT_NAME=0000:05:00.0"
193         with mock.patch("yardstick.ssh.SSH") as ssh:
194             ssh_mock = mock.Mock(autospec=ssh.SSH)
195             ssh_mock.execute = \
196                     mock.Mock(return_value=(1, pattern, ""))
197             ssh.return_value = ssh_mock
198         status = StandaloneContextHelper.get_virtual_devices(ssh_mock, "0000:00:05.0")
199         self.assertIsNotNone(status)
200
201     def _get_file_abspath(self, filename):
202         curr_path = os.path.dirname(os.path.abspath(__file__))
203         file_path = os.path.join(curr_path, filename)
204         return file_path
205
206     def test_read_config_file(self):
207         self.helper.file_path = self._get_file_abspath(self.NODE_SAMPLE)
208         status = self.helper.read_config_file()
209         self.assertIsNotNone(status)
210
211     def test_parse_pod_file(self):
212         self.helper.file_path = self._get_file_abspath("dummy")
213         self.assertRaises(IOError, self.helper.parse_pod_file, self.helper.file_path)
214
215         self.helper.file_path = self._get_file_abspath(self.NODE_SAMPLE)
216         self.assertRaises(TypeError, self.helper.parse_pod_file, self.helper.file_path)
217
218         self.helper.file_path = self._get_file_abspath(self.NODE_SRIOV_SAMPLE)
219         self.assertIsNotNone(self.helper.parse_pod_file(self.helper.file_path))
220
221     def test_get_mac_address(self):
222         status = StandaloneContextHelper.get_mac_address()
223         self.assertIsNotNone(status)
224
225     @mock.patch('yardstick.ssh.SSH')
226     def test_get_mgmt_ip(self, mock_ssh):
227         with mock.patch("yardstick.ssh.SSH") as ssh:
228             ssh_mock = mock.Mock(autospec=ssh.SSH)
229             ssh_mock.execute = \
230                     mock.Mock(return_value=(1, "1.2.3.4 00:00:00:00:00:01", ""))
231             ssh.return_value = ssh_mock
232         status = StandaloneContextHelper.get_mgmt_ip(ssh_mock, "00:00:00:00:00:01", "1.1.1.1/24", {})
233         self.assertIsNotNone(status)
234
235     @mock.patch('yardstick.ssh.SSH')
236     def test_get_mgmt_ip_no(self, mock_ssh):
237         with mock.patch("yardstick.ssh.SSH") as ssh:
238             ssh_mock = mock.Mock(autospec=ssh.SSH)
239             ssh_mock.execute = \
240                     mock.Mock(return_value=(1, "", ""))
241             ssh.return_value = ssh_mock
242
243         model.WAIT_FOR_BOOT = 0
244         status = StandaloneContextHelper.get_mgmt_ip(ssh_mock, "99", "1.1.1.1/24", {})
245         self.assertIsNone(status)
246
247 class ServerTestCase(unittest.TestCase):
248
249     NETWORKS = {
250         'mgmt': {'cidr': '152.16.100.10/24'},
251         'private_0': {
252          'phy_port': "0000:05:00.0",
253          'vpci': "0000:00:07.0",
254          'driver': 'i40e',
255          'mac': '',
256          'cidr': '152.16.100.10/24',
257          'gateway_ip': '152.16.100.20'},
258         'public_0': {
259          'phy_port': "0000:05:00.1",
260          'vpci': "0000:00:08.0",
261          'driver': 'i40e',
262          'mac': '',
263          'cidr': '152.16.40.10/24',
264          'gateway_ip': '152.16.100.20'}
265     }
266     def setUp(self):
267         self.server = model.Server()
268
269     def test___init__(self):
270         self.assertIsNotNone(self.server)
271
272     def test_build_vnf_interfaces(self):
273         vnf = {
274             "network_ports": {
275                 'mgmt': {'cidr': '152.16.100.10/24'},
276                 'xe0': ['private_0'],
277                 'xe1': ['public_0'],
278             }
279         }
280         status = model.Server.build_vnf_interfaces(vnf, self.NETWORKS)
281         self.assertIsNotNone(status)
282
283     def test_generate_vnf_instance(self):
284         vnf = {
285             "network_ports": {
286                 'mgmt': {'cidr': '152.16.100.10/24'},
287                 'xe0': ['private_0'],
288                 'xe1': ['public_0'],
289             }
290         }
291         status = self.server.generate_vnf_instance({}, self.NETWORKS, "1.1.1.1/24", 'vm_0', vnf, '00:00:00:00:00:01')
292         self.assertIsNotNone(status)
293
294 class OvsDeployTestCase(unittest.TestCase):
295
296     NETWORKS = {
297         'mgmt': {'cidr': '152.16.100.10/24'},
298         'private_0': {
299          'phy_port': "0000:05:00.0",
300          'vpci': "0000:00:07.0",
301          'driver': 'i40e',
302          'mac': '',
303          'cidr': '152.16.100.10/24',
304          'gateway_ip': '152.16.100.20'},
305         'public_0': {
306          'phy_port': "0000:05:00.1",
307          'vpci': "0000:00:08.0",
308          'driver': 'i40e',
309          'mac': '',
310          'cidr': '152.16.40.10/24',
311          'gateway_ip': '152.16.100.20'}
312     }
313     @mock.patch('yardstick.ssh.SSH')
314     def setUp(self, mock_ssh):
315         self.ovs_deploy = model.OvsDeploy(mock_ssh, '/tmp/dpdk-devbind.py', {})
316
317     def test___init__(self):
318         self.assertIsNotNone(self.ovs_deploy.connection)
319
320     @mock.patch('yardstick.benchmark.contexts.standalone.model.os')
321     def test_prerequisite(self, mock_ssh):
322         self.ovs_deploy.helper = mock.Mock()
323         self.assertIsNone(self.ovs_deploy.prerequisite())
324
325     @mock.patch('yardstick.benchmark.contexts.standalone.model.os')
326     def test_prerequisite(self, mock_ssh):
327         self.ovs_deploy.helper = mock.Mock()
328         self.ovs_deploy.connection.execute = \
329                     mock.Mock(return_value=(1, "1.2.3.4 00:00:00:00:00:01", ""))
330         self.ovs_deploy.prerequisite = mock.Mock()
331         self.assertIsNone(self.ovs_deploy.ovs_deploy())