2014-01-21 00:09:13 +08:00
|
|
|
# Copyright (C) 2013, 2014 Red Hat, Inc.
|
2013-03-18 05:06:52 +08:00
|
|
|
#
|
2018-04-04 21:35:41 +08:00
|
|
|
# This work is licensed under the GNU GPLv2 or later.
|
2018-03-21 03:00:02 +08:00
|
|
|
# See the COPYING file in the top-level directory.
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
import os
|
2018-03-18 23:32:25 +08:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
import virtinst
|
2020-01-27 06:12:09 +08:00
|
|
|
from virtinst import cli
|
2018-03-21 00:18:35 +08:00
|
|
|
from virtinst import DeviceDisk
|
2013-04-12 04:32:00 +08:00
|
|
|
|
2013-04-11 07:48:07 +08:00
|
|
|
from tests import utils
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-12 04:32:00 +08:00
|
|
|
|
2018-09-04 03:21:11 +08:00
|
|
|
def _make_guest(conn=None, os_variant=None):
|
2018-02-21 02:00:24 +08:00
|
|
|
if not conn:
|
2018-09-04 03:21:11 +08:00
|
|
|
conn = utils.URIs.open_testdriver_cached()
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2018-09-07 08:28:05 +08:00
|
|
|
g = virtinst.Guest(conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
g.name = "TestGuest"
|
2019-05-12 01:34:21 +08:00
|
|
|
g.currentMemory = int(200 * 1024)
|
|
|
|
g.memory = int(400 * 1024)
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2017-06-29 04:12:04 +08:00
|
|
|
if os_variant:
|
2018-09-13 05:23:01 +08:00
|
|
|
g.set_os_name(os_variant)
|
2014-12-10 00:33:43 +08:00
|
|
|
|
|
|
|
# File disk
|
2018-03-21 00:18:35 +08:00
|
|
|
d = virtinst.DeviceDisk(conn)
|
2018-10-13 22:06:25 +08:00
|
|
|
d.path = "/dev/default-pool/new-test-suite.img"
|
2014-12-10 00:33:43 +08:00
|
|
|
if d.wants_storage_creation():
|
|
|
|
parent_pool = d.get_parent_pool()
|
2018-03-21 00:18:35 +08:00
|
|
|
vol_install = virtinst.DeviceDisk.build_vol_install(conn,
|
2018-10-13 22:06:25 +08:00
|
|
|
os.path.basename(d.path), parent_pool, .0000001, True)
|
2014-12-10 00:33:43 +08:00
|
|
|
d.set_vol_install(vol_install)
|
|
|
|
d.validate()
|
|
|
|
g.add_device(d)
|
|
|
|
|
|
|
|
# Block disk
|
2018-03-21 00:18:35 +08:00
|
|
|
d = virtinst.DeviceDisk(conn)
|
2018-10-13 22:06:25 +08:00
|
|
|
d.path = "/dev/disk-pool/diskvol1"
|
2014-12-10 00:33:43 +08:00
|
|
|
d.validate()
|
|
|
|
g.add_device(d)
|
|
|
|
|
|
|
|
# Network device
|
2018-03-21 00:18:35 +08:00
|
|
|
dev = virtinst.DeviceInterface(conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
g.add_device(dev)
|
|
|
|
|
|
|
|
return g
|
|
|
|
|
|
|
|
|
|
|
|
def _make_installer(location=None, conn=None):
|
2018-02-23 03:57:10 +08:00
|
|
|
conn = conn or utils.URIs.open_testdriver_cached()
|
2018-10-13 06:35:09 +08:00
|
|
|
cdrom = not location and "/dev/null" or None
|
|
|
|
inst = virtinst.Installer(conn, location=location, cdrom=cdrom)
|
2014-12-10 00:33:43 +08:00
|
|
|
return inst
|
|
|
|
|
|
|
|
|
|
|
|
class TestXMLMisc(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Misc tests for various XML special behavior. These should only aim for
|
|
|
|
testing any particularly tricky bits, general XML generation should
|
2020-01-27 07:29:39 +08:00
|
|
|
be through virt-install examples in test_cli
|
2014-12-10 00:33:43 +08:00
|
|
|
"""
|
2018-02-23 03:57:10 +08:00
|
|
|
@property
|
|
|
|
def conn(self):
|
|
|
|
return utils.URIs.open_testdefault_cached()
|
|
|
|
|
2014-12-10 00:33:43 +08:00
|
|
|
def _compare(self, guest, filebase, do_install):
|
2020-01-27 07:11:43 +08:00
|
|
|
filename = os.path.join("tests/data/xmlconfig", filebase + ".xml")
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2018-09-04 03:21:11 +08:00
|
|
|
installer = _make_installer(conn=guest.conn)
|
|
|
|
inst_xml, boot_xml = installer.start_install(
|
|
|
|
guest, return_xml=True, dry=True)
|
2014-12-10 00:33:43 +08:00
|
|
|
if do_install:
|
2013-08-15 07:43:02 +08:00
|
|
|
actualXML = inst_xml
|
|
|
|
else:
|
|
|
|
actualXML = boot_xml
|
|
|
|
|
2014-12-10 00:33:43 +08:00
|
|
|
utils.diff_compare(actualXML, filename)
|
|
|
|
utils.test_create(guest.conn, actualXML)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-10-03 19:20:54 +08:00
|
|
|
def testDiskNumbers(self):
|
2014-12-10 00:33:43 +08:00
|
|
|
# Various testing our target generation
|
2020-06-03 21:14:34 +08:00
|
|
|
#
|
|
|
|
# Note: using single quotes in strings to avoid
|
|
|
|
# codespell flagging the 'ba' assert
|
|
|
|
self.assertEqual('a', DeviceDisk.num_to_target(1))
|
|
|
|
self.assertEqual('b', DeviceDisk.num_to_target(2))
|
|
|
|
self.assertEqual('z', DeviceDisk.num_to_target(26))
|
|
|
|
self.assertEqual('aa', DeviceDisk.num_to_target(27))
|
|
|
|
self.assertEqual('ab', DeviceDisk.num_to_target(28))
|
|
|
|
self.assertEqual('az', DeviceDisk.num_to_target(52))
|
|
|
|
self.assertEqual('ba', DeviceDisk.num_to_target(53))
|
|
|
|
self.assertEqual('zz', DeviceDisk.num_to_target(27 * 26))
|
|
|
|
self.assertEqual('aaa', DeviceDisk.num_to_target(27 * 26 + 1))
|
|
|
|
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('hda'), 0)
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('hdb'), 1)
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('sdz'), 25)
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('sdaa'), 26)
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('vdab'), 27)
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('vdaz'), 51)
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('xvdba'), 52)
|
|
|
|
self.assertEqual(DeviceDisk.target_to_num('xvdzz'),
|
2014-12-10 00:33:43 +08:00
|
|
|
26 * (25 + 1) + 25)
|
2020-06-03 21:14:34 +08:00
|
|
|
self.assertEqual(DeviceDisk.target_to_num('xvdaaa'),
|
2014-12-10 00:33:43 +08:00
|
|
|
26 * 26 * 1 + 26 * 1 + 0)
|
2014-02-12 22:44:40 +08:00
|
|
|
|
2018-03-21 00:18:35 +08:00
|
|
|
disk = virtinst.DeviceDisk(self.conn)
|
2020-06-03 21:14:34 +08:00
|
|
|
disk.bus = 'ide'
|
2013-10-03 20:40:24 +08:00
|
|
|
|
2020-06-03 21:14:34 +08:00
|
|
|
self.assertEqual('hda', disk.generate_target([]))
|
|
|
|
self.assertEqual('hdb', disk.generate_target(['hda']))
|
|
|
|
self.assertEqual('hdc', disk.generate_target(['hdb', 'sda']))
|
|
|
|
self.assertEqual('hdb', disk.generate_target(['hda', 'hdd']))
|
2013-10-03 20:40:24 +08:00
|
|
|
|
2014-12-10 00:33:43 +08:00
|
|
|
def testQuickTreeinfo(self):
|
|
|
|
# Simple sanity test to make sure detect_distro works. test-urls
|
|
|
|
# does much more exhaustive testing but it's only run occasionally
|
|
|
|
i = _make_installer(
|
2020-01-27 07:29:39 +08:00
|
|
|
location="tests/data/cli/fakefedoratree")
|
2018-09-04 03:21:11 +08:00
|
|
|
g = _make_guest()
|
2013-08-11 06:48:43 +08:00
|
|
|
v = i.detect_distro(g)
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual(v, "fedora17")
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-12-10 00:33:43 +08:00
|
|
|
i = _make_installer(
|
2020-01-27 07:29:39 +08:00
|
|
|
location="tests/data/cli/fakerhel6tree")
|
2018-09-04 03:21:11 +08:00
|
|
|
g = _make_guest()
|
2014-12-10 00:33:43 +08:00
|
|
|
v = i.detect_distro(g)
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual(v, "rhel6.0")
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2018-10-13 22:11:26 +08:00
|
|
|
def testCDROMInsert(self):
|
|
|
|
# After set_install_defaults, cdrom media should be inserted
|
|
|
|
i = _make_installer()
|
|
|
|
g = _make_guest()
|
|
|
|
i.set_install_defaults(g)
|
|
|
|
for disk in g.devices.disk:
|
|
|
|
if disk.device == "cdrom" and disk.path == "/dev/null":
|
|
|
|
return
|
|
|
|
raise AssertionError("Didn't find inserted cdrom media")
|
|
|
|
|
2014-12-10 00:33:43 +08:00
|
|
|
def testCPUTopology(self):
|
|
|
|
# Test CPU topology determining
|
2018-03-21 03:10:04 +08:00
|
|
|
cpu = virtinst.DomainCpu(self.conn)
|
2020-01-27 21:59:02 +08:00
|
|
|
cpu.set_topology_defaults(6)
|
|
|
|
assert cpu.sockets is None
|
|
|
|
|
2014-12-10 00:33:43 +08:00
|
|
|
cpu.sockets = "2"
|
|
|
|
cpu.set_topology_defaults(6)
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual([cpu.sockets, cpu.cores, cpu.threads], [2, 3, 1])
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2018-03-21 03:10:04 +08:00
|
|
|
cpu = virtinst.DomainCpu(self.conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
cpu.cores = "4"
|
|
|
|
cpu.set_topology_defaults(9)
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual([cpu.sockets, cpu.cores, cpu.threads], [2, 4, 1])
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2018-03-21 03:10:04 +08:00
|
|
|
cpu = virtinst.DomainCpu(self.conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
cpu.threads = "3"
|
|
|
|
cpu.set_topology_defaults(14)
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual([cpu.sockets, cpu.cores, cpu.threads], [4, 1, 3])
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2018-03-21 03:10:04 +08:00
|
|
|
cpu = virtinst.DomainCpu(self.conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
cpu.sockets = 5
|
|
|
|
cpu.cores = 2
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual(cpu.vcpus_from_topology(), 10)
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2018-03-21 03:10:04 +08:00
|
|
|
cpu = virtinst.DomainCpu(self.conn)
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual(cpu.vcpus_from_topology(), 1)
|
2014-12-10 00:33:43 +08:00
|
|
|
|
2018-09-13 00:36:58 +08:00
|
|
|
def test_set_defaults_double(self):
|
|
|
|
"""
|
|
|
|
Check that a common config has idempotent set_defaults
|
|
|
|
"""
|
2018-09-13 05:23:01 +08:00
|
|
|
g = _make_guest(conn=utils.URIs.open_kvm(), os_variant="fedora-unknown")
|
2018-09-13 00:36:58 +08:00
|
|
|
|
|
|
|
g.set_defaults(None)
|
|
|
|
xml1 = g.get_xml()
|
|
|
|
g.set_defaults(None)
|
|
|
|
xml2 = g.get_xml()
|
|
|
|
self.assertEqual(xml1, xml2)
|
|
|
|
|
2018-09-14 03:03:36 +08:00
|
|
|
def test_guest_osinfo_metadata(self):
|
|
|
|
g = _make_guest()
|
|
|
|
self.assertEqual(g.osinfo.name, "generic")
|
|
|
|
g.set_os_name("fedora17")
|
|
|
|
self.assertEqual(g.osinfo.name, "fedora17")
|
|
|
|
|
|
|
|
g = _make_guest()
|
|
|
|
g._metadata.libosinfo.os_id = "http://fedoraproject.org/fedora/20" # pylint: disable=protected-access
|
|
|
|
self.assertEqual(g.osinfo.name, "fedora20")
|
|
|
|
|
|
|
|
g = _make_guest()
|
|
|
|
g._metadata.libosinfo.os_id = "http://example.com/idontexit" # pylint: disable=protected-access
|
|
|
|
self.assertEqual(g.osinfo.name, "generic")
|
|
|
|
|
2019-06-08 21:11:00 +08:00
|
|
|
@utils.run_without_testsuite_hacks
|
2018-03-18 23:32:25 +08:00
|
|
|
def test_dir_searchable(self):
|
|
|
|
# Normally the dir searchable test is skipped in the unittest,
|
|
|
|
# but let's contrive an example that should trigger all the code
|
2018-10-12 06:52:45 +08:00
|
|
|
# to ensure it isn't horribly broken
|
2020-01-29 08:03:44 +08:00
|
|
|
conn = utils.URIs.open_kvm()
|
|
|
|
|
2020-01-31 05:17:42 +08:00
|
|
|
def _set_caps_baselabel_uid(uid):
|
|
|
|
secmodel = [s for s in conn.caps.host.secmodels
|
|
|
|
if s.model == "dac"][0]
|
|
|
|
for baselabel in [b for b in secmodel.baselabels
|
|
|
|
if b.type in ["qemu", "kvm"]]:
|
|
|
|
baselabel.content = "+%s:+%s" % (uid, uid)
|
|
|
|
|
|
|
|
tmpobj = tempfile.TemporaryDirectory(prefix="virtinst-test-search")
|
|
|
|
tmpdir = tmpobj.name
|
|
|
|
try:
|
|
|
|
# Invalid uid
|
|
|
|
_set_caps_baselabel_uid(-1)
|
2020-01-29 08:03:44 +08:00
|
|
|
searchdata = virtinst.DeviceDisk.check_path_search(conn, tmpdir)
|
2020-01-31 05:17:42 +08:00
|
|
|
self.assertEqual(searchdata.uid, None)
|
|
|
|
|
|
|
|
# Use our uid, verify it shows we have expected access
|
|
|
|
_set_caps_baselabel_uid(os.getuid())
|
2020-02-06 03:37:15 +08:00
|
|
|
searchdata = virtinst.DeviceDisk.check_path_search(conn,
|
|
|
|
tmpdir + "/footest")
|
2020-01-31 05:17:42 +08:00
|
|
|
self.assertEqual(searchdata.uid, os.getuid())
|
|
|
|
self.assertEqual(searchdata.fixlist, [])
|
|
|
|
|
|
|
|
# Remove perms on the tmpdir, now it should report failures
|
|
|
|
os.chmod(tmpdir, 0o000)
|
|
|
|
searchdata = virtinst.DeviceDisk.check_path_search(conn, tmpdir)
|
|
|
|
self.assertEqual(searchdata.fixlist, [tmpdir])
|
|
|
|
|
2020-01-29 08:03:44 +08:00
|
|
|
errdict = virtinst.DeviceDisk.fix_path_search(searchdata)
|
2019-06-08 21:11:00 +08:00
|
|
|
self.assertTrue(not bool(errdict))
|
|
|
|
|
2020-01-31 05:17:42 +08:00
|
|
|
# Mock setfacl to definitely fail
|
2020-01-29 20:30:51 +08:00
|
|
|
with unittest.mock.patch("virtinst.diskbackend.SETFACL",
|
|
|
|
"getfacl"):
|
|
|
|
errdict = virtinst.DeviceDisk.fix_path_search(searchdata)
|
|
|
|
|
2020-01-31 05:17:42 +08:00
|
|
|
finally:
|
|
|
|
# Reset changes we made
|
|
|
|
conn.invalidate_caps()
|
|
|
|
os.chmod(tmpdir, 0o777)
|
2020-01-29 20:30:51 +08:00
|
|
|
|
2020-01-29 08:03:44 +08:00
|
|
|
def test_path_in_use(self):
|
|
|
|
# Extra tests for DeviceDisk.path_in_use
|
|
|
|
conn = utils.URIs.open_kvm()
|
|
|
|
|
|
|
|
# Comparing against kernel
|
|
|
|
vms = virtinst.DeviceDisk.path_in_use_by(
|
|
|
|
conn, "/dev/default-pool/test-arm-kernel")
|
|
|
|
assert vms == ["test-arm-kernel"]
|
2019-06-07 06:35:36 +08:00
|
|
|
|
|
|
|
def test_nonpredicatble_generate(self):
|
2019-06-11 04:08:45 +08:00
|
|
|
kvm_uri = utils.URIs.kvm.replace(",predictable", "")
|
2020-01-27 06:12:09 +08:00
|
|
|
kvmconn = cli.getConnection(kvm_uri)
|
|
|
|
testconn = cli.getConnection("test:///default")
|
2019-06-07 06:35:36 +08:00
|
|
|
|
|
|
|
testuuid = virtinst.Guest.generate_uuid(self.conn)
|
2019-06-11 04:08:45 +08:00
|
|
|
randomuuid = virtinst.Guest.generate_uuid(testconn)
|
2019-06-07 06:35:36 +08:00
|
|
|
self.assertTrue(randomuuid != testuuid)
|
|
|
|
self.assertTrue(len(randomuuid) == len(testuuid))
|
|
|
|
|
|
|
|
testmac = virtinst.DeviceInterface.generate_mac(self.conn)
|
2019-06-11 04:08:45 +08:00
|
|
|
randommac = virtinst.DeviceInterface.generate_mac(testconn)
|
|
|
|
qemumac = virtinst.DeviceInterface.generate_mac(kvmconn)
|
2019-06-07 06:35:36 +08:00
|
|
|
self.assertTrue(randommac != testmac)
|
2019-06-11 04:08:45 +08:00
|
|
|
self.assertTrue(qemumac != testmac)
|
2019-06-07 06:35:36 +08:00
|
|
|
self.assertTrue(len(randommac) == len(testmac))
|
2019-06-08 04:39:33 +08:00
|
|
|
|
2020-07-06 06:39:15 +08:00
|
|
|
# Ensure is_conflict_net doesn't error on None
|
|
|
|
virtinst.DeviceInterface.is_conflict_net(self.conn, None)
|
|
|
|
|
2019-06-08 04:39:33 +08:00
|
|
|
def test_support_misc(self):
|
|
|
|
try:
|
|
|
|
self.conn.lookupByName("foobar-idontexist")
|
|
|
|
except Exception as e:
|
|
|
|
if not self.conn.support.is_libvirt_error_no_domain(e):
|
|
|
|
raise
|
2020-01-29 20:30:51 +08:00
|
|
|
|
|
|
|
def test_disk_backend(self):
|
|
|
|
# Test get_size() with vol_install
|
|
|
|
disk = virtinst.DeviceDisk(self.conn)
|
|
|
|
pool = self.conn.storagePoolLookupByName("default-pool")
|
|
|
|
vol_install = disk.build_vol_install(self.conn, "newvol1.img",
|
|
|
|
pool, 1, False)
|
|
|
|
disk.set_vol_install(vol_install)
|
|
|
|
assert disk.get_size() == 1.0
|
|
|
|
|
|
|
|
# Test some blockdev inspecting
|
2020-01-29 21:57:10 +08:00
|
|
|
conn = utils.URIs.openconn("test:///default")
|
2020-01-29 20:30:51 +08:00
|
|
|
if os.path.exists("/dev/loop0"):
|
|
|
|
disk = virtinst.DeviceDisk(conn)
|
|
|
|
disk.path = "/dev/loop0"
|
|
|
|
assert disk.type == "block"
|
|
|
|
disk.get_size()
|
2020-01-29 21:57:10 +08:00
|
|
|
|
|
|
|
# Test sparse cloning
|
|
|
|
tmpinput = tempfile.NamedTemporaryFile()
|
|
|
|
open(tmpinput.name, "wb").write(b'\0' * 10000)
|
|
|
|
|
|
|
|
srcdisk = virtinst.DeviceDisk(conn)
|
|
|
|
srcdisk.path = tmpinput.name
|
|
|
|
|
|
|
|
newdisk = virtinst.DeviceDisk(conn)
|
|
|
|
tmpoutput = tempfile.NamedTemporaryFile()
|
|
|
|
os.unlink(tmpoutput.name)
|
|
|
|
newdisk.path = tmpoutput.name
|
|
|
|
newdisk.set_local_disk_to_clone(srcdisk, True)
|
|
|
|
newdisk.build_storage(None)
|
|
|
|
|
|
|
|
# Test cloning onto existing disk
|
|
|
|
newdisk.path = newdisk.path
|
|
|
|
newdisk.set_local_disk_to_clone(srcdisk, True)
|
|
|
|
newdisk.build_storage(None)
|