diff --git a/behave/README.md b/behave/README.md new file mode 100644 index 000000000..f078414d8 --- /dev/null +++ b/behave/README.md @@ -0,0 +1,14 @@ +BDD for Mock +============ + +This test-suite can destroy your system! Not intentionally, but some steps +require us to use root (e.g. install or remove packages). **Never** execute +this test suite on your host system, allocate some disposable machine. + +How to run the tests +-------------------- + +1. Install the Mock RPM that you want to test. + +2. Run `$ behave` command in this directory, with `--tags tagname` if you want + to test only subset of all provided scenarios. diff --git a/behave/__init__.py b/behave/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/behave/features/environment.py b/behave/environment.py similarity index 96% rename from behave/features/environment.py rename to behave/environment.py index 01bc23666..abcb0023a 100644 --- a/behave/features/environment.py +++ b/behave/environment.py @@ -11,7 +11,8 @@ import requests -from testlib import Mock, no_output +from testlib.mock import Mock +from testlib.commands import no_output def _random_string(length): diff --git a/behave/features/steps/other.py b/behave/steps/other.py similarity index 99% rename from behave/features/steps/other.py rename to behave/steps/other.py index b0557c98d..1dc21a1ab 100644 --- a/behave/features/steps/other.py +++ b/behave/steps/other.py @@ -235,7 +235,7 @@ def step_impl(context): assert_that(lockfile_data["buildroot"]["rpms"], has_item(has_entries({"name": "filesystem"}))) - schemafile = os.path.join(os.path.dirname(__file__), '..', '..', '..', + schemafile = os.path.join(os.path.dirname(__file__), '..', '..', "mock", "docs", "buildroot-lock-schema-1.0.0.json") with open(schemafile, "r", encoding="utf-8") as fd: diff --git a/behave/testlib/__init__.py b/behave/testlib/__init__.py new file mode 100644 index 000000000..a54a12ca0 --- /dev/null +++ b/behave/testlib/__init__.py @@ -0,0 +1 @@ +""" Helper library for Mock's BDD """ diff --git a/behave/testlib/commands.py b/behave/testlib/commands.py new file mode 100644 index 000000000..54304b900 --- /dev/null +++ b/behave/testlib/commands.py @@ -0,0 +1,61 @@ +""" +Executing commands in Mock's behave test suite. +""" + +from contextlib import contextmanager +import io +import shlex +import subprocess +import sys + + +@contextmanager +def no_output(): + """ + Suppress stdout/stderr when it is not captured by behave + https://github.com/behave/behave/issues/863 + """ + real_out = sys.stdout, sys.stderr + sys.stdout = io.StringIO() + sys.stderr = io.StringIO() + yield + sys.stdout, sys.stderr = real_out + + +def quoted_cmd(cmd): + """ shell quoted cmd array as string """ + return " ".join(shlex.quote(arg) for arg in cmd) + + +def run(cmd): + """ + Return exitcode, stdout, stderr. It's bad there's no such thing in behave + directly. + """ + try: + process = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) + stdout, stderr = process.communicate() + print(f"Command exit status {process.returncode} in: {quoted_cmd(cmd)}") + if stdout: + print("stdout:") + print(stdout) + if stderr: + print("stderr:") + print(stderr) + return process.returncode, stdout, stderr + except (FileNotFoundError, PermissionError) as e: + print(f"Error running command {quoted_cmd(cmd)}: {e}") + return -1, "", str(e) + + +def run_check(cmd): + """ run, but check nonzero exit status """ + retcode, stdout, stderr = run(cmd) + if retcode != 0: + raise Exception(f"Command failed with return code {retcode}: {quoted_cmd(cmd)}\n{stderr}") + return stdout, stderr diff --git a/behave/testlib.py b/behave/testlib/mock.py similarity index 72% rename from behave/testlib.py rename to behave/testlib/mock.py index 2a4322c79..0bdb28268 100644 --- a/behave/testlib.py +++ b/behave/testlib/mock.py @@ -1,64 +1,11 @@ -""" helpers for Copr BDD tests """ +""" +Stateful "Mock" command object. +""" -from contextlib import contextmanager -import io from pathlib import Path -import shlex import os -import subprocess -import sys - - -@contextmanager -def no_output(): - """ - Suppress stdout/stderr when it is not captured by behave - https://github.com/behave/behave/issues/863 - """ - real_out = sys.stdout, sys.stderr - sys.stdout = io.StringIO() - sys.stderr = io.StringIO() - yield - sys.stdout, sys.stderr = real_out - - -def quoted_cmd(cmd): - """ shell quoted cmd array as string """ - return " ".join(shlex.quote(arg) for arg in cmd) - - -def run(cmd): - """ - Return exitcode, stdout, stderr. It's bad there's no such thing in behave - directly. - """ - try: - process = subprocess.Popen( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, - ) - stdout, stderr = process.communicate() - print(f"Command exit status {process.returncode} in: {quoted_cmd(cmd)}") - if stdout: - print("stdout:") - print(stdout) - if stderr: - print("stderr:") - print(stderr) - return process.returncode, stdout, stderr - except (FileNotFoundError, PermissionError) as e: - print(f"Error running command {quoted_cmd(cmd)}: {e}") - return -1, "", str(e) - - -def run_check(cmd): - """ run, but check nonzero exit status """ - retcode, stdout, stderr = run(cmd) - if retcode != 0: - raise Exception(f"Command failed with return code {retcode}: {quoted_cmd(cmd)}\n{stderr}") - return stdout, stderr + +from testlib.commands import run_check class Mock: @@ -187,4 +134,4 @@ def assert_is_subset(set_a, set_b): """ assert that SET_A is subset of SET_B """ if set_a.issubset(set_b): return - raise AssertionError("Set {} is not a subset of {}".format(set_a, set_b)) + raise AssertionError(f"Set {set_a} is not a subset of {set_b}")