diff tests/e2e/run_e2e.py @ 3415:814e118d9ef3

tests: end-2-end tests first draft: - e2e tests are launched inside the new docker e2e test environment - `run_e2e.py` launch the docker container, mount the current code base in it, launch the e2e tests and print report in real time - `conftest.py` are pytest fixtures managing many things such as account creation, fake files management, JSON or Domish.Element parsing, fake editor, etc. - `test_jp.py` are end-to-end test done with `jp`. `sh` library is used to make tests writting as user-friendly as possible. The `SAT_TEST_ENV_E2E` environment variable is checked, and tests will be skipped if it's not set.
author Goffi <goffi@goffi.org>
date Thu, 12 Nov 2020 14:53:16 +0100
parents
children d4558f3cbf13
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/e2e/run_e2e.py	Thu Nov 12 14:53:16 2020 +0100
@@ -0,0 +1,94 @@
+#!/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()