Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
3414:ffe7a6d6018a | 3415:814e118d9ef3 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 # SàT: an XMPP client | |
4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 """Run end-to-end tests in appropriate Docker environment""" | |
20 | |
21 import sys | |
22 from pathlib import Path | |
23 import tempfile | |
24 from textwrap import dedent | |
25 import sh | |
26 from sat.core import exceptions | |
27 | |
28 KEEP_OPT = "--keep" | |
29 | |
30 | |
31 def live_out(data): | |
32 sys.stdout.write(data) | |
33 sys.stdout.flush() | |
34 | |
35 | |
36 def live_err(data): | |
37 sys.stderr.write(data) | |
38 sys.stderr.flush() | |
39 | |
40 | |
41 def use_e2e_env(): | |
42 if KEEP_OPT in sys.argv: | |
43 keep_containers = True | |
44 sys.argv.remove(KEEP_OPT) | |
45 else: | |
46 keep_containers = False | |
47 for p in Path.cwd().parents: | |
48 package_path = p / "sat" | |
49 docker_path = p / "docker" | |
50 if package_path.is_dir() and docker_path.is_dir(): | |
51 sat_root_path = p | |
52 break | |
53 else: | |
54 raise exceptions.NotFound( | |
55 "Can't find root of SàT code, are you sure that you are running the test " | |
56 "from the backend repository?" | |
57 ) | |
58 | |
59 compose_e2e_path = docker_path / "docker-compose_e2e.yml" | |
60 if not compose_e2e_path.is_file(): | |
61 raise exceptions.NotFound('"docker-compose_e2e.yml" file can\'t be found') | |
62 | |
63 with tempfile.TemporaryDirectory() as temp_dir: | |
64 override_path = Path(temp_dir) / "test_override.yml" | |
65 with override_path.open("w") as f: | |
66 f.write(dedent(f"""\ | |
67 version: "3.6" | |
68 services: | |
69 sat: | |
70 volumes: | |
71 - type: bind | |
72 source: {sat_root_path} | |
73 target: /src/sat | |
74 read_only: true | |
75 """)) | |
76 | |
77 docker_compose = sh.docker_compose.bake( | |
78 "-f", compose_e2e_path, "-f", override_path) | |
79 docker_compose.up("-d") | |
80 | |
81 try: | |
82 docker_compose.exec( | |
83 "-T", "--workdir", "/src/sat/tests", "sat", | |
84 "pytest", "-o", "cache_dir=/tmp", *sys.argv[1:], color="yes", | |
85 _in=sys.stdin, _out=live_out, _out_bufsize=0, _err=live_err, _err_bufsize=0 | |
86 ) | |
87 except sh.ErrorReturnCode as e: | |
88 sys.exit(e.exit_code) | |
89 finally: | |
90 if not keep_containers: | |
91 docker_compose.down(volumes=True) | |
92 | |
93 if __name__ == "__main__": | |
94 use_e2e_env() |