e2e.py raw

   1  #!/usr/bin/env python3
   2  """Playwright smoke test for smesh multi-SW bus connectivity."""
   3  
   4  import sys
   5  import time
   6  from playwright.sync_api import sync_playwright
   7  
   8  TEST_SK = "328615b6c92aa6527fc175a67670222daabc69fa2b84c2ded5f6907f78f2b0f8"
   9  TEST_PK = "5dbeb1d7e84d0a4fb3fac47868438dc9135b35f25e27ae933e306e3584bf69a8"
  10  BASE = "http://smesh.test:8090"
  11  LOG_FILE = "/tmp/browser-debug.log"
  12  
  13  
  14  def main():
  15      with sync_playwright() as p:
  16          browser = p.chromium.launch(
  17              headless=True,
  18              args=[
  19                  f"--unsafely-treat-insecure-origin-as-secure="
  20                  f"{BASE},"
  21                  f"http://marmot.smesh.test:8090,"
  22                  f"http://relay.smesh.test:8090",
  23              ],
  24          )
  25          ctx = browser.new_context()
  26          page = ctx.new_page()
  27  
  28          logs = []
  29          page.on("console", lambda m: logs.append(f"{m.type}: {m.text}"))
  30  
  31          # Inject key into localStorage before page JS runs.
  32          page.add_init_script(f"""
  33          localStorage.setItem('smesh-key', '{TEST_SK}');
  34          localStorage.setItem('smesh-pubkey', '{TEST_PK}');
  35          localStorage.setItem('smesh-mode', 'nsec');
  36          """)
  37  
  38          page.goto(BASE, wait_until="networkidle", timeout=30000)
  39          time.sleep(8)  # SWs register, bus connects, events flow
  40  
  41          # Read server-side log for SW bus connections.
  42          try:
  43              with open(LOG_FILE) as f:
  44                  server_logs = f.read()
  45          except FileNotFoundError:
  46              server_logs = ""
  47  
  48          checks = {
  49              "page_loaded": page.title() != "",
  50              "app_render": page.evaluate(
  51                  "document.querySelector('#app-root') !== null"
  52              ),
  53              "shell_bus": "bus connected" in server_logs
  54              and "[shell]" in server_logs,
  55              "relay_bus": "[relay]" in server_logs
  56              and "bus connected" in server_logs,
  57              "marmot_bus": "[marmot]" in server_logs
  58              and "bus connected" in server_logs,
  59              "no_errors": not any(
  60                  "error" in l.lower()
  61                  and "sw-diag" not in l
  62                  and "favicon" not in l.lower()
  63                  for l in logs
  64              ),
  65          }
  66  
  67          ok = True
  68          for name, passed in checks.items():
  69              status = "PASS" if passed else "FAIL"
  70              print(f"  {status}: {name}")
  71              if not passed:
  72                  ok = False
  73  
  74          if not ok:
  75              print("\n--- PAGE CONSOLE LOGS ---")
  76              for l in logs:
  77                  print(f"  {l}")
  78              if server_logs:
  79                  print("\n--- SERVER LOGS (last 40 lines) ---")
  80                  for l in server_logs.strip().split("\n")[-40:]:
  81                      print(f"  {l}")
  82  
  83          browser.close()
  84          sys.exit(0 if ok else 1)
  85  
  86  
  87  if __name__ == "__main__":
  88      main()
  89