Skip to content

Commit 6d76d85

Browse files
committed
test: unit test for addon validator webhook
1 parent 38bcc48 commit 6d76d85

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

pkg/handlers/generic/lifecycle/config/cm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ func (h *HelmChartGetter) getInfoFor(
123123
return &settings, err
124124
}
125125

126+
// For returns the HelmChart info for the given component from the configmap referenced in the cluster variables.
127+
// It first checks the configmap referenced in the cluster variables.
128+
// If not found, it returns the HelmChart info from the default configmap.
126129
func (h *HelmChartGetter) For(
127130
ctx context.Context,
128131
log logr.Logger,

pkg/webhook/cluster/addons_validator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
v1 "k8s.io/api/admission/v1"
1212
corev1 "k8s.io/api/core/v1"
13-
"k8s.io/apimachinery/pkg/api/errors"
13+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1414
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1515
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1616
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -64,7 +64,8 @@ func (a *addonsValidator) validate(
6464
)
6565
}
6666

67-
if clusterConfig.Addons != nil && clusterConfig.Addons.HelmChartConfig != nil {
67+
if clusterConfig != nil && clusterConfig.Addons != nil &&
68+
clusterConfig.Addons.HelmChartConfig != nil {
6869
// Check if custom helm chart ConfigMap is provided
6970
if err := validateCustomHelmChartConfigMapExists(
7071
ctx,
@@ -91,7 +92,7 @@ func validateCustomHelmChartConfigMapExists(
9192
Name: name,
9293
}, configMap)
9394
if err != nil {
94-
if errors.IsNotFound(err) {
95+
if apierrors.IsNotFound(err) {
9596
return fmt.Errorf(
9697
"HelmChart ConfigMap %q referenced in the cluster variables not found: %w",
9798
name,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cluster
5+
6+
import (
7+
"strings"
8+
"testing"
9+
10+
. "github.com/onsi/gomega"
11+
corev1 "k8s.io/api/core/v1"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
14+
15+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
16+
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
17+
)
18+
19+
func TestBlockWithNonExistentHelmChartConfig(t *testing.T) {
20+
g := NewWithT(t)
21+
22+
cluster := &clusterv1.Cluster{
23+
ObjectMeta: metav1.ObjectMeta{
24+
Name: "cluster-with-non-existent-helm-chart-configmap",
25+
Namespace: metav1.NamespaceDefault,
26+
},
27+
Spec: clusterv1.ClusterSpec{
28+
Topology: &clusterv1.Topology{
29+
Variables: []clusterv1.ClusterVariable{
30+
*helmChartConfigVariable(t, "non-existent-helm-chart-configmap"),
31+
},
32+
},
33+
},
34+
}
35+
err := env.Client.Create(ctx, cluster)
36+
g.Expect(err).To(HaveOccurred())
37+
g.Expect(strings.Contains(
38+
err.Error(),
39+
"HelmChart ConfigMap \"non-existent-helm-chart-configmap\" referenced in the cluster variables not found",
40+
)).
41+
To(BeTrue(), "Expected error to be of type IsNotFound")
42+
}
43+
44+
func TestAllowWithExistingHelmChartConfig(t *testing.T) {
45+
g := NewWithT(t)
46+
47+
configmap := &corev1.ConfigMap{
48+
ObjectMeta: metav1.ObjectMeta{
49+
Name: "custom-helm-chart-configmap",
50+
Namespace: metav1.NamespaceDefault,
51+
},
52+
Data: map[string]string{
53+
"ccm": "test chart config data",
54+
},
55+
}
56+
g.Expect(env.Client.Create(ctx, configmap)).ToNot(HaveOccurred())
57+
58+
cluster := &clusterv1.Cluster{
59+
ObjectMeta: metav1.ObjectMeta{
60+
Name: "custom-helm-chart-configmap",
61+
Namespace: metav1.NamespaceDefault,
62+
},
63+
Spec: clusterv1.ClusterSpec{
64+
Topology: &clusterv1.Topology{
65+
Variables: []clusterv1.ClusterVariable{
66+
*helmChartConfigVariable(t, "custom-helm-chart-configmap"),
67+
},
68+
},
69+
},
70+
}
71+
g.Expect(env.Client.Create(ctx, cluster)).ToNot(HaveOccurred())
72+
}
73+
74+
func helmChartConfigVariable(
75+
t *testing.T,
76+
name string,
77+
) *clusterv1.ClusterVariable {
78+
t.Helper()
79+
hv, err := apivariables.MarshalToClusterVariable(
80+
"clusterConfig",
81+
&apivariables.ClusterConfigSpec{
82+
Addons: &apivariables.Addons{
83+
GenericAddons: v1alpha1.GenericAddons{
84+
HelmChartConfig: &v1alpha1.HelmChartConfig{
85+
ConfigMapRef: v1alpha1.LocalObjectReference{
86+
Name: name,
87+
},
88+
},
89+
},
90+
},
91+
},
92+
)
93+
if err != nil {
94+
t.Fatalf("failed to create addon variable: %s", err)
95+
}
96+
return hv
97+
}

0 commit comments

Comments
 (0)