-
Notifications
You must be signed in to change notification settings - Fork 183
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f23743e
fix: Better method to detect helm chart version in
harshshahsumo 63bbe8a
lint
harshshahsumo 428773a
Adding unit tests
harshshahsumo ccf82d2
Replacing the regex with the strings
harshshahsumo cd83f9d
Address comments for better easy to read syntax
harshshahsumo 61bdf08
Removed comments and replaced %s with %q
harshshahsumo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package helm | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
@@ -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%\"", | ||
// } | ||
replacements := []string{ | ||
"app.kubernetes.io/version: \"%s\"", | ||
"chart: \"sumologic-%s\"", | ||
"chart: sumologic-%s", | ||
"client: k8s_%s", | ||
"value: \"%s\"", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!