Skip to content

Commit 1b80d4d

Browse files
committed
Test spawn
1 parent b59ac8e commit 1b80d4d

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ jobs:
2626
steps:
2727
- uses: actions/checkout@v3
2828

29+
- uses: actions/setup-python@v4
30+
2931
- name: Get Docker version
3032
run: |
3133
docker --version
3234
docker-compose --version
3335
34-
- name: Run JupyterHub
35-
run: |
36-
docker-compose up -d
36+
- name: Install pytest
37+
run: pip install pytest
3738

38-
- name: check hun is running
39-
run: |
40-
./ci/check_hub.sh
39+
- name: Add a test admin token
40+
run: cat ci/config_token.py >> jupyterhub_config.py
41+
42+
- name: Run JupyterHub
43+
run: docker-compose up -d
4144

42-
# Todo: check server can be spawned
45+
- name: Test
46+
run: pytest -vs

ci/config_token.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
c.JupyterHub.load_roles = [
2+
{
3+
"name": "test-admin",
4+
"scopes": ["admin:users", "admin:servers", "access:servers"],
5+
"services": ["test"],
6+
}
7+
]
8+
9+
c.JupyterHub.services = [
10+
{
11+
"name": "test",
12+
"api_token": "test-token-123",
13+
}
14+
]

tests/test_spawn.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)