-
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
Conversation
template tests
Please add unit tests for this to prevent regression in case of change |
tests/helm/goldenfile_test.go
Outdated
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) |
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.
As it's correct, it's not so easy to understand the regex. I would recommend to create list of strings (regexes if strings won't be sufficient), and then iterate over them and replace them in the output
. I wouldn't want to maintain it in actual form after all
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.
addressed
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.
I believe this is better. It's following more DRY concept
tests/helm/goldenfile_test.go
Outdated
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%\"", | ||
} |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
addressed
tests/helm/goldenfile_test.go
Outdated
for oldString, newString := range replacements { | ||
output = strings.ReplaceAll(output, oldString, newString) | ||
} |
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.
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%")) | |
} |
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.
addressed
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.
Almost good to go, a few things need fixing still
tests/helm/goldenfile_test.go
Outdated
// 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%\"", | ||
// } |
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!
tests/helm/goldenfile_test.go
Outdated
"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 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.
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.
addressed
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.
Looks good 🚢
This change finds a new method using regex for updating the helm chart version without affecting any other version that matches it
Find the Ticket
Checklist