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.
17 # Unittest for yardstick.benchmark.contexts.standalone.model
20 from __future__ import absolute_import
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
33 class ModelLibvirtTestCase(unittest.TestCase):
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)
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)
44 def test_virsh_create_vm(self):
45 with mock.patch("yardstick.ssh.SSH") as ssh:
46 ssh_mock = mock.Mock(autospec=ssh.SSH)
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)
53 def test_virsh_destroy_vm(self):
54 with mock.patch("yardstick.ssh.SSH") as ssh:
55 ssh_mock = mock.Mock(autospec=ssh.SSH)
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)
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)
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)
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)
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)
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)
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):
99 with mock.patch("yardstick.ssh.SSH") as ssh:
100 ssh_mock = mock.Mock(autospec=ssh.SSH)
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])
108 def test_split_cpu_list(self):
109 result = Libvirt.split_cpu_list("1,2,3")
110 self.assertEqual(result, [1, 2, 3])
112 def test_get_numa_nodes(self):
113 result = Libvirt.get_numa_nodes()
114 self.assertIsNotNone(result)
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)
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)
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)
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)
137 class StandaloneContextHelperTestCase(unittest.TestCase):
139 NODE_SAMPLE = "nodes_sample.yaml"
140 NODE_SRIOV_SAMPLE = "nodes_sriov_sample.yaml"
143 'mgmt': {'cidr': '152.16.100.10/24'},
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'},
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'}
157 self.helper = StandaloneContextHelper()
159 def test___init__(self):
160 self.assertIsNone(self.helper.file_path)
162 def test_install_req_libs(self):
163 with mock.patch("yardstick.ssh.SSH") as ssh:
164 ssh_mock = mock.Mock(autospec=ssh.SSH)
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)
171 def test_get_kernel_module(self):
172 with mock.patch("yardstick.ssh.SSH") as ssh:
173 ssh_mock = mock.Mock(autospec=ssh.SSH)
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")
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)
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)
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)
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)
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)
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)
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)
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)
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))
221 def test_get_mac_address(self):
222 status = StandaloneContextHelper.get_mac_address()
223 self.assertIsNotNone(status)
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)
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)
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)
240 mock.Mock(return_value=(1, "", ""))
241 ssh.return_value = ssh_mock
243 model.WAIT_FOR_BOOT = 0
244 status = StandaloneContextHelper.get_mgmt_ip(ssh_mock, "99", "1.1.1.1/24", {})
245 self.assertIsNone(status)
247 class ServerTestCase(unittest.TestCase):
250 'mgmt': {'cidr': '152.16.100.10/24'},
252 'phy_port': "0000:05:00.0",
253 'vpci': "0000:00:07.0",
256 'cidr': '152.16.100.10/24',
257 'gateway_ip': '152.16.100.20'},
259 'phy_port': "0000:05:00.1",
260 'vpci': "0000:00:08.0",
263 'cidr': '152.16.40.10/24',
264 'gateway_ip': '152.16.100.20'}
267 self.server = model.Server()
269 def test___init__(self):
270 self.assertIsNotNone(self.server)
272 def test_build_vnf_interfaces(self):
275 'mgmt': {'cidr': '152.16.100.10/24'},
276 'xe0': ['private_0'],
280 status = model.Server.build_vnf_interfaces(vnf, self.NETWORKS)
281 self.assertIsNotNone(status)
283 def test_generate_vnf_instance(self):
286 'mgmt': {'cidr': '152.16.100.10/24'},
287 'xe0': ['private_0'],
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)
294 class OvsDeployTestCase(unittest.TestCase):
297 'mgmt': {'cidr': '152.16.100.10/24'},
299 'phy_port': "0000:05:00.0",
300 'vpci': "0000:00:07.0",
303 'cidr': '152.16.100.10/24',
304 'gateway_ip': '152.16.100.20'},
306 'phy_port': "0000:05:00.1",
307 'vpci': "0000:00:08.0",
310 'cidr': '152.16.40.10/24',
311 'gateway_ip': '152.16.100.20'}
313 @mock.patch('yardstick.ssh.SSH')
314 def setUp(self, mock_ssh):
315 self.ovs_deploy = model.OvsDeploy(mock_ssh, '/tmp/dpdk-devbind.py', {})
317 def test___init__(self):
318 self.assertIsNotNone(self.ovs_deploy.connection)
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())
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())