Clean up factor_test before adding DeAnchor call

This commit is contained in:
monopole
2021-09-15 17:05:17 -07:00
parent 402f6ca72b
commit e17a007719

View File

@@ -5,7 +5,7 @@ package resource_test
import ( import (
"fmt" "fmt"
"reflect" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -15,7 +15,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/filesys"
) )
func TestSliceFromBytes(t *testing.T) { func TestRNodesFromBytes(t *testing.T) {
type testCase struct { type testCase struct {
input string input string
expected []string expected []string
@@ -399,60 +399,11 @@ binaryData:
} }
} }
func TestSliceFromBytesMore(t *testing.T) { func TestMoreRNodesFromBytes(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,
},
}
type expected struct { type expected struct {
out []map[string]interface{} out []string
isErr bool isErr bool
} }
testCases := map[string]struct { testCases := map[string]struct {
input []byte input []byte
exp expected exp expected
@@ -465,16 +416,16 @@ func TestSliceFromBytesMore(t *testing.T) {
}, },
"noBytes": { "noBytes": {
input: []byte{}, input: []byte{},
exp: expected{ exp: expected{},
out: []map[string]interface{}{},
},
}, },
"goodJson": { "goodJson": {
input: []byte(` input: []byte(`
{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie"}} {"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie"}}
`), `),
exp: expected{ exp: expected{
out: []map[string]interface{}{testConfigMap}, out: []string{
`{"apiVersion": "v1", "kind": "ConfigMap", "metadata": {"name": "winnie"}}`,
},
}, },
}, },
"goodYaml1": { "goodYaml1": {
@@ -485,7 +436,12 @@ metadata:
name: winnie name: winnie
`), `),
exp: expected{ exp: expected{
out: []map[string]interface{}{testConfigMap}, out: []string{`
apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
`},
}, },
}, },
"goodYaml2": { "goodYaml2": {
@@ -501,7 +457,17 @@ metadata:
name: winnie name: winnie
`), `),
exp: expected{ 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": { "localConfigYaml": {
@@ -520,7 +486,12 @@ metadata:
name: winnie name: winnie
`), `),
exp: expected{ exp: expected{
out: []map[string]interface{}{testConfigMap}, out: []string{`
apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
`},
}, },
}, },
"garbageInOneOfTwoObjects": { "garbageInOneOfTwoObjects": {
@@ -545,7 +516,7 @@ WOOOOOOOOOOOOOOOOOOOOOOOOT: woot
`), `),
exp: expected{ exp: expected{
out: []map[string]interface{}{}, out: []string{},
}, },
}, },
"Missing .metadata.name in object": { "Missing .metadata.name in object": {
@@ -591,9 +562,18 @@ items:
name: winnie name: winnie
`), `),
exp: expected{ exp: expected{
out: []map[string]interface{}{ out: []string{`
testConfigMap, apiVersion: v1
testConfigMap}, kind: ConfigMap
metadata:
name: winnie
`, `
apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
`,
},
}, },
}, },
"ConfigMapList": { "ConfigMapList": {
@@ -611,9 +591,9 @@ items:
name: winnie name: winnie
`), `),
exp: expected{ exp: expected{
out: []map[string]interface{}{ out: []string{
testConfigMap, `{"apiVersion": "v1", "kind": "ConfigMap", "metadata": {"name": "winnie"}}`,
testConfigMap, `{"apiVersion": "v1", "kind": "ConfigMap", "metadata": {"name": "winnie"}}`,
}, },
}, },
}, },
@@ -626,7 +606,7 @@ items:
kind: Deployment kind: Deployment
metadata: metadata:
name: deployment-a name: deployment-a
spec: &hostAliases spec: &foo
template: template:
spec: spec:
hostAliases: hostAliases:
@@ -638,23 +618,42 @@ items:
metadata: metadata:
name: deployment-b name: deployment-b
spec: spec:
<<: *hostAliases *foo
`), `),
exp: expected{ exp: expected{
// TODO(3271): This should work. out: []string{
// https://github.com/kubernetes-sigs/kustomize/issues/3271 `{"apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": "deployment-a"}, ` +
// json.Marshal(obj) fails on the 2nd list item. `"spec": {"template": {"spec": {"hostAliases": [{"hostnames": ["a.example.com"], "ip": "8.8.8.8"}]}}}}`,
// The value of the 1st list item's first spec field is `{"apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": "deployment-b"}, ` +
// map[string]interface{} `"spec": {"template": {"spec": {"hostAliases": [{"hostnames": ["a.example.com"], "ip": "8.8.8.8"}]}}}}`},
// The value of the 2nd list item's first spec field is },
// map[interface{}]interface{} },
// which causes a encoding/json.UnsupportedTypeError. "simpleAnchor": {
isErr: true, input: []byte(`
out: []map[string]interface{}{testDeploymentList}, 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 { for n := range testCases {
tc := testCases[n] tc := testCases[n]
t.Run(n, func(t *testing.T) { t.Run(n, func(t *testing.T) {
@@ -666,15 +665,10 @@ items:
assert.False(t, tc.exp.isErr) assert.False(t, tc.exp.isErr)
assert.Equal(t, len(tc.exp.out), len(rs)) assert.Equal(t, len(tc.exp.out), len(rs))
for i := range rs { for i := range rs {
rsMap, err := rs[i].Map() actual, err := rs[i].String()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal( assert.Equal(
t, fmt.Sprintf("%v", tc.exp.out[i]), fmt.Sprintf("%v", rsMap)) t, strings.TrimSpace(tc.exp.out[i]), strings.TrimSpace(actual))
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)
}
} }
}) })
} }