Mercurial > libervia-backend
comparison tests/e2e/run_e2e.py @ 4305:4cd4922de876
tests: reformat tests using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 26 Sep 2024 16:11:56 +0200 |
parents | dfccc90cacc6 |
children |
comparison
equal
deleted
inserted
replaced
4304:92a886f31581 | 4305:4cd4922de876 |
---|---|
28 import sat_templates | 28 import sat_templates |
29 from libervia import web | 29 from libervia import web |
30 from libervia.backend.core import exceptions | 30 from libervia.backend.core import exceptions |
31 from libervia.backend.tools.common import regex | 31 from libervia.backend.tools.common import regex |
32 import yaml | 32 import yaml |
33 | |
33 try: | 34 try: |
34 from yaml import CLoader as Loader, CDumper as Dumper | 35 from yaml import CLoader as Loader, CDumper as Dumper |
35 except ImportError: | 36 except ImportError: |
36 from yaml import Loader, Dumper | 37 from yaml import Loader, Dumper |
37 | 38 |
41 OPT_KEEP_VNC = "--keep-vnc" | 42 OPT_KEEP_VNC = "--keep-vnc" |
42 OPT_KEEP_BROWSER = "--keep-browser" | 43 OPT_KEEP_BROWSER = "--keep-browser" |
43 OPT_VISUAL = "--visual" | 44 OPT_VISUAL = "--visual" |
44 OPT_DEV_MODE = "--dev-mode" | 45 OPT_DEV_MODE = "--dev-mode" |
45 | 46 |
46 dev_mode_inst = dedent("""\ | 47 dev_mode_inst = dedent( |
48 """\ | |
47 Here is a short script to start working with a logged account: | 49 Here is a short script to start working with a logged account: |
48 | 50 |
49 from helium import * | 51 from helium import * |
50 start_firefox() | 52 start_firefox() |
51 go_to("https://libervia-web.test:8443/login") | 53 go_to("https://libervia-web.test:8443/login") |
52 write("account1", "login") | 54 write("account1", "login") |
53 write("test", "password") | 55 write("test", "password") |
54 click("log in") | 56 click("log in") |
55 """) | 57 """ |
58 ) | |
56 report_buffer = io.StringIO() | 59 report_buffer = io.StringIO() |
57 live_out_buf = [] | 60 live_out_buf = [] |
58 live_err_buf = [] | 61 live_err_buf = [] |
59 | 62 |
60 | 63 |
61 def live_out(data): | 64 def live_out(data): |
62 if live_out_buf: | 65 if live_out_buf: |
63 # we may get bytes when buffer is reached and we are in the middle of an unicode | 66 # we may get bytes when buffer is reached and we are in the middle of an unicode |
64 # sequence. In this case we buffer it, and print it when it's complete | 67 # sequence. In this case we buffer it, and print it when it's complete |
65 if isinstance(data, str): | 68 if isinstance(data, str): |
66 data = b''.join(live_out_buf).decode() + data | 69 data = b"".join(live_out_buf).decode() + data |
67 live_out_buf.clear() | 70 live_out_buf.clear() |
68 else: | 71 else: |
69 live_out_buf.append(data) | 72 live_out_buf.append(data) |
70 return | 73 return |
71 try: | 74 try: |
78 | 81 |
79 | 82 |
80 def live_err(data): | 83 def live_err(data): |
81 if live_err_buf: | 84 if live_err_buf: |
82 if isinstance(data, str): | 85 if isinstance(data, str): |
83 data = b''.join(live_err_buf).decode() + data | 86 data = b"".join(live_err_buf).decode() + data |
84 live_err_buf.clear() | 87 live_err_buf.clear() |
85 else: | 88 else: |
86 live_err_buf.append(data) | 89 live_err_buf.append(data) |
87 return | 90 return |
88 try: | 91 try: |
109 def set_env(override, name, value="1"): | 112 def set_env(override, name, value="1"): |
110 """Set environement variable""" | 113 """Set environement variable""" |
111 environment = override["services"]["backend"].setdefault("environment", {}) | 114 environment = override["services"]["backend"].setdefault("environment", {}) |
112 environment[name] = value | 115 environment[name] = value |
113 | 116 |
117 | |
114 def write_report_log(path, log_raw, with_ansi=False): | 118 def write_report_log(path, log_raw, with_ansi=False): |
115 log_raw = str(log_raw) | 119 log_raw = str(log_raw) |
116 if with_ansi: | 120 if with_ansi: |
117 # we save 2 versions: one with ANSI escape codes | 121 # we save 2 versions: one with ANSI escape codes |
118 report_ansi = path.with_suffix(".ansi") | 122 report_ansi = path.with_suffix(".ansi") |
119 with report_ansi.open('w') as f: | 123 with report_ansi.open("w") as f: |
120 f.write(log_raw) | 124 f.write(log_raw) |
121 # and one cleaner, without them | 125 # and one cleaner, without them |
122 report_log = path.with_suffix(".log") | 126 report_log = path.with_suffix(".log") |
123 with report_log.open('w') as f: | 127 with report_log.open("w") as f: |
124 f.write(regex.RE_ANSI_REMOVE.sub('', log_raw)) | 128 f.write(regex.RE_ANSI_REMOVE.sub("", log_raw)) |
125 else: | 129 else: |
126 report_log = path.with_suffix(".log") | 130 report_log = path.with_suffix(".log") |
127 with report_log.open('w') as f: | 131 with report_log.open("w") as f: |
128 f.write(log_raw) | 132 f.write(log_raw) |
133 | |
129 | 134 |
130 def use_e2e_env(): | 135 def use_e2e_env(): |
131 rev = os.getenv("DOCKER_LIBERVIA_REV", "dev") | 136 rev = os.getenv("DOCKER_LIBERVIA_REV", "dev") |
132 print(f"Running tests for Libervia {rev}") | 137 print(f"Running tests for Libervia {rev}") |
133 visual = get_opt(OPT_VISUAL) | 138 visual = get_opt(OPT_VISUAL) |
179 raise exceptions.NotFound('"docker-compose-e2e.yml" file can\'t be found') | 184 raise exceptions.NotFound('"docker-compose-e2e.yml" file can\'t be found') |
180 | 185 |
181 with tempfile.TemporaryDirectory(prefix="libervia_test_e2e_") as temp_dir: | 186 with tempfile.TemporaryDirectory(prefix="libervia_test_e2e_") as temp_dir: |
182 override_path = Path(temp_dir) / "test_override.yml" | 187 override_path = Path(temp_dir) / "test_override.yml" |
183 override = yaml.load( | 188 override = yaml.load( |
184 dedent(f"""\ | 189 dedent( |
190 f"""\ | |
185 version: "3.6" | 191 version: "3.6" |
186 services: | 192 services: |
187 backend: | 193 backend: |
188 volumes: | 194 volumes: |
189 - type: bind | 195 - type: bind |
203 - type: bind | 209 - type: bind |
204 source: {libervia_templates_source} | 210 source: {libervia_templates_source} |
205 target: {libervia_templates_target} | 211 target: {libervia_templates_target} |
206 read_only: true | 212 read_only: true |
207 """ | 213 """ |
208 ), | 214 ), |
209 Loader=Loader | 215 Loader=Loader, |
210 ) | 216 ) |
211 | 217 |
212 if keep_profiles: | 218 if keep_profiles: |
213 set_env(override, "LIBERVIA_TEST_E2E_KEEP_PROFILES") | 219 set_env(override, "LIBERVIA_TEST_E2E_KEEP_PROFILES") |
214 | 220 |
220 | 226 |
221 with override_path.open("w") as f: | 227 with override_path.open("w") as f: |
222 yaml.dump(override, f, Dumper=Dumper) | 228 yaml.dump(override, f, Dumper=Dumper) |
223 | 229 |
224 docker_compose = sh.docker_compose.bake( | 230 docker_compose = sh.docker_compose.bake( |
225 "-f", compose_e2e_path, "-f", override_path) | 231 "-f", compose_e2e_path, "-f", override_path |
232 ) | |
226 docker_compose.up("-d") | 233 docker_compose.up("-d") |
227 | 234 |
228 p = docker_compose.exec( | 235 p = docker_compose.exec( |
229 "--workdir", "/src/libervia-backend/tests", "backend", | 236 "--workdir", |
230 "pytest", "-o", "cache_dir=/tmp", *sys.argv[1:], color="yes", | 237 "/src/libervia-backend/tests", |
231 _in=sys.stdin, _out=live_out, _out_bufsize=0, _err=live_err, _err_bufsize=0, | 238 "backend", |
232 _bg=True | 239 "pytest", |
240 "-o", | |
241 "cache_dir=/tmp", | |
242 *sys.argv[1:], | |
243 color="yes", | |
244 _in=sys.stdin, | |
245 _out=live_out, | |
246 _out_bufsize=0, | |
247 _err=live_err, | |
248 _err_bufsize=0, | |
249 _bg=True, | |
233 ) | 250 ) |
234 if visual: | 251 if visual: |
235 vnc_port = docker_compose.port("backend", "5900").split(':', 1)[1].strip() | 252 vnc_port = docker_compose.port("backend", "5900").split(":", 1)[1].strip() |
236 p_vnc = sh.vncviewer( | 253 p_vnc = sh.vncviewer( |
237 f"localhost:{vnc_port}", | 254 f"localhost:{vnc_port}", |
238 _bg=True, | 255 _bg=True, |
239 # vncviewer exits with 1 when we send an SIGTERM to it, and it's printed | 256 # vncviewer exits with 1 when we send an SIGTERM to it, and it's printed |
240 # before we can catch it (it's happening in a thread). Thus we set exit | 257 # before we can catch it (it's happening in a thread). Thus we set exit |
241 # code 1 as OK to avoid the backtrace. | 258 # code 1 as OK to avoid the backtrace. |
242 _ok_code=[0, 1] | 259 _ok_code=[0, 1], |
243 ) | 260 ) |
244 else: | 261 else: |
245 p_vnc = None | 262 p_vnc = None |
246 | 263 |
247 try: | 264 try: |
252 # we need to make `report_dest` explicitely local with "./", otherwise | 269 # we need to make `report_dest` explicitely local with "./", otherwise |
253 # docker parse takes it as a container path due to the presence of ":" | 270 # docker parse takes it as a container path due to the presence of ":" |
254 # with `isoformat()`. | 271 # with `isoformat()`. |
255 sh.docker.cp(f"{libervia_cont_id}:/reports", f"./{report_dest}") | 272 sh.docker.cp(f"{libervia_cont_id}:/reports", f"./{report_dest}") |
256 write_report_log( | 273 write_report_log( |
257 report_dest/"report", | 274 report_dest / "report", report_buffer.getvalue(), with_ansi=True |
258 report_buffer.getvalue(), | |
259 with_ansi=True | |
260 ) | 275 ) |
261 write_report_log( | 276 write_report_log( |
262 report_dest/"backend", | 277 report_dest / "backend", docker_compose.logs("--no-log-prefix", "backend") |
263 docker_compose.logs("--no-log-prefix", "backend") | |
264 ) | 278 ) |
265 write_report_log( | 279 write_report_log( |
266 report_dest/"web", | 280 report_dest / "web", docker_compose.logs("--no-log-prefix", "web") |
267 docker_compose.logs("--no-log-prefix", "web") | |
268 ) | 281 ) |
269 print(f"report saved to {report_dest}") | 282 print(f"report saved to {report_dest}") |
270 sys.exit(e.exit_code) | 283 sys.exit(e.exit_code) |
271 finally: | 284 finally: |
272 if p_vnc is not None and p_vnc.is_alive() and not keep_vnc: | 285 if p_vnc is not None and p_vnc.is_alive() and not keep_vnc: |