test(state): harden chartify skip-schema flag detection
Agent-Logs-Url: https://github.com/helmfile/helmfile/sessions/a695cbff-c37a-403a-9658-09f4fdaa65d0 Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
parent
715fb7b758
commit
76536e45aa
|
|
@ -1,6 +1,10 @@
|
|||
package state
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAppendSkipSchemaValidationFlagToChartifyTemplateArgs(t *testing.T) {
|
||||
enable := true
|
||||
|
|
@ -43,6 +47,14 @@ func TestAppendSkipSchemaValidationFlagToChartifyTemplateArgs(t *testing.T) {
|
|||
templateArgs: "--skip-schema-validation --kube-context default",
|
||||
want: "--skip-schema-validation --kube-context default",
|
||||
},
|
||||
{
|
||||
name: "does not treat similar flag values as existing flag",
|
||||
release: &ReleaseSpec{
|
||||
SkipSchemaValidation: &enable,
|
||||
},
|
||||
templateArgs: "--set name=foo--skip-schema-validation",
|
||||
want: "--set name=foo--skip-schema-validation --skip-schema-validation",
|
||||
},
|
||||
{
|
||||
name: "does not add flag when disabled",
|
||||
release: &ReleaseSpec{},
|
||||
|
|
@ -60,9 +72,7 @@ func TestAppendSkipSchemaValidationFlagToChartifyTemplateArgs(t *testing.T) {
|
|||
}
|
||||
|
||||
got := st.appendSkipSchemaValidationFlagToChartifyTemplateArgs(tt.templateArgs, tt.release)
|
||||
if got != tt.want {
|
||||
t.Fatalf("appendSkipSchemaValidationFlagToChartifyTemplateArgs() = %q, want %q", got, tt.want)
|
||||
}
|
||||
require.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1637,15 +1637,27 @@ func (st *HelmState) processChartification(chartification *Chartify, release *Re
|
|||
}
|
||||
|
||||
func (st *HelmState) appendSkipSchemaValidationFlagToChartifyTemplateArgs(templateArgs string, release *ReleaseSpec) string {
|
||||
if !st.shouldSkipSchemaValidation(release, false) || strings.Contains(templateArgs, "--skip-schema-validation") {
|
||||
if !st.shouldSkipSchemaValidation(release, false) || hasTemplateArg(templateArgs, "--skip-schema-validation") {
|
||||
return templateArgs
|
||||
}
|
||||
|
||||
if templateArgs == "" {
|
||||
return "--skip-schema-validation"
|
||||
}
|
||||
return appendTemplateArg(templateArgs, "--skip-schema-validation")
|
||||
}
|
||||
|
||||
return templateArgs + " --skip-schema-validation"
|
||||
func hasTemplateArg(templateArgs, arg string) bool {
|
||||
for _, token := range strings.Fields(templateArgs) {
|
||||
if token == arg || strings.HasPrefix(token, arg+"=") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func appendTemplateArg(templateArgs, arg string) string {
|
||||
if templateArgs == "" {
|
||||
return arg
|
||||
}
|
||||
return templateArgs + " " + arg
|
||||
}
|
||||
|
||||
// processLocalChart handles local chart processing
|
||||
|
|
|
|||
Loading…
Reference in New Issue