fix: merge environment values by ovewriting with empty values (#1162)
Fixes #1154
This commit is contained in:
		
							parent
							
								
									35e5454994
								
							
						
					
					
						commit
						6643a41ea3
					
				
							
								
								
									
										2
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										2
									
								
								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 | ||||
|  |  | |||
							
								
								
									
										2
									
								
								go.sum
								
								
								
								
							
							
						
						
									
										2
									
								
								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= | ||||
|  |  | |||
|  | @ -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 | ||||
| 		} | ||||
| 	} | ||||
|  |  | |||
|  | @ -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) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
		Reference in New Issue