Ejemplo n.º 1
0
def test_docker_registries() -> None:
    registries = DockerRegistries.from_dict(
        {
            "reg1": {"address": "myregistry1domain:port"},
            "reg2": {"address": "myregistry2domain:port"},
        }
    )

    assert registries.default == ()
    assert list(registries.get()) == []
    assert len(list(registries.get("@reg1"))) == 1
    assert len(list(registries.get("@reg2"))) == 1
    assert len(list(registries.get("@reg1", "@reg2"))) == 2
    assert next(registries.get("@reg1")).address == "myregistry1domain:port"
    assert next(registries.get("@reg2")).address == "myregistry2domain:port"
    assert next(registries.get("@reg2")).default is False
    assert [r.address for r in registries.get("@reg1", "@reg2")] == [
        "myregistry1domain:port",
        "myregistry2domain:port",
    ]

    with pytest.raises(DockerRegistryOptionsNotFoundError):
        list(registries.get("@reg3"))

    assert list(registries.get("myregistry3domain:port")) == [
        DockerRegistryOptions(address="myregistry3domain:port")
    ]

    # Test defaults.
    registries = DockerRegistries.from_dict(
        {
            "reg1": {"address": "myregistry1domain:port", "default": "false"},
            "reg2": {"address": "myregistry2domain:port", "default": "true"},
            "reg3": {"address": "myregistry3domain:port", "default": "true"},
        }
    )

    assert next(registries.get("@reg2")).default is True
    assert [r.address for r in registries.default] == [
        "myregistry2domain:port",
        "myregistry3domain:port",
    ]

    # Implicit registry options from address.
    assert next(registries.get("myunregistereddomain:port")) == DockerRegistryOptions(
        address="myunregistereddomain:port"
    )
Ejemplo n.º 2
0
 def assert_tags(name: str, *expect_tags: str) -> None:
     tgt = rule_runner.get_target(Address("docker/test", target_name=name))
     fs = DockerFieldSet.create(tgt)
     tags = fs.image_refs(
         "image",
         DockerRegistries.from_dict({}),
         version_context,
     )
     assert expect_tags == tags
Ejemplo n.º 3
0
def test_image_ref_formatting(raw_values: dict, expect_raises: ContextManager,
                              image_refs: tuple[str, ...]) -> None:
    address = Address("test", target_name=raw_values.pop("name", "image"))
    tgt = DockerImageTarget(raw_values, address)
    field_set = DockerFieldSet.create(tgt)
    default_repository = "{name}"
    registries = DockerRegistries.from_dict({})
    interpolation_context = DockerInterpolationContext.from_dict({})
    with expect_raises:
        assert (field_set.image_refs(default_repository, registries,
                                     interpolation_context) == image_refs)
Ejemplo n.º 4
0
def test_skip_push() -> None:
    registries = DockerRegistries.from_dict(
        {
            "reg1": {"address": "registry1"},
            "reg2": {"address": "registry2", "skip_push": True},
            "reg3": {"address": "registry3", "skip_push": "false"},
        }
    )

    reg1, reg2, reg3 = registries.get("@reg1", "@reg2", "@reg3")
    assert reg1.skip_push is False
    assert reg2.skip_push is True
    assert reg3.skip_push is False
Ejemplo n.º 5
0
def test_extra_image_tags() -> None:
    registries = DockerRegistries.from_dict(
        {
            "reg1": {"address": "registry1"},
            "reg2": {
                "address": "registry2",
                "extra_image_tags": ["latest", "v{build_args.VERSION}"],
            },
        }
    )

    reg1, reg2 = registries.get("@reg1", "@reg2")
    assert reg1.extra_image_tags == ()
    assert reg2.extra_image_tags == ("latest", "v{build_args.VERSION}")
Ejemplo n.º 6
0
    def image_refs(
        self,
        default_repository: str,
        registries: DockerRegistries,
        interpolation_context: DockerInterpolationContext,
    ) -> tuple[str, ...]:
        """The image refs are the full image name, including any registry and version tag.

        In the Docker world, the term `tag` is used both for what we here prefer to call the image
        `ref`, as well as for the image version, or tag, that is at the end of the image name
        separated with a colon. By introducing the image `ref` we can retain the use of `tag` for
        the version part of the image name.

        Returns all image refs to apply to the Docker image, on the form:

            [<registry>/]<repository-name>[:<tag>]

        Where the `<repository-name>` may contain any number of separating slashes `/`, depending on
        the `default_repository` from configuration or the `repository` field on the target
        `docker_image`.

        This method will always return a non-empty tuple.
        """
        repository = self.format_repository(default_repository,
                                            interpolation_context)
        image_names = tuple(":".join(
            s
            for s in [repository,
                      self.format_tag(tag, interpolation_context)] if s)
                            for tag in self.tags.value or ())

        registries_options = tuple(
            registries.get(*(self.registries.value or [])))
        if not registries_options:
            # The image name is also valid as image ref without registry.
            return image_names

        return tuple("/".join([registry.address, image_name])
                     for image_name in image_names
                     for registry in registries_options)
Ejemplo n.º 7
0
 def registries(self) -> DockerRegistries:
     return DockerRegistries.from_dict(self._registries)
Ejemplo n.º 8
0
 def registries(self) -> DockerRegistries:
     return DockerRegistries.from_dict(self.options.registries)