sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/repository/template_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 "fmt" 21 "testing" 22 23 . "github.com/onsi/gomega" 24 25 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" 26 yaml "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor" 27 "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test" 28 ) 29 30 // Nb.We are using core objects vs Machines/Cluster etc. because it is easier to test (you don't have to deal with CRDs 31 // or schema issues), but this is ok because a template can be any yaml that complies the clusterctl contract. 32 var templateMapYaml = []byte("apiVersion: v1\n" + 33 "data:\n" + 34 fmt.Sprintf(" variable: ${%s}\n", variableName) + 35 "kind: ConfigMap\n" + 36 "metadata:\n" + 37 " name: manager") 38 39 func Test_newTemplate(t *testing.T) { 40 type args struct { 41 rawYaml []byte 42 configVariablesClient config.VariablesClient 43 processor yaml.Processor 44 targetNamespace string 45 skipTemplateProcess bool 46 } 47 type want struct { 48 variables []string 49 targetNamespace string 50 } 51 tests := []struct { 52 name string 53 args args 54 want want 55 wantErr bool 56 }{ 57 { 58 name: "variable is replaced and namespace fixed", 59 args: args{ 60 rawYaml: templateMapYaml, 61 configVariablesClient: test.NewFakeVariableClient().WithVar(variableName, variableValue), 62 processor: yaml.NewSimpleProcessor(), 63 targetNamespace: "ns1", 64 skipTemplateProcess: false, 65 }, 66 want: want{ 67 variables: []string{variableName}, 68 targetNamespace: "ns1", 69 }, 70 wantErr: false, 71 }, 72 { 73 name: "List variable only", 74 args: args{ 75 rawYaml: templateMapYaml, 76 configVariablesClient: test.NewFakeVariableClient(), 77 processor: yaml.NewSimpleProcessor(), 78 targetNamespace: "ns1", 79 skipTemplateProcess: true, 80 }, 81 want: want{ 82 variables: []string{variableName}, 83 targetNamespace: "ns1", 84 }, 85 wantErr: false, 86 }, 87 } 88 for _, tt := range tests { 89 t.Run(tt.name, func(t *testing.T) { 90 g := NewWithT(t) 91 92 got, err := NewTemplate(TemplateInput{ 93 RawArtifact: tt.args.rawYaml, 94 ConfigVariablesClient: tt.args.configVariablesClient, 95 Processor: tt.args.processor, 96 TargetNamespace: tt.args.targetNamespace, 97 SkipTemplateProcess: tt.args.skipTemplateProcess, 98 }) 99 if tt.wantErr { 100 g.Expect(err).To(HaveOccurred()) 101 return 102 } 103 g.Expect(err).ToNot(HaveOccurred()) 104 105 g.Expect(got.Variables()).To(Equal(tt.want.variables)) 106 g.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) 107 108 if tt.args.skipTemplateProcess { 109 return 110 } 111 112 // check variable replaced in components 113 yml, err := got.Yaml() 114 g.Expect(err).ToNot(HaveOccurred()) 115 g.Expect(yml).To(ContainSubstring(fmt.Sprintf("variable: %s", variableValue))) 116 }) 117 } 118 } 119 120 func TestMergeTemplates(t *testing.T) { 121 g := NewWithT(t) 122 123 templateYAMLGen := func(name, variableValue, sameVariableValue string) []byte { 124 return []byte(fmt.Sprintf(`apiVersion: v1 125 data: 126 variable: ${%s} 127 samevariable: ${SAME_VARIABLE:-%s} 128 kind: ConfigMap 129 metadata: 130 name: %s`, variableValue, sameVariableValue, name)) 131 } 132 133 template1, err := NewTemplate(TemplateInput{ 134 RawArtifact: templateYAMLGen("foo", "foo", "val-1"), 135 ConfigVariablesClient: test.NewFakeVariableClient().WithVar("foo", "foo-value"), 136 Processor: yaml.NewSimpleProcessor(), 137 TargetNamespace: "ns1", 138 SkipTemplateProcess: false, 139 }) 140 if err != nil { 141 t.Fatalf("failed to create template %v", err) 142 } 143 144 template2, err := NewTemplate(TemplateInput{ 145 RawArtifact: templateYAMLGen("bar", "bar", "val-2"), 146 ConfigVariablesClient: test.NewFakeVariableClient().WithVar("bar", "bar-value"), 147 Processor: yaml.NewSimpleProcessor(), 148 TargetNamespace: "ns1", 149 SkipTemplateProcess: false, 150 }) 151 if err != nil { 152 t.Fatalf("failed to create template %v", err) 153 } 154 155 merged, err := MergeTemplates(template1, template2) 156 g.Expect(err).ToNot(HaveOccurred()) 157 g.Expect(merged.Objs()).To(HaveLen(2)) 158 g.Expect(merged.VariableMap()).To(HaveLen(3)) 159 160 // Make sure that the SAME_VARIABLE default value comes from the first template 161 // that defines it 162 g.Expect(merged.VariableMap()["SAME_VARIABLE"]).NotTo(BeNil()) 163 g.Expect(*merged.VariableMap()["SAME_VARIABLE"]).To(Equal("val-1")) 164 }