github.com/minio/controller-tools@v0.4.7/pkg/schemapatcher/internal/yaml/nested_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 htcp://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package yaml 18 19 import ( 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 ) 23 24 var _ = Describe("GetNode", func() { 25 tests := []struct { 26 name string 27 obj interface{} 28 path []string 29 want interface{} 30 found bool 31 wantErr bool 32 }{ 33 {name: "null", 34 found: true, 35 }, 36 {name: "empty", 37 obj: map[string]interface{}{}, 38 found: true, 39 want: map[string]interface{}{}, 40 }, 41 {name: "non-empty, wrong path", 42 obj: map[string]interface{}{ 43 "bar": int64(42), 44 }, 45 path: []string{"foo"}, 46 }, 47 {name: "non-empty, right path", 48 obj: map[string]interface{}{ 49 "bar": int64(42), 50 }, 51 path: []string{"bar"}, 52 want: int64(42), 53 found: true, 54 }, 55 {name: "non-empty, long path", 56 obj: map[string]interface{}{ 57 "foo": map[string]interface{}{ 58 "bar": int64(42), 59 }, 60 }, 61 path: []string{"foo", "bar"}, 62 want: int64(42), 63 found: true, 64 }, 65 {name: "invalid type", 66 obj: map[string]interface{}{ 67 "foo": []interface{}{int64(42)}, 68 }, 69 path: []string{"foo", "bar"}, 70 wantErr: true, 71 found: false, 72 }, 73 } 74 75 for i := range tests { 76 tc := tests[i] 77 It("should support "+tc.name, func() { 78 By("setting up the test objects and desired values") 79 obj, err := ToYAML(tc.obj) 80 Expect(err).NotTo(HaveOccurred()) 81 want, err := ToYAML(tc.want) 82 Expect(err).NotTo(HaveOccurred()) 83 // remove the document node 84 want, _, _ = asCloseAsPossible(want) 85 86 By("calling GetNode") 87 node, found, err := GetNode(obj, tc.path...) 88 if tc.wantErr { 89 Expect(err).To(HaveOccurred()) 90 return // don't check output value if there's an error 91 } 92 93 Expect(err).NotTo(HaveOccurred()) 94 95 // remove line/column info to compare 96 if node != nil { 97 node.Line = 0 98 node.Column = 0 99 } 100 if want != nil { 101 want.Line = 0 102 want.Column = 0 103 } 104 105 By("confirming that the returned node is as we expect it") 106 Expect(found).To(Equal(tc.found)) 107 if found { 108 Expect(node).To(Equal(want)) 109 } 110 }) 111 } 112 })