From e17a007719c2dca65a230e25efdac354f69d632f Mon Sep 17 00:00:00 2001 From: monopole Date: Wed, 15 Sep 2021 17:05:17 -0700 Subject: [PATCH] Clean up factor_test before adding DeAnchor call --- api/resource/factory_test.go | 168 +++++++++++++++++------------------ 1 file changed, 81 insertions(+), 87 deletions(-) diff --git a/api/resource/factory_test.go b/api/resource/factory_test.go index 7d01eb695..905b86202 100644 --- a/api/resource/factory_test.go +++ b/api/resource/factory_test.go @@ -5,7 +5,7 @@ package resource_test import ( "fmt" - "reflect" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -15,7 +15,7 @@ import ( "sigs.k8s.io/kustomize/kyaml/filesys" ) -func TestSliceFromBytes(t *testing.T) { +func TestRNodesFromBytes(t *testing.T) { type testCase struct { input string expected []string @@ -399,60 +399,11 @@ binaryData: } } -func TestSliceFromBytesMore(t *testing.T) { - testConfigMap := - map[string]interface{}{ - "apiVersion": "v1", - "kind": "ConfigMap", - "metadata": map[string]interface{}{ - "name": "winnie", - }, - } - testDeploymentSpec := map[string]interface{}{ - "template": map[string]interface{}{ - "spec": map[string]interface{}{ - "hostAliases": []interface{}{ - map[string]interface{}{ - "hostnames": []interface{}{ - "a.example.com", - }, - "ip": "8.8.8.8", - }, - }, - }, - }, - } - testDeploymentA := map[string]interface{}{ - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": map[string]interface{}{ - "name": "deployment-a", - }, - "spec": testDeploymentSpec, - } - testDeploymentB := map[string]interface{}{ - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": map[string]interface{}{ - "name": "deployment-b", - }, - "spec": testDeploymentSpec, - } - testDeploymentList := - map[string]interface{}{ - "apiVersion": "v1", - "kind": "DeploymentList", - "items": []interface{}{ - testDeploymentA, - testDeploymentB, - }, - } - +func TestMoreRNodesFromBytes(t *testing.T) { type expected struct { - out []map[string]interface{} + out []string isErr bool } - testCases := map[string]struct { input []byte exp expected @@ -465,16 +416,16 @@ func TestSliceFromBytesMore(t *testing.T) { }, "noBytes": { input: []byte{}, - exp: expected{ - out: []map[string]interface{}{}, - }, + exp: expected{}, }, "goodJson": { input: []byte(` {"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie"}} `), exp: expected{ - out: []map[string]interface{}{testConfigMap}, + out: []string{ + `{"apiVersion": "v1", "kind": "ConfigMap", "metadata": {"name": "winnie"}}`, + }, }, }, "goodYaml1": { @@ -485,7 +436,12 @@ metadata: name: winnie `), exp: expected{ - out: []map[string]interface{}{testConfigMap}, + out: []string{` +apiVersion: v1 +kind: ConfigMap +metadata: + name: winnie +`}, }, }, "goodYaml2": { @@ -501,7 +457,17 @@ metadata: name: winnie `), exp: expected{ - out: []map[string]interface{}{testConfigMap, testConfigMap}, + out: []string{` +apiVersion: v1 +kind: ConfigMap +metadata: + name: winnie +`, ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: winnie +`}, }, }, "localConfigYaml": { @@ -520,7 +486,12 @@ metadata: name: winnie `), exp: expected{ - out: []map[string]interface{}{testConfigMap}, + out: []string{` +apiVersion: v1 +kind: ConfigMap +metadata: + name: winnie +`}, }, }, "garbageInOneOfTwoObjects": { @@ -545,7 +516,7 @@ WOOOOOOOOOOOOOOOOOOOOOOOOT: woot `), exp: expected{ - out: []map[string]interface{}{}, + out: []string{}, }, }, "Missing .metadata.name in object": { @@ -591,9 +562,18 @@ items: name: winnie `), exp: expected{ - out: []map[string]interface{}{ - testConfigMap, - testConfigMap}, + out: []string{` +apiVersion: v1 +kind: ConfigMap +metadata: + name: winnie +`, ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: winnie +`, + }, }, }, "ConfigMapList": { @@ -611,9 +591,9 @@ items: name: winnie `), exp: expected{ - out: []map[string]interface{}{ - testConfigMap, - testConfigMap, + out: []string{ + `{"apiVersion": "v1", "kind": "ConfigMap", "metadata": {"name": "winnie"}}`, + `{"apiVersion": "v1", "kind": "ConfigMap", "metadata": {"name": "winnie"}}`, }, }, }, @@ -626,7 +606,7 @@ items: kind: Deployment metadata: name: deployment-a - spec: &hostAliases + spec: &foo template: spec: hostAliases: @@ -638,23 +618,42 @@ items: metadata: name: deployment-b spec: - <<: *hostAliases + *foo `), exp: expected{ - // TODO(3271): This should work. - // https://github.com/kubernetes-sigs/kustomize/issues/3271 - // json.Marshal(obj) fails on the 2nd list item. - // The value of the 1st list item's first spec field is - // map[string]interface{} - // The value of the 2nd list item's first spec field is - // map[interface{}]interface{} - // which causes a encoding/json.UnsupportedTypeError. - isErr: true, - out: []map[string]interface{}{testDeploymentList}, + out: []string{ + `{"apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": "deployment-a"}, ` + + `"spec": {"template": {"spec": {"hostAliases": [{"hostnames": ["a.example.com"], "ip": "8.8.8.8"}]}}}}`, + `{"apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": "deployment-b"}, ` + + `"spec": {"template": {"spec": {"hostAliases": [{"hostnames": ["a.example.com"], "ip": "8.8.8.8"}]}}}}`}, + }, + }, + "simpleAnchor": { + input: []byte(` +apiVersion: v1 +kind: ConfigMap +metadata: + name: wildcard +data: + color: &color-used blue + feeling: *color-used +`), + exp: expected{ + // TODO(#3675) : the anchor should be replaced. + // Anchors are replaced in the List above due to a different code path + // (when the list is inlined). + out: []string{` +apiVersion: v1 +kind: ConfigMap +metadata: + name: wildcard +data: + color: &color-used blue + feeling: *color-used +`}, }, }, } - for n := range testCases { tc := testCases[n] t.Run(n, func(t *testing.T) { @@ -666,15 +665,10 @@ items: assert.False(t, tc.exp.isErr) assert.Equal(t, len(tc.exp.out), len(rs)) for i := range rs { - rsMap, err := rs[i].Map() + actual, err := rs[i].String() assert.NoError(t, err) assert.Equal( - t, fmt.Sprintf("%v", tc.exp.out[i]), fmt.Sprintf("%v", rsMap)) - m, _ := rs[i].Map() - if !reflect.DeepEqual(tc.exp.out[i], m) { - t.Fatalf("%s:\nexpected: %v\n actual: %v", - n, tc.exp.out[i], m) - } + t, strings.TrimSpace(tc.exp.out[i]), strings.TrimSpace(actual)) } }) }