view tests/e2e/run_e2e.py @ 3426:8c25489bb79c

plugin XEP-0077: use `xmpp_domain` when set for `host`
author Goffi <goffi@goffi.org>
date Fri, 27 Nov 2020 16:29:06 +0100
parents 814e118d9ef3
children d4558f3cbf13
line wrap: on
line source

#!/usr/bin/env python3

# SàT: an XMPP client
# Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Run end-to-end tests in appropriate Docker environment"""

import sys
from pathlib import Path
import tempfile
from textwrap import dedent
import sh
from sat.core import exceptions

KEEP_OPT = "--keep"


def live_out(data):
    sys.stdout.write(data)
    sys.stdout.flush()


def live_err(data):
    sys.stderr.write(data)
    sys.stderr.flush()


def use_e2e_env():
    if KEEP_OPT in sys.argv:
        keep_containers = True
        sys.argv.remove(KEEP_OPT)
    else:
        keep_containers = False
    for p in Path.cwd().parents:
        package_path = p / "sat"
        docker_path = p / "docker"
        if package_path.is_dir() and docker_path.is_dir():
            sat_root_path = p
            break
    else:
        raise exceptions.NotFound(
            "Can't find root of SàT code, are you sure that you are running the test "
            "from the backend repository?"
        )

    compose_e2e_path = docker_path / "docker-compose_e2e.yml"
    if not compose_e2e_path.is_file():
        raise exceptions.NotFound('"docker-compose_e2e.yml" file can\'t be found')

    with tempfile.TemporaryDirectory() as temp_dir:
        override_path = Path(temp_dir) / "test_override.yml"
        with override_path.open("w") as f:
            f.write(dedent(f"""\
                version: "3.6"
                services:
                  sat:
                    volumes:
                      - type: bind
                        source: {sat_root_path}
                        target: /src/sat
                        read_only: true
                """))

        docker_compose = sh.docker_compose.bake(
            "-f", compose_e2e_path, "-f", override_path)
        docker_compose.up("-d")

        try:
            docker_compose.exec(
            "-T", "--workdir", "/src/sat/tests", "sat",
            "pytest", "-o", "cache_dir=/tmp", *sys.argv[1:], color="yes",
            _in=sys.stdin, _out=live_out, _out_bufsize=0, _err=live_err, _err_bufsize=0
        )
        except sh.ErrorReturnCode as e:
            sys.exit(e.exit_code)
        finally:
            if not keep_containers:
                docker_compose.down(volumes=True)

if __name__ == "__main__":
    use_e2e_env()