diff tests/e2e/run_e2e.py @ 3498:d78b5eae912a

tests: update following names change
author Goffi <goffi@goffi.org>
date Fri, 16 Apr 2021 18:32:34 +0200
parents 7550ae9cfbac
children 8ee0140cc8b6
line wrap: on
line diff
--- a/tests/e2e/run_e2e.py	Fri Apr 16 18:32:16 2021 +0200
+++ b/tests/e2e/run_e2e.py	Fri Apr 16 18:32:34 2021 +0200
@@ -25,10 +25,10 @@
 from datetime import datetime
 import sh
 import io
-import re
 import sat_templates
 import libervia
 from sat.core import exceptions
+from sat.tools.common import regex
 import yaml
 try:
     from yaml import CLoader as Loader, CDumper as Dumper
@@ -48,22 +48,48 @@
 
     from helium import *
     start_firefox()
-    go_to("https://libervia.test:8443/login")
+    go_to("https://libervia-web.test:8443/login")
     write("account1", "login")
     write("test", "password")
     click("log in")
     """)
 report_buffer = io.StringIO()
+live_out_buf = []
+live_err_buf = []
 
 
 def live_out(data):
-    sys.stdout.write(data)
+    if live_out_buf:
+        # we may get bytes when buffer is reached and we are in the middle of an unicode
+        # sequence. In this case we buffer it, and print it when it's complete
+        if isinstance(data, str):
+            data = b''.join(live_out_buf).decode() + data
+            live_out_buf.clear()
+        else:
+            live_out_buf.append(data)
+            return
+    try:
+        sys.stdout.write(data)
+    except TypeError:
+        live_out_buf.append(data)
+        return
     sys.stdout.flush()
     report_buffer.write(data)
 
 
 def live_err(data):
-    sys.stderr.write(data)
+    if live_err_buf:
+        if isinstance(data, str):
+            data = b''.join(live_err_buf).decode() + data
+            live_err_buf.clear()
+        else:
+            live_err_buf.append(data)
+            return
+    try:
+        sys.stderr.write(data)
+    except TypeError:
+        live_err_buf.append(data)
+        return
     sys.stderr.flush()
     report_buffer.write(data)
 
@@ -82,9 +108,24 @@
 
 def set_env(override, name, value="1"):
     """Set environement variable"""
-    environment = override["services"]["sat"].setdefault("environment", {})
+    environment = override["services"]["backend"].setdefault("environment", {})
     environment[name] = value
 
+def write_report_log(path, log_raw, with_ansi=False):
+    log_raw = str(log_raw)
+    if with_ansi:
+        # we save 2 versions: one with ANSI escape codes
+        report_ansi = path.with_suffix(".ansi")
+        with report_ansi.open('w') as f:
+            f.write(log_raw)
+        # and one cleaner, without them
+        report_log = path.with_suffix(".log")
+        with report_log.open('w') as f:
+            f.write(regex.RE_ANSI_REMOVE.sub('', log_raw))
+    else:
+        report_log = path.with_suffix(".log")
+        with report_log.open('w') as f:
+            f.write(log_raw)
 
 def use_e2e_env():
     visual = get_opt(OPT_VISUAL)
@@ -105,7 +146,7 @@
         package_path = p / "sat"
         docker_path = p / "docker"
         if package_path.is_dir() and docker_path.is_dir():
-            sat_root_path = p
+            backend_root_path = p
             break
     else:
         raise exceptions.NotFound(
@@ -113,14 +154,14 @@
             "from the backend repository?"
         )
 
-    libervia_path = Path(libervia.__file__).parent.resolve()
-    libervia_root_path = libervia_path.parent
-    if (libervia_root_path / ".hg").is_dir():
-        libervia_source = libervia_root_path
-        libervia_target = "/src/libervia"
+    libervia_web_path = Path(libervia.__file__).parent.resolve()
+    libervia_web_root_path = libervia_web_path.parent
+    if (libervia_web_root_path / ".hg").is_dir():
+        libervia_web_source = libervia_web_root_path
+        libervia_web_target = "/src/libervia"
     else:
-        libervia_source = libervia_path
-        libervia_target = "/src/libervia/libervia"
+        libervia_web_source = libervia_web_path
+        libervia_web_target = "/src/libervia/libervia"
 
     sat_templates_path = Path(sat_templates.__file__).parent.resolve()
     sat_templates_root_path = sat_templates_path.parent
@@ -131,9 +172,9 @@
         sat_templates_source = sat_templates_path
         sat_templates_target = "/src/sat_templates/sat_templates"
 
-    compose_e2e_path = docker_path / "docker-compose_e2e.yml"
+    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')
+        raise exceptions.NotFound('"docker-compose-e2e.yml" file can\'t be found')
 
     with tempfile.TemporaryDirectory(prefix="sat_test_e2e_") as temp_dir:
         override_path = Path(temp_dir) / "test_override.yml"
@@ -141,21 +182,21 @@
             dedent(f"""\
                 version: "3.6"
                 services:
