github.com/minio/controller-tools@v0.4.7/pkg/schemapatcher/internal/yaml/set_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 "gopkg.in/yaml.v3" 23 ) 24 25 var _ = Describe("SetNode", func() { 26 tests := []struct { 27 name string 28 obj interface{} 29 value interface{} 30 path []string 31 want interface{} 32 wantErr bool 33 }{ 34 {name: "null"}, 35 {name: "empty, null value", 36 obj: map[string]interface{}{}, 37 value: nil, 38 want: nil, 39 }, 40 {name: "empty, non-null value", 41 obj: map[string]interface{}{}, 42 value: int64(64), 43 want: int64(64), 44 }, 45 {name: "non-empty, unknown path", 46 obj: map[string]interface{}{ 47 "bar": int64(42), 48 }, 49 value: "foo", 50 path: []string{"foo"}, 51 want: map[string]interface{}{ 52 "bar": int64(42), 53 "foo": "foo", 54 }, 55 }, 56 {name: "non-empty, existing path", 57 obj: map[string]interface{}{ 58 "bar": int64(42), 59 }, 60 value: "foo", 61 path: []string{"bar"}, 62 want: map[string]interface{}{ 63 "bar": "foo", 64 }, 65 }, 66 {name: "non-empty, long path", 67 obj: map[string]interface{}{ 68 "foo": map[string]interface{}{ 69 "bar": int64(42), 70 }, 71 }, 72 value: "foo", 73 path: []string{"foo", "bar"}, 74 want: map[string]interface{}{ 75 "foo": map[string]interface{}{ 76 "bar": "foo", 77 }, 78 }, 79 }, 80 {name: "invalid type", 81 obj: map[string]interface{}{ 82 "foo": []interface{}{int64(42)}, 83 }, 84 value: "foo", 85 path: []string{"foo", "bar"}, 86 wantErr: true, 87 }, 88 } 89 90 for i := range tests { 91 tc := tests[i] 92 It("should support "+tc.name, func() { 93 By("setting up the test objects and desired values") 94 obj, err := ToYAML(tc.obj) 95 Expect(err).NotTo(HaveOccurred()) 96 value, err := ToYAML(tc.value) 97 Expect(err).NotTo(HaveOccurred()) 98 value, _, _ = asCloseAsPossible(value) 99 want, err := ToYAML(tc.want) 100 Expect(err).NotTo(HaveOccurred()) 101 102 By("calling SetNode") 103 if tc.wantErr { 104 Expect(SetNode(obj, *value, tc.path...)).NotTo(Succeed()) 105 return // don't check output value if there's an error 106 } 107 108 Expect(SetNode(obj, *value, tc.path...)).To(Succeed()) 109 110 By("marshalling the desired and actual objects") 111 wantOut, err := yaml.Marshal(want) 112 Expect(err).NotTo(HaveOccurred()) 113 objOut, err := yaml.Marshal(obj) 114 Expect(err).NotTo(HaveOccurred()) 115 116 By("comparing actual and desired output") 117 Expect(string(wantOut)).To(Equal(string(objOut))) 118 }) 119 } 120 })