sigs.k8s.io/cluster-api@v1.6.3/exp/runtime/topologymutation/variables_test.go (about) 1 /* 2 Copyright 2022 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 http://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 topologymutation 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 24 ) 25 26 func Test_GetRawTemplateVariable(t *testing.T) { 27 g := NewWithT(t) 28 29 varA := apiextensionsv1.JSON{Raw: toJSON("a")} 30 tests := []struct { 31 name string 32 variables map[string]apiextensionsv1.JSON 33 variableName string 34 expectedValue *apiextensionsv1.JSON 35 expectedFound bool 36 expectedErr bool 37 }{ 38 { 39 name: "Fails for invalid variable reference", 40 variables: nil, 41 variableName: "invalid[", 42 expectedValue: nil, 43 expectedFound: false, 44 expectedErr: true, 45 }, 46 { 47 name: "variable not found", 48 variables: nil, 49 variableName: "notEsists", 50 expectedValue: nil, 51 expectedFound: false, 52 expectedErr: false, 53 }, 54 { 55 name: "return a variable", 56 variables: map[string]apiextensionsv1.JSON{ 57 "a": varA, 58 }, 59 variableName: "a", 60 expectedValue: &varA, 61 expectedFound: true, 62 expectedErr: false, 63 }, 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 value, found, err := GetVariable(tt.variables, tt.variableName) 68 69 g.Expect(value).To(BeComparableTo(tt.expectedValue)) 70 g.Expect(found).To(Equal(tt.expectedFound)) 71 if tt.expectedErr { 72 g.Expect(err).To(HaveOccurred()) 73 } else { 74 g.Expect(err).ToNot(HaveOccurred()) 75 } 76 }) 77 } 78 } 79 80 func Test_GetStringTemplateVariable(t *testing.T) { 81 g := NewWithT(t) 82 83 varA := apiextensionsv1.JSON{Raw: toJSON("a")} 84 tests := []struct { 85 name string 86 variables map[string]apiextensionsv1.JSON 87 variableName string 88 expectedValue string 89 expectedFound bool 90 expectedErr bool 91 }{ 92 { 93 name: "Fails for invalid variable reference", 94 variables: nil, 95 variableName: "invalid[", 96 expectedValue: "", 97 expectedFound: false, 98 expectedErr: true, 99 }, 100 { 101 name: "variable not found", 102 variables: nil, 103 variableName: "notEsists", 104 expectedValue: "", 105 expectedFound: false, 106 expectedErr: false, 107 }, 108 { 109 name: "variable not found", 110 variables: map[string]apiextensionsv1.JSON{ 111 "a": varA, 112 }, 113 variableName: "a", 114 expectedValue: "a", 115 expectedFound: true, 116 expectedErr: false, 117 }, 118 } 119 for _, tt := range tests { 120 t.Run(tt.name, func(t *testing.T) { 121 value, found, err := GetStringVariable(tt.variables, tt.variableName) 122 123 g.Expect(value).To(Equal(tt.expectedValue)) 124 g.Expect(found).To(Equal(tt.expectedFound)) 125 if tt.expectedErr { 126 g.Expect(err).To(HaveOccurred()) 127 } else { 128 g.Expect(err).ToNot(HaveOccurred()) 129 } 130 }) 131 } 132 }