-                  sat:
+                  backend:
                     volumes:
                       - type: bind
-                        source: {sat_root_path}
+                        source: {backend_root_path}
                         target: /src/sat
                         read_only: true
-                  libervia:
+                  web:
                     volumes:
                       - type: bind
-                        source: {sat_root_path}
+                        source: {backend_root_path}
                         target: /src/sat
                         read_only: true
                       - type: bind
-                        source: {libervia_source}
-                        target: {libervia_target}
+                        source: {libervia_web_source}
+                        target: {libervia_web_target}
                         read_only: true
                       - type: bind
                         source: {sat_templates_source}
@@ -167,13 +208,13 @@
         )
 
         if keep_profiles:
-            set_env(override, "SAT_TEST_E2E_KEEP_PROFILES")
+            set_env(override, "LIBERVIA_TEST_E2E_KEEP_PROFILES")
 
         if visual:
-            set_env(override, "SAT_TEST_E2E_LIBERVIA_NO_HEADLESS")
+            set_env(override, "LIBERVIA_TEST_E2E_WEB_NO_HEADLESS")
 
         if keep_browser:
-            set_env(override, "SAT_TEST_E2E_LIBERVIA_KEEP_BROWSER")
+            set_env(override, "LIBERVIA_TEST_E2E_WEB_KEEP_BROWSER")
 
         with override_path.open("w") as f:
             yaml.dump(override, f, Dumper=Dumper)
@@ -183,13 +224,13 @@
         docker_compose.up("-d")
 
         p = docker_compose.exec(
-            "-T", "--workdir", "/src/sat/tests", "sat",
+            "-T", "--workdir", "/src/sat/tests", "backend",
             "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,
             _bg=True
         )
         if visual:
-            vnc_port = docker_compose.port("sat", "5900").split(':', 1)[1].strip()
+            vnc_port = docker_compose.port("backend", "5900").split(':', 1)[1].strip()
             p_vnc = sh.vncviewer(
                 f"localhost:{vnc_port}",
                 _bg=True,
@@ -204,22 +245,25 @@
         try:
             p.wait()
         except sh.ErrorReturnCode as e:
-            sat_cont_id = docker_compose.ps("-q", "sat").strip()
-            report_dest = Path(f"reports_{datetime.now().isoformat()}/")
+            libervia_cont_id = docker_compose.ps("-q", "backend").strip()
+            report_dest = Path(f"report_{datetime.now().isoformat()}/")
             # we need to make `report_dest` explicitely local with "./", otherwise
             # docker parse takes it as a container path due to the presence of ":"
             # with `isoformat()`.
-            sh.docker.cp(f"{sat_cont_id}:/reports", f"./{report_dest}")
-            # we save 2 versions: one with ANSI escape codes
-            report_ansi = report_dest / "report.ansi"
-            with report_ansi.open('w') as f:
-                f.write(report_buffer.getvalue())
-            # and one without (cf. https://stackoverflow.com/a/14693789)
-            ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
-            report_log = report_dest / "report.log"
-            with report_log.open('w') as f:
-                f.write(ansi_escape.sub('', report_buffer.getvalue()))
-
+            sh.docker.cp(f"{libervia_cont_id}:/reports", f"./{report_dest}")
+            write_report_log(
+                report_dest/"report",
+                report_buffer.getvalue(),
+                with_ansi=True
+            )
+            write_report_log(
+                report_dest/"backend",
+                docker_compose.logs("--no-log-prefix", "backend")
+            )
+            write_report_log(
+                report_dest/"web",
+                docker_compose.logs("--no-log-prefix", "web")
+            )
             print(f"report saved to {report_dest}")
             sys.exit(e.exit_code)
         finally: