def test_normalization_and_validation(test_input, result): if type(result) == type and issubclass(result, Exception): with pytest.raises(result): normalized = api_utils.normalize_image_add_source(test_input) api_utils.validate_image_add_source(normalized, api_spec) else: normalized = api_utils.normalize_image_add_source(test_input) api_utils.validate_image_add_source(normalized, api_spec) assert normalized == result
def test_normalization_and_validation(): good_requests = [ # Basic Tag Case ({ 'tag': 'nginx' }, { 'source': { 'tag': { 'pullstring': 'nginx' } } }), # Basic Tag w/Dockerfile ({ 'tag': 'docker.io/nginx', 'dockerfile': b64_dockerfile }, { 'source': { 'tag': { 'pullstring': 'docker.io/nginx', 'dockerfile': b64_dockerfile } } }), # Basic Digest + Tag ({ 'tag': 'docker.io/library/nginx:latest', 'digest': test_digest, 'created_at': test_ts }, { 'source': { 'digest': { 'pullstring': 'docker.io/library/nginx@{}'.format(test_digest), 'tag': 'docker.io/library/nginx:latest', 'creation_timestamp_override': test_ts } } }), # Basic Digest + Tag ({ 'tag': 'docker.io/library/nginx:latest', 'digest': test_digest, 'created_at': test_ts }, { 'source': { 'digest': { 'pullstring': 'docker.io/library/nginx@{}'.format(test_digest), 'tag': 'docker.io/library/nginx:latest', 'creation_timestamp_override': test_ts } } }), # Basic Digest + Tag + Dodckerfile ({ 'tag': 'docker.io/library/nginx:latest', 'digest': test_digest, 'created_at': test_ts, 'dockerfile': b64_dockerfile }, { 'source': { 'digest': { 'pullstring': 'docker.io/library/nginx@{}'.format(test_digest), 'tag': 'docker.io/library/nginx:latest', 'creation_timestamp_override': test_ts, 'dockerfile': b64_dockerfile } } }), # Digest pullstring + Tag + ts ({ 'tag': 'docker.io/library/nginx:latest', 'digest': 'docker.io/library/nginx@{}'.format(test_digest), 'created_at': test_ts }, { 'source': { 'digest': { 'pullstring': 'docker.io/library/nginx@{}'.format(test_digest), 'tag': 'docker.io/library/nginx:latest', 'creation_timestamp_override': test_ts } } }), ] bad_requests = [ # Malformed tag ({ 'tag': 'docker.io/library/nginx@sha123' }, Exception), # Tag + Digest only (no ts) ({ 'tag': 'docker.io/library/nginx:latest', 'digest': 'sh256:abc' }, Exception), # Digest Only ({ 'digest': 'sh256:abc' }, Exception), # Digest pullstring only ({ 'digest': 'docker.io/nginx@sha256:abc' }, Exception) ] matrix = good_requests + bad_requests for test_input, result in matrix: if type(result) == type and issubclass(result, Exception): with pytest.raises(result): normalized = api_utils.normalize_image_add_source(test_input) api_utils.validate_image_add_source(normalized, api_spec) else: normalized = api_utils.normalize_image_add_source(test_input) api_utils.validate_image_add_source(normalized, api_spec) assert normalized == result
def test_normalization_and_validation(): good_requests = [ # Basic Tag Case ({"tag": "nginx"}, {"source": {"tag": {"pullstring": "nginx"}}}), # Basic Tag w/Dockerfile ( {"tag": "docker.io/nginx", "dockerfile": b64_dockerfile}, { "source": { "tag": { "pullstring": "docker.io/nginx", "dockerfile": b64_dockerfile, } } }, ), # Basic Digest + Tag ( { "tag": "docker.io/library/nginx:latest", "digest": test_digest, "created_at": test_ts, }, { "source": { "digest": { "pullstring": "docker.io/library/nginx@{}".format(test_digest), "tag": "docker.io/library/nginx:latest", "creation_timestamp_override": test_ts, } } }, ), # Basic Digest + Tag ( { "tag": "docker.io/library/nginx:latest", "digest": test_digest, "created_at": test_ts, }, { "source": { "digest": { "pullstring": "docker.io/library/nginx@{}".format(test_digest), "tag": "docker.io/library/nginx:latest", "creation_timestamp_override": test_ts, } } }, ), # Basic Digest + Tag + Dodckerfile ( { "tag": "docker.io/library/nginx:latest", "digest": test_digest, "created_at": test_ts, "dockerfile": b64_dockerfile, }, { "source": { "digest": { "pullstring": "docker.io/library/nginx@{}".format(test_digest), "tag": "docker.io/library/nginx:latest", "creation_timestamp_override": test_ts, "dockerfile": b64_dockerfile, } } }, ), # Digest pullstring + Tag + ts ( { "tag": "docker.io/library/nginx:latest", "digest": "docker.io/library/nginx@{}".format(test_digest), "created_at": test_ts, }, { "source": { "digest": { "pullstring": "docker.io/library/nginx@{}".format(test_digest), "tag": "docker.io/library/nginx:latest", "creation_timestamp_override": test_ts, } } }, ), # Digest pullstring + Tag + ts ( { "source": { "archive": { "digest": "sha256:b9e8479820fb3a1a2f8ec426dd4ffc129e3a320392ce28dde6ae2d2d29ce2682" } } }, { "source": { "archive": { "digest": "sha256:b9e8479820fb3a1a2f8ec426dd4ffc129e3a320392ce28dde6ae2d2d29ce2682" } } }, ), ] bad_requests = [ # Malformed tag ({"tag": "docker.io/library/nginx@sha123"}, Exception), # Tag + Digest only (no ts) ({"tag": "docker.io/library/nginx:latest", "digest": "sh256:abc"}, Exception), # Digest Only ({"digest": "sh256:abc"}, Exception), # Digest pullstring only ({"digest": "docker.io/nginx@sha256:abc"}, Exception), ] matrix = good_requests + bad_requests for test_input, result in matrix: if type(result) == type and issubclass(result, Exception): with pytest.raises(result): normalized = api_utils.normalize_image_add_source(test_input) api_utils.validate_image_add_source(normalized, api_spec) else: normalized = api_utils.normalize_image_add_source(test_input) api_utils.validate_image_add_source(normalized, api_spec) assert normalized == result