Blame dev/run-tests-container.py

Pierre-Yves Chibon 479e0a
#! /usr/bin/env python
Pierre-Yves Chibon 479e0a
Clement Verna 37420e
import argparse
Clement Verna 37420e
import os
Clement Verna 37420e
import subprocess as sp
Clement Verna 37420e
Clement Verna 37420e
Clement Verna 37420e
def setup_parser():
Clement Verna 37420e
    """ Setup the cli arguments """
Clement Verna 37420e
    parser = argparse.ArgumentParser(prog="pagure-test")
Clement Verna 37420e
    parser.add_argument(
Clement Verna 37420e
        "test_case", nargs="?", default="", help="Run the given test case"
Clement Verna 37420e
    )
Clement Verna 37420e
    parser.add_argument(
Clement Verna 37420e
        "--fedora",
Clement Verna 37420e
        action="store_true",
Clement Verna 37420e
        help="Run the tests in fedora environment (DEFAULT)",
Clement Verna 37420e
    )
Clement Verna 37420e
    parser.add_argument(
Clement Verna 37420e
        "--centos",
Clement Verna 37420e
        action="store_true",
Clement Verna 37420e
        help="Run the tests in centos environment",
Clement Verna 37420e
    )
Clement Verna 37420e
    parser.add_argument(
Pierre-Yves Chibon 86597a
        "--pip",
Pierre-Yves Chibon 86597a
        action="store_true",
Pierre-Yves Chibon 86597a
        help="Run the tests in a venv on a Fedora host",
Pierre-Yves Chibon 86597a
    )
Pierre-Yves Chibon 86597a
    parser.add_argument(
Clement Verna 37420e
        "--skip-build",
Clement Verna 37420e
        dest="skip_build",
Clement Verna 37420e
        action="store_false",
Clement Verna 37420e
        help="Skip building the container image",
Clement Verna 37420e
    )
Pierre-Yves Chibon e944ac
    parser.add_argument(
Pierre-Yves Chibon e944ac
        "--shell",
Pierre-Yves Chibon e944ac
        dest="shell",
Pierre-Yves Chibon e944ac
        action="store_true",
Pierre-Yves Chibon e944ac
        help="Gives you a shell into the container instead "
Pierre-Yves Chibon e944ac
        "of running the tests",
Pierre-Yves Chibon e944ac
    )
Clement Verna 37420e
Clement Verna 37420e
    return parser
Clement Verna 37420e
Clement Verna 37420e
Clement Verna 37420e
if __name__ == "__main__":
Clement Verna 37420e
    parser = setup_parser()
Clement Verna 37420e
    args = parser.parse_args()
Clement Verna 37420e
Clement Verna 37420e
    if args.centos is True:
Pierre-Yves Chibon 479e0a
        container_names = ["pagure-c7-rpms-py2"]
Pierre-Yves Chibon 479e0a
        container_files = ["centos7-rpms-py2"]
Pierre-Yves Chibon 479e0a
    elif args.fedora is True:
Pierre-Yves Chibon 479e0a
        container_names = ["pagure-f29-rpms-py3"]
Pierre-Yves Chibon 479e0a
        container_files = ["f29-rpms-py3"]
Pierre-Yves Chibon 86597a
    elif args.pip is True:
Pierre-Yves Chibon 86597a
        container_names = ["pagure-fedora-pip-py3"]
Pierre-Yves Chibon 86597a
        container_files = ["fedora-pip-py3"]
Clement Verna 37420e
    else:
Pierre-Yves Chibon 86597a
        container_names = [
Pierre-Yves Chibon 73d120
            "pagure-f29-rpms-py3",
Pierre-Yves Chibon 73d120
            "pagure-c7-rpms-py2",
Pierre-Yves Chibon 73d120
            "pagure-fedora-pip-py3",
Pierre-Yves Chibon 86597a
        ]
Pierre-Yves Chibon 86597a
        container_files = [
Pierre-Yves Chibon 73d120
            "f29-rpms-py3",
Pierre-Yves Chibon 73d120
            "centos7-rpms-py2",
Pierre-Yves Chibon 73d120
            "fedora-pip-py3",
Pierre-Yves Chibon 86597a
        ]
Clement Verna 37420e
Pierre-Yves Chibon 02e44a
    failed = []
Pierre-Yves Chibon 479e0a
    for idx, container_name in enumerate(container_names):
Pierre-Yves Chibon 479e0a
        if args.skip_build is not False:
Pierre-Yves Chibon 479e0a
            print("------ Building Container Image -----")
