Commit 16938e7e authored by Philippe Pepiot's avatar Philippe Pepiot
Browse files

tests: use click for argument parsing

  * Use a sweet library
  * Previous code with argparse wasn't compatible with python3
  * Add a test-requirements.txt file
parent dade1a87
......@@ -9,6 +9,6 @@ env:
- IMAGE=centos6
- IMAGE=centos7
install:
- pip install testinfra
- pip install -r test-requirements.txt
script:
- ./run-tests.py test $IMAGE -v
- ./run-tests.py test $IMAGE -- -v
......@@ -84,7 +84,7 @@ The command ``./run-test.py test centos6`` built a provisioned image
(highstate) and run testinfra tests on it.
Note that you can add any parameters that testinfra (pytest) accept, for
instance ``./run-test.py test centos6 -k idempotence --pdb``.
instance ``./run-test.py test centos6 -- -k idempotence --pdb``.
.. _saem_ref: https://www.cubicweb.org/project/cubicweb-saem_ref
......
#!/usr/bin/env python
import sys
import click
import os
import argparse
import subprocess
BASEDIR = os.path.abspath(os.path.dirname(__file__))
_formula = "saemref"
_images = ["centos6", "centos7"]
def get_tag(image, salt=False):
......@@ -31,7 +29,19 @@ def image_exists(image):
raise RuntimeError("Cannot test if image exists")
def _build(image, salt=False):
image_option = click.argument("image", type=click.Choice(["centos6", "centos7"]))
salt_option = click.option('--salt', is_flag=True, help="Run salt highstate")
@click.group()
def cli():
pass
@cli.command(help="Build a container")
@image_option
@salt_option
def build(image, salt):
dockerfile = "test/{0}.Dockerfile".format(image)
tag = get_tag(image, salt)
if salt:
......@@ -57,31 +67,37 @@ def _build(image, salt=False):
])
def build(args, remain):
_build(args.image, args.salt)
def test(args, remain):
tag = get_tag(args.image, True)
_build(args.image, True)
@cli.command(
help="Build a salted (highstate) container and run tests on it",
context_settings={"allow_extra_args": True},
)
@click.pass_context
@image_option
def test(ctx, image):
tag = get_tag(image, True)
ctx.invoke(build, image=image, salt=True)
import pytest
return pytest.main(["--docker-image", tag] + remain)
return pytest.main(["--docker-image", tag] + ctx.args)
def dev(args, remain):
tag = get_tag(args.image, args.salt)
@cli.command(help="Run a container and spawn an interactive shell inside")
@click.pass_context
@image_option
@salt_option
def dev(ctx, image, salt):
tag = get_tag(image, salt)
if not image_exists(tag):
_build(args.image, args.salt)
ctx.invoke(build, image=image, salt=salt)
cmd = [
"docker", "run", "-d", "--hostname", args.image,
"docker", "run", "-d", "--hostname", image,
"-v", "{0}/test/minion.conf:/etc/salt/minion.d/minion.conf".format(BASEDIR),
"-v", "{0}/test/salt:/srv/salt".format(BASEDIR),
"-v", "{0}/test/pillar:/srv/pillar".format(BASEDIR),
"-v", "{0}/{1}:/srv/formula/{1}".format(BASEDIR, _formula),
]
if args.image in ("centos7",):
if image in ("centos7",):
# Systemd require privileged container
cmd.append("--privileged")
cmd.append(tag)
......@@ -96,22 +112,4 @@ def dev(args, remain):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Test {0} formula in docker".format(_formula))
subparsers = parser.add_subparsers(help="sub-command help")
parser_build = subparsers.add_parser("build", help="build images")
parser_build.add_argument("image", choices=_images)
parser_build.add_argument("--salt", action="store_true")
parser_build.set_defaults(func=build)
parser_dev = subparsers.add_parser("dev", help="drop a shell in dev container")
parser_dev.add_argument("image", choices=_images)
parser_dev.add_argument("--salt", action="store_true")
parser_dev.set_defaults(func=dev)
parser_test = subparsers.add_parser("test", help="provision a container and run tests on it")
parser_test.add_argument("image", choices=_images)
parser_test.set_defaults(func=test)
args, remain = parser.parse_known_args()
sys.exit(args.func(args, remain))
cli()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment