Skip to content

Commit

Permalink
fix(get_docker_image_by_version): make it more roubst to errors
Browse files Browse the repository at this point in the history
In some runs that are using AMIs which doesn't have corresponding
docker images, we've seen this failures, when c-s stress command
is running:

```
File "../sdcm/stress_thread.py", line 173, in _run_cs_stress
    cassandra_stress = get_docker_image_by_version(
        self.node_list[0].get_scylla_binary_version())
  File "../sdcm/utils/version_utils.py", line 354, in get_docker_image_by_version
    for image in all_tags['results']:
KeyError: 'results'
```

improved `get_docker_image_by_version` to be more robust on handling
errors, and melformed responses.
  • Loading branch information
fruch committed Nov 27, 2022
1 parent 9979a9e commit b09f98a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
15 changes: 9 additions & 6 deletions sdcm/utils/version_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,17 @@ def get_docker_image_by_version(scylla_version: str):
scylla_version = short_scylla_version

for page_number in count(start=1):
all_tags = requests.get(url=f'https://hub.docker.com/v2/repositories/{docker_repo}/'
f'tags?page_size=50&page={page_number}').json()
for image in all_tags['results']:
if scylla_version == image['name']:
return f"{docker_repo}:{image['name']}"
try:
all_tags = requests.get(url=f'https://hub.docker.com/v2/repositories/{docker_repo}/'
f'tags?page_size=50&page={page_number}').json()
except requests.RequestException:
break
for image in all_tags.get('results', []):
image_name = image.get('name')
if image_name and scylla_version == image_name:
return f"{docker_repo}:{image_name}"
if not all_tags.get('next'):
break

# if image wasn't found, default to the latest releases
return default_image

Expand Down
24 changes: 24 additions & 0 deletions unit_tests/test_version_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import os
import unittest
from unittest import mock

import pytest
import requests

import sdcm
from sdcm.utils.version_utils import (
Expand Down Expand Up @@ -412,3 +414,25 @@ def test_get_docker_image_by_version(version, expected):
def test_get_docker_image_by_version_broken_string_version():
with pytest.raises(AssertionError):
get_docker_image_by_version(scylla_version="broken_version")


def test_get_docker_image_by_version_fallback_on_errors():
def mock_requests_factory(response_stub):
return mock.Mock(**{
'json.return_value': response_stub,
})

def raise_request_error(**kwargs):
raise requests.HTTPError()

non_existing_version = '5.2.0~dev-0.20221124.999b7f5d9b77'
expected_fallback = 'scylladb/scylla:latest'
with unittest.mock.patch("requests.get") as get_mock:
get_mock.side_effect = lambda **_: mock_requests_factory({})
assert get_docker_image_by_version(scylla_version=non_existing_version) == expected_fallback

get_mock.side_effect = lambda **_: mock_requests_factory({"results": [{'name': ''}, {}]})
assert get_docker_image_by_version(scylla_version=non_existing_version) == expected_fallback

get_mock.side_effect = raise_request_error
assert get_docker_image_by_version(scylla_version=non_existing_version) == expected_fallback

0 comments on commit b09f98a

Please sign in to comment.