From 6643a41ea3bb2e8f87a860d562cfdb6b9ca72d1b Mon Sep 17 00:00:00 2001 From: KUOKA Yusuke Date: Sun, 29 Mar 2020 20:47:23 +0900 Subject: [PATCH] fix: merge environment values by ovewriting with empty values (#1162) Fixes #1154 --- go.mod | 2 +- go.sum | 2 + pkg/environment/environment.go | 2 +- pkg/environment/environment_test.go | 98 +++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 pkg/environment/environment_test.go diff --git a/go.mod b/go.mod index 275a5060..b32c4f5a 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/hashicorp/go-version v1.2.0 github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c // indirect github.com/huandu/xstrings v1.2.0 // indirect - github.com/imdario/mergo v0.3.8 + github.com/imdario/mergo v0.3.9 github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/mattn/go-colorable v0.1.4 // indirect github.com/mattn/go-isatty v0.0.10 // indirect diff --git a/go.sum b/go.sum index a414cbd0..5c792a19 100644 --- a/go.sum +++ b/go.sum @@ -473,6 +473,8 @@ github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28= github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ= github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= +github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= +github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb v0.0.0-20190411212539-d24b7ba8c4c4 h1:3K3KcD4S6/Y2hevi70EzUTNKOS3cryQyhUnkjE6Tz0w= github.com/influxdata/influxdb v0.0.0-20190411212539-d24b7ba8c4c4/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY= diff --git a/pkg/environment/environment.go b/pkg/environment/environment.go index f125b2e9..ba95213f 100644 --- a/pkg/environment/environment.go +++ b/pkg/environment/environment.go @@ -58,7 +58,7 @@ func (e *Environment) Merge(other *Environment) (*Environment, error) { } copy := e.DeepCopy() if other != nil { - if err := mergo.Merge(©, other, mergo.WithOverride); err != nil { + if err := mergo.Merge(©, other, mergo.WithOverride, mergo.WithOverwriteWithEmptyValue); err != nil { return nil, err } } diff --git a/pkg/environment/environment_test.go b/pkg/environment/environment_test.go new file mode 100644 index 00000000..dd2038ea --- /dev/null +++ b/pkg/environment/environment_test.go @@ -0,0 +1,98 @@ +package environment + +import ( + "github.com/google/go-cmp/cmp" + "testing" +) + +// See https://github.com/roboll/helmfile/issues/1150 +func TestMerge_OverwriteNilValue_Issue1150(t *testing.T) { + dst := &Environment{ + Name: "dst", + Values: map[string]interface{}{ + "components": map[string]interface{}{ + "etcd-operator": nil, + }, + }, + Defaults: nil, + } + + src := &Environment{ + Name: "src", + Values: map[string]interface{}{ + "components": map[string]interface{}{ + "etcd-operator": map[string]interface{}{ + "version": "0.10.3", + }, + }, + }, + Defaults: nil, + } + + merged, err := dst.Merge(src) + if err != nil { + t.Fatal(err) + } + + actual := merged.Values + + expected := map[string]interface{}{ + "components": map[string]interface{}{ + "etcd-operator": map[string]interface{}{ + "version": "0.10.3", + }, + }, + } + + if diff := cmp.Diff(expected, actual); diff != "" { + t.Errorf(diff) + } +} + +// See https://github.com/roboll/helmfile/issues/1154 +func TestMerge_OverwriteWithNilValue_Issue1154(t *testing.T) { + dst := &Environment{ + Name: "dst", + Values: map[string]interface{}{ + "components": map[string]interface{}{ + "etcd-operator": map[string]interface{}{ + "version": "0.10.0", + }, + }, + }, + Defaults: nil, + } + + src := &Environment{ + Name: "src", + Values: map[string]interface{}{ + "components": map[string]interface{}{ + "etcd-operator": map[string]interface{}{ + "version": "0.10.3", + }, + "prometheus": nil, + }, + }, + Defaults: nil, + } + + merged, err := dst.Merge(src) + if err != nil { + t.Fatal(err) + } + + actual := merged.Values + + expected := map[string]interface{}{ + "components": map[string]interface{}{ + "etcd-operator": map[string]interface{}{ + "version": "0.10.3", + }, + "prometheus": nil, + }, + } + + if diff := cmp.Diff(expected, actual); diff != "" { + t.Errorf(diff) + } +}