Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better method to detect helm chart version in template tests #3781

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 165 additions & 2 deletions tests/helm/goldenfile_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helm

import (
"fmt"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -132,11 +133,173 @@ 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%\"",
// }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out code should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

replacements := []string{
"app.kubernetes.io/version: \"%s\"",
"chart: \"sumologic-%s\"",
"chart: sumologic-%s",
"client: k8s_%s",
"value: \"%s\"",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use %q instead of %s for items that need to be surrounded in quotes, as this allows you to avoid escapes here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

}
output := yaml
output = strings.ReplaceAll(output, releaseName, "RELEASE-NAME")
output = strings.ReplaceAll(output, chartVersion, "%CURRENT_CHART_VERSION%")
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")
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)
})
}
}