From eb9db0c53be25c00f586aa3220fcd47805a1a6a9 Mon Sep 17 00:00:00 2001 From: EvB Date: Tue, 2 May 2017 18:10:20 -0400 Subject: [PATCH] test(api/tag): unit test movetag --- endpoints/api/test/test_tag.py | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 endpoints/api/test/test_tag.py diff --git a/endpoints/api/test/test_tag.py b/endpoints/api/test/test_tag.py new file mode 100644 index 000000000..ea8c09b0e --- /dev/null +++ b/endpoints/api/test/test_tag.py @@ -0,0 +1,57 @@ +import pytest + +from mock import patch, Mock + +from endpoints.api.test.shared import client_with_identity, conduct_api_call +from endpoints.api.tag import RepositoryTag, RestoreTag +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_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): + 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 is '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), + ('nonexistantimage', 'newtag', 404), + ('image1', 'generatemanifestfail', 201), + ('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): + params = {'repository': 'devtable/repo', 'tag': test_tag} + request_body = {'image': test_image} + conduct_api_call(authd_client, RepositoryTag, 'put', params, request_body, expected_status)