virtinst: uri: Rework MagicURI to work with passed in fakeuri

Rather than individual options for each possible hypervisor,
and annotations like 'remote' or 'session', just have it take a
fake URI to mock

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-02-02 18:16:48 -05:00
parent 1b6e33e8b9
commit 9a0d49a718
5 changed files with 36 additions and 44 deletions

View File

@ -40,6 +40,10 @@ class TestConn(unittest.TestCase):
assert conn.get_uri_transport() == "tls"
# Hit fakuuri validation error, for old style opts
with self.assertRaises(RuntimeError):
cli.getConnection(fakeuri + ",qemu")
@unittest.mock.patch.dict(os.environ,
{"LIBVIRT_DEFAULT_URI": "test:///default"})
def test_default_uri(self):

View File

@ -60,34 +60,39 @@ class _URIs(object):
_testtmpl = "__virtinst_test__test://%s,predictable"
self.test_default = _testtmpl % "/default"
self.test_full = _testtmpl % (os.getcwd() + "/tests/testdriver.xml")
self.test_suite = _testtmpl % (os.getcwd() + "/tests/testsuite.xml")
self.test_remote = self.test_full + ",remote"
self.test_defaultpool_collision = (_testtmpl % (os.getcwd() +
"/tests/data/cli/testdriver-defaultpool-collision.xml"))
self.xen = self.test_full + _caps("xen-rhel5.4.xml") + ",xen"
self.lxc = self.test_full + _caps("lxc.xml") + ",lxc"
self.vz = self.test_full + _caps("vz.xml") + ",vz"
self.test_full = _testtmpl % (os.getcwd() + "/tests/testdriver.xml")
def _m(fakeuri):
return self.test_full + ",fakeuri=%s" % fakeuri
self.test_remote = _m("test+tls://fakeuri.example.com/")
_uri_qemu = "%s,qemu" % self.test_full
_uri_kvm = _uri_qemu + _domcaps("kvm-x86_64-domcaps.xml")
self.xen = _m("xen:///") + _caps("xen-rhel5.4.xml")
self.lxc = _m("lxc:///") + _caps("lxc.xml")
self.vz = _m("vz:///") + _caps("vz.xml")
_uri_qemu = _m("qemu:///system")
_kvm_x86_caps = _caps("kvm-x86_64.xml") + _domcaps("kvm-x86_64-domcaps.xml")
self.kvm = _uri_qemu + _kvm_x86_caps
self.kvm_remote = _m("qemu+tls://fakeuri.example.com/system") + _kvm_x86_caps
self.kvm_session = _m("qemu:///session") + _kvm_x86_caps
_uri_kvm = _uri_qemu + _kvm_x86_caps
_uri_kvm_rhel7 = _uri_qemu + _domcaps("kvm-x86_64-rhel7-domcaps.xml")
_uri_kvm_q35 = _uri_qemu + _domcaps("kvm-x86_64-domcaps-q35.xml")
_uri_kvm_amd_sev = _uri_qemu + _domcaps("kvm-x86_64-domcaps-amd-sev.xml")
_uri_kvm_aarch64 = _uri_qemu + _domcaps("kvm-aarch64-domcaps.xml")
_uri_qemu_riscv64 = _uri_qemu + _domcaps("qemu-riscv64-domcaps.xml")
self.kvm = _uri_kvm + _caps("kvm-x86_64.xml")
self.kvm_remote = _uri_kvm + _caps("kvm-x86_64.xml") + ",remote"
self.kvm_nodomcaps = _uri_qemu + _caps("kvm-x86_64.xml")
self.kvm_rhel = _uri_kvm_rhel7 + _caps("kvm-x86_64-rhel7.xml")
self.kvm_q35 = _uri_kvm_q35 + _caps("kvm-x86_64.xml")
self.kvm_amd_sev = _uri_kvm_amd_sev + _caps("kvm-x86_64.xml")
self.kvm_session = self.kvm + ",session"
self.kvm_armv7l = _uri_kvm + _caps("kvm-armv7l.xml")
self.kvm_armv7l_nodomcaps = _uri_qemu + _caps("kvm-armv7l.xml")
self.kvm_armv7l = self.kvm_armv7l_nodomcaps + _domcaps("kvm-x86_64-domcaps.xml")
self.kvm_aarch64 = _uri_kvm_aarch64 + _caps("kvm-aarch64.xml")
self.kvm_ppc64le = _uri_kvm + _caps("kvm-ppc64le.xml")
self.kvm_s390x = _uri_kvm + _caps("kvm-s390x.xml")

