Skip to content

Commit

Permalink
test: unit test for addon validator webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
supershal committed Jan 24, 2025
1 parent 38bcc48 commit 0563813
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/handlers/generic/lifecycle/config/cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (h *HelmChartGetter) getInfoFor(
return &settings, err
}

// For returns the HelmChart info for the given component from the configmap referenced in the cluster variables.
// It first checks the configmap referenced in the cluster variables.
// If not found, it returns the HelmChart info from the default configmap.
func (h *HelmChartGetter) For(
ctx context.Context,
log logr.Logger,
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/cluster/addons_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

v1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand Down Expand Up @@ -91,7 +91,7 @@ func validateCustomHelmChartConfigMapExists(
Name: name,
}, configMap)
if err != nil {
if errors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
return fmt.Errorf(
"HelmChart ConfigMap %q referenced in the cluster variables not found: %w",
name,
Expand Down
97 changes: 97 additions & 0 deletions pkg/webhook/cluster/addons_validator_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2024 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package cluster

import (
"strings"
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
)

func TestBlockWithNonExistentHelmChartConfig(t *testing.T) {
g := NewWithT(t)

cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-with-non-existent-helm-chart-configmap",
Namespace: metav1.NamespaceDefault,
},
Spec: clusterv1.ClusterSpec{
Topology: &clusterv1.Topology{
Variables: []clusterv1.ClusterVariable{
*helmChartConfigVariable(t, "non-existent-helm-chart-configmap"),
},
},
},
}
err := env.Client.Create(ctx, cluster)
g.Expect(err).To(HaveOccurred())
g.Expect(strings.Contains(
err.Error(),
"HelmChart ConfigMap \"non-existent-helm-chart-configmap\" referenced in the cluster variables not found",
)).
To(BeTrue(), "Expected error to be of type IsNotFound")
}

func TestAllowWithExistingHelmChartConfig(t *testing.T) {
g := NewWithT(t)

configmap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "custom-helm-chart-configmap",
Namespace: metav1.NamespaceDefault,
},
Data: map[string]string{
"ccm": "test chart config data",
},
}
g.Expect(env.Client.Create(ctx, configmap)).ToNot(HaveOccurred())

cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "custom-helm-chart-configmap",
Namespace: metav1.NamespaceDefault,
},
Spec: clusterv1.ClusterSpec{
Topology: &clusterv1.Topology{
Variables: []clusterv1.ClusterVariable{
*helmChartConfigVariable(t, "non-existent-helm-chart-configmap"),
},
},
},
}
g.Expect(env.Client.Create(ctx, cluster)).ToNot(HaveOccurred())
}

func helmChartConfigVariable(
t *testing.T,
name string,
) *clusterv1.ClusterVariable {
t.Helper()
hv, err := apivariables.MarshalToClusterVariable(
"clusterConfig",
&apivariables.ClusterConfigSpec{
Addons: &apivariables.Addons{
GenericAddons: v1alpha1.GenericAddons{
HelmChartConfig: &v1alpha1.HelmChartConfig{
ConfigMapRef: v1alpha1.LocalObjectReference{
Name: name,
},
},
},
},
},
)
if err != nil {
t.Fatalf("failed to create addon variable: %s", err)
}
return hv
}

0 comments on commit 0563813

Please sign in to comment.