diff --git a/.travis.yml b/.travis.yml index 976ed0c6..47c3b850 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,5 +20,6 @@ install: - pip freeze script: - pyflakes dockerspawner - - docker pull jupyterhub/singleuser:$JUPYTERHUB + - docker pull jupyterhub/singleuser:0.8 + - docker pull jupyterhub/singleuser:0.9 - travis_retry py.test --cov dockerspawner tests -v diff --git a/tests/test_dockerspawner.py b/tests/test_dockerspawner.py index eb9795f8..30b7502c 100644 --- a/tests/test_dockerspawner.py +++ b/tests/test_dockerspawner.py @@ -1,6 +1,9 @@ """Tests for DockerSpawner class""" +import json + import pytest + from jupyterhub.tests.test_api import add_user, api_request from jupyterhub.tests.mocking import public_url from jupyterhub.tests.utils import async_requests @@ -30,3 +33,38 @@ def test_start_stop(app): r.raise_for_status() print(r.text) assert "kernels" in r.json() + + +@pytest.mark.gen_test +@pytest.mark.parametrize("image", ["0.8", "0.9", "nomatch"]) +def test_image_whitelist(app, image): + name = "checker" + add_user(app.db, app, name=name) + user = app.users[name] + assert isinstance(user.spawner, DockerSpawner) + user.spawner.remove_containers = True + user.spawner.image_whitelist = { + "0.9": "jupyterhub/singleuser:0.9", + "0.8": "jupyterhub/singleuser:0.8", + } + token = user.new_api_token() + # start the server + r = yield api_request( + app, "users", name, "server", method="post", data=json.dumps({"image": image}) + ) + if image not in user.spawner.image_whitelist: + with pytest.raises(Exception): + r.raise_for_status() + return + while r.status_code == 202: + # request again + r = yield api_request(app, "users", name, "server", method="post") + assert r.status_code == 201, r.text + url = url_path_join(public_url(app, user), "api/status") + r = yield async_requests.get(url, headers={"Authorization": "token %s" % token}) + r.raise_for_status() + assert r.headers['x-jupyterhub-version'].startswith(image) + r = yield api_request( + app, "users", name, "server", method="delete", + ) + r.raise_for_status()