Пример #1
0
def test_stage_image_tag() -> None:
    d = Docker()
    stage_tag = d.stage_image_tag(stage="finalstage")
    assert (
        stage_tag ==
        "docker-registry:5000/test/testing:2a7958c61a31a38a365aa347147aba2aaaaaaa-finalstage"
    )
Пример #2
0
def test_test_image_tag() -> None:
    d = Docker()
    stage_tag = d.test_image_tag()
    assert (
        stage_tag ==
        "docker-registry:5000/test/testing:2a7958c61a31a38a365aa347147aba2aaaaaaa-development"
    )
Пример #3
0
def test_get_stages_names(value: str, expected: List[str]) -> None:
    d = Docker()

    with tempfile.NamedTemporaryFile() as f:
        encoded_string = str.encode(value, encoding="UTF-8")
        f.write(encoded_string)
        f.seek(0)
        d.dockerfile = Path(f.name)
        stage_names = [stage.name for stage in d.get_stages()]
        assert stage_names == expected
Пример #4
0
def test_lifecycle_hooks_build(_: mock.MagicMock) -> None:
    d = Docker()

    Plugin, hook_calls = call_tracking_plugin_factory()
    with load_plugin(Plugin):
        d.build_stages()

    assert [*hook_calls.keys()] == [
        "container_build_begin",
        "container_build_stage_begin",
        "container_build_stage_complete",
        "container_build_complete",
    ]
Пример #5
0
    def __init__(
        self,
        track: str,
        secret_name: str = "",
        file_secret_name: str = "",
        basic_auth_secret_name: str = "",
        image: str = "",
        urls: str = "",
        is_dependent_project: bool = False,
        deploy_name: str = "",
        **kwargs: str,
    ) -> None:
        # Set variables from arguments and if they do not exist,
        # then default to settings variables.
        for env_var, attr in PROJECT_ARG_SETTINGS_MAPPING.items():
            if attr in kwargs:
                setattr(self, attr, kwargs[attr])
            else:
                setattr(self, attr, getattr(settings, env_var, ""))
        postfix = self.name

        self.track = track
        self.image = image
        self.secret_name = secret_name
        self.file_secret_name = file_secret_name
        self.basic_auth_secret_name = basic_auth_secret_name
        self.urls = urls
        self.is_dependent_project = is_dependent_project

        if not image:
            docker = Docker()
            self.image = docker.image_tag

        # TODO: Only set secret names if there are actual secrets
        if not self.secret_name:
            self.secret_name = get_secret_name(track=track, postfix=postfix)

        if not self.file_secret_name:
            file_secret_postfix = f"{postfix}-file"
            self.file_secret_name = get_secret_name(
                track=track, postfix=file_secret_postfix)

        self.deploy_name = deploy_name
        if not deploy_name:
            self.deploy_name = get_deploy_name(track=track, postfix=postfix)

        self.dependency_projects = ([] if is_dependent_project else
                                    self.get_dependency_projects(self.track))

        secret_prefixes = ([
            f"{env_var_safe_key(self.name)}_{settings.K8S_SECRET_PREFIX}"
        ] if self.is_dependent_project else [
            settings.K8S_SECRET_PREFIX,
            f"{env_var_safe_key(self.name)}_{settings.K8S_SECRET_PREFIX}",
        ])
        self.secret_data = get_environment_vars_by_prefixes(
            prefixes=secret_prefixes)
        for dep_project in self.dependency_projects:
            self.secret_data[
                f"{env_var_safe_key(dep_project.name)}_URL"] = dep_project.url
Пример #6
0
def test_get_stages_from_line(value: str, expected: int) -> None:
    d = Docker()

    test_strings = {
        value: expected,
        value.lower(): expected,
        value.upper(): expected
    }

    for string, result in test_strings.items():
        with tempfile.NamedTemporaryFile() as f:
            encoded_string = str.encode(string, encoding="UTF-8")
            f.write(encoded_string)
            f.seek(0)
            f.seek(0)
            d.dockerfile = Path(f.name)
            assert len(d.get_stages()) == result
Пример #7
0
def test_get_image_tags(env_tags: Optional[str], expected: List[str]) -> None:
    intermediate_stage = "stage"

    # Patch environment
    env = {}
    if env_tags is not None:
        env["DOCKER_IMAGE_TAGS"] = env_tags

    with mock.patch.dict("os.environ", env):
        s = Settings()

    # Patch settings
    with mock.patch.object(settings, "DOCKER_IMAGE_TAGS", s.DOCKER_IMAGE_TAGS):
        d = Docker()
        stage_tags = d.get_image_tags(stage=intermediate_stage,
                                      final_image=False)
        final_tags = d.get_image_tags(stage="", final_image=True)

    assert final_tags == expected
    assert stage_tags == [f"{tag}-{intermediate_stage}" for tag in expected]
Пример #8
0
def test_get_image_tags_amount(stage: str, final_image: bool,
                               expected_amount: int) -> None:
    d = Docker()
    images_tags = d.get_image_tags(stage=stage, final_image=final_image)
    assert len(images_tags) == expected_amount
Пример #9
0
def test_get_docker_git_ref_tag_values(value: str, expected: str) -> None:
    assert Docker.get_docker_git_ref_tag(value) == expected
Пример #10
0
def test_incorrect_login() -> None:
    d = Docker()
    with pytest.raises(Exception):
        d.login(password="******")
Пример #11
0
def test_login() -> None:
    d = Docker()
    d.login()
Пример #12
0
def test_get_image_tags_name(stage: str, final_image: bool,
                             expected_tags: List[str]) -> None:
    d = Docker()
    images_tags = d.get_image_tags(stage=stage, final_image=final_image)
    assert images_tags == sorted(expected_tags)
Пример #13
0
def test_incorrect_dockerfile_path() -> None:
    dockerfile_path = "/i_do_not_exist/Dockerfile"
    with pytest.raises(FileNotFoundError):
        Docker(dockerfile_path)
Пример #14
0
def test_get_image_tags_amount(stage: str, final_image: bool,
                               expected_amount: int) -> None:
    d = Docker()
    images_tags = d.get_image_tags(stage=stage, final_image=final_image)
    assert len(images_tags) == expected_amount


@pytest.mark.parametrize(
    "stage, final_image, expected_tags",
    [
        (
            "",
            False,
            [
                settings.GIT_COMMIT_SHA,
                Docker.get_docker_git_ref_tag(settings.GIT_COMMIT_REF_NAME),
            ],
        ),
        (
            "",
            True,
            [
                settings.GIT_COMMIT_SHA,
                Docker.get_docker_git_ref_tag(settings.GIT_COMMIT_REF_NAME),
            ],
        ),
        (
            "nonfinal",
            False,
            [
                f"{settings.GIT_COMMIT_SHA}-nonfinal",