Skip to content

Commit 9f5075f

Browse files
committed
[#1949] Reset mechanism for ui tests
1 parent 9718ede commit 9f5075f

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

tests/ui/playwright/tests_poc/conftest.py

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,55 +87,92 @@ def _wait_http_ok(url: str, timeout: float = 90.0) -> None:
8787
raise RuntimeError(f"Timeout waiting for {url}")
8888

8989

90+
def _reset_db_and_server(base_url: str) -> None:
91+
"""Drop & recreate DB schema, restart server, wait until healthy."""
92+
pg = f"{PROJECT_NAME}-postgres-1"
93+
srv = f"{PROJECT_NAME}-server-1"
94+
95+
sql = "DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;"
96+
subprocess.run(
97+
[
98+
"docker",
99+
"exec",
100+
"-i",
101+
pg,
102+
"psql",
103+
"-U",
104+
"stork",
105+
"-d",
106+
"stork",
107+
"-v",
108+
"ON_ERROR_STOP=1",
109+
"-c",
110+
sql,
111+
],
112+
check=True,
113+
text=True,
114+
)
115+
116+
subprocess.run(["docker", "restart", srv], check=True, text=True)
117+
118+
_wait_http_ok(f"{base_url}/api/version", timeout=120)
119+
120+
try:
121+
_dc_cmd("run", "--no-deps", "register", "register", "--non-interactive")
122+
except Exception:
123+
pass
124+
125+
90126
# --- pytest fixtures ---------------------------------------------------------
91127

92128

93129
@pytest.fixture(scope="session")
94-
def stork_base_url() -> str:
130+
def setup() -> None:
95131
"""
96132
SAME environment as system tests, with UI assets enabled via override file.
97133
98134
Workflow:
99135
- If STORK_REUSE=1: just wait for health (reuse an already-running stack).
100136
- Else: build and start postgres, server, agent-kea; then try registering.
137+
138+
Note: This fixture performs environment setup once per test session.
139+
Tests should use BASE_URL directly for the URL; this fixture returns nothing.
101140
"""
102-
# env used by system compose & for Apple Silicon
141+
103142
os.environ.setdefault("IPWD", str(ROOT))
104143
os.environ.setdefault("DOCKER_DEFAULT_PLATFORM", "linux/amd64")
105144

106145
if os.getenv("STORK_REUSE") == "1":
107146
_wait_http_ok(f"{BASE_URL}/api/version", timeout=120)
108-
return BASE_URL
147+
return
109148

110-
# Fresh stack
111149
_hard_cleanup()
112150

113-
# Build only what we need, then bring them up
114151
_dc_cmd("build", "--", "postgres", "server", "agent-kea")
115152
_dc_cmd("up", "-d", "--", "postgres")
116-
_dc_cmd(
117-
"up", "-d", "--", "server"
118-
) # NOTE: service name is 'server' (overridden to target server-ui)
153+
_dc_cmd("up", "-d", "--", "server")
119154
_dc_cmd("up", "-d", "--", "agent-kea")
120155

121-
# Wait until API responds (verifies port mapping + UI-enabled image)
122156
_wait_http_ok(f"{BASE_URL}/api/version", timeout=120)
123157

124-
# Register the agent (non-fatal for UI flows)
125158
try:
126159
_dc_cmd("run", "--no-deps", "register", "register", "--non-interactive")
127160
except subprocess.CalledProcessError as e:
128161
print("WARN: 'register' helper failed; continuing for UI tests.\n", e)
129162

130-
return BASE_URL
131-
132163

133164
@pytest.fixture(scope="function")
134-
def logged_in_page(page: Page, stork_base_url: str):
165+
def logged_in_page(page: Page, setup):
135166
"""Open login and authenticate with seeded admin credentials."""
136-
from tests.ui.playwright.pages.login_page import LoginPage # lazy import
167+
from tests.ui.playwright.pages.login_page import LoginPage
137168

138169
lp = LoginPage(page)
139-
lp.open(stork_base_url)
170+
lp.open(BASE_URL)
140171
lp.login("admin", "admin")
141172
return page
173+
174+
175+
@pytest.fixture(autouse=True, scope="function")
176+
def clean_before_each_test(setup):
177+
"""Ensure a clean environment before every test."""
178+
_reset_db_and_server(BASE_URL)

0 commit comments

Comments
 (0)