|
| 1 | +from os import getenv |
| 2 | +import requests |
| 3 | +import pytest |
| 4 | +import time |
| 5 | +from uuid import uuid4 |
| 6 | + |
| 7 | + |
| 8 | +TIMEOUT = 120 |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope="session") |
| 12 | +def api_request(): |
| 13 | + def _api_request(method, path, **kwargs): |
| 14 | + hub_url = getenv("HUB_URL", "http://localhost:8000").rstrip("/") |
| 15 | + m = getattr(requests, method) |
| 16 | + url = f"{hub_url}{path}" |
| 17 | + r = m(url, headers={"Authorization": "token test-token-123"}, **kwargs) |
| 18 | + r.raise_for_status() |
| 19 | + return r |
| 20 | + |
| 21 | + return _api_request |
| 22 | + |
| 23 | + |
| 24 | +def wait_for_hub(api_request): |
| 25 | + # Wait for the hub to be ready |
| 26 | + now = time.time() |
| 27 | + try: |
| 28 | + api_request("get", "/hub/api") |
| 29 | + except requests.exceptions.RequestException: |
| 30 | + if time.time() - now > TIMEOUT: |
| 31 | + raise TimeoutError(f"Hub did not start in {TIMEOUT} seconds") |
| 32 | + time.sleep(5) |
| 33 | + |
| 34 | + |
| 35 | +def test_create_user_and_server(api_request): |
| 36 | + wait_for_hub(api_request) |
| 37 | + |
| 38 | + # Create a new user |
| 39 | + username = str(uuid4()) |
| 40 | + api_request("post", "/hub/api/users", json={"usernames": [username]}) |
| 41 | + |
| 42 | + # Start a server for the user |
| 43 | + api_request("post", f"/hub/api/users/{username}/server") |
| 44 | + |
| 45 | + # Wait for the server |
| 46 | + ready = False |
| 47 | + now = time.time() |
| 48 | + while not ready: |
| 49 | + wait_r = api_request("get", f"/hub/api/users/{username}").json() |
| 50 | + if wait_r["servers"][""]["ready"]: |
| 51 | + ready = True |
| 52 | + break |
| 53 | + if time.time() - now > TIMEOUT: |
| 54 | + raise TimeoutError(f"Singleuser server did not start in {TIMEOUT} seconds") |
| 55 | + time.sleep(5) |
| 56 | + |
| 57 | + # Call the jupyter-server API |
| 58 | + server_r = api_request("get", f"/user/{username}/api").json() |
| 59 | + assert "version" in server_r |
| 60 | + contents_r = api_request("get", f"/user/{username}/api/contents").json() |
| 61 | + assert "content" in contents_r |
0 commit comments