virtinst: Centralize in_testsuite checking

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-07-17 18:46:54 -04:00
parent 44aa0b093e
commit 380a44318a
6 changed files with 24 additions and 28 deletions

View File

@ -12,10 +12,6 @@ import requests
from virtinst import log
def _in_testsuite():
return "VIRTINST_TEST_SUITE" in os.environ
def _make_mock_url(url, filesyntax):
if url.endswith("treeinfo"):
# If the url is requesting treeinfo, give a fake treeinfo from

View File

@ -131,7 +131,7 @@ def setupLogging(appname, debug_stdout, do_quiet, cli_app=True):
vi_dir = VirtinstConnection.get_app_cache_dir()
logfile = os.path.join(vi_dir, appname + ".log")
if in_testsuite():
if xmlutil.in_testsuite():
vi_dir = None
logfile = None
@ -204,10 +204,6 @@ def setupLogging(appname, debug_stdout, do_quiet, cli_app=True):
log.debug("Launched with command line: %s", " ".join(sys.argv))
def in_testsuite():
return "VIRTINST_TEST_SUITE" in os.environ
##############################
# Libvirt connection helpers #
##############################
@ -387,7 +383,7 @@ def _run_console(domain, message, args):
argstr = " ".join([shlex.quote(a) for a in args])
print_stdout(message % {"command": argstr})
if in_testsuite():
if xmlutil.in_testsuite():
args = ["/bin/test"]
child = os.fork()
@ -454,7 +450,7 @@ def connect_console(guest, domain, consolecb, wait, destroy_on_exit):
def get_meter():
import virtinst.progress
quiet = (get_global_state().quiet or in_testsuite())
quiet = (get_global_state().quiet or xmlutil.in_testsuite())
return virtinst.progress.make_meter(quiet=quiet)
@ -568,7 +564,7 @@ def autocomplete(parser):
break
kwargs = {"validator": _completer_validator}
if in_testsuite():
if xmlutil.in_testsuite():
import io
kwargs["output_stream"] = io.BytesIO()
kwargs["exit_method"] = sys.exit
@ -576,7 +572,7 @@ def autocomplete(parser):
# This fdopen hackery is to avoid argcomplete debug_stream behavior
# from taking over an fd that pytest wants to use
fake_fdopen = os.fdopen
if in_testsuite():
if xmlutil.in_testsuite():
def fake_fdopen_cb(*args, **kwargs):
return sys.stderr
fake_fdopen = fake_fdopen_cb
@ -585,7 +581,7 @@ def autocomplete(parser):
try:
argcomplete.autocomplete(parser, **kwargs)
except SystemExit:
if in_testsuite():
if xmlutil.in_testsuite():
output = kwargs["output_stream"].getvalue().decode("utf-8")
print(output)
raise
@ -1812,14 +1808,14 @@ def _determine_default_autoconsole_type(guest, installer):
log.debug("No viewer to launch for graphics type '%s'", gtype)
return None
if not HAS_VIRTVIEWER and not in_testsuite(): # pragma: no cover
if not HAS_VIRTVIEWER and not xmlutil.in_testsuite(): # pragma: no cover
log.warning(_("Unable to connect to graphical console: "
"virt-viewer not installed. Please install "
"the 'virt-viewer' package."))
return None
if (not os.environ.get("DISPLAY", "") and
not in_testsuite()): # pragma: no cover
not xmlutil.in_testsuite()): # pragma: no cover
log.warning(_("Graphics requested but DISPLAY is not set. "
"Not running virt-viewer."))
return None

View File

@ -9,9 +9,10 @@ import weakref
import libvirt
from . import Capabilities
from . import pollhelpers
from . import support
from . import Capabilities
from . import xmlutil
from .guest import Guest
from .logger import log
from .nodedev import NodeDevice
@ -50,7 +51,7 @@ class VirtinstConnection(object):
@staticmethod
def in_testsuite():
return "VIRTINST_TEST_SUITE" in os.environ
return xmlutil.in_testsuite()
def __init__(self, uri):
_initial_uri = uri or ""

View File

@ -12,13 +12,10 @@ import re
from gi.repository import Libosinfo
from . import xmlutil
from .logger import log
def _in_testsuite():
return "VIRTINST_TEST_SUITE" in os.environ
def _media_create_from_location(location):
if not hasattr(Libosinfo.Media, "create_from_location_with_flags"):
return Libosinfo.Media.create_from_location( # pragma: no cover
@ -261,7 +258,7 @@ class _OSDB(object):
if location.startswith("/"):
location = "file://" + location
if _in_testsuite() and not location.startswith("file:"):
if xmlutil.in_testsuite() and not location.startswith("file:"):
# We have mock network tests, but we don't want to pass the
# fake URL to libosinfo because it slows down the testcase
return None

View File

@ -603,7 +603,7 @@ def build_guest_instance(conn, options):
###########################
def _sleep(secs):
if not cli.in_testsuite():
if not virtinst.xmlutil.in_testsuite():
time.sleep(secs) # pragma: no cover
@ -649,12 +649,12 @@ class WaitHandler:
"""
_sleep(1)
if self._wait_forever:
if cli.in_testsuite():
if virtinst.xmlutil.in_testsuite():
return True
return False # pragma: no cover
time_elapsed = (time.time() - self._start_time)
return (time_elapsed >= self._wait_secs) or cli.in_testsuite()
return (time_elapsed >= self._wait_secs) or virtinst.xmlutil.in_testsuite()
def _print_cloudinit_passwd(installer):
@ -668,7 +668,7 @@ def _print_cloudinit_passwd(installer):
stdins = [sys.stdin]
timeout = 10
if sys.stdin.closed or not sys.stdin.isatty():
if not cli.in_testsuite(): # pragma: no cover
if not virtinst.xmlutil.in_testsuite(): # pragma: no cover
return
stdins = []
timeout = .0001
@ -740,7 +740,7 @@ def start_install(guest, installer, options):
virtinst.Installer.cleanup_created_disks(guest, meter)
cli.install_fail(guest)
if cli.in_testsuite() and options.destroy_on_exit:
if virtinst.xmlutil.in_testsuite() and options.destroy_on_exit:
# Helps with unit testing
_destroy_on_exit(domain)

View File

@ -5,6 +5,8 @@
# See the COPYING file in the top-level directory.
#
import os
def listify(l):
if l is None:
@ -65,3 +67,7 @@ def raise_programming_error(cond, msg):
"""
if cond:
raise RuntimeError("programming error: %s" % msg)
def in_testsuite():
return "VIRTINST_TEST_SUITE" in os.environ