Skip to content

Commit

Permalink
Retry ensure_image_stream_tag() on conflict (OSBS-5369)
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Waugh <twaugh@redhat.com>
  • Loading branch information
twaugh committed Apr 13, 2018
1 parent 572bf99 commit d9cf206
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions osbs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ def put_image_stream_tag(self, tag_id, tag):
check_response(response)
return response

@retry_on_conflict
def ensure_image_stream_tag(self, stream, tag_name, tag_template,
scheduled=False):
stream_id = stream['metadata']['name']
Expand Down
63 changes: 63 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,69 @@ def verify_image_stream_tag(*args, **kwargs):
self._make_tag_template(),
expected_scheduled) == expected_change)

@pytest.mark.parametrize(('status_codes', 'should_raise'), [ # noqa
([http_client.OK], False),
([http_client.CONFLICT, http_client.CONFLICT, http_client.OK], False),
([http_client.CONFLICT, http_client.OK], False),
([http_client.CONFLICT, http_client.CONFLICT, http_client.UNAUTHORIZED], True),
([http_client.UNAUTHORIZED], True),
([http_client.CONFLICT for _ in range(OS_CONFLICT_MAX_RETRIES + 1)], True),
])
def test_retry_ensure_image_stream_tag(self, openshift,
status_codes, should_raise):
get_expectation = (flexmock(openshift)
.should_receive('_get')
.times(len(status_codes)))
put_expectation = (flexmock(openshift)
.should_receive('_put')
.times(len(status_codes)))
for status_code in status_codes:
get_response = HttpResponse(http_client.NOT_FOUND,
headers={},
content=b'')
put_response = HttpResponse(status_code,
headers={},
content=b'')
get_expectation = get_expectation.and_return(get_response)
put_expectation = put_expectation.and_return(put_response)

(flexmock(time)
.should_receive('sleep')
.and_return(None))

fn = openshift.ensure_image_stream_tag
args = (
{
'kind': 'ImageStream',
'metadata': {
'name': 'imagestream',
},
'spec': {
'dockerImageRepository': 'registry.example.com/repo',
},
},
'tag',
{
'kind': 'ImageStreamTag',
'metadata': {
'name': 'imagestream:tag',
},
'tag': {
'name': 'tag',
'from': {
'kind': 'DockerImage',
'name': 'registry.example.com/repo:tag',
},
'importPolicy': {},
},
})

if should_raise:
with pytest.raises(OsbsResponseException):
fn(*args)
else:
fn(*args)

@pytest.mark.parametrize(('kwargs', 'called'), (
({'use_auth': True, 'use_kerberos': True}, False),
({'use_auth': True, 'username': 'foo', 'password': 'bar'}, False),
Expand Down

0 comments on commit d9cf206

Please sign in to comment.