sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/repository/template_client_test.go (about) 1 /* 2 Copyright 2020 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 repository 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 . "github.com/onsi/gomega" 25 "github.com/pkg/errors" 26 27 clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" 28 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" 29 yaml "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor" 30 "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" 31 ) 32 33 func Test_templates_Get(t *testing.T) { 34 p1 := config.NewProvider("p1", "", clusterctlv1.BootstrapProviderType) 35 36 type fields struct { 37 version string 38 provider config.Provider 39 repository Repository 40 configVariablesClient config.VariablesClient 41 processor yaml.Processor 42 } 43 type args struct { 44 flavor string 45 targetNamespace string 46 listVariablesOnly bool 47 } 48 type want struct { 49 variables []string 50 targetNamespace string 51 } 52 tests := []struct { 53 name string 54 fields fields 55 args args 56 want want 57 wantErr bool 58 }{ 59 { 60 name: "pass if default template exists", 61 fields: fields{ 62 version: "v1.0", 63 provider: p1, 64 repository: NewMemoryRepository(). 65 WithPaths("root", ""). 66 WithDefaultVersion("v1.0"). 67 WithFile("v1.0", "cluster-template.yaml", templateMapYaml), 68 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 69 processor: yaml.NewSimpleProcessor(), 70 }, 71 args: args{ 72 flavor: "", 73 targetNamespace: "ns1", 74 listVariablesOnly: false, 75 }, 76 want: want{ 77 variables: []string{variableName}, 78 targetNamespace: "ns1", 79 }, 80 wantErr: false, 81 }, 82 { 83 name: "pass if template for a flavor exists", 84 fields: fields{ 85 version: "v1.0", 86 provider: p1, 87 repository: NewMemoryRepository(). 88 WithPaths("root", ""). 89 WithDefaultVersion("v1.0"). 90 WithFile("v1.0", "cluster-template-prod.yaml", templateMapYaml), 91 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 92 processor: yaml.NewSimpleProcessor(), 93 }, 94 args: args{ 95 flavor: "prod", 96 targetNamespace: "ns1", 97 listVariablesOnly: false, 98 }, 99 want: want{ 100 variables: []string{variableName}, 101 targetNamespace: "ns1", 102 }, 103 wantErr: false, 104 }, 105 { 106 name: "fails if template does not exists", 107 fields: fields{ 108 version: "v1.0", 109 provider: p1, 110 repository: NewMemoryRepository(). 111 WithPaths("root", ""). 112 WithDefaultVersion("v1.0"), 113 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 114 processor: yaml.NewSimpleProcessor(), 115 }, 116 args: args{ 117 flavor: "", 118 targetNamespace: "ns1", 119 listVariablesOnly: false, 120 }, 121 wantErr: true, 122 }, 123 { 124 name: "fails if variables does not exists", 125 fields: fields{ 126 version: "v1.0", 127 provider: p1, 128 repository: NewMemoryRepository(). 129 WithPaths("root", ""). 130 WithDefaultVersion("v1.0"). 131 WithFile("v1.0", "cluster-template.yaml", templateMapYaml), 132 configVariablesClient: test.NewFakeVariableClient(), 133 processor: yaml.NewSimpleProcessor(), 134 }, 135 args: args{ 136 flavor: "", 137 targetNamespace: "ns1", 138 listVariablesOnly: false, 139 }, 140 wantErr: true, 141 }, 142 { 143 name: "pass if variables does not exists but skipTemplateProcess flag is set", 144 fields: fields{ 145 version: "v1.0", 146 provider: p1, 147 repository: NewMemoryRepository(). 148 WithPaths("root", ""). 149 WithDefaultVersion("v1.0"). 150 WithFile("v1.0", "cluster-template.yaml", templateMapYaml), 151 configVariablesClient: test.NewFakeVariableClient(), 152 processor: yaml.NewSimpleProcessor(), 153 }, 154 args: args{ 155 flavor: "", 156 targetNamespace: "ns1", 157 listVariablesOnly: true, 158 }, 159 want: want{ 160 variables: []string{variableName}, 161 targetNamespace: "ns1", 162 }, 163 wantErr: false, 164 }, 165 { 166 name: "returns error if processor is unable to get variables", 167 fields: fields{ 168 version: "v1.0", 169 provider: p1, 170 repository: NewMemoryRepository(). 171 WithPaths("root", ""). 172 WithDefaultVersion("v1.0"). 173 WithFile("v1.0", "cluster-template.yaml", templateMapYaml), 174 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 175 processor: test.NewFakeProcessor().WithGetVariablesErr(errors.New("cannot get vars")).WithTemplateName("cluster-template.yaml"), 176 }, 177 args: args{ 178 targetNamespace: "ns1", 179 listVariablesOnly: true, 180 }, 181 wantErr: true, 182 }, 183 } 184 for _, tt := range tests { 185 t.Run(tt.name, func(t *testing.T) { 186 g := NewWithT(t) 187 188 f := newTemplateClient( 189 TemplateClientInput{ 190 version: tt.fields.version, 191 provider: tt.fields.provider, 192 repository: tt.fields.repository, 193 configVariablesClient: tt.fields.configVariablesClient, 194 processor: tt.fields.processor, 195 }, 196 ) 197 got, err := f.Get(context.Background(), tt.args.flavor, tt.args.targetNamespace, tt.args.listVariablesOnly) 198 if tt.wantErr { 199 g.Expect(err).To(HaveOccurred()) 200 return 201 } 202 g.Expect(err).ToNot(HaveOccurred()) 203 204 g.Expect(got.Variables()).To(Equal(tt.want.variables)) 205 g.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) 206 207 // check variable replaced in yaml 208 yaml, err := got.Yaml() 209 g.Expect(err).ToNot(HaveOccurred()) 210 211 if !tt.args.listVariablesOnly { 212 g.Expect(yaml).To(ContainSubstring(fmt.Sprintf("variable: %s", variableValue))) 213 } 214 215 // check if target namespace is set 216 for _, o := range got.Objs() { 217 g.Expect(o.GetNamespace()).To(Equal(tt.want.targetNamespace)) 218 } 219 }) 220 } 221 }