cli: Add more code coverage

This commit is contained in:
Cole Robinson 2019-06-10 18:13:31 -04:00
parent 75101ce27b
commit cc366fff7d
8 changed files with 43 additions and 41 deletions

View File

@ -1,6 +1,6 @@
<domain type="kvm">
<name>foobar</name>
<uuid>00000000-1111-2222-3333-444444444444</uuid>
<uuid>12345678-12F4-1234-1234-123456789AFA</uuid>
<genid/>
<description>foobar &amp; baz</description>
<metadata>

View File

@ -450,6 +450,7 @@ c = vinst.add_category("xml-comparsion", "--connect %(URI-KVM)s --noautoconsole
# Singleton element test #1, for simpler strings
c.add_compare("""
--memory 1024
--uuid 12345678-12F4-1234-1234-123456789AFA
--vcpus 4,cores=2,threads=2,sockets=2 --cpuset=1,3-5
--cpu host-copy
--description \"foobar & baz\"
@ -1175,18 +1176,20 @@ c = vclon.add_category("remote", "--connect %(URI-TEST-REMOTE)s")
c.add_valid("-o test --auto-clone") # Auto flag, no storage
c.add_valid("--original-xml " + _CLONE_MANAGED + " --auto-clone") # Auto flag w/ managed storage
c.add_invalid("--original-xml " + _CLONE_UNMANAGED + " --auto-clone") # Auto flag w/ local storage, which is invalid for remote connection
c.add_invalid("--original-xml " + _CLONE_UNMANAGED + " --auto-clone") # Auto flag w/ local storage, which is invalid for remote connection
c = vclon.add_category("misc", "")
c.add_compare("--connect %(URI-KVM)s -o test-clone --auto-clone --clone-running", "clone-auto1")
c.add_compare("--connect %(URI-TEST-FULL)s -o test-clone-simple --name newvm --auto-clone --clone-running", "clone-auto2")
c.add_valid("-o test --auto-clone") # Auto flag, no storage
c.add_valid("-o test --auto-clone --uuid 12345678-12F4-1234-1234-123456789AFA --reflink --mac 12:34:56:1A:B2:C3") # Auto flag, no storage
c.add_valid("--original-xml " + _CLONE_MANAGED + " --auto-clone") # Auto flag w/ managed storage
c.add_valid("--original-xml " + _CLONE_UNMANAGED + " --auto-clone") # Auto flag w/ local storage
c.add_valid("--connect %(URI-TEST-FULL)s -o test-clone --auto-clone --clone-running") # Auto flag, actual VM, skip state check
c.add_valid("--connect %(URI-TEST-FULL)s -o test-clone-simple -n newvm --preserve-data --file %(EXISTIMG1)s") # Preserve data shouldn't complain about existing volume
c.add_valid("-n clonetest --original-xml " + _CLONE_UNMANAGED + " --file %(EXISTIMG3)s --file %(EXISTIMG4)s --check path_exists=off") # Skip existing file check
c.add_invalid("--auto-clone") # Just the auto flag
c.add_invalid("-o test --file foo") # Didn't specify new name
c.add_invalid("--connect %(URI-TEST-FULL)s -o test-many-devices --auto-clone") # VM is running, but --clone-running isn't passed
c.add_invalid("--connect %(URI-TEST-FULL)s -o test-clone-simple -n newvm --file %(EXISTIMG1)s --clone-running") # Should complain about overwriting existing file
@ -1225,6 +1228,7 @@ _VMX_IMG = "%s/tests/virtconv-files/vmx_input/test1.vmx" % os.getcwd()
vconv = App("virt-convert")
c = vconv.add_category("misc", "--connect %(URI-KVM)s --dry")
c.add_valid(_VMX_IMG + " --disk-format qcow2") # hits some more code paths than print-xml
c.add_invalid(_VMX_IMG + " --input-format foo") # invalid input format
c.add_invalid("%(EXISTIMG1)s") # invalid input file

View File

@ -38,7 +38,7 @@ def get_original_guest(guest_name, origfile, design):
try:
design.original_xml = origxml
return
except (ValueError, RuntimeError) as e:
except (ValueError, RuntimeError) as e: # pragma: no cover
fail(e)
if not guest_name:
@ -153,9 +153,7 @@ def main(conn=None):
cli.convert_old_force(options)
cli.parse_check(options.check)
cli.set_prompt(options.prompt)
if conn is None:
conn = cli.getConnection(options.connect)
conn = cli.getConnection(options.connect, conn=conn)
if (options.new_diskfile is None and
options.auto_clone is False and
@ -203,7 +201,7 @@ def main(conn=None):
logging.debug("end clone")
return 0
if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
try:
sys.exit(main())
except SystemExit as sys_e:

View File

@ -82,8 +82,7 @@ def main(conn=None):
options = parse_args()
cli.setupLogging("virt-convert", options.debug, options.quiet)
if conn is None:
conn = cli.getConnection(options.connect)
conn = cli.getConnection(options.connect, conn=conn)
if options.xmlonly:
options.dry = True
options.quiet = True
@ -110,18 +109,18 @@ def main(conn=None):
if options.xmlonly:
print_stdout(installer.start_install(guest, return_xml=True)[1],
do_force=True)
elif not options.dry:
elif not options.dry or cli.in_testsuite():
print_stdout(_("Creating guest '%s'.") % guest.name)
domain = installer.start_install(guest)
cli.connect_console(guest, domain, conscb, True, False)
except Exception:
except Exception: # pragma: no cover
converter.cleanup()
raise
return 0
if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
try:
sys.exit(main())
except SystemExit as sys_e:

View File

@ -190,12 +190,6 @@ def convert_old_cpuset(options):
def convert_old_networks(options):
if options.nonetworks:
if options.mac:
fail(_("Cannot use --mac with --nonetworks"))
if options.bridge:
fail(_("Cannot use --bridge with --nonetworks"))
if options.network:
fail(_("Cannot use --nonetworks with --network"))
options.network = ["none"]
macs = virtinst.xmlutil.listify(options.mac)
@ -649,7 +643,7 @@ def start_install(guest, installer, options):
print_stdout(_("Domain creation completed."))
if not options.transient and not domain.isActive():
if options.noreboot or not installer.has_install_phase():
print_stdout(
print_stdout( # pragma: no cover
_("You can restart your domain by running:\n %s") %
cli.virsh_start_cmd(guest))
else:
@ -658,7 +652,7 @@ def start_install(guest, installer, options):
cli.connect_console(guest, domain, conscb, True,
options.destroy_on_exit)
except KeyboardInterrupt:
except KeyboardInterrupt: # pragma: no cover
logging.debug("", exc_info=True)
print_stderr(_("Domain install interrupted."))
raise
@ -668,6 +662,10 @@ def start_install(guest, installer, options):
installer.cleanup_created_disks(guest, meter)
cli.install_fail(guest)
if cli.in_testsuite() and options.destroy_on_exit:
# Helps with unit testing
_destroy_on_exit(domain)
def check_domain(installer, domain, conscb, transient,
wait_for_install, wait_time, start_time):
@ -682,14 +680,14 @@ def check_domain(installer, domain, conscb, transient,
logging.debug("Domain state after install: %s", state)
if state == libvirt.VIR_DOMAIN_CRASHED:
fail(_("Domain has crashed."))
fail(_("Domain has crashed.")) # pragma: no cover
return not domain.isActive()
except libvirt.libvirtError as e:
if transient and e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
logging.debug("transient VM shutdown and disappeared.")
return True
raise
raise # pragma: no cover
if check_domain_inactive():
return
@ -700,9 +698,9 @@ def check_domain(installer, domain, conscb, transient,
# the former case, libvirt may not have caught up yet with the
# VM having exited, so wait a bit and check again
if not cli.in_testsuite():
time.sleep(2)
time.sleep(2) # pragma: no cover
if check_domain_inactive():
return
return # pragma: no cover
# If we reach here, the VM still appears to be running.
if not wait_for_install or wait_time == 0:
@ -728,15 +726,15 @@ def check_domain(installer, domain, conscb, transient,
# Wait loop
while True:
if not domain.isActive():
if not domain.isActive(): # pragma: no cover
print_stdout(_("Domain has shutdown. Continuing."))
break
if not cli.in_testsuite():
if not cli.in_testsuite(): # pragma: no cover
time.sleep(1)
time_elapsed = (time.time() - start_time)
if not cli.in_testsuite():
if not cli.in_testsuite(): # pragma: no cover
if wait_forever:
continue
if time_elapsed < wait_time:
@ -919,13 +917,13 @@ def _destroy_on_exit(domain):
isactive = bool(domain and domain.isActive())
if isactive:
domain.destroy()
except libvirt.libvirtError as e:
except libvirt.libvirtError as e: # pragma: no cover
if e.get_error_code() != libvirt.VIR_ERR_NO_DOMAIN:
logging.debug("Error invoking atexit destroy_on_exit",
exc_info=True)
def set_test_stub_options(options):
def set_test_stub_options(options): # pragma: no cover
# Set some basic options that will let virt-install succeed. Helps
# save boiler plate typing when testing new command line additions
if not options.test_stub_command:
@ -975,8 +973,7 @@ def main(conn=None):
set_test_stub_options(options)
convert_old_os_options(options)
if conn is None:
conn = cli.getConnection(options.connect)
conn = cli.getConnection(options.connect, conn=conn)
if options.test_media_detection:
do_test_media_detection(conn, options)
@ -992,7 +989,7 @@ def main(conn=None):
return 0
if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
try:
sys.exit(main())
except SystemExit as sys_e:

View File

@ -466,8 +466,7 @@ def main(conn=None):
if options.confirm and not options.print_xml:
options.print_diff = True
if conn is None:
conn = cli.getConnection(options.connect)
conn = cli.getConnection(options.connect, conn)
domain = None
active_xmlobj = None
@ -525,7 +524,7 @@ def main(conn=None):
return 0
if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
try:
sys.exit(main())
except SystemExit as sys_e:

View File

@ -12,7 +12,8 @@ Configuration variables that can be set at build time
import os
import sys
if sys.version_info.major != 3 or sys.version_info.minor < 4:
if (sys.version_info.major != 3 or
sys.version_info.minor < 4): # pragma: no cover
print("python 3.4 or later is required, your's is %s" %
sys.version_info)
sys.exit(1)
@ -24,7 +25,7 @@ _filepath = os.path.abspath(__file__)
_srcdir = os.path.abspath(os.path.join(os.path.dirname(_filepath), ".."))
_cfgpath = os.path.join(os.path.dirname(_filepath), "cli.cfg")
if os.path.exists(_cfgpath):
_cfg.read(_cfgpath)
_cfg.read(_cfgpath) # pragma: no cover
_istest = "VIRTINST_TEST_SUITE" in os.environ
_running_from_srcdir = os.path.exists(
@ -35,7 +36,7 @@ def _split_list(commastr):
return [d for d in commastr.split(",") if d]
def _get_param(name, default):
def _get_param(name, default): # pragma: no cover
if _istest:
return default
try:
@ -53,7 +54,7 @@ def _setup_gsettings_path(schemadir):
import shutil
exe = shutil.which("glib-compile-schemas")
if not exe:
if not exe: # pragma: no cover
raise RuntimeError("You must install glib-compile-schemas to run "
"virt-manager from git.")
subprocess.check_call([exe, "--strict", schemadir])
@ -87,7 +88,7 @@ class _CLIConfig(object):
self.icon_dir = os.path.join(_srcdir, "data")
self.gsettings_dir = self.icon_dir
_setup_gsettings_path(self.gsettings_dir)
else:
else: # pragma: no cover
self.ui_dir = os.path.join(prefix, "share", "virt-manager", "ui")
self.icon_dir = os.path.join(prefix, "share", "virt-manager",
"icons")

View File

@ -210,7 +210,11 @@ def in_testsuite():
# Libvirt connection helpers #
##############################
def getConnection(uri):
def getConnection(uri, conn=None):
if conn:
# preopened connection passed in via test suite
return conn
logging.debug("Requesting libvirt URI %s", (uri or "default"))
conn = VirtinstConnection(uri)
conn.open(_openauth_cb, None)