Skip to content

Commit fb4e453

Browse files
authored
ci: Configure numtracker as part of compose (#1641)
Configure numtracker as part of compose services
1 parent 619c918 commit fb4e453

4 files changed

Lines changed: 52 additions & 14 deletions

File tree

‎justfile‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ RUNNER := "docker"
44
compose +ARGS="up -d":
55
{{ RUNNER }} compose -f tests/system_tests/compose.yaml {{ARGS}}
66

7-
configure-adsim: (compose "exec" "numtracker" "/app/numtracker" "client" "configure" "adsim"
8-
"--directory" '/tmp/'
9-
"--scan" '{instrument}-{scan_number}'
10-
"--detector" '{instrument}-{scan_number}-{detector}'
11-
"--number" "43")
12-
13-
services: compose configure-adsim
14-
157
serve *OPTS:
168
#!/usr/bin/env bash
179
source tests/system_tests/.env

‎tests/system_tests/compose.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ services:
5656
- KC_BOOTSTRAP_ADMIN_PASSWORD=admin
5757
- KC_BOOTSTRAP_ADMIN_USERNAME=admin
5858
- KEYCLOAK_REALM=master
59+
- NT_URL=http://localhost:8406/graphql
5960
command: ["uv", "run", "/startup.py"]
6061
depends_on:
6162
keycloak:

‎tests/system_tests/services/startup.py‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# requires-python = ">=3.11"
33
# dependencies = [
44
# "mantelo==2.2.1",
5+
# "requests"
56
# ]
67
# ///
78
"""Configure the local Keycloak instance used by the system tests."""
@@ -11,12 +12,14 @@
1112
from functools import wraps
1213
from typing import Any
1314

15+
import requests
1416
from mantelo import KeycloakAdmin
1517

16-
SERVER = os.environ.get("KEYCLOAK_SERVER")
17-
REALM = os.environ.get("KEYCLOAK_REALM")
18-
ADMIN_USERNAME = os.environ.get("KC_BOOTSTRAP_ADMIN_USERNAME")
19-
ADMIN_PASSWORD = os.environ.get("KC_BOOTSTRAP_ADMIN_PASSWORD")
18+
SERVER = os.environ.get("KEYCLOAK_SERVER", "http://localhost:8081")
19+
REALM = os.environ.get("KEYCLOAK_REALM", "master")
20+
ADMIN_USERNAME = os.environ.get("KC_BOOTSTRAP_ADMIN_USERNAME", "admin")
21+
ADMIN_PASSWORD = os.environ.get("KC_BOOTSTRAP_ADMIN_PASSWORD", "admin")
22+
NT_URL = os.environ.get("NT_URL", "http://localhost:8406/graphql")
2023

2124
USERS = {"alice": "alice", "bob": "bob"}
2225

@@ -138,6 +141,9 @@ def cleanup_components() -> None:
138141

139142
def create_users() -> None:
140143
for username, password in USERS.items():
144+
if admin.users.get(username=username):
145+
print(f">> Skipping {username} (exists)")
146+
continue
141147
response, _ = admin.users.as_raw().post({"username": username, "enabled": True})
142148
user_id = response.headers["Location"].rsplit("/", 1)[-1]
143149
admin.users(user_id).reset_password.put(
@@ -229,6 +235,7 @@ def create_user_service_account_client(
229235
def create_clients() -> None:
230236
create_cli_client(client_id="ixx-cli-blueapi", aud="ixx-blueapi")
231237
create_cli_client(client_id="tiled-cli", aud="tiled")
238+
create_cli_client(client_id="numtracker", aud="numtracker")
232239
create_web_client(
233240
client_id="ixx-blueapi",
234241
aud="ixx-blueapi",
@@ -254,7 +261,42 @@ def create_clients() -> None:
254261
)
255262

256263

264+
def configure_numtracker():
265+
token_url = SERVER + "/realms/master/protocol/openid-connect/token"
266+
response = requests.post(
267+
token_url,
268+
data={
269+
"client_id": "system-test-blueapi-admin",
270+
"client_secret": "secret",
271+
"grant_type": "client_credentials",
272+
},
273+
)
274+
response.raise_for_status()
275+
access_token = response.json().get("access_token")
276+
277+
response = requests.post(
278+
NT_URL,
279+
json={
280+
"query": """mutation {
281+
configure(instrument: "adsim",
282+
config: {directory: "/tmp/",
283+
scan: "{instrument}-{scan_number}",
284+
detector: "{instrument}-{scan_number}-{detector}",
285+
scanNumber: 43}) {
286+
scanTemplate
287+
}
288+
}"""
289+
},
290+
headers={"authorization": "Bearer " + access_token},
291+
)
292+
response.raise_for_status()
293+
if response.json().get("errors") is not None:
294+
raise Exception(response.json())
295+
print(">> Configurated numtracker")
296+
297+
257298
if __name__ == "__main__":
258299
cleanup_components()
259300
create_users()
260301
create_clients()
302+
configure_numtracker()

‎tests/system_tests/test_blueapi_system.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def clean_existing_tasks(rest_client: BlueapiRestClient):
271271
def reset_numtracker():
272272
server_config = load_config(Path(_DATA_PATH, "config.yaml"))
273273
nt_url = server_config.numtracker.url # type: ignore - if numtracker is None we should fail
274-
requests.post(
274+
response = requests.post(
275275
str(nt_url),
276276
json={
277277
"query": f"""mutation {{
@@ -285,7 +285,10 @@ def reset_numtracker():
285285
}}"""
286286
},
287287
headers={"authorization": "Bearer " + get_access_token(User.admin)},
288-
).raise_for_status()
288+
)
289+
response.raise_for_status()
290+
if response.json().get("errors") is not None:
291+
raise Exception(response.json())
289292
yield
290293

291294

0 commit comments

Comments
 (0)