Add kunstruct impl of Get/SetDataMap, replace Resource.Merge

This commit is contained in:
jregan
2020-12-21 07:22:03 -08:00
parent 35087ed0cc
commit 735befef19
10 changed files with 280 additions and 48 deletions

View File

@@ -6,6 +6,8 @@ package kunstruct
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
var kunstructured = NewKunstructuredFactoryImpl().FromMap(map[string]interface{}{
@@ -557,3 +559,139 @@ func compareValues(t *testing.T, name string, pathToField string, expectedValue
t.Logf("%T value at `%s`", typedV, pathToField)
}
}
func TestKunstGetDataMap(t *testing.T) {
emptyMap := map[string]string{}
testCases := map[string]struct {
theMap map[string]interface{}
expected map[string]string
}{
"actuallyNil": {
theMap: nil,
expected: emptyMap,
},
"empty": {
theMap: map[string]interface{}{},
expected: emptyMap,
},
"mostlyEmpty": {
theMap: map[string]interface{}{
"hey": "there",
},
expected: emptyMap,
},
"noNameConfigMap": {
theMap: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
},
expected: emptyMap,
},
"configMap": {
theMap: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "winnie",
},
"data": map[string]interface{}{
"wine": "cabernet",
"truck": "ford",
"rocket": "falcon9",
"planet": "mars",
"city": "brownsville",
},
},
// order irrelevant, because assert.Equals is smart about maps.
expected: map[string]string{
"city": "brownsville",
"wine": "cabernet",
"planet": "mars",
"rocket": "falcon9",
"truck": "ford",
},
},
}
for n := range testCases {
tc := testCases[n]
t.Run(n, func(t *testing.T) {
kunstr := NewKunstructuredFactoryImpl().FromMap(tc.theMap)
m := kunstr.GetDataMap()
if !assert.Equal(t, tc.expected, m) {
t.FailNow()
}
})
}
}
func TestKunstSetDataMap(t *testing.T) {
testCases := map[string]struct {
theMap map[string]interface{}
input map[string]string
expected map[string]string
}{
"empty": {
theMap: map[string]interface{}{},
input: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
expected: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
},
"replace": {
theMap: map[string]interface{}{
"foo": 3,
"data": map[string]string{
"rocket": "falcon9",
"planet": "mars",
},
},
input: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
expected: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
},
"clear1": {
theMap: map[string]interface{}{
"foo": 3,
"data": map[string]string{
"rocket": "falcon9",
"planet": "mars",
},
},
input: map[string]string{},
expected: map[string]string{},
},
"clear2": {
theMap: map[string]interface{}{
"foo": 3,
"data": map[string]string{
"rocket": "falcon9",
"planet": "mars",
},
},
input: nil,
expected: map[string]string{},
},
}
for n := range testCases {
tc := testCases[n]
t.Run(n, func(t *testing.T) {
kunstr := NewKunstructuredFactoryImpl().FromMap(tc.theMap)
kunstr.SetDataMap(tc.input)
m := kunstr.GetDataMap()
if !assert.Equal(t, tc.expected, m) {
t.FailNow()
}
})
}
}