View File

@ -58,11 +58,9 @@ class VirtinstConnection(object):
if MagicURI.uri_is_magic(_initial_uri):
self._magic_uri = MagicURI(_initial_uri)
self._open_uri = self._magic_uri.open_uri
self._uri = self._magic_uri.make_fake_uri()
self._uri = self._magic_uri.fakeuri or self._open_uri
self._fake_conn_predictable = self._magic_uri.predictable
self._fake_conn_remote = self._magic_uri.remote
self._fake_conn_session = self._magic_uri.session
self._fake_conn_version = self._magic_uri.conn_version
self._fake_libvirt_version = self._magic_uri.libvirt_version
else:
@ -71,8 +69,6 @@ class VirtinstConnection(object):
self._uri = _initial_uri
self._fake_conn_predictable = False
self._fake_conn_remote = False
self._fake_conn_session = False
self._fake_libvirt_version = None
self._fake_conn_version = None
@ -143,6 +139,9 @@ class VirtinstConnection(object):
return bool(self._libvirtconn)
def open(self, authcb, cbdata):
if self._magic_uri:
self._magic_uri.validate()
# Mirror the set of libvirt.c virConnectCredTypeDefault
valid_auth_options = [
libvirt.VIR_CRED_AUTHNAME,
@ -339,9 +338,9 @@ class VirtinstConnection(object):
###################
def is_remote(self):
return (self._fake_conn_remote or self._uriobj.hostname)
return self._uriobj.hostname
def is_session_uri(self):
return (self._fake_conn_session or self.get_uri_path() == "/session")
return self.get_uri_path() == "/session"
def get_uri_hostname(self):
return self._uriobj.hostname

View File

@ -105,8 +105,7 @@ class MagicURI(object):
* 'predictable': Generate predictable UUIDs, MAC addresses, and
temporary file names.
* 'remote': Have the code consider this as a remote URI
* 'session': Have the code consider this as a session URI
* 'fakeuri': The URI to advertise as the actual connection URI
* 'connver=%d': Override the connection (hv) version
* 'libver=%d': Override the libvirt version
* 'caps=%s': Points to a file with capabilities XML, that will
@ -114,7 +113,6 @@ class MagicURI(object):
files in test/capabilities-xml/
* 'domcaps=%s': Points to a file with domain capabilities XML, that
will be returned in conn.getDomainCapabilities
* qemu, xen, lxc or vz: Fake the specified hypervisor
See tests/utils.py for example URLs
"""
@ -140,46 +138,32 @@ class MagicURI(object):
return ret
self.predictable = pop_bool("predictable")
self.remote = pop_bool("remote")
self.session = pop_bool("session")
self.fakeuri = opts.pop("fakeuri", None)
self.capsfile = opts.pop("caps", None)
self.domcapsfile = opts.pop("domcaps", None)
self.hv = None
if pop_bool("qemu"):
self.hv = "qemu"
if pop_bool("lxc"):
self.hv = "lxc"
if pop_bool("xen"):
self.hv = "xen"
if pop_bool("vz"):
self.hv = "vz"
self.conn_version = opts.pop("connver", None)
if self.conn_version:
self.conn_version = int(self.conn_version)
elif self.hv:
elif self.fakeuri:
self.conn_version = 10000000000
self.libvirt_version = opts.pop("libver", None)
if self.libvirt_version:
self.libvirt_version = int(self.libvirt_version)
assert not opts
self._err = None
if opts:
self._err = "MagicURI has unhandled opts=%s" % opts
##############
# Public API #
##############
def make_fake_uri(self):
"""
If self.hv is set, we need to make a fake URI so that Connection
URI handling bits have something to work with.
"""
if self.hv:
return self.hv + "+abc:///system"
return self.open_uri
def validate(self):
if self._err:
raise RuntimeError(self._err)
def overwrite_conn_functions(self, conn):
"""
@ -209,7 +193,7 @@ class MagicURI(object):
conn.getDomainCapabilities = fake_domcaps
if self.hv:
if self.fakeuri:
origcreate = conn.createXML
origdefine = conn.defineXML
def newcreate(xml, flags):