|
6 | 6 | import sys |
7 | 7 | import tempfile |
8 | 8 | import time |
9 | | -import traceback |
10 | 9 | from contextlib import contextmanager |
11 | 10 | from multiprocessing import Process |
12 | 11 | from subprocess import Popen |
|
38 | 37 |
|
39 | 38 |
|
40 | 39 | def _on_error_callback(ws_app, *_): |
41 | | - print(traceback.format_exc()) |
42 | 40 | ws_app.close() |
43 | 41 |
|
44 | 42 |
|
@@ -210,7 +208,9 @@ def run_cli(args) -> Generator: |
210 | 208 |
|
211 | 209 | @requires("playwright") |
212 | 210 | @contextmanager |
213 | | -def run_app_in_cloud(app_folder: str, app_name: str = "app.py", extra_args: [str] = []) -> Generator: |
| 211 | +def run_app_in_cloud( |
| 212 | + app_folder: str, app_name: str = "app.py", extra_args: List[str] = [], debug: bool = True |
| 213 | +) -> Generator: |
214 | 214 | """This utility is used to automate testing e2e application with lightning_app.ai.""" |
215 | 215 | # 1. Validate the provide app_folder is correct. |
216 | 216 | if not os.path.exists(os.path.join(app_folder, "app.py")): |
@@ -239,7 +239,8 @@ def run_app_in_cloud(app_folder: str, app_name: str = "app.py", extra_args: [str |
239 | 239 | with tempfile.TemporaryDirectory() as tmpdir: |
240 | 240 | env_copy = os.environ.copy() |
241 | 241 | env_copy["PACKAGE_LIGHTNING"] = "1" |
242 | | - env_copy["LIGHTNING_DEBUG"] = "1" |
| 242 | + if debug: |
| 243 | + env_copy["LIGHTNING_DEBUG"] = "1" |
243 | 244 | shutil.copytree(app_folder, tmpdir, dirs_exist_ok=True) |
244 | 245 | # TODO - add -no-cache to the command line. |
245 | 246 | process = Popen( |
@@ -308,7 +309,7 @@ def run_app_in_cloud(app_folder: str, app_name: str = "app.py", extra_args: [str |
308 | 309 | """, |
309 | 310 | [LIGHTNING_CLOUD_PROJECT_ID], |
310 | 311 | ) |
311 | | - admin_page.goto(f"{Config.url}/{Config.username}/apps") |
| 312 | + admin_page.goto(f"{Config.url}/{Config.username}/apps", timeout=60 * 1000) |
312 | 313 |
|
313 | 314 | # Closing the Complete your profile dialog |
314 | 315 | try: |
@@ -339,15 +340,6 @@ def run_app_in_cloud(app_folder: str, app_name: str = "app.py", extra_args: [str |
339 | 340 | print("'Create Project' dialog not visible, skipping.") |
340 | 341 |
|
341 | 342 | admin_page.locator(f"text={name}").click() |
342 | | - admin_page.evaluate( |
343 | | - """data => { |
344 | | - window.localStorage.setItem('gridUserId', data[0]); |
345 | | - window.localStorage.setItem('gridUserKey', data[1]); |
346 | | - window.localStorage.setItem('gridUserToken', data[2]); |
347 | | - } |
348 | | - """, |
349 | | - [Config.id, Config.key, token], |
350 | | - ) |
351 | 343 | sleep(5) |
352 | 344 | # Scroll to the bottom of the page. Used to capture all logs. |
353 | 345 | admin_page.evaluate( |
@@ -376,8 +368,9 @@ def run_app_in_cloud(app_folder: str, app_name: str = "app.py", extra_args: [str |
376 | 368 | assert len(lightning_apps) == 1 |
377 | 369 | app_id = lightning_apps[0].id |
378 | 370 |
|
379 | | - process = Process(target=print_logs, kwargs={"app_id": app_id}) |
380 | | - process.start() |
| 371 | + if debug: |
| 372 | + process = Process(target=print_logs, kwargs={"app_id": app_id}) |
| 373 | + process.start() |
381 | 374 |
|
382 | 375 | while True: |
383 | 376 | try: |
@@ -418,40 +411,11 @@ def fetch_logs(component_names: Optional[List[str]] = None) -> Generator: |
418 | 411 | except KeyboardInterrupt: |
419 | 412 | pass |
420 | 413 | finally: |
421 | | - has_finished = False |
422 | | - while not has_finished: |
423 | | - try: |
424 | | - button = admin_page.locator('[data-cy="stop"]') |
425 | | - try: |
426 | | - button.wait_for(timeout=3 * 1000) |
427 | | - button.click() |
428 | | - except (playwright._impl._api_types.Error, playwright._impl._api_types.TimeoutError): |
429 | | - pass |
430 | | - context.close() |
431 | | - browser.close() |
432 | | - |
433 | | - list_lightningapps = client.lightningapp_instance_service_list_lightningapp_instances( |
434 | | - project.project_id |
435 | | - ) |
436 | | - |
437 | | - for lightningapp in list_lightningapps.lightningapps: |
438 | | - if lightningapp.name != name: |
439 | | - continue |
440 | | - try: |
441 | | - res = client.lightningapp_instance_service_delete_lightningapp_instance( |
442 | | - project_id=project.project_id, |
443 | | - id=lightningapp.id, |
444 | | - ) |
445 | | - assert res == {} |
446 | | - except ApiException as e: |
447 | | - print(f"Failed to delete {lightningapp.name}. Exception {e}") |
448 | | - |
449 | | - process.kill() |
450 | | - has_finished = True |
451 | | - except Exception: |
452 | | - pass |
453 | | - |
454 | | - Popen("lightning disconnect", shell=True).wait() |
| 414 | + if debug: |
| 415 | + process.kill() |
| 416 | + |
| 417 | + context.close() |
| 418 | + browser.close() |
455 | 419 |
|
456 | 420 |
|
457 | 421 | def wait_for(page, callback: Callable, *args, **kwargs) -> Any: |
|
0 commit comments