Repository: ansible/awx
File: awx/main/tests/unit/test_validators.py (Python)

Code before:


@pytest.mark.parametrize(
    ("image_name", "is_valid"),
    [
        ("registry.com:8080/myapp:tag", True),
        ("registry.com:8080/myapp@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("registry.com:8080/myapp:tag2@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("registry.com:8080/myapp@sha256:badbadbadbad", False),
        ("registry.com:8080/myapp:invalid~tag", False),
        ("bad_hostname.com:8080/myapp:tag", False),
        ("localhost:8080@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("localhost:8080/name@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("localhost:http/name@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", False),
        ("localhost@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("registry.com:8080/myapp@bad", False),
        ("registry.com:8080/myapp@2bad", False),
    ],
)
def test_valid_container_image_name(image_name, is_valid):
    if is_valid:
        validate_container_image_name(image_name)
    else:
        with pytest.raises(ValidationError):
            validate_container_image_name(image_name)


Review comment (suggestion):
Someone might still referenced saved local images (don't pull, distributed in advance, for networks with lots of restrictions)

So some ideas for that scenario:

 - localhost/foo
 - 28d67b2807af
 - foo

I think these should pass as the image name.

I would like to see some test cases _without_ the `:`, because you can still do this and let it default to `:latest`.

```
$ podman run --rm --tty --interactive quay.io/ansible/ansible-builder whoami
root
```

Code after:


@pytest.mark.parametrize(
    ("image_name", "is_valid"),
    [
        ("localhost", True),
        ("short", True),
        ("simple/name", True),
        ("ab/ab/ab/ab", True),
        ("foo.com/", False),
        ("", False),
        ("localhost/foo", True),
        ("3asdasdf3", True),
        ("xn--7o8h.com/myimage", True),
        ("Asdf.com/foo/bar", True),
        ("Foo/FarB", False),
        ("registry.com:8080/myapp:tag", True),
        ("registry.com:8080/myapp@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("registry.com:8080/myapp:tag2@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("registry.com:8080/myapp@sha256:badbadbadbad", False),
        ("registry.com:8080/myapp:invalid~tag", False),
        ("bad_hostname.com:8080/myapp:tag", False),
        ("localhost:8080@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("localhost:8080/name@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("localhost:http/name@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", False),
        ("localhost@sha256:be178c0543eb17f5f3043021c9e5fcf30285e557a4fc309cce97ff9ca6182912", True),
        ("registry.com:8080/myapp@bad", False),
        ("registry.com:8080/myapp@2bad", False),
    ],
)
def test_valid_container_image_name(image_name, is_valid):
    if is_valid:
        validate_container_image_name(image_name)
    else:
        with pytest.raises(ValidationError):
            validate_container_image_name(image_name)