diff --git a/controlpanel/api/helm.py b/controlpanel/api/helm.py index c3978da80..e6b704961 100644 --- a/controlpanel/api/helm.py +++ b/controlpanel/api/helm.py @@ -14,6 +14,10 @@ # Cache helm repository metadata for 5 minutes (expressed as seconds). CACHE_FOR_MINUTES = 5 * 60 +ERRORS_TO_IGNORE = [ + "release: already exists", + "uninstallation completed with 1 error(s): uninstall: failed to purge the release", +] def get_repo_path(): @@ -87,7 +91,7 @@ def _execute(*args, **kwargs): stderr = proc.stderr.read() log.warning(stderr) log.warning(stdout) - if "error" in stderr.lower() or "error" in stdout.lower(): + if should_raise_error(stderr, stdout): raise HelmError(stderr) except subprocess.CalledProcessError as proc_ex: # Subprocess specific exception handling should capture stderr too. @@ -117,6 +121,27 @@ def _execute(*args, **kwargs): return proc +def should_raise_error(stderr, stdout): + lower_error_string = stderr.lower() + lower_out_string = stdout.lower() + if "error" not in lower_error_string and "error" not in lower_out_string: + return False + + if should_ignore_error(lower_error_string) or should_ignore_error(lower_out_string): + return False + + return True + + +def should_ignore_error(error_string): + + for error in ERRORS_TO_IGNORE: + if error in error_string: + return True + + return False + + def update_helm_repository(force=False): """ Updates the helm repository and returns a dictionary representation of diff --git a/tests/api/test_helm.py b/tests/api/test_helm.py index ea6a61668..a8b834f3b 100644 --- a/tests/api/test_helm.py +++ b/tests/api/test_helm.py @@ -260,3 +260,25 @@ def test_list_releases_with_namespace(): "qux", ] mock_execute.assert_called_once_with("list", "-aq", "--namespace", "some-ns") + + +@pytest.mark.parametrize( + "stderr, stdout, raise_error", + [ + ("Error: release: already exists", "All good", False), + ("Error: Something that should throw", "All good", True), + ("All good", "Error: Something that should throw", True), + ("All good", "All good", False), + ( + ( + "Error: uninstallation completed with 1 error(s): " + "uninstall: Failed to purge the release" + ), + "All good", + False, + ), + ], +) +def test_should_raise_error(stderr, stdout, raise_error): + result = helm.should_raise_error(stderr, stdout) + assert result == raise_error