fix(#510): fix golangci-lint run error,add the unit test, add the compatibility when there is blank in the args values.

Signed-off-by: guofutan <guofutan@tencent.com>
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
guofutan 2022-11-11 13:53:08 +08:00 committed by yxxhero
parent 7be64f29cf
commit 4cc07daced
2 changed files with 36 additions and 9 deletions

View File

@ -133,18 +133,22 @@ func New(helmBinary string, enableLiveOutput bool, logger *zap.SugaredLogger, ku
func (helm *execer) SetExtraArgs(args ...string) { func (helm *execer) SetExtraArgs(args ...string) {
var extraArgs []string var extraArgs []string
var renderArgs []string
// reset the postRenderers and filter the post-renderer args from args and put into helm.postRenderers // reset the postRenderers and filter --post-renderer=xx or --post-renderer xxx from args and put into helm.postRenderers
helm.postRenderers = nil for i := 0; i < len(args); i++ {
for _, arg := range args { if strings.HasPrefix(args[i], "--post-renderer=") || strings.HasPrefix(args[i], "--post-renderer-args=") {
if strings.HasPrefix(arg, "--post-renderer=") || strings.HasPrefix(arg, "--post-renderer-args=") { renderArgs = append(renderArgs, args[i])
helm.postRenderers = append(helm.postRenderers, arg) } else if (args[i] == "--post-renderer" || args[i] == "--post-renderer-args") && i < len(args)-1 {
continue renderArgs = append(renderArgs, args[i])
renderArgs = append(renderArgs, args[i+1])
i++
} else {
extraArgs = append(extraArgs, args[i])
} }
extraArgs = append(extraArgs, arg)
} }
helm.extra = extraArgs helm.extra = extraArgs
helm.postRenderers = renderArgs
} }
func (helm *execer) SetHelmBinary(bin string) { func (helm *execer) SetHelmBinary(bin string) {
@ -263,7 +267,7 @@ func (helm *execer) SyncRelease(context HelmContext, name, chart string, flags .
} else { } else {
env["HELM_TILLER_HISTORY_MAX"] = strconv.Itoa(context.HistoryMax) env["HELM_TILLER_HISTORY_MAX"] = strconv.Itoa(context.HistoryMax)
} }
if helm.IsHelm3() { if helm.IsHelm3() {
flags = append(flags, helm.postRenderers...) flags = append(flags, helm.postRenderers...)
} }

View File

@ -69,6 +69,29 @@ func Test_SetExtraArgs(t *testing.T) {
if !reflect.DeepEqual(helm.extra, []string{"alpha", "beta"}) { if !reflect.DeepEqual(helm.extra, []string{"alpha", "beta"}) {
t.Error("helmexec.SetExtraArgs() - two extra arguments missing (overwriting the previous value)") t.Error("helmexec.SetExtraArgs() - two extra arguments missing (overwriting the previous value)")
} }
helm.SetExtraArgs("--post-renderer=aaa")
fmt.Println(helm.postRenderers)
if !reflect.DeepEqual(helm.postRenderers, []string{"--post-renderer=aaa"}) {
t.Error("helmexec.SetExtraArgs() - post-renderer assign arguments missing ")
}
helm.SetExtraArgs("--post-renderer", "aaa")
fmt.Println(helm.postRenderers)
if !reflect.DeepEqual(helm.postRenderers, []string{"--post-renderer", "aaa"}) {
t.Error("helmexec.SetExtraArgs() - post-renderer blank arguments missing ")
}
helm.SetExtraArgs("--post-renderer-args=bbb")
fmt.Println(helm.postRenderers)
if !reflect.DeepEqual(helm.postRenderers, []string{"--post-renderer-args=bbb"}) {
t.Error("helmexec.SetExtraArgs() - post-renderer-args assign arguments missing")
}
helm.SetExtraArgs("--post-renderer", "aaa", "--post-renderer-args=bbb")
if !reflect.DeepEqual(helm.postRenderers, []string{"--post-renderer", "aaa", "--post-renderer-args=bbb"}) {
t.Error("helmexec.SetExtraArgs() - post-renderer arguments not be set correct")
}
} }
func Test_SetHelmBinary(t *testing.T) { func Test_SetHelmBinary(t *testing.T) {