Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
fix docker tests for release (#406)
Browse files Browse the repository at this point in the history
* fix docker tests for release

* fix docker io
  • Loading branch information
mike0sv authored Sep 14, 2022
1 parent 0a19b96 commit 51f328a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
32 changes: 22 additions & 10 deletions mlem/contrib/docker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from contextlib import contextmanager
from functools import wraps
from threading import Lock
from typing import Any, Generator, Iterator, Tuple, Union
from typing import Any, Dict, Generator, Iterator, List, Optional, Tuple, Union

import docker
import requests
Expand Down Expand Up @@ -108,23 +108,35 @@ def create_docker_client(
client.close()


def image_exists_at_dockerhub(tag):
def image_exists_at_dockerhub(tag, library=False):
repo, tag = tag.split(":")
lib = "library/" if library else ""
resp = requests.get(
f"https://registry.hub.docker.com/v1/repositories/{repo}/tags/{tag}"
f"https://registry.hub.docker.com/v2/repositories/{lib}{repo}/tags/{tag}"
)
time.sleep(1) # rate limiting
return resp.status_code == 200


def repository_tags_at_dockerhub(repo):
resp = requests.get(
f"https://registry.hub.docker.com/v1/repositories/{repo}/tags"
def repository_tags_at_dockerhub(
repo, library=False, max_results: Optional[int] = 100
):
lib = "library/" if library else ""
res: List[Dict] = []
next_page = (
f"https://registry.hub.docker.com/v2/repositories/{lib}{repo}/tags"
)
time.sleep(1) # rate limiting
if resp.status_code != 200:
return {}
return {tag["name"] for tag in resp.json()}
while next_page is not None and (
max_results is None or len(res) <= max_results
):
resp = requests.get(next_page, params={"page_size": 1000})
if resp.status_code != 200:
return {}
res.extend(resp.json()["results"])
next_page = resp.json()["next"]
time.sleep(0.1) # rate limiting

return {tag["name"] for tag in res}


def wrap_docker_error(f):
Expand Down
7 changes: 7 additions & 0 deletions tests/contrib/test_docker/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from testcontainers.core.container import DockerContainer as TestContainer

from mlem.contrib.docker.base import DockerDaemon, DockerEnv, RemoteRegistry
from mlem.contrib.docker.context import use_mlem_source
from mlem.contrib.docker.utils import is_docker_running
from tests.conftest import long

Expand Down Expand Up @@ -95,3 +96,9 @@ def docker_test(f):
not has_docker(), reason="docker is unavailable or skipped"
)
return long(mark(skip(f)))


@pytest.fixture(scope="session", autouse=True)
def mlem_source():
with use_mlem_source("whl"):
yield
6 changes: 3 additions & 3 deletions tests/contrib/test_docker/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def test_docker_registry_io():
registry = DockerIORegistry()
client = docker.DockerClient()

client.images.pull("hello-world:latest")
client.images.pull("library/hello-world:latest")

assert registry.get_host() == "https://index.docker.io/v1/"
registry.push(client, "hello-world:latest")
image = DockerImage(name="hello-world")
registry.push(client, "library/hello-world:latest")
image = DockerImage(name="library/hello-world")
assert registry.image_exists(client, image)


Expand Down
10 changes: 6 additions & 4 deletions tests/contrib/test_docker/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

@docker_test
def test_image_exists():
assert image_exists_at_dockerhub(f"python:{get_python_version()}-slim")
assert image_exists_at_dockerhub(
f"python:{get_python_version()}-slim", library=True
)
assert image_exists_at_dockerhub("minio/minio:latest")
assert image_exists_at_dockerhub("postgres:alpine")
assert image_exists_at_dockerhub("registry:latest")
assert image_exists_at_dockerhub("postgres:alpine", library=True)
assert image_exists_at_dockerhub("registry:latest", library=True)


@docker_test
Expand All @@ -25,7 +27,7 @@ def test_image_not_exists():

@docker_test
def test_repository_tags():
tags = repository_tags_at_dockerhub("python")
tags = repository_tags_at_dockerhub("python", library=True)
assert f"{get_python_version()}-slim" in tags
assert get_python_version() in tags

Expand Down

0 comments on commit 51f328a

Please sign in to comment.