4
4
package nutanix
5
5
6
6
import (
7
- "bytes"
8
7
"fmt"
8
+ "io"
9
+ "os"
9
10
"os/exec"
10
11
"path/filepath"
11
12
"strings"
12
13
"testing"
13
- "text/template"
14
14
15
15
"github.com/stretchr/testify/assert"
16
16
"github.com/stretchr/testify/require"
17
- corev1 "k8s.io/api/core/v1"
18
- "sigs.k8s.io/yaml"
19
17
20
18
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
21
19
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
@@ -34,7 +32,8 @@ ignoredNodeIPs: [ "1.2.3.4" ]
34
32
35
33
# The Secret containing the credentials will be created by the handler.
36
34
createSecret: false
37
- secretName: nutanix-ccm-credentials`
35
+ secretName: nutanix-ccm-credentials
36
+ `
38
37
39
38
expectedWithoutAdditionalTrustBundle = `prismCentralEndPoint: prism-central.nutanix.com
40
39
prismCentralPort: 9440
@@ -43,18 +42,18 @@ ignoredNodeIPs: [ "1.2.3.4" ]
43
42
44
43
# The Secret containing the credentials will be created by the handler.
45
44
createSecret: false
46
- secretName: nutanix-ccm-credentials`
45
+ secretName: nutanix-ccm-credentials
46
+ `
47
47
)
48
48
49
49
var templateFile = filepath .Join (
50
50
moduleRootDir (),
51
51
"charts" ,
52
52
"cluster-api-runtime-extensions-nutanix" ,
53
- "templates " ,
53
+ "addons " ,
54
54
"ccm" ,
55
55
"nutanix" ,
56
- "manifests" ,
57
- "helm-addon-installation.yaml" ,
56
+ "values-template.yaml" ,
58
57
)
59
58
60
59
func Test_templateValues (t * testing.T ) {
@@ -141,42 +140,17 @@ func Test_templateValues(t *testing.T) {
141
140
}
142
141
}
143
142
144
- // readCCMTemplateFromProjectHelmChart gets the CCM template from the Helm chart in the project
145
- // and renders it with dummy values, finally extracting the embedded template that will be used by
146
- // CAAPH when installing the Nutanix CCM addon.
147
- // This is important to do this way to ensure that the hard-to-read double templating works as expected.
148
143
func readCCMTemplateFromProjectHelmChart (t * testing.T ) string {
149
144
t .Helper ()
150
-
151
- // Mimic the Helm templating using dummy values that will render the template correctly.
152
- const dummyValues = `---
153
- hooks:
154
- ccm:
155
- nutanix:
156
- helmAddonStrategy:
157
- defaultValueTemplateConfigMap:
158
- create: true
159
- `
160
- templateData := map [string ]interface {}{}
161
- require .NoError (t , yaml .Unmarshal ([]byte (dummyValues ), & templateData ))
162
-
163
- // And set that as the value of Values in the templateData.
164
- templateData ["Values" ] = templateData
165
-
166
- // Run the actual template as Helm would.
167
- var templatedBytes bytes.Buffer
168
- require .NoError (
169
- t ,
170
- template .Must (
171
- template .New (
172
- "helm-addon-installation.yaml" ).ParseFiles (templateFile ),
173
- ).Execute (& templatedBytes , templateData ),
174
- )
175
- cm := & corev1.ConfigMap {}
176
- require .NoError (t , yaml .UnmarshalStrict (templatedBytes .Bytes (), cm ))
177
-
178
- // And return the values from the template.
179
- return cm .Data ["values.yaml" ]
145
+ f , err := os .Open (templateFile )
146
+ if err != nil {
147
+ t .Errorf ("failed to open template file %v" , err )
148
+ }
149
+ b , err := io .ReadAll (f )
150
+ if err != nil {
151
+ t .Errorf ("failed to open template file %v" , err )
152
+ }
153
+ return string (b )
180
154
}
181
155
182
156
func moduleRootDir () string {
0 commit comments