|
| 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