|
22 | 22 | from dockerfile_parse import DockerfileParser |
23 | 23 | from dockerfile_parse.parser import image_from |
24 | 24 | from dockerfile_parse.constants import COMMENT_INSTRUCTION |
25 | | -from dockerfile_parse.util import b2u, u2b, Context |
| 25 | +from dockerfile_parse.util import b2u, u2b, Context, ImageName |
26 | 26 | from tests.fixtures import dfparser, instruction |
27 | 27 |
|
28 | 28 | NON_ASCII = "žluťoučký" |
|
31 | 31 | instruction = instruction # pylint: disable=self-assigning-variable |
32 | 32 |
|
33 | 33 |
|
| 34 | +@pytest.mark.parametrize(('image_string', 'dictionary'), [ |
| 35 | + ( |
| 36 | + " ", |
| 37 | + {"namespace": None, "registry": None, "tag": None, "repo": None}, |
| 38 | + ), ( |
| 39 | + "registry.org/namespace/repo:tag", |
| 40 | + {"namespace": "namespace", "registry": "registry.org", "tag": "tag", "repo": "repo"}, |
| 41 | + ), ( |
| 42 | + "/namespace/repo:tag", |
| 43 | + {"namespace": "namespace", "registry": None, "tag": "tag", "repo": "repo"}, |
| 44 | + ), ( |
| 45 | + "registry.org/repo:tag", |
| 46 | + {"namespace": None, "registry": "registry.org", "tag": "tag", "repo": "repo"}, |
| 47 | + ), ( |
| 48 | + "registry.org/repo", |
| 49 | + {"namespace": None, "registry": "registry.org", "tag": None, "repo": "repo"}, |
| 50 | + ), ( |
| 51 | + "registry.org/repo@sha256:hash", |
| 52 | + {"namespace": None, "registry": "registry.org", "tag": "sha256:hash", "repo": "repo"}, |
| 53 | + ) |
| 54 | +]) |
| 55 | +class TestImageName(object): |
| 56 | + def test_util_image_name_parse(self, image_string, dictionary): |
| 57 | + image = ImageName.parse(image_string) |
| 58 | + assert image.namespace == dictionary["namespace"] |
| 59 | + assert image.registry == dictionary["registry"] |
| 60 | + assert image.tag == dictionary["tag"] |
| 61 | + assert image.repo == dictionary["repo"] |
| 62 | + |
| 63 | + def test_util_image_name_get_repo(self, image_string, dictionary): |
| 64 | + image = ImageName.parse(image_string) |
| 65 | + repo = "/".join(filter(None, (dictionary["namespace"], dictionary["repo"]))) |
| 66 | + assert image.get_repo() == (repo if repo != "" else None) |
| 67 | + assert image.get_repo(explicit_namespace=True) == "{0}/{1}".format( |
| 68 | + dictionary["namespace"] if dictionary["namespace"] else "library", dictionary["repo"]) |
| 69 | + |
| 70 | + def test_util_image_name_to_str(self, image_string, dictionary): |
| 71 | + image = ImageName.parse(image_string) |
| 72 | + if dictionary["repo"] is None: |
| 73 | + with pytest.raises(RuntimeError): |
| 74 | + image.to_str() |
| 75 | + else: |
| 76 | + assert image.to_str() == image_string.lstrip('/') |
| 77 | + assert image.to_str() == (image_string.lstrip('/') if image_string else None) |
| 78 | + assert image.to_str(explicit_tag=True) == \ |
| 79 | + image_string.lstrip('/') + (':latest' if dictionary["tag"] is None else "") |
| 80 | + |
| 81 | + def test_image_name_hash(self, image_string, dictionary): |
| 82 | + image = ImageName.parse(image_string) |
| 83 | + if dictionary["repo"] is None: |
| 84 | + with pytest.raises(RuntimeError): |
| 85 | + hash(image) |
| 86 | + else: |
| 87 | + hash(image) |
| 88 | + |
| 89 | + def test_image_name_repr(self, image_string, dictionary): |
| 90 | + # so linter won't trip on unused argument |
| 91 | + del dictionary |
| 92 | + image = ImageName.parse(image_string) |
| 93 | + repr(image) |
| 94 | + |
| 95 | + def test_image_name_comparison(self, image_string, dictionary): |
| 96 | + # make sure that "==" is implemented correctly on both Python major releases |
| 97 | + i1 = ImageName.parse(image_string) |
| 98 | + i2 = ImageName(registry=dictionary["registry"], namespace=dictionary["namespace"], |
| 99 | + repo=dictionary["repo"], |
| 100 | + tag=dictionary["tag"]) |
| 101 | + assert i1 == i2 |
| 102 | + |
| 103 | + i2 = ImageName(registry='foo.com', namespace='spam', repo='bar', tag='2') |
| 104 | + # pylint: disable=unneeded-not |
| 105 | + assert not i1 == i2 |
| 106 | + |
| 107 | + i1 = ImageName.parse(i2) |
| 108 | + assert i1 == i2 |
| 109 | + |
| 110 | + i1 = i2.copy() |
| 111 | + assert i1 == i2 |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize(('repo', 'organization', 'enclosed_repo'), ( |
| 115 | + ('fedora', 'spam', 'spam/fedora'), |
| 116 | + ('spam/fedora', 'spam', 'spam/fedora'), |
| 117 | + ('spam/fedora', 'maps', 'maps/spam-fedora'), |
| 118 | +)) |
| 119 | +@pytest.mark.parametrize('registry', ( |
| 120 | + 'example.registry.com', |
| 121 | + 'example.registry.com:8888', |
| 122 | + None, |
| 123 | +)) |
| 124 | +@pytest.mark.parametrize('tag', ('bacon', None)) |
| 125 | +def test_image_name_enclose(repo, organization, enclosed_repo, registry, tag): |
| 126 | + reference = repo |
| 127 | + if tag: |
| 128 | + reference = '{}:{}'.format(repo, tag) |
| 129 | + if registry: |
| 130 | + reference = '{}/{}'.format(registry, reference) |
| 131 | + |
| 132 | + image_name = ImageName.parse(reference) |
| 133 | + assert image_name.get_repo() == repo |
| 134 | + assert image_name.registry == registry |
| 135 | + assert image_name.tag == tag |
| 136 | + |
| 137 | + image_name.enclose(organization) |
| 138 | + assert image_name.get_repo() == enclosed_repo |
| 139 | + # Verify that registry and tag are unaffected |
| 140 | + assert image_name.registry == registry |
| 141 | + assert image_name.tag == tag |
| 142 | + |
| 143 | + |
34 | 144 | class TestDockerfileParser(object): |
35 | 145 | def test_all_versions_match(self): |
36 | 146 | def read_version(fp, regex): |
|
0 commit comments