diff --git a/tests/conftest.py b/tests/conftest.py index 92e5e2f4..fad46e09 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,25 +22,32 @@ def __getattr__(self, k): return self.__dict__[k] +async def remove_docker_image(image_name): + async with Docker() as docker: + try: + await docker.images.delete(image_name, force=True) + except DockerError: + pass + + @pytest.fixture(scope='module') def minimal_repo(): return "https://github.com/jtpio/test-binder" @pytest.fixture(scope='module') -def image_name(): - return "tljh-repo2docker-test:master" +def minimal_repo_uppercase(): + return "https://github.com/jtpio/TEST-BINDER" -@pytest.mark.asyncio @pytest.fixture(scope='module') -async def remove_test_image(image_name): - docker = Docker() - try: - await docker.images.delete(image_name) - except DockerError: - pass - await docker.close() +def generated_image_name(): + return "jtpio-test-binder:master" + + +@pytest.fixture(scope='module') +def image_name(): + return "tljh-repo2docker-test:master" @pytest.fixture(scope='module') @@ -71,3 +78,15 @@ def fin(): request.addfinalizer(fin) io_loop.run_sync(make_app) return mocked_app + + +@pytest.fixture(autouse=True) +def remove_all_test_images(image_name, generated_image_name, io_loop): + try: + yield + finally: + async def _clean(): + await remove_docker_image(image_name) + await remove_docker_image(generated_image_name) + + io_loop.run_sync(_clean) diff --git a/tests/test_builder.py b/tests/test_builder.py index ab798924..f175834b 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -6,7 +6,7 @@ @pytest.mark.asyncio -async def test_add_environment(app, remove_test_image, minimal_repo, image_name): +async def test_add_environment(app, minimal_repo, image_name): name, ref = image_name.split(":") r = await add_environment(app, repo=minimal_repo, name=name, ref=ref) assert r.status_code == 200 @@ -17,7 +17,7 @@ async def test_add_environment(app, remove_test_image, minimal_repo, image_name) @pytest.mark.asyncio -async def test_delete_environment(app, remove_test_image, minimal_repo, image_name): +async def test_delete_environment(app, minimal_repo, image_name): name, ref = image_name.split(":") await add_environment(app, repo=minimal_repo, name=name, ref=ref) await wait_for_image(image_name=image_name) @@ -32,14 +32,25 @@ async def test_delete_environment(app, remove_test_image, minimal_repo, image_na @pytest.mark.asyncio -async def test_delete_unknown_environment(app, remove_test_image): +async def test_delete_unknown_environment(app): r = await remove_environment(app, image_name="image-not-found:12345") assert r.status_code == 404 @pytest.mark.asyncio -async def test_no_repo(app): - r = await add_environment(app, repo="") +async def test_uppercase_repo(app, minimal_repo_uppercase, generated_image_name): + r = await add_environment(app, repo=minimal_repo_uppercase) + assert r.status_code == 200 + image = await wait_for_image(image_name=generated_image_name) + assert ( + image["ContainerConfig"]["Labels"]["tljh_repo2docker.image_name"] == generated_image_name + ) + + +@pytest.mark.asyncio +async def test_no_repo(app, image_name): + name, ref = image_name.split(":") + r = await add_environment(app, repo="", name=name, ref=ref) assert r.status_code == 400 @@ -47,8 +58,9 @@ async def test_no_repo(app): @pytest.mark.parametrize( "memory, cpu", [("abcded", ""), ("", "abcde"),], ) -async def test_wrong_limits(app, minimal_repo, memory, cpu): - r = await add_environment(app, repo=minimal_repo, memory=memory, cpu=cpu) +async def test_wrong_limits(app, minimal_repo, image_name, memory, cpu): + name, ref = image_name.split(":") + r = await add_environment(app, repo=minimal_repo, name=name, ref=ref, memory=memory, cpu=cpu) assert r.status_code == 400 assert "must be a number" in r.text diff --git a/tests/test_images.py b/tests/test_images.py index 1c90c60e..b0050499 100644 --- a/tests/test_images.py +++ b/tests/test_images.py @@ -21,7 +21,7 @@ async def test_images_list_not_admin(app): @pytest.mark.asyncio -async def test_spawn_page(app, remove_test_image, minimal_repo, image_name): +async def test_spawn_page(app, minimal_repo, image_name): cookies = await app.login_user('admin') # go to the spawn page diff --git a/tests/test_logs.py b/tests/test_logs.py index b99dc109..e41a784e 100644 --- a/tests/test_logs.py +++ b/tests/test_logs.py @@ -21,7 +21,7 @@ def next_event(it): @pytest.mark.asyncio -async def test_stream_simple(app, remove_test_image, minimal_repo, image_name): +async def test_stream_simple(app, minimal_repo, image_name): name, ref = image_name.split(":") await add_environment(app, repo=minimal_repo, name=name, ref=ref) r = await api_request(app, "environments", image_name, "logs", stream=True) @@ -38,7 +38,7 @@ async def test_stream_simple(app, remove_test_image, minimal_repo, image_name): @pytest.mark.asyncio -async def test_no_build(app, remove_test_image, image_name, request): +async def test_no_build(app, image_name, request): r = await api_request(app, "environments", "image-not-found:12345", "logs", stream=True) request.addfinalizer(r.close) assert r.status_code == 404 diff --git a/tests/utils.py b/tests/utils.py index a509fe29..9c4523a7 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,7 +6,7 @@ async def add_environment( - app, *, repo, ref="master", name="tljh-repo2docker-test", memory="", cpu="" + app, *, repo, ref="master", name="", memory="", cpu="" ): """Use the POST endpoint to add a new environment""" r = await api_request( @@ -24,18 +24,16 @@ async def wait_for_image(*, image_name): """wait until an image is built""" count, retries = 0, 60 * 10 image = None - docker = Docker() - while count < retries: - await asyncio.sleep(1) - try: - image = await docker.images.inspect(image_name) - except DockerError: - count += 1 - continue - else: - break - - await docker.close() + async with Docker() as docker: + while count < retries: + await asyncio.sleep(1) + try: + image = await docker.images.inspect(image_name) + except DockerError: + count += 1 + continue + else: + break return image diff --git a/tljh_repo2docker/docker.py b/tljh_repo2docker/docker.py index 44962fbd..5e091732 100644 --- a/tljh_repo2docker/docker.py +++ b/tljh_repo2docker/docker.py @@ -65,7 +65,7 @@ async def build_image(repo, ref, name="", memory=None, cpu=None): # default to the repo name if no name specified # and sanitize the name of the docker image name = name or urlparse(repo).path.strip("/") - name = name.replace("/", "-") + name = name.lower().replace("/", "-") image_name = f"{name}:{ref}" # memory is specified in GB