Skip to content

Commit 33c4f5b

Browse files
committed
fix: fixes up tests for templating
1 parent 7c4e1ba commit 33c4f5b

File tree

4 files changed

+24
-46
lines changed

4 files changed

+24
-46
lines changed

charts/cluster-api-runtime-extensions-nutanix/addons/ccm/nutanix/values-template.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ prismCentralInsecure: {{ .PrismCentralInsecure }}
44
{{- with .PrismCentralAdditionalTrustBundle }}
55
prismCentralAdditionalTrustBundle: {{ printf "%q" . }}
66
{{- end }}
7+
{{- with .ControlPlaneEndpointHost }}
8+
ignoredNodeIPs: [ {{ printf "%q" . }} ]
9+
{{- end }}
710

811
# The Secret containing the credentials will be created by the handler.
912
createSecret: false

charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ data:
2828
node-feature-discovery: |
2929
ChartVersion: 0.16.1
3030
RepositoryURL: {{ if .Values.selfHostedRegistry }}oci://helm-repository.{{ .Release.Namespace }}.svc/charts{{ else }}https://kubernetes-sigs.github.io/node-feature-discovery/charts{{ end }}
31-
nutanix-ccm: |
32-
ChartName: nutanix-cloud-provider
31+
nutanix-cloud-provider: |
3332
ChartVersion: 0.4.1
3433
RepositoryURL: {{ if .Values.selfHostedRegistry }}oci://helm-repository.{{ .Release.Namespace }}.svc/charts{{ else }}https://nutanix.github.io/helm/{{ end }}
3534
nutanix-csi-storage: |
@@ -39,7 +38,6 @@ data:
3938
ChartVersion: 3.0.5
4039
RepositoryURL: {{ if .Values.selfHostedRegistry }}oci://helm-repository.{{ .Release.Namespace }}.svc/charts{{ else }}https://piraeus.io/helm-charts/{{ end }}
4140
tigera-operator: |
42-
ChartName: tigera-operator
4341
ChartVersion: v3.28.1
4442
RepositoryURL: {{ if .Values.selfHostedRegistry }}oci://helm-repository.{{ .Release.Namespace }}.svc/charts{{ else }}https://docs.tigera.io/calico/charts{{ end }}
4543
kind: ConfigMap

hack/addons/kustomize/ccm/nutanix/manifests/values-template.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ prismCentralInsecure: {{ .PrismCentralInsecure }}
44
{{- with .PrismCentralAdditionalTrustBundle }}
55
prismCentralAdditionalTrustBundle: {{ printf "%q" . }}
66
{{- end }}
7+
{{- with .ControlPlaneEndpointHost }}
8+
ignoredNodeIPs: [ {{ printf "%q" . }} ]
9+
{{- end }}
710

811
# The Secret containing the credentials will be created by the handler.
912
createSecret: false

pkg/handlers/generic/lifecycle/ccm/nutanix/handler_test.go

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
package nutanix
55

66
import (
7-
"bytes"
87
"fmt"
8+
"io"
9+
"os"
910
"os/exec"
1011
"path/filepath"
1112
"strings"
1213
"testing"
13-
"text/template"
1414

1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17-
corev1 "k8s.io/api/core/v1"
18-
"sigs.k8s.io/yaml"
1917

2018
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
2119
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
@@ -34,7 +32,8 @@ ignoredNodeIPs: [ "1.2.3.4" ]
3432
3533
# The Secret containing the credentials will be created by the handler.
3634
createSecret: false
37-
secretName: nutanix-ccm-credentials`
35+
secretName: nutanix-ccm-credentials
36+
`
3837

3938
expectedWithoutAdditionalTrustBundle = `prismCentralEndPoint: prism-central.nutanix.com
4039
prismCentralPort: 9440
@@ -43,18 +42,18 @@ ignoredNodeIPs: [ "1.2.3.4" ]
4342
4443
# The Secret containing the credentials will be created by the handler.
4544
createSecret: false
46-
secretName: nutanix-ccm-credentials`
45+
secretName: nutanix-ccm-credentials
46+
`
4747
)
4848

4949
var templateFile = filepath.Join(
5050
moduleRootDir(),
5151
"charts",
5252
"cluster-api-runtime-extensions-nutanix",
53-
"templates",
53+
"addons",
5454
"ccm",
5555
"nutanix",
56-
"manifests",
57-
"helm-addon-installation.yaml",
56+
"values-template.yaml",
5857
)
5958

6059
func Test_templateValues(t *testing.T) {
@@ -141,42 +140,17 @@ func Test_templateValues(t *testing.T) {
141140
}
142141
}
143142

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.
148143
func readCCMTemplateFromProjectHelmChart(t *testing.T) string {
149144
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)
180154
}
181155

182156
func moduleRootDir() string {

0 commit comments

Comments
 (0)