From 10331d9560dbf7049960b7be4e96dfe2283c43f5 Mon Sep 17 00:00:00 2001 From: Sam Wronski Date: Thu, 17 Jun 2021 01:46:35 -0700 Subject: [PATCH 1/4] Add GetValidatedDataMap to rnode --- kyaml/yaml/rnode.go | 21 ++++++++ kyaml/yaml/rnode_test.go | 100 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 75fc700f39..328bd07f08 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -546,6 +546,27 @@ func (rn *RNode) GetBinaryDataMap() map[string]string { return result } +func (rn *RNode) GetValidatedDataMap(expected []string) (map[string]string, error) { + dataMap := rn.GetDataMap() + err := rn.validateDataMap(dataMap, expected) + return dataMap, err +} + +func (rn *RNode) validateDataMap(dataMap map[string]string, expectedKeys []string) error { + for key := range dataMap { + found := false + for _, expected := range expectedKeys { + if expected == key { + found = true + } + } + if !found { + return fmt.Errorf("an unexpected key (%v) was found", key) + } + } + return nil +} + func (rn *RNode) SetDataMap(m map[string]string) { if rn == nil { log.Fatal("cannot set data map on nil Rnode") diff --git a/kyaml/yaml/rnode_test.go b/kyaml/yaml/rnode_test.go index 5477750d47..7a4498a2e0 100644 --- a/kyaml/yaml/rnode_test.go +++ b/kyaml/yaml/rnode_test.go @@ -4,6 +4,7 @@ package yaml import ( + "fmt" "reflect" "strings" "testing" @@ -185,6 +186,105 @@ func TestRNodeGetDataMap(t *testing.T) { } } +func TestRNodeGetValidatedDataMap(t *testing.T) { + emptyMap := map[string]string{} + testCases := map[string]struct { + theMap map[string]interface{} + theAllowedKeys []string + expected map[string]string + expectedError error + }{ + "nilResultEmptyKeys": { + theMap: nil, + theAllowedKeys: []string{}, + expected: emptyMap, + expectedError: nil, + }, + "empty": { + theMap: map[string]interface{}{}, + theAllowedKeys: []string{}, + expected: emptyMap, + expectedError: nil, + }, + "expectedKeysMatch": { + theMap: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "winnie", + }, + "data": map[string]string{ + "wine": "cabernet", + "truck": "ford", + "rocket": "falcon9", + "planet": "mars", + "city": "brownsville", + }, + }, + theAllowedKeys: []string{ + "wine", + "truck", + "rocket", + "planet", + "city", + "plane", + "country", + }, + // order irrelevant, because assert.Equals is smart about maps. + expected: map[string]string{ + "city": "brownsville", + "wine": "cabernet", + "planet": "mars", + "rocket": "falcon9", + "truck": "ford", + }, + expectedError: nil, + }, + "unexpectedKeyInConfigMap": { + theMap: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "winnie", + }, + "data": map[string]string{ + "wine": "cabernet", + "truck": "ford", + "rocket": "falcon9", + }, + }, + theAllowedKeys: []string{ + "wine", + "truck", + }, + // order irrelevant, because assert.Equals is smart about maps. + expected: map[string]string{ + "wine": "cabernet", + "rocket": "falcon9", + "truck": "ford", + }, + expectedError: fmt.Errorf("an unexpected key (rocket) was found"), + }, + } + + for n := range testCases { + tc := testCases[n] + t.Run(n, func(t *testing.T) { + rn, err := FromMap(tc.theMap) + if !assert.NoError(t, err) { + t.FailNow() + } + m, err := rn.GetValidatedDataMap(tc.theAllowedKeys) + if !assert.Equal(t, tc.expected, m) { + t.FailNow() + } + if !assert.Equal(t, tc.expectedError, err) { + t.FailNow() + } + }) + } +} + func TestRNodeSetDataMap(t *testing.T) { testCases := map[string]struct { theMap map[string]interface{} From a1f1c2d32f4c679e48f32244ff9059cc3fa660bd Mon Sep 17 00:00:00 2001 From: Sam Wronski Date: Thu, 17 Jun 2021 11:47:56 -0700 Subject: [PATCH 2/4] Add documentation --- kyaml/yaml/rnode.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 328bd07f08..7bc2ea483c 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -546,6 +546,8 @@ func (rn *RNode) GetBinaryDataMap() map[string]string { return result } +// GetValidatedDataMap retrieves the data map and returns an error if the data +// map contains entries which are not included in the expected set func (rn *RNode) GetValidatedDataMap(expected []string) (map[string]string, error) { dataMap := rn.GetDataMap() err := rn.validateDataMap(dataMap, expected) From b01da61d8338894c12c0c9b539a822d76c2fa600 Mon Sep 17 00:00:00 2001 From: Sam Wronski Date: Thu, 17 Jun 2021 16:12:09 -0700 Subject: [PATCH 3/4] Update argument name --- kyaml/yaml/rnode.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 7bc2ea483c..021133ba5a 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -547,10 +547,10 @@ func (rn *RNode) GetBinaryDataMap() map[string]string { } // GetValidatedDataMap retrieves the data map and returns an error if the data -// map contains entries which are not included in the expected set -func (rn *RNode) GetValidatedDataMap(expected []string) (map[string]string, error) { +// map contains entries which are not included in the expectedKeys set. +func (rn *RNode) GetValidatedDataMap(expectedKeys []string) (map[string]string, error) { dataMap := rn.GetDataMap() - err := rn.validateDataMap(dataMap, expected) + err := rn.validateDataMap(dataMap, expectedKeys) return dataMap, err } From 1801d332875c9fbb0204df30e45b9b22f142d4f0 Mon Sep 17 00:00:00 2001 From: Sam Wronski Date: Fri, 18 Jun 2021 10:14:17 -0700 Subject: [PATCH 4/4] Add error when datamap is nil --- kyaml/yaml/rnode.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 021133ba5a..e0678a7eb5 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -555,6 +555,9 @@ func (rn *RNode) GetValidatedDataMap(expectedKeys []string) (map[string]string, } func (rn *RNode) validateDataMap(dataMap map[string]string, expectedKeys []string) error { + if dataMap == nil { + return fmt.Errorf("The datamap is unassigned") + } for key := range dataMap { found := false for _, expected := range expectedKeys {