1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 22:56:15 +01:00

behave: Print debug information when running commands

Run behave with -DDEBUG=1 to enable the debug mode
This commit is contained in:
Daniel Mach 2022-12-09 14:26:35 +01:00
parent 3027edc0eb
commit 1839e834c6
4 changed files with 28 additions and 10 deletions

View File

@ -21,7 +21,7 @@ def after_scenario(context, scenario):
if "destructive" in scenario.tags:
# start a new container after a destructive test
context.podman.kill()
context.podman = podman.Podman()
context.podman = podman.Podman(context)
context.osc.clear()
common.check_exit_code(context)
@ -46,7 +46,7 @@ def before_all(context):
# absolute path to .../behave/fixtures
context.fixtures = os.path.join(os.path.dirname(__file__), "..", "fixtures")
context.podman = podman.Podman()
context.podman = podman.Podman(context)
context.osc = osc.Osc(context)

View File

@ -6,6 +6,13 @@ import subprocess
import behave
def debug(context, *args):
if not context.config.userdata.get("DEBUG", False):
return
msg = " ".join((str(i).strip() for i in args))
print(f"DEBUG: {msg}")
def makedirs(path):
try:
os.makedirs(path)
@ -62,16 +69,14 @@ def run_in_context(context, cmd, can_fail=False, **run_args):
env["PATH"] = path.replace("$PATH", env["PATH"])
run_args["env"] = env
if context.config.userdata.get("DEBUG", False):
print(f"DEBUG: command: {cmd}")
debug(context, "Running command:", cmd)
context.cmd_exitcode, context.cmd_stdout, context.cmd_stderr = run(cmd, **run_args)
context.cmd_exitcode_checked = False
if context.config.userdata.get("DEBUG", False):
print(f"DEBUG: exit code: {context.cmd_exitcode}")
print(f"DEBUG: stdout: {context.cmd_stdout}")
print(f"DEBUG: stderr: {context.cmd_stderr}")
debug(context, "> return code:", context.cmd_exitcode)
debug(context, "> stdout:", context.cmd_stdout)
debug(context, "> stderr:", context.cmd_stderr)
if not can_fail and context.cmd_exitcode != 0:
raise AssertionError('Running command "%s" failed: %s' % (cmd, context.cmd_exitcode))

View File

@ -6,6 +6,7 @@ import time
import behave
from steps.common import debug
from steps.common import run_in_context
@ -15,6 +16,7 @@ class Osc:
raise RuntimeError("context doesn't have 'podman' object set")
self.context = context
debug(self.context, "Osc.__init__()")
self.temp = None
self.clear()
@ -25,6 +27,7 @@ class Osc:
pass
def clear(self):
debug(self.context, "Osc.clear()")
if self.temp:
shutil.rmtree(self.temp)
self.temp = tempfile.mkdtemp(prefix="osc_behave_")

View File

@ -1,8 +1,12 @@
import subprocess
from steps.common import debug
class Podman:
def __init__(self):
def __init__(self, context):
self.context = context
debug(context, "Podman.__init__()")
self.container_id = None
self.run()
self.wait_on_systemd()
@ -16,6 +20,7 @@ class Podman:
def _run(self, args, check=True):
cmd = ["podman"] + args
debug(self.context, "Running command:", cmd)
proc = subprocess.run(
cmd,
stdout=subprocess.PIPE,
@ -23,9 +28,13 @@ class Podman:
encoding="utf-8",
check=check,
)
debug(self.context, "> return code:", proc.returncode)
debug(self.context, "> stdout:", proc.stdout)
debug(self.context, "> stderr:", proc.stderr)
return proc
def run(self):
debug(self.context, "Podman.run()")
args = [
"run",
"--name", "obs-server-behave",
@ -45,6 +54,7 @@ class Podman:
def kill(self):
if not self.container_id:
return
debug(self.context, "Podman.kill()")
args = ["kill", self.container_id]
self._run(args)
self.container_id = None
@ -65,4 +75,4 @@ class Podman:
if line.startswith("443/tcp"):
# return <port> from: "443/tcp -> 0.0.0.0:<port>"
return line.split(":")[-1]
return None
raise RuntimeError(f"Could not determine port of container {self.container_id}")