Skip to content

Commit

Permalink
Merge pull request jupyterhub#23 from jtpio/lowercase-repo-name
Browse files Browse the repository at this point in the history
Lowercase the autogenerated name from the repo
  • Loading branch information
jtpio authored May 25, 2020
2 parents f6675e1 + 9ae6a40 commit 658a8d2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 34 deletions.
39 changes: 29 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
26 changes: 19 additions & 7 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -32,23 +32,35 @@ 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


@pytest.mark.asyncio
@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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
24 changes: 11 additions & 13 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tljh_repo2docker/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 658a8d2

Please sign in to comment.