Pierre-Yves Chibon 02e44a
            output_code = sp.call(
Pierre-Yves Chibon 479e0a
                [
Pierre-Yves Chibon 479e0a
                    "podman",
Pierre-Yves Chibon 479e0a
                    "build",
Karsten Hopp 099860
                    "--build-arg",
Karsten Hopp 099860
                    "branch={}".format(os.environ.get("BRANCH") or "master"),
Karsten Hopp 099860
                    "--build-arg",
Pierre-Yves Chibon 73d120
                    "repo={}".format(
Pierre-Yves Chibon 73d120
                        os.environ.get("REPO")
Pierre-Yves Chibon 73d120
                        or "https://pagure.io/pagure.git"
Pierre-Yves Chibon 73d120
                    ),
Pierre-Yves Chibon 479e0a
                    "--rm",
Pierre-Yves Chibon 479e0a
                    "-t",
Pierre-Yves Chibon 479e0a
                    container_name,
Pierre-Yves Chibon 479e0a
                    "-f",
Pierre-Yves Chibon 479e0a
                    "dev/containers/%s" % container_files[idx],
Pierre-Yves Chibon 479e0a
                    "dev/containers",
Pierre-Yves Chibon 479e0a
                ]
Clement Verna 37420e
            )
Pierre-Yves Chibon 02e44a
            if output_code:
Pierre-Yves Chibon 02e44a
                print("Failed building: %s", container_name)
Pierre-Yves Chibon 02e44a
                break
Pierre-Yves Chibon e944ac
Pierre-Yves Chibon 479e0a
        result_path = "{}/results_{}".format(os.getcwd(), container_files[idx])
Pierre-Yves Chibon 479e0a
        if not os.path.exists(result_path):
Pierre-Yves Chibon 479e0a
            os.mkdir(result_path)
Pierre-Yves Chibon e944ac
Pierre-Yves Chibon 479e0a
        if args.shell:
Pierre-Yves Chibon 479e0a
            print("--------- Shelling in the container --------------")
Pierre-Yves Chibon 479e0a
            command = [
Pierre-Yves Chibon e944ac
                "podman",
Pierre-Yves Chibon e944ac
                "run",
Pierre-Yves Chibon e944ac
                "-it",
Pierre-Yves Chibon e944ac
                "--rm",
Pierre-Yves Chibon e944ac
                "--name",
Pierre-Yves Chibon e944ac
                container_name,
Pierre-Yves Chibon e944ac
                "-v",
Pierre-Yves Chibon b8a95b
                "{}/results_{}:/pagure/results:z".format(
Pierre-Yves Chibon 73d120
                    os.getcwd(), container_files[idx]
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 479e0a
                "-e",
Karsten Hopp 099860
                "BRANCH={}".format(os.environ.get("BRANCH") or "master"),
Pierre-Yves Chibon 479e0a
                "-e",
Pierre-Yves Chibon 73d120
                "REPO={}".format(
Pierre-Yves Chibon 73d120
                    os.environ.get("REPO") or "https://pagure.io/pagure.git"
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 479e0a
                "--entrypoint=/bin/bash",
Pierre-Yves Chibon e944ac
                container_name,
Pierre-Yves Chibon e944ac
            ]
Pierre-Yves Chibon 479e0a
            sp.call(command)
Pierre-Yves Chibon 479e0a
        else:
Pierre-Yves Chibon 479e0a
            print("--------- Running Test --------------")
Pierre-Yves Chibon 02e44a
            command = [
Pierre-Yves Chibon 02e44a
                "podman",
Pierre-Yves Chibon 02e44a
                "run",
Pierre-Yves Chibon 02e44a
                "-it",
Pierre-Yves Chibon 02e44a
                "--rm",
Pierre-Yves Chibon 02e44a
                "--name",
Pierre-Yves Chibon 02e44a
                container_name,
Pierre-Yves Chibon 02e44a
                "-v",
Pierre-Yves Chibon 02e44a
                "{}/results_{}:/pagure/results:z".format(
Pierre-Yves Chibon 73d120
                    os.getcwd(), container_files[idx]
Pierre-Yves Chibon 73d120
                ),
Pierre-Yves Chibon 02e44a
                "-e",
Karsten Hopp 099860
                "BRANCH={}".format(os.environ.get("BRANCH") or "master"),
Pierre-Yves Chibon 02e44a
                "-e",
Pierre-Yves Chibon 73d120
                "REPO={}".format(
Pierre-Yves Chibon 73d120
                    os.environ.get("REPO") or "https://pagure.io/pagure.git"
Pierre-Yves Chibon 73d120
                ),
Karsten Hopp 6a8f07
                "-e",
Karsten Hopp 6a8f07
                "TESTCASE={}".format(args.test_case or ""),
Pierre-Yves Chibon 02e44a
                container_name,
Pierre-Yves Chibon 02e44a
            ]
Pierre-Yves Chibon 02e44a
            output_code = sp.call(command)
Pierre-Yves Chibon 02e44a
            if output_code:
Pierre-Yves Chibon 02e44a
                failed.append(container_name)
Pierre-Yves Chibon 02e44a
Pierre-Yves Chibon 02e44a
    if not args.shell:
Pierre-Yves Chibon 02e44a
        print("\nSummary:")
Pierre-Yves Chibon 02e44a
        if not failed:
Pierre-Yves Chibon 02e44a
            print("  ALL TESTS PASSED")
Pierre-Yves Chibon 02e44a
        else:
Pierre-Yves Chibon 02e44a
            print("  %s TESTS FAILED:" % len(failed))
Pierre-Yves Chibon 02e44a
            for fail in failed:
Pierre-Yves Chibon 02e44a
                print("    - %s" % fail)