code-stye Yapf: 2 files updated
This commit is contained in:
parent
25b9d044de
commit
467c72a9ac
2 changed files with 55 additions and 24 deletions
|
@ -2,10 +2,10 @@
|
|||
|
||||
from flask import request, abort
|
||||
|
||||
from endpoints.api import (resource, nickname, require_repo_read, require_repo_write,
|
||||
RepositoryParamResource, log_action, validate_json_request,
|
||||
path_param, parse_args, query_param, truthy_bool,
|
||||
disallow_for_app_repositories, disallow_under_trust)
|
||||
from endpoints.api import (
|
||||
resource, nickname, require_repo_read, require_repo_write, RepositoryParamResource, log_action,
|
||||
validate_json_request, path_param, parse_args, query_param, truthy_bool,
|
||||
disallow_for_app_repositories, disallow_under_trust)
|
||||
from endpoints.exception import NotFound
|
||||
from endpoints.api.image import image_view
|
||||
from endpoints.v2.manifest import _generate_and_store_manifest
|
||||
|
@ -23,7 +23,8 @@ class ListRepositoryTags(RepositoryParamResource):
|
|||
@disallow_for_app_repositories
|
||||
@parse_args()
|
||||
@query_param('specificTag', 'Filters the tags to the specific tag.', type=str, default='')
|
||||
@query_param('limit', 'Limit to the number of results to return per page. Max 100.', type=int, default=50)
|
||||
@query_param('limit', 'Limit to the number of results to return per page. Max 100.', type=int,
|
||||
default=50)
|
||||
@query_param('page', 'Page index for the results. Default 1.', type=int, default=1)
|
||||
@nickname('listRepoTags')
|
||||
def get(self, namespace, repository, parsed_args):
|
||||
|
@ -112,11 +113,14 @@ class RepositoryTag(RepositoryParamResource):
|
|||
model.tag.create_or_update_tag(namespace, repository, tag, image_id)
|
||||
|
||||
username = get_authenticated_user().username
|
||||
log_action('move_tag' if original_image_id else 'create_tag', namespace,
|
||||
{'username': username, 'repo': repository, 'tag': tag,
|
||||
'namespace': namespace, 'image': image_id,
|
||||
'original_image': original_image_id},
|
||||
repo=model.repository.get_repository(namespace, repository))
|
||||
log_action('move_tag' if original_image_id else 'create_tag', namespace, {
|
||||
'username': username,
|
||||
'repo': repository,
|
||||
'tag': tag,
|
||||
'namespace': namespace,
|
||||
'image': image_id,
|
||||
'original_image': original_image_id
|
||||
}, repo=model.repository.get_repository(namespace, repository))
|
||||
|
||||
_generate_and_store_manifest(namespace, repository, tag)
|
||||
|
||||
|
@ -132,8 +136,10 @@ class RepositoryTag(RepositoryParamResource):
|
|||
|
||||
username = get_authenticated_user().username
|
||||
log_action('delete_tag', namespace,
|
||||
{'username': username, 'repo': repository, 'namespace': namespace, 'tag': tag},
|
||||
repo=model.repository.get_repository(namespace, repository))
|
||||
{'username': username,
|
||||
'repo': repository,
|
||||
'namespace': namespace,
|
||||
'tag': tag}, repo=model.repository.get_repository(namespace, repository))
|
||||
|
||||
return '', 204
|
||||
|
||||
|
@ -143,6 +149,7 @@ class RepositoryTag(RepositoryParamResource):
|
|||
@path_param('tag', 'The name of the tag')
|
||||
class RepositoryTagImages(RepositoryParamResource):
|
||||
""" Resource for listing the images in a specific repository tag. """
|
||||
|
||||
@require_repo_read
|
||||
@nickname('listTagImages')
|
||||
@disallow_for_app_repositories
|
||||
|
@ -184,12 +191,13 @@ class RepositoryTagImages(RepositoryParamResource):
|
|||
image_map.pop(ancestor_id, None)
|
||||
|
||||
return {
|
||||
'images': [image_view(image, image_map_all) for image in all_images
|
||||
if not parsed_args['owned'] or (str(image.id) in image_map)]
|
||||
'images': [
|
||||
image_view(image, image_map_all) for image in all_images
|
||||
if not parsed_args['owned'] or (str(image.id) in image_map)
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
|
||||
@resource('/v1/repository/<apirepopath:repository>/tag/<tag>/restore')
|
||||
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
||||
@path_param('tag', 'The name of the tag')
|
||||
|
@ -246,8 +254,8 @@ class RestoreTag(RepositoryParamResource):
|
|||
if existing_image is not None:
|
||||
log_data['original_image'] = existing_image.docker_image_id
|
||||
|
||||
log_action('revert_tag', namespace,
|
||||
log_data, repo=model.repository.get_repository(namespace, repository))
|
||||
log_action('revert_tag', namespace, log_data, repo=model.repository.get_repository(
|
||||
namespace, repository))
|
||||
|
||||
return {
|
||||
'image_id': image_id,
|
||||
|
|
|
@ -8,71 +8,91 @@ from features import FeatureNameValue
|
|||
|
||||
from test.fixtures import *
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def get_repo_image():
|
||||
def mock_callable(namespace, repository, image_id):
|
||||
img = Mock(repository='fetched_repository') if image_id == 'image1' else None
|
||||
return img
|
||||
|
||||
with patch('endpoints.api.tag.model.image.get_repo_image', side_effect=mock_callable) as mk:
|
||||
yield mk
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def get_repository():
|
||||
with patch('endpoints.api.tag.model.image.get_repo_image', return_value='mock_repo') as mk:
|
||||
yield mk
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def get_repo_tag_image():
|
||||
def mock_get_repo_tag_image(repository, tag):
|
||||
tag_img = Mock(docker_image_id='mock_docker_image_id') if tag == 'existing-tag' else None
|
||||
return tag_img
|
||||
with patch('endpoints.api.tag.model.tag.get_repo_tag_image', side_effect=mock_get_repo_tag_image):
|
||||
|
||||
with patch('endpoints.api.tag.model.tag.get_repo_tag_image',
|
||||
side_effect=mock_get_repo_tag_image):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def restore_tag_to_manifest():
|
||||
def mock_restore_tag_to_manifest(repository, tag, manifest_digest):
|
||||
tag_img = Mock(docker_image_id='mock_docker_image_id') if tag == 'existing-tag' else None
|
||||
return tag_img
|
||||
with patch('endpoints.api.tag.model.tag.restore_tag_to_manifest', side_effect=mock_restore_tag_to_manifest):
|
||||
|
||||
with patch('endpoints.api.tag.model.tag.restore_tag_to_manifest',
|
||||
side_effect=mock_restore_tag_to_manifest):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def restore_tag_to_image():
|
||||
def mock_restore_tag_to_image(repository, tag, image_id):
|
||||
tag_img = Mock(docker_image_id='mock_docker_image_id') if tag == 'existing-tag' else None
|
||||
return tag_img
|
||||
with patch('endpoints.api.tag.model.tag.restore_tag_to_image', side_effect=mock_restore_tag_to_image):
|
||||
|
||||
with patch('endpoints.api.tag.model.tag.restore_tag_to_image',
|
||||
side_effect=mock_restore_tag_to_image):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def create_or_update_tag():
|
||||
with patch('endpoints.api.tag.model.tag.create_or_update_tag') as mk:
|
||||
yield mk
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def generate_manifest():
|
||||
def mock_callable(namespace, repository, tag):
|
||||
if tag == 'generatemanifestfail':
|
||||
raise Exception('test_failure')
|
||||
|
||||
with patch('endpoints.api.tag._generate_and_store_manifest', side_effect=mock_callable) as mk:
|
||||
yield mk
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def authd_client(client):
|
||||
with client_with_identity('devtable', client) as cl:
|
||||
yield cl
|
||||
|
||||
|
||||
@pytest.mark.parametrize('test_image,test_tag,expected_status', [
|
||||
('image1', '-INVALID-TAG-NAME', 400),
|
||||
('image1', '.INVALID-TAG-NAME', 400),
|
||||
('image1', 'INVALID-TAG_NAME-BECAUSE-THIS-IS-WAY-WAY-TOO-LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG', 400),
|
||||
('image1',
|
||||
'INVALID-TAG_NAME-BECAUSE-THIS-IS-WAY-WAY-TOO-LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG',
|
||||
400),
|
||||
('nonexistantimage', 'newtag', 404),
|
||||
('image1', 'generatemanifestfail', None),
|
||||
('image1', 'existing-tag', 201),
|
||||
('image1', 'newtag', 201),
|
||||
])
|
||||
def test_move_tag(test_image, test_tag, expected_status, get_repo_image, get_repo_tag_image, create_or_update_tag, generate_manifest, authd_client):
|
||||
def test_move_tag(test_image, test_tag, expected_status, get_repo_image, get_repo_tag_image,
|
||||
create_or_update_tag, generate_manifest, authd_client):
|
||||
params = {'repository': 'devtable/repo', 'tag': test_tag}
|
||||
request_body = {'image': test_image}
|
||||
if expected_status is None:
|
||||
|
@ -81,16 +101,19 @@ def test_move_tag(test_image, test_tag, expected_status, get_repo_image, get_rep
|
|||
else:
|
||||
conduct_api_call(authd_client, RepositoryTag, 'put', params, request_body, expected_status)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('test_manifest,test_tag,manifest_generated,expected_status', [
|
||||
(None, 'newtag', True, 200),
|
||||
(None, 'generatemanifestfail', True, None),
|
||||
('manifest1', 'newtag', False, 200),
|
||||
])
|
||||
def test_restore_tag(test_manifest, test_tag, manifest_generated, expected_status, get_repository, restore_tag_to_manifest, restore_tag_to_image, generate_manifest, authd_client):
|
||||
def test_restore_tag(test_manifest, test_tag, manifest_generated, expected_status, get_repository,
|
||||
restore_tag_to_manifest, restore_tag_to_image, generate_manifest,
|
||||
authd_client):
|
||||
params = {'repository': 'devtable/repo', 'tag': test_tag}
|
||||
request_body = {'image': 'image1'}
|
||||
if test_manifest is not None:
|
||||
request_body['manifest_digest'] = test_manifest
|
||||
request_body['manifest_digest'] = test_manifest
|
||||
if expected_status is None:
|
||||
with pytest.raises(Exception):
|
||||
conduct_api_call(authd_client, RestoreTag, 'post', params, request_body, expected_status)
|
||||
|
|
Reference in a new issue