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.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):
101 with mock.patch("yardstick.ssh.SSH") as ssh:
102 ssh_mock = mock.Mock(autospec=ssh.SSH)
104 mock.Mock(return_value=(0, "a", ""))
105 ssh.return_value = ssh_mock
106 mock_create_snapshot_qemu.return_value = "0.img"
108 status = Libvirt.build_vm_xml(ssh_mock, {}, "test", "vm_0", 0)
109 self.assertEqual(status[0], result[0])
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)
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)
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)
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)
131 class StandaloneContextHelperTestCase(unittest.TestCase):
133 NODE_SAMPLE = "nodes_sample.yaml"
134 NODE_SRIOV_SAMPLE = "nodes_sriov_sample.yaml"
137 'mgmt': {'cidr': '152.16.100.10/24'},
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'},
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'}
151 self.helper = StandaloneContextHelper()
153 def test___init__(self):
154 self.assertIsNone(self.helper.file_path)
156 def test_install_req_libs(self):
157 with mock.patch("yardstick.ssh.SSH") as ssh:
158 ssh_mock = mock.Mock(autospec=ssh.SSH)
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)
165 def test_get_kernel_module(self):
166 with mock.patch("yardstick.ssh.SSH") as ssh:
167 ssh_mock = mock.Mock(autospec=ssh.SSH)
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")
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)
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)
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)
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)
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)
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)
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)
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)
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))
215 def test_get_mac_address(self):
216 status = StandaloneContextHelper.get_mac_address()
217 self.assertIsNotNone(status)
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)
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)
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)
234 mock.Mock(return_value=(1, "", ""))
235 ssh.return_value = ssh_mock
237 model.WAIT_FOR_BOOT = 0
238 status = StandaloneContextHelper.get_mgmt_ip(ssh_mock, "99", "1.1.1.1/24", {})
239 self.assertIsNone(status)
241 class ServerTestCase(unittest.TestCase):
244 'mgmt': {'cidr': '152.16.100.10/24'},
246 'phy_port': "0000:05:00.0",
247 'vpci': "0000:00:07.0",
250 'cidr': '152.16.100.10/24',
251 'gateway_ip': '152.16.100.20'},
253 'phy_port': "0000:05:00.1",
254 'vpci': "0000:00:08.0",
257 'cidr': '152.16.40.10/24',
258 'gateway_ip': '152.16.100.20'}
261 self.server = model.Server()
263 def test___init__(self):
264 self.assertIsNotNone(self.server)
266 def test_build_vnf_interfaces(self):
269 'mgmt': {'cidr': '152.16.100.10/24'},
270 'xe0': ['private_0'],
274 status = model.Server.build_vnf_interfaces(vnf, self.NETWORKS)
275 self.assertIsNotNone(status)
277 def test_generate_vnf_instance(self):
280 'mgmt': {'cidr': '152.16.100.10/24'},
281 'xe0': ['private_0'],
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)
288 class OvsDeployTestCase(unittest.TestCase):
291 'mgmt': {'cidr': '152.16.100.10/24'},
293 'phy_port': "0000:05:00.0",
294 'vpci': "0000:00:07.0",
297 'cidr': '152.16.100.10/24',
298 'gateway_ip': '152.16.100.20'},
300 'phy_port': "0000:05:00.1",
301 'vpci': "0000:00:08.0",
304 'cidr': '152.16.40.10/24',
305 'gateway_ip': '152.16.100.20'}
307 @mock.patch('yardstick.ssh.SSH')
308 def setUp(self, mock_ssh):
309 self.ovs_deploy = model.OvsDeploy(mock_ssh, '/tmp/dpdk-devbind.py', {})
311 def test___init__(self):
312 self.assertIsNotNone(self.ovs_deploy.connection)
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())
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())