update test to table driven with assertions & test GetMergedValues (#1721)
This commit is contained in:
		
							parent
							
								
									04b5151285
								
							
						
					
					
						commit
						5963dd364d
					
				
							
								
								
									
										2
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										2
									
								
								go.mod
								
								
								
								
							|  | @ -184,6 +184,7 @@ require ( | ||||||
| 	github.com/docker/go-metrics v0.0.1 // indirect | 	github.com/docker/go-metrics v0.0.1 // indirect | ||||||
| 	github.com/dustin/go-humanize v1.0.1 // indirect | 	github.com/dustin/go-humanize v1.0.1 // indirect | ||||||
| 	github.com/emicklei/go-restful/v3 v3.11.0 // indirect | 	github.com/emicklei/go-restful/v3 v3.11.0 // indirect | ||||||
|  | 	github.com/evanphx/json-patch v5.9.0+incompatible // indirect | ||||||
| 	github.com/extism/go-sdk v1.3.1 // indirect | 	github.com/extism/go-sdk v1.3.1 // indirect | ||||||
| 	github.com/felixge/httpsnoop v1.0.4 // indirect | 	github.com/felixge/httpsnoop v1.0.4 // indirect | ||||||
| 	github.com/fxamacker/cbor/v2 v2.7.0 // indirect | 	github.com/fxamacker/cbor/v2 v2.7.0 // indirect | ||||||
|  | @ -232,6 +233,7 @@ require ( | ||||||
| 	github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | 	github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | ||||||
| 	github.com/mailru/easyjson v0.7.7 // indirect | 	github.com/mailru/easyjson v0.7.7 // indirect | ||||||
| 	github.com/mattn/go-isatty v0.0.20 // indirect | 	github.com/mattn/go-isatty v0.0.20 // indirect | ||||||
|  | 	github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect | ||||||
| 	github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect | 	github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect | ||||||
| 	github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect | 	github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect | ||||||
| 	github.com/moby/locker v1.0.1 // indirect | 	github.com/moby/locker v1.0.1 // indirect | ||||||
|  |  | ||||||
							
								
								
									
										4
									
								
								go.sum
								
								
								
								
							
							
						
						
									
										4
									
								
								go.sum
								
								
								
								
							|  | @ -443,6 +443,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m | ||||||
| github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= | github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= | ||||||
| github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= | github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= | ||||||
| github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= | ||||||
|  | github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls= | ||||||
|  | github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= | ||||||
| github.com/extism/go-sdk v1.3.1 h1:eVpuv36b67Km/tAb7Cq6msHEW8kkdFgpZO/7fCwjuoE= | github.com/extism/go-sdk v1.3.1 h1:eVpuv36b67Km/tAb7Cq6msHEW8kkdFgpZO/7fCwjuoE= | ||||||
| github.com/extism/go-sdk v1.3.1/go.mod h1:tPMWfCSOThie3LSTSZKbrQjRm2oAXxUUjSE4HJWjYQM= | github.com/extism/go-sdk v1.3.1/go.mod h1:tPMWfCSOThie3LSTSZKbrQjRm2oAXxUUjSE4HJWjYQM= | ||||||
| github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= | github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= | ||||||
|  | @ -774,6 +776,8 @@ github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRC | ||||||
| github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= | github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= | ||||||
| github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||||||
| github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= | github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= | ||||||
|  | github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= | ||||||
|  | github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= | ||||||
| github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= | github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= | ||||||
| github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= | github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= | ||||||
| github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= | github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= | ||||||
|  |  | ||||||
|  | @ -4,12 +4,20 @@ import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
| 	"github.com/stretchr/testify/require" | 	"github.com/stretchr/testify/require" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // See https://github.com/roboll/helmfile/issues/1150
 | func TestMerge_Issues(t *testing.T) { | ||||||
| func TestMerge_OverwriteNilValue_Issue1150(t *testing.T) { | 	tests := []struct { | ||||||
| 	dst := &Environment{ | 		name     string | ||||||
|  | 		dst      *Environment | ||||||
|  | 		src      *Environment | ||||||
|  | 		expected map[string]any | ||||||
|  | 	}{ | ||||||
|  | 		{ | ||||||
|  | 			name: "OverwriteNilValue_Issue1150", | ||||||
|  | 			dst: &Environment{ | ||||||
| 				Name: "dst", | 				Name: "dst", | ||||||
| 				Values: map[string]any{ | 				Values: map[string]any{ | ||||||
| 					"components": map[string]any{ | 					"components": map[string]any{ | ||||||
|  | @ -17,9 +25,8 @@ func TestMerge_OverwriteNilValue_Issue1150(t *testing.T) { | ||||||
| 					}, | 					}, | ||||||
| 				}, | 				}, | ||||||
| 				Defaults: nil, | 				Defaults: nil, | ||||||
| 	} | 			}, | ||||||
| 
 | 			src: &Environment{ | ||||||
| 	src := &Environment{ |  | ||||||
| 				Name: "src", | 				Name: "src", | ||||||
| 				Values: map[string]any{ | 				Values: map[string]any{ | ||||||
| 					"components": map[string]any{ | 					"components": map[string]any{ | ||||||
|  | @ -29,31 +36,18 @@ func TestMerge_OverwriteNilValue_Issue1150(t *testing.T) { | ||||||
| 					}, | 					}, | ||||||
| 				}, | 				}, | ||||||
| 				Defaults: nil, | 				Defaults: nil, | ||||||
| 	} | 			}, | ||||||
| 
 | 			expected: map[string]any{ | ||||||
| 	merged, err := dst.Merge(src) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	actual := merged.Values |  | ||||||
| 
 |  | ||||||
| 	expected := map[string]any{ |  | ||||||
| 				"components": map[string]any{ | 				"components": map[string]any{ | ||||||
| 					"etcd-operator": map[string]any{ | 					"etcd-operator": map[string]any{ | ||||||
| 						"version": "0.10.3", | 						"version": "0.10.3", | ||||||
| 					}, | 					}, | ||||||
| 				}, | 				}, | ||||||
| 	} | 			}, | ||||||
| 
 | 		}, | ||||||
| 	if diff := cmp.Diff(expected, actual); diff != "" { | 		{ | ||||||
| 		t.Errorf(diff) | 			name: "OverwriteWithNilValue_Issue1154", | ||||||
| 	} | 			dst: &Environment{ | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // See https://github.com/roboll/helmfile/issues/1154
 |  | ||||||
| func TestMerge_OverwriteWithNilValue_Issue1154(t *testing.T) { |  | ||||||
| 	dst := &Environment{ |  | ||||||
| 				Name: "dst", | 				Name: "dst", | ||||||
| 				Values: map[string]any{ | 				Values: map[string]any{ | ||||||
| 					"components": map[string]any{ | 					"components": map[string]any{ | ||||||
|  | @ -63,9 +57,8 @@ func TestMerge_OverwriteWithNilValue_Issue1154(t *testing.T) { | ||||||
| 					}, | 					}, | ||||||
| 				}, | 				}, | ||||||
| 				Defaults: nil, | 				Defaults: nil, | ||||||
| 	} | 			}, | ||||||
| 
 | 			src: &Environment{ | ||||||
| 	src := &Environment{ |  | ||||||
| 				Name: "src", | 				Name: "src", | ||||||
| 				Values: map[string]any{ | 				Values: map[string]any{ | ||||||
| 					"components": map[string]any{ | 					"components": map[string]any{ | ||||||
|  | @ -76,26 +69,26 @@ func TestMerge_OverwriteWithNilValue_Issue1154(t *testing.T) { | ||||||
| 					}, | 					}, | ||||||
| 				}, | 				}, | ||||||
| 				Defaults: nil, | 				Defaults: nil, | ||||||
| 	} | 			}, | ||||||
| 
 | 			expected: map[string]any{ | ||||||
| 	merged, err := dst.Merge(src) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	actual := merged.Values |  | ||||||
| 
 |  | ||||||
| 	expected := map[string]any{ |  | ||||||
| 				"components": map[string]any{ | 				"components": map[string]any{ | ||||||
| 					"etcd-operator": map[string]any{ | 					"etcd-operator": map[string]any{ | ||||||
| 						"version": "0.10.3", | 						"version": "0.10.3", | ||||||
| 					}, | 					}, | ||||||
| 					"prometheus": nil, | 					"prometheus": nil, | ||||||
| 				}, | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if diff := cmp.Diff(expected, actual); diff != "" { | 	for _, tt := range tests { | ||||||
| 		t.Errorf(diff) | 		t.Run(tt.name, func(t *testing.T) { | ||||||
|  | 			merged, err := tt.dst.Merge(tt.src) | ||||||
|  | 			require.NoError(t, err) | ||||||
|  | 
 | ||||||
|  | 			actual := merged.Values | ||||||
|  | 			assert.Empty(t, cmp.Diff(tt.expected, actual), "unexpected diff") | ||||||
|  | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -105,3 +98,46 @@ func TestNew(t *testing.T) { | ||||||
| 
 | 
 | ||||||
| 	require.Equal(t, envName, env.Name, "environment name should be %s, but got %s", envName, env.Name) | 	require.Equal(t, envName, env.Name, "environment name should be %s, but got %s", envName, env.Name) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestEnvironment_DeepCopy(t *testing.T) { | ||||||
|  | 	env := &Environment{ | ||||||
|  | 		Name: "test", | ||||||
|  | 		Values: map[string]any{ | ||||||
|  | 			"foo": "bar", | ||||||
|  | 		}, | ||||||
|  | 		Defaults: map[string]any{ | ||||||
|  | 			"baz": "qux", | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	copy := env.DeepCopy() | ||||||
|  | 
 | ||||||
|  | 	assert.Equal(t, env.Name, copy.Name) | ||||||
|  | 	assert.Equal(t, env.Values, copy.Values) | ||||||
|  | 	assert.Equal(t, env.Defaults, copy.Defaults) | ||||||
|  | 
 | ||||||
|  | 	copy.Values["foo"] = "modified" | ||||||
|  | 	assert.NotEqual(t, env.Values["foo"], copy.Values["foo"]) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestEnvironment_GetMergedValues(t *testing.T) { | ||||||
|  | 	env := &Environment{ | ||||||
|  | 		Name: "test", | ||||||
|  | 		Values: map[string]any{ | ||||||
|  | 			"foo": "bar", | ||||||
|  | 		}, | ||||||
|  | 		Defaults: map[string]any{ | ||||||
|  | 			"baz": "qux", | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	mergedValues, err := env.GetMergedValues() | ||||||
|  | 	require.NoError(t, err) | ||||||
|  | 
 | ||||||
|  | 	expected := map[string]any{ | ||||||
|  | 		"foo": "bar", | ||||||
|  | 		"baz": "qux", | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	assert.Equal(t, expected, mergedValues) | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue