Skip to content

Commit

Permalink
Minor fixes (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skazza94 committed Jun 16, 2023
1 parent 3fe328f commit 3a7a5e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Kathara/setting/Setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ONE_WEEK: int = 604800

DEFAULTS: Dict[str, Any] = {
"image": 'kathara/base',
"image": 'kathara/quagga',
"manager_type": 'docker',
"terminal": utils.exec_by_platform(lambda: '/usr/bin/xterm', lambda: '', lambda: 'Terminal'),
"open_terminals": True,
Expand Down
41 changes: 20 additions & 21 deletions src/Kathara/webhooks/DockerHubApi.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging
from functools import partial
from multiprocessing.dummy import Pool

import requests

from ..exceptions import HTTPConnectionError
from ..utils import get_pool_size, chunk_list
from ..utils import get_pool_size

DOCKER_HUB_KATHARA_URL = "https://hub.docker.com/v2/repositories/kathara/?page_size=-1"
DOCKER_HUB_KATHARA_IMAGES_URL = "https://hub.docker.com/v2/repositories/kathara/?page_size=-1"
DOCKER_HUB_KATHARA_TAGS_URL = "https://hub.docker.com/v2/repositories/{image_name}/tags/?page_size=-1&ordering"

EXCLUDED_IMAGES = ['megalos-bgp-manager']

Expand All @@ -28,13 +28,13 @@ def get_images() -> filter:
"""
try:
logging.debug("Getting Kathara images from Docker Hub...")
response = requests.get(DOCKER_HUB_KATHARA_URL)
response = requests.get(DOCKER_HUB_KATHARA_IMAGES_URL)
except requests.exceptions.ConnectionError as e:
raise HTTPConnectionError(str(e))

if response.status_code != 200:
logging.debug("Docker Hub replied with status code %s.", response.status_code)
raise HTTPConnectionError("Docker Hub replied with status code %s." % response.status_code)
logging.debug(f"Docker Hub replied with status code {response.status_code}.")
raise HTTPConnectionError(f"Docker Hub replied with status code {response.status_code}.")

return filter(
lambda x: not x['is_private'] and x['repository_type'] == 'image' and x['name'] not in EXCLUDED_IMAGES,
Expand All @@ -43,7 +43,7 @@ def get_images() -> filter:

@staticmethod
def get_tagged_images() -> list[str]:
"""Returns the tagged names of all the active Kathara Docker on from Docker Hub.
"""Returns the list of available Kathara images on Docker Hub with all the active tags.
Returns:
list[str]: A list of strings representing the tagged Docker images in the
Expand All @@ -55,32 +55,31 @@ def get_tagged_images() -> list[str]:
images = list(DockerHubApi.get_images())
tagged_images = []

def get_image_tag(tags, image):
def get_image_tag(image):
image_name = f"{image['namespace']}/{image['name']}"
try:
logging.debug(f"Getting Kathara tags for image '{image_name}' from Docker Hub...")
response = requests.get(
f"https://hub.docker.com/v2/repositories/{image_name}/tags/?page_size=-1&ordering"
)
logging.debug(f"Getting tags for image `{image_name}` from Docker Hub...")
response = requests.get(DOCKER_HUB_KATHARA_TAGS_URL.format(image_name=image_name))
except requests.exceptions.ConnectionError as e:
raise HTTPConnectionError(str(e))

if response.status_code != 200:
logging.debug(f"Docker Hub replied with status code %s for image '{image_name}'.", response.status_code)
logging.debug(
f"Error while retrieving tags for image `{image_name}. "
f"Docker Hub replied with status code {response.status_code}."
)
raise HTTPConnectionError(
f"Docker Hub replied with status code %s for image '{image_name}'." % response.status_code)
f"Error while retrieving tags for image `{image_name}`. "
f"Docker Hub replied with status code {response.status_code}."
)

tags.extend(list(map(
tagged_images.extend(list(map(
lambda x: f"{image_name}:{x['name']}" if x['name'] != "latest" else image_name,
filter(lambda x: x['tag_status'] == 'active', response.json()['results'])
)))

pool_size = get_pool_size()
machines_pool = Pool(pool_size)

items = chunk_list(images, pool_size)

for chunk in items:
machines_pool.map(func=partial(get_image_tag, tagged_images), iterable=chunk)
tags_pool = Pool(pool_size)
tags_pool.map(func=get_image_tag, iterable=images)

return sorted(tagged_images)
15 changes: 11 additions & 4 deletions tests/webhooks/docker_hub_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

sys.path.insert(0, './')

from src.Kathara.webhooks.DockerHubApi import DockerHubApi, EXCLUDED_IMAGES
from src.Kathara.webhooks.DockerHubApi import DockerHubApi, EXCLUDED_IMAGES, DOCKER_HUB_KATHARA_IMAGES_URL, \
DOCKER_HUB_KATHARA_TAGS_URL
from src.Kathara.exceptions import HTTPConnectionError

EXCLUDED_IMAGES.append("excluded")
Expand Down Expand Up @@ -50,6 +51,7 @@ def docker_hub_get_tags_response(mock_response):
def test_get_images(mock_requests_get, docker_hub_get_images_response):
mock_requests_get.return_value = docker_hub_get_images_response
images = list(DockerHubApi.get_images())
mock_requests_get.assert_called_once_with(DOCKER_HUB_KATHARA_IMAGES_URL)
assert len(images) == 2
assert images[0]['name'] == 'test'
assert images[1]['name'] == 'test-2'
Expand All @@ -74,14 +76,19 @@ def test_get_images_status_code_error(mock_requests_get):
#
@mock.patch("requests.get")
@mock.patch("src.Kathara.webhooks.DockerHubApi.DockerHubApi.get_images")
def test_get_tagged_images(mock_get_images, mock_request_get, docker_hub_get_tags_response):
def test_get_tagged_images(mock_get_images, mock_requests_get, docker_hub_get_tags_response):
mock_get_images.return_value = [
{'name': 'test-0', 'namespace': 'kathara', 'is_private': False, 'repository_type': 'image'},
{'name': 'test-1', 'namespace': 'kathara', 'is_private': False, 'repository_type': 'image'}
]
mock_request_get.return_value = docker_hub_get_tags_response
mock_requests_get.return_value = docker_hub_get_tags_response
tagged_images = DockerHubApi.get_tagged_images()
print(tagged_images)

calls = [
mock.call(DOCKER_HUB_KATHARA_TAGS_URL.format(image_name="kathara/test-0")),
mock.call(DOCKER_HUB_KATHARA_TAGS_URL.format(image_name="kathara/test-1"))
]
mock_requests_get.assert_has_calls(calls, any_order=True)
assert len(tagged_images) == 8
assert 'kathara/test-0' in tagged_images
assert 'kathara/test-0:tag-latest' in tagged_images
Expand Down

0 comments on commit 3a7a5e7

Please sign in to comment.