From f23743ea4ba2de30e11d5c1906d7e21bc9bece51 Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Tue, 25 Jun 2024 12:57:28 -0500 Subject: [PATCH] fix: Better method to detect helm chart version in template tests --- tests/helm/goldenfile_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/helm/goldenfile_test.go b/tests/helm/goldenfile_test.go index 8a83755a63..4288242bfa 100644 --- a/tests/helm/goldenfile_test.go +++ b/tests/helm/goldenfile_test.go @@ -1,6 +1,7 @@ package helm import ( + "fmt" "os" "regexp" "strings" @@ -132,10 +133,19 @@ func runGoldenFileTest(t *testing.T, valuesFileName string, outputFileName strin // expected templates func fixupRenderedYaml(yaml string, chartVersion string) string { checksumRegex := regexp.MustCompile("checksum/config: [a-z0-9]{64}") + patternString := fmt.Sprintf(`(?m)^(\s*app\.kubernetes\.io\/version:\s*"%s"\s*)$|^(\s*chart:\s*"sumologic-%s"\s*)$|^(\s*chart:\s*sumologic-%s\s*)$|^(\s*client:\s*k8s_%s\s*)$|^(\s*value:\s*"%s"\s*)$`, + chartVersion, chartVersion, chartVersion, chartVersion, chartVersion) + pattern := regexp.MustCompile(patternString) + replacement := `%CURRENT_CHART_VERSION%` output := yaml output = strings.ReplaceAll(output, releaseName, "RELEASE-NAME") - output = strings.ReplaceAll(output, chartVersion, "%CURRENT_CHART_VERSION%") + output = pattern.ReplaceAllStringFunc(output, func(match string) string { + versionRegex := regexp.MustCompile(chartVersion) + version := versionRegex.FindString(match) + + return strings.Replace(match, version, replacement, 1) + }) output = checksumRegex.ReplaceAllLiteralString(output, "checksum/config: '%CONFIG_CHECKSUM%'") output = strings.TrimSuffix(output, "\n") return output