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
|
2018-03-21 00:18:35 +08:00
|
|
|
from virtinst import DeviceDisk
|
2015-04-07 03:42:40 +08:00
|
|
|
from virtcli import CLIConfig
|
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.type = "kvm"
|
|
|
|
g.name = "TestGuest"
|
|
|
|
g.memory = int(200 * 1024)
|
|
|
|
g.maxmemory = int(400 * 1024)
|
|
|
|
g.uuid = "12345678-1234-1234-1234-123456789012"
|
2018-03-21 00:18:35 +08:00
|
|
|
gdev = virtinst.DeviceGraphics(conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
gdev.type = "vnc"
|
|
|
|
gdev.keymap = "ja"
|
|
|
|
g.add_device(gdev)
|
|
|
|
g.features.pae = False
|
|
|
|
g.vcpus = 5
|
|
|
|
|
|
|
|
g.emulator = "/usr/lib/xen/bin/qemu-dm"
|
|
|
|
g.os.arch = "i686"
|
|
|
|
g.os.os_type = "hvm"
|
|
|
|
|
2017-06-29 04:12:04 +08:00
|
|
|
if os_variant:
|
|
|
|
g.os_variant = os_variant
|
2014-12-10 00:33:43 +08:00
|
|
|
|
|
|
|
# Floppy disk
|
|
|
|
path = "/dev/default-pool/testvol1.img"
|
2018-03-21 00:18:35 +08:00
|
|
|
d = DeviceDisk(conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
d.path = path
|
|
|
|
d.device = d.DEVICE_FLOPPY
|
|
|
|
d.validate()
|
|
|
|
g.add_device(d)
|
|
|
|
|
|
|
|
# File disk
|
|
|
|
path = "/dev/default-pool/new-test-suite.img"
|
2018-03-21 00:18:35 +08:00
|
|
|
d = virtinst.DeviceDisk(conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
d.path = path
|
|
|
|
|
|
|
|
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,
|
2014-12-10 00:33:43 +08:00
|
|
|
os.path.basename(path), parent_pool, .0000001, True)
|
|
|
|
d.set_vol_install(vol_install)
|
|
|
|
|
|
|
|
d.validate()
|
|
|
|
g.add_device(d)
|
|
|
|
|
|
|
|
# Block disk
|
|
|
|
path = "/dev/disk-pool/diskvol1"
|
2018-03-21 00:18:35 +08:00
|
|
|
d = virtinst.DeviceDisk(conn)
|
2014-12-10 00:33:43 +08:00
|
|
|
d.path = path
|
|
|
|
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
|
|
|
dev.macaddr = "22:22:33:44:55:66"
|
2018-03-21 00:18:35 +08:00
|
|
|
dev.type = virtinst.DeviceInterface.TYPE_VIRTUAL
|
2014-12-10 00:33:43 +08:00
|
|
|
dev.source = "default"
|
|
|
|
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()
|
2014-12-10 00:33:43 +08:00
|
|
|
inst = virtinst.DistroInstaller(conn)
|
|
|
|
if location:
|
|
|
|
inst.location = location
|
|
|
|
else:
|
|
|
|
inst.location = "/dev/null"
|
|
|
|
inst.cdrom = True
|
|
|
|
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
|
|
|
|
be through virt-install examples in clitest
|
|
|
|
"""
|
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):
|
|
|
|
filename = os.path.join("tests/xmlconfig-xml", 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
|
2018-03-21 00:18:35 +08:00
|
|
|
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)
|
2018-03-21 00:18:35 +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)
|
2013-10-03 20:40:24 +08:00
|
|
|
disk.bus = "ide"
|
|
|
|
|
2017-05-05 23:39:59 +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-02-17 23:41:02 +08:00
|
|
|
disk.bus = "virtio-scsi"
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual("sdb",
|
2014-12-10 00:33:43 +08:00
|
|
|
disk.generate_target(["sda", "sdg", "sdi"], 0))
|
2017-05-05 23:39:59 +08:00
|
|
|
self.assertEqual("sdh", disk.generate_target(["sda", "sdg"], 1))
|
2014-02-12 22:44:40 +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(
|
|
|
|
location="tests/cli-test-xml/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(
|
|
|
|
location="tests/cli-test-xml/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
|
|
|
|
|
|
|
def testCPUTopology(self):
|
|
|
|
# Test CPU topology determining
|
2018-03-21 03:10:04 +08:00
|
|
|
cpu = virtinst.DomainCpu(self.conn)
|
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
|
|
|
|
2015-07-21 01:06:34 +08:00
|
|
|
def test_hyperv_clock(self):
|
|
|
|
def _make(connver):
|
2018-02-23 03:57:10 +08:00
|
|
|
conn = utils.URIs.open_kvm(libver=1002002, connver=connver)
|
2017-06-29 04:12:04 +08:00
|
|
|
g = _make_guest(conn=conn, os_variant="win7")
|
2015-07-21 01:06:34 +08:00
|
|
|
g.emulator = "/usr/libexec/qemu-kvm"
|
|
|
|
return g
|
|
|
|
|
|
|
|
try:
|
|
|
|
g = _make(2000000)
|
|
|
|
self._compare(g, "install-hyperv-clock", True)
|
|
|
|
|
|
|
|
g = _make(1009000)
|
|
|
|
self._compare(g, "install-hyperv-noclock", True)
|
|
|
|
|
|
|
|
CLIConfig.stable_defaults = True
|
|
|
|
|
|
|
|
g = _make(1005003)
|
|
|
|
self._compare(g, "install-hyperv-clock", True)
|
|
|
|
|
|
|
|
g = _make(1005002)
|
|
|
|
self._compare(g, "install-hyperv-noclock", True)
|
|
|
|
finally:
|
|
|
|
CLIConfig.stable_defaults = False
|
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-03-21 00:27:37 +08:00
|
|
|
from virtinst.devices.disk import _is_dir_searchable
|
2018-03-18 23:32:25 +08:00
|
|
|
oldtest = os.environ.pop("VIRTINST_TEST_SUITE")
|
|
|
|
try:
|
|
|
|
uid = -1
|
|
|
|
username = "fakeuser-zzzz"
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
self.assertFalse(_is_dir_searchable(uid, username, tmpdir))
|
|
|
|
finally:
|
|
|
|
os.environ["VIRTINST_TEST_SUITE"] = oldtest
|