1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-13 07:56:14 +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: if "destructive" in scenario.tags:
# start a new container after a destructive test # start a new container after a destructive test
context.podman.kill() context.podman.kill()
context.podman = podman.Podman() context.podman = podman.Podman(context)
context.osc.clear() context.osc.clear()
common.check_exit_code(context) common.check_exit_code(context)
@ -46,7 +46,7 @@ def before_all(context):
# absolute path to .../behave/fixtures # absolute path to .../behave/fixtures
context.fixtures = os.path.join(os.path.dirname(__file__), "..", "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) context.osc = osc.Osc(context)

View File

@ -6,6 +6,13 @@ import subprocess
import behave 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): def makedirs(path):
try: try:
os.makedirs(path) 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"]) env["PATH"] = path.replace("$PATH", env["PATH"])
run_args["env"] = env run_args["env"] = env
if context.config.userdata.get("DEBUG", False): debug(context, "Running command:", cmd)
print(f"DEBUG: command: {cmd}")
context.cmd_exitcode, context.cmd_stdout, context.cmd_stderr = run(cmd, **run_args) context.cmd_exitcode, context.cmd_stdout, context.cmd_stderr = run(cmd, **run_args)
context.cmd_exitcode_checked = False context.cmd_exitcode_checked = False
if context.config.userdata.get("DEBUG", False): debug(context, "> return code:", context.cmd_exitcode)
print(f"DEBUG: exit code: {context.cmd_exitcode}") debug(context, "> stdout:", context.cmd_stdout)
print(f"DEBUG: stdout: {context.cmd_stdout}") debug(context, "> stderr:", context.cmd_stderr)
print(f"DEBUG: stderr: {context.cmd_stderr}")
if not can_fail and context.cmd_exitcode != 0: if not can_fail and context.cmd_exitcode != 0:
raise AssertionError('Running command "%s" failed: %s' % (cmd, context.cmd_exitcode)) raise AssertionError('Running command "%s" failed: %s' % (cmd, context.cmd_exitcode))

View File

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

View File

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