sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/repository/clusterclass_client_test.go (about) 1 /* 2 Copyright 2021 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_ClusterClassClient_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 name 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 clusterclass of name 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", "clusterclass-dev.yaml", templateMapYaml), 68 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 69 processor: yaml.NewSimpleProcessor(), 70 }, 71 args: args{ 72 name: "dev", 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: "fails if clusterclass does not exists", 84 fields: fields{ 85 version: "v1.0", 86 provider: p1, 87 repository: NewMemoryRepository(). 88 WithPaths("root", ""). 89 WithDefaultVersion("v1.0"), 90 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 91 processor: yaml.NewSimpleProcessor(), 92 }, 93 args: args{ 94 name: "dev", 95 targetNamespace: "ns1", 96 listVariablesOnly: false, 97 }, 98 wantErr: true, 99 }, 100 { 101 name: "fails if variables does not exists", 102 fields: fields{ 103 version: "v1.0", 104 provider: p1, 105 repository: NewMemoryRepository(). 106 WithPaths("root", ""). 107 WithDefaultVersion("v1.0"). 108 WithFile("v1.0", "clusterclass-dev.yaml", templateMapYaml), 109 configVariablesClient: test.NewFakeVariableClient(), 110 processor: yaml.NewSimpleProcessor(), 111 }, 112 args: args{ 113 name: "dev", 114 targetNamespace: "ns1", 115 listVariablesOnly: false, 116 }, 117 wantErr: true, 118 }, 119 { 120 name: "pass if variables does not exists but skipTemplateProcess flag is set", 121 fields: fields{ 122 version: "v1.0", 123 provider: p1, 124 repository: NewMemoryRepository(). 125 WithPaths("root", ""). 126 WithDefaultVersion("v1.0"). 127 WithFile("v1.0", "clusterclass-dev.yaml", templateMapYaml), 128 configVariablesClient: test.NewFakeVariableClient(), 129 processor: yaml.NewSimpleProcessor(), 130 }, 131 args: args{ 132 name: "dev", 133 targetNamespace: "ns1", 134 listVariablesOnly: true, 135 }, 136 want: want{ 137 variables: []string{variableName}, 138 targetNamespace: "ns1", 139 }, 140 wantErr: false, 141 }, 142 { 143 name: "returns error if processor is unable to get variables", 144 fields: fields{ 145 version: "v1.0", 146 provider: p1, 147 repository: NewMemoryRepository(). 148 WithPaths("root", ""). 149 WithDefaultVersion("v1.0"). 150 WithFile("v1.0", "clusterclass-dev.yaml", templateMapYaml), 151 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 152 processor: test.NewFakeProcessor().WithGetVariablesErr(errors.New("cannot get vars")).WithTemplateName("clusterclass-dev.yaml"), 153 }, 154 args: args{ 155 targetNamespace: "ns1", 156 listVariablesOnly: true, 157 }, 158 wantErr: true, 159 }, 160 } 161 for _, tt := range tests { 162 t.Run(tt.name, func(t *testing.T) { 163 g := NewWithT(t) 164 165 ctx := context.Background() 166 167 f := newClusterClassClient( 168 ClusterClassClientInput{ 169 version: tt.fields.version, 170 provider: tt.fields.provider, 171 repository: tt.fields.repository, 172 configVariablesClient: tt.fields.configVariablesClient, 173 processor: tt.fields.processor, 174 }, 175 ) 176 got, err := f.Get(ctx, tt.args.name, tt.args.targetNamespace, tt.args.listVariablesOnly) 177 if tt.wantErr { 178 g.Expect(err).To(HaveOccurred()) 179 return 180 } 181 g.Expect(err).ToNot(HaveOccurred()) 182 183 g.Expect(got.Variables()).To(Equal(tt.want.variables)) 184 g.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) 185 186 // check variable replaced in yaml 187 yaml, err := got.Yaml() 188 g.Expect(err).ToNot(HaveOccurred()) 189 190 if !tt.args.listVariablesOnly { 191 g.Expect(yaml).To(ContainSubstring(fmt.Sprintf("variable: %s", variableValue))) 192 } 193 194 // check if target namespace is set 195 for _, o := range got.Objs() { 196 g.Expect(o.GetNamespace()).To(Equal(tt.want.targetNamespace)) 197 } 198 }) 199 } 200 }