From 4bbf6fd1334c1e78fc7a09ef952cb38339b24f09 Mon Sep 17 00:00:00 2001 From: Ivan Yordanov Date: Tue, 13 Jun 2023 13:37:28 +0200 Subject: [PATCH] Fix helm version check (#242) --- kpops/component_handlers/helm_wrapper/helm.py | 8 ++++--- .../helm_wrapper/test_helm_wrapper.py | 21 +++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/kpops/component_handlers/helm_wrapper/helm.py b/kpops/component_handlers/helm_wrapper/helm.py index 4834c0972..cf7b71dbe 100644 --- a/kpops/component_handlers/helm_wrapper/helm.py +++ b/kpops/component_handlers/helm_wrapper/helm.py @@ -198,11 +198,13 @@ def get_manifest(self, release_name: str, namespace: str) -> Iterable[HelmTempla def get_version(self) -> Version: command = ["helm", "version", "--short"] short_version = self.__execute(command) - version_match = re.search(r"v(\d+\.\d+\.\d+)", short_version) + version_match = re.search(r"^v(\d+(\.\d+){0,2})", short_version) if version_match is None: raise RuntimeError("Could not parse the Helm version.") - - major, minor, patch = map(int, version_match.group(1).split(".")) + version = version_match.group(1) + while re.match(r"^(\d+(\.\d+)?)$", version): + version += ".0" + major, minor, patch = map(int, version.split(".")) return Version(major, minor, patch) @staticmethod diff --git a/tests/component_handlers/helm_wrapper/test_helm_wrapper.py b/tests/component_handlers/helm_wrapper/test_helm_wrapper.py index a599cdea4..4cc4ca620 100644 --- a/tests/component_handlers/helm_wrapper/test_helm_wrapper.py +++ b/tests/component_handlers/helm_wrapper/test_helm_wrapper.py @@ -456,8 +456,17 @@ def test_should_call_run_command_method_when_helm_template_without_optional_args ], ) - def test_should_call_helm_version(self, run_command: MagicMock): - run_command.return_value = "v3.12.0+gc9f554d" + @pytest.mark.parametrize( + "version", + [ + "v3.12.0+gc9f554d", + "v3.12.0", + "v3.12", + "v3", + ], + ) + def test_should_call_helm_version(self, run_command: MagicMock, version): + run_command.return_value = version Helm(helm_config=HelmConfig()) run_command.assert_called_once_with( @@ -477,3 +486,11 @@ def test_should_raise_exception_if_helm_version_is_old( assert str(runtime_error.value) == ( "The supported Helm version is 3.x.x. The current Helm version is 2.9.0" ) + + def test_should_raise_exception_if_helm_version_cannot_be_parsed( + self, run_command: MagicMock + ): + run_command.return_value = "123" + with pytest.raises(RuntimeError) as runtime_error: + Helm(helm_config=HelmConfig()) + assert str(runtime_error.value) == ("Could not parse the Helm version.")