From f23743ea4ba2de30e11d5c1906d7e21bc9bece51 Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Tue, 25 Jun 2024 12:57:28 -0500 Subject: [PATCH 1/6] 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 From 63bbe8a6d70aeca6d91e729db9967762dce74b4f Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Tue, 25 Jun 2024 15:03:07 -0500 Subject: [PATCH 2/6] lint --- tests/helm/goldenfile_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/helm/goldenfile_test.go b/tests/helm/goldenfile_test.go index 4288242bfa..8cba197989 100644 --- a/tests/helm/goldenfile_test.go +++ b/tests/helm/goldenfile_test.go @@ -133,8 +133,13 @@ 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) + 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%` @@ -145,7 +150,7 @@ func fixupRenderedYaml(yaml string, chartVersion string) string { 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 From 428773a2a390913f0f2a2d27aba3efdb930561ee Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Wed, 3 Jul 2024 15:31:18 -0500 Subject: [PATCH 3/6] Adding unit tests --- tests/helm/goldenfile_test.go | 147 ++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/tests/helm/goldenfile_test.go b/tests/helm/goldenfile_test.go index 8cba197989..9bead68481 100644 --- a/tests/helm/goldenfile_test.go +++ b/tests/helm/goldenfile_test.go @@ -155,3 +155,150 @@ func fixupRenderedYaml(yaml string, chartVersion string) string { output = strings.TrimSuffix(output, "\n") return output } + +func TestFixupRenderedYaml_MultipleOccurrences(t *testing.T) { + testcases := []struct { + name string + yaml string + chartVersion string + expected string + }{ + { + name: "single occurrence", + yaml: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "2.0.0" + chart: "sumologic-2.0.0" + client: k8s_2.0.0 + value: "2.0.0" + another_value: "sumologic-2.0.0" +checksum/config: abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890 +`, + chartVersion: "2.0.0", + expected: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + chart: "sumologic-%CURRENT_CHART_VERSION%" + client: k8s_%CURRENT_CHART_VERSION% + value: "%CURRENT_CHART_VERSION%" + another_value: "sumologic-2.0.0" +checksum/config: '%CONFIG_CHECKSUM%'`, + }, + { + name: "multiple occurrences", + yaml: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "3.0.0" + chart: "sumologic-3.0.0" + client: k8s_3.0.0 + value: "3.0.0" + another_value: "sumologic-3.0.0" + some_field: "3.0.0" + yet_another_field: "sumologic-3.0.0" +checksum/config: abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890`, + chartVersion: "3.0.0", + expected: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "%CURRENT_CHART_VERSION%" + chart: "sumologic-%CURRENT_CHART_VERSION%" + client: k8s_%CURRENT_CHART_VERSION% + value: "%CURRENT_CHART_VERSION%" + another_value: "sumologic-3.0.0" + some_field: "3.0.0" + yet_another_field: "sumologic-3.0.0" +checksum/config: '%CONFIG_CHECKSUM%'`, + }, + { + name: "no occurrence", + yaml: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "4.0.0" + chart: "sumologic-4.0.0" + client: k8s_4.0.0 + value: "4.0.0" +checksum/config: abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890`, + chartVersion: "5.0.0", + expected: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "4.0.0" + chart: "sumologic-4.0.0" + client: k8s_4.0.0 + value: "4.0.0" +checksum/config: '%CONFIG_CHECKSUM%'`, + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + fixedYAML := fixupRenderedYaml(tc.yaml, tc.chartVersion) + assert.Equal(t, tc.expected, fixedYAML, "Unexpected result for test case %s", tc.name) + }) + } +} + +func TestFixupRenderedYaml_NoReplacement(t *testing.T) { + testcases := []struct { + name string + yaml string + chartVersion string + expected string + }{ + { + name: "no chartVersion present", + yaml: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "1.0.0" + chart: "sumologic-1.0.0" + client: k8s_1.0.0 + value: "1.0.0" +checksum/config: abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890`, + chartVersion: "2.0.0", + expected: ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: my-config +data: + app.kubernetes.io/version: "1.0.0" + chart: "sumologic-1.0.0" + client: k8s_1.0.0 + value: "1.0.0" +checksum/config: '%CONFIG_CHECKSUM%'`, + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + fixedYAML := fixupRenderedYaml(tc.yaml, tc.chartVersion) + assert.Equal(t, tc.expected, fixedYAML, "Unexpected result for test case %s", tc.name) + }) + } +} From ccf82d2355b408d3f6430e2c06c24e40021b9ef0 Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Mon, 8 Jul 2024 10:43:01 -0500 Subject: [PATCH 4/6] Replacing the regex with the strings --- tests/helm/goldenfile_test.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/tests/helm/goldenfile_test.go b/tests/helm/goldenfile_test.go index 9bead68481..ebedb081f8 100644 --- a/tests/helm/goldenfile_test.go +++ b/tests/helm/goldenfile_test.go @@ -133,24 +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%` + replacements := map[string]string{ + fmt.Sprintf("app.kubernetes.io/version: \"%s\"", chartVersion): "app.kubernetes.io/version: \"%CURRENT_CHART_VERSION%\"", + fmt.Sprintf("chart: \"sumologic-%s\"", chartVersion): "chart: \"sumologic-%CURRENT_CHART_VERSION%\"", + fmt.Sprintf("chart: sumologic-%s", chartVersion): "chart: sumologic-%CURRENT_CHART_VERSION%", + fmt.Sprintf("client: k8s_%s", chartVersion): "client: k8s_%CURRENT_CHART_VERSION%", + fmt.Sprintf("value: \"%s\"", chartVersion): "value: \"%CURRENT_CHART_VERSION%\"", + } output := yaml output = strings.ReplaceAll(output, releaseName, "RELEASE-NAME") - output = pattern.ReplaceAllStringFunc(output, func(match string) string { - versionRegex := regexp.MustCompile(chartVersion) - version := versionRegex.FindString(match) - - return strings.Replace(match, version, replacement, 1) - }) + for oldString, newString := range replacements { + output = strings.ReplaceAll(output, oldString, newString) + } output = checksumRegex.ReplaceAllLiteralString(output, "checksum/config: '%CONFIG_CHECKSUM%'") output = strings.TrimSuffix(output, "\n") return output From cd83f9d7912c632308d3711d21d8166f53ed2507 Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Thu, 11 Jul 2024 12:13:29 -0500 Subject: [PATCH 5/6] Address comments for better easy to read syntax --- tests/helm/goldenfile_test.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/helm/goldenfile_test.go b/tests/helm/goldenfile_test.go index ebedb081f8..707ddd3292 100644 --- a/tests/helm/goldenfile_test.go +++ b/tests/helm/goldenfile_test.go @@ -133,18 +133,24 @@ 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}") - replacements := map[string]string{ - fmt.Sprintf("app.kubernetes.io/version: \"%s\"", chartVersion): "app.kubernetes.io/version: \"%CURRENT_CHART_VERSION%\"", - fmt.Sprintf("chart: \"sumologic-%s\"", chartVersion): "chart: \"sumologic-%CURRENT_CHART_VERSION%\"", - fmt.Sprintf("chart: sumologic-%s", chartVersion): "chart: sumologic-%CURRENT_CHART_VERSION%", - fmt.Sprintf("client: k8s_%s", chartVersion): "client: k8s_%CURRENT_CHART_VERSION%", - fmt.Sprintf("value: \"%s\"", chartVersion): "value: \"%CURRENT_CHART_VERSION%\"", + // replacements := map[string]string{ + // fmt.Sprintf("app.kubernetes.io/version: \"%s\"", chartVersion): "app.kubernetes.io/version: \"%CURRENT_CHART_VERSION%\"", + // fmt.Sprintf("chart: \"sumologic-%s\"", chartVersion): "chart: \"sumologic-%CURRENT_CHART_VERSION%\"", + // fmt.Sprintf("chart: sumologic-%s", chartVersion): "chart: sumologic-%CURRENT_CHART_VERSION%", + // fmt.Sprintf("client: k8s_%s", chartVersion): "client: k8s_%CURRENT_CHART_VERSION%", + // fmt.Sprintf("value: \"%s\"", chartVersion): "value: \"%CURRENT_CHART_VERSION%\"", + // } + replacements := []string{ + "app.kubernetes.io/version: \"%s\"", + "chart: \"sumologic-%s\"", + "chart: sumologic-%s", + "client: k8s_%s", + "value: \"%s\"", } - output := yaml output = strings.ReplaceAll(output, releaseName, "RELEASE-NAME") - for oldString, newString := range replacements { - output = strings.ReplaceAll(output, oldString, newString) + for _, r := range replacements { + output = strings.ReplaceAll(output, fmt.Sprintf(r, chartVersion), fmt.Sprintf(r, "%CURRENT_CHART_VERSION%")) } output = checksumRegex.ReplaceAllLiteralString(output, "checksum/config: '%CONFIG_CHECKSUM%'") output = strings.TrimSuffix(output, "\n") From 61bdf081ceecc2259c572faa7aaf961c183cd0e5 Mon Sep 17 00:00:00 2001 From: Harsh Shah Date: Thu, 11 Jul 2024 13:02:49 -0500 Subject: [PATCH 6/6] Removed comments and replaced %s with %q --- tests/helm/goldenfile_test.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/helm/goldenfile_test.go b/tests/helm/goldenfile_test.go index 707ddd3292..c62a492b7f 100644 --- a/tests/helm/goldenfile_test.go +++ b/tests/helm/goldenfile_test.go @@ -133,19 +133,13 @@ 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}") - // replacements := map[string]string{ - // fmt.Sprintf("app.kubernetes.io/version: \"%s\"", chartVersion): "app.kubernetes.io/version: \"%CURRENT_CHART_VERSION%\"", - // fmt.Sprintf("chart: \"sumologic-%s\"", chartVersion): "chart: \"sumologic-%CURRENT_CHART_VERSION%\"", - // fmt.Sprintf("chart: sumologic-%s", chartVersion): "chart: sumologic-%CURRENT_CHART_VERSION%", - // fmt.Sprintf("client: k8s_%s", chartVersion): "client: k8s_%CURRENT_CHART_VERSION%", - // fmt.Sprintf("value: \"%s\"", chartVersion): "value: \"%CURRENT_CHART_VERSION%\"", - // } + replacements := []string{ - "app.kubernetes.io/version: \"%s\"", + "app.kubernetes.io/version: %q", "chart: \"sumologic-%s\"", "chart: sumologic-%s", "client: k8s_%s", - "value: \"%s\"", + "value: %q", } output := yaml output = strings.ReplaceAll(output, releaseName, "RELEASE-NAME")