From 6b1ffb6e994a2d4092f0f54b7a7b496c7176876b Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Thu, 13 Jul 2023 20:41:52 +0200 Subject: [PATCH] add some tests --- doc/architecture/Domain.md | 1 + src/main/python/ddadevops/domain/image.py | 21 +++++++++++++++++++++ src/test/python/domain/test_image.py | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/doc/architecture/Domain.md b/doc/architecture/Domain.md index 7e76a96..afb8378 100644 --- a/doc/architecture/Domain.md +++ b/doc/architecture/Domain.md @@ -12,6 +12,7 @@ classDiagram } class Image { + image_naming image_dockerhub_user image_dockerhub_password image_publish_tag diff --git a/src/main/python/ddadevops/domain/image.py b/src/main/python/ddadevops/domain/image.py index d24b59b..5fcfc5b 100644 --- a/src/main/python/ddadevops/domain/image.py +++ b/src/main/python/ddadevops/domain/image.py @@ -1,3 +1,4 @@ +from enum import Enum from typing import List, Dict from .common import ( filter_none, @@ -5,15 +6,23 @@ from .common import ( ) +class NamingType(Enum): + NAME_ONLY = 1 + NAME_AND_MODULE = 2 + + class Image(Validateable): def __init__( self, inp: dict, ): + self.module = inp.get("module") + self.name = inp.get("name") self.image_dockerhub_user = inp.get("image_dockerhub_user") self.image_dockerhub_password = inp.get("image_dockerhub_password") self.image_tag = inp.get("image_tag") self.image_build_commons_path = inp.get("image_build_commons_path") + self.image_naming = NamingType[inp.get("image_naming", "NAME_ONLY")] self.image_use_package_common_files = inp.get( "image_use_package_common_files", True ) @@ -23,8 +32,10 @@ class Image(Validateable): def validate(self) -> List[str]: result = [] + result += self.__validate_is_not_empty__("name") result += self.__validate_is_not_empty__("image_dockerhub_user") result += self.__validate_is_not_empty__("image_dockerhub_password") + result += self.__validate_is_not_empty__("image_naming") if not self.image_use_package_common_files: result += self.__validate_is_not_empty__("image_build_commons_path") result += self.__validate_is_not_empty__("image_build_commons_dir_name") @@ -37,6 +48,16 @@ class Image(Validateable): ] return "/".join(filter_none(commons_path)) + "/" + def image_name(self) -> str: + result: List[str] = [self.name] # type: ignore + if ( + self.image_naming == NamingType.NAME_AND_MODULE + and self.module + and self.module != "" + ): + result.append(self.module) + return "-".join(result) + @classmethod def get_mapping_default(cls) -> List[Dict[str, str]]: return [ diff --git a/src/test/python/domain/test_image.py b/src/test/python/domain/test_image.py index e26c98d..765aa3d 100644 --- a/src/test/python/domain/test_image.py +++ b/src/test/python/domain/test_image.py @@ -12,3 +12,16 @@ def test_devops_build_commons_path(): assert image is not None assert image.is_valid() assert "docker/" == image.build_commons_path() + +def test_should_calculate_image_name(): + sut = build_devops({}) + image = sut.specialized_builds[BuildType.IMAGE] + assert "name" == image.image_name() + + sut = build_devops({'image_naming': "NAME_ONLY"}) + image = sut.specialized_builds[BuildType.IMAGE] + assert "name" == image.image_name() + + sut = build_devops({'image_naming': "NAME_AND_MODULE"}) + image = sut.specialized_builds[BuildType.IMAGE] + assert "name-module" == image.image_name()