From 2c0c805d1a73a49be5346bcb5125fc3a89dd61cf Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Sun, 29 Mar 2020 20:39:15 +0900 Subject: [PATCH] fix: merge environment values by ovewriting with empty values 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) + } +}