|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package generic |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/go-logr/logr/testr" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 13 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 14 | + |
| 15 | + carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 16 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight" |
| 17 | +) |
| 18 | + |
| 19 | +func TestNewConfigurationCheck(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + cluster *clusterv1.Cluster |
| 23 | + expectedResult preflight.CheckResult |
| 24 | + expectedGenericClusterConfigSpec bool |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "global image registry mirror config", |
| 28 | + cluster: &clusterv1.Cluster{ |
| 29 | + Spec: clusterv1.ClusterSpec{ |
| 30 | + Topology: &clusterv1.Topology{ |
| 31 | + Variables: []clusterv1.ClusterVariable{ |
| 32 | + { |
| 33 | + Name: carenv1.ClusterConfigVariableName, |
| 34 | + Value: v1.JSON{ |
| 35 | + Raw: []byte(`{ |
| 36 | + "globalImageRegistryMirror": { |
| 37 | + "credentials": { |
| 38 | + "secretRef": { |
| 39 | + "name": "nai-demo-image-registry-mirror-credentials" |
| 40 | + } |
| 41 | + }, |
| 42 | + "url": "https://artifactory.canaveral-corp.us-west-2.aws" |
| 43 | + } |
| 44 | + }`), |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + expectedResult: preflight.CheckResult{ |
| 52 | + Allowed: true, |
| 53 | + }, |
| 54 | + expectedGenericClusterConfigSpec: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "multiple image registries config", |
| 58 | + cluster: &clusterv1.Cluster{ |
| 59 | + Spec: clusterv1.ClusterSpec{ |
| 60 | + Topology: &clusterv1.Topology{ |
| 61 | + Variables: []clusterv1.ClusterVariable{ |
| 62 | + { |
| 63 | + Name: carenv1.ClusterConfigVariableName, |
| 64 | + Value: v1.JSON{ |
| 65 | + Raw: []byte(`{ |
| 66 | + "imageRegistries": [ |
| 67 | + { |
| 68 | + "url": "https://my-registry.io", |
| 69 | + "credentials": { |
| 70 | + "secretRef": { |
| 71 | + "name": "my-registry-credentials" |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + ] |
| 76 | + }`), |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + expectedResult: preflight.CheckResult{ |
| 84 | + Allowed: true, |
| 85 | + }, |
| 86 | + expectedGenericClusterConfigSpec: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "invalid cluster config json", |
| 90 | + cluster: &clusterv1.Cluster{ |
| 91 | + Spec: clusterv1.ClusterSpec{ |
| 92 | + Topology: &clusterv1.Topology{ |
| 93 | + Variables: []clusterv1.ClusterVariable{ |
| 94 | + { |
| 95 | + Name: carenv1.ClusterConfigVariableName, |
| 96 | + Value: v1.JSON{ |
| 97 | + Raw: []byte(`{invalid-json`), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + expectedResult: preflight.CheckResult{ |
| 105 | + Allowed: false, |
| 106 | + Error: true, |
| 107 | + Causes: []preflight.Cause{ |
| 108 | + { |
| 109 | + Message: "Failed to unmarshal cluster variable clusterConfig: failed to unmarshal json: invalid character 'i' looking for beginning of object key string", |
| 110 | + Field: "cluster.spec.topology.variables[.name=clusterConfig].genericClusterConfigSpec", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + expectedGenericClusterConfigSpec: false, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.name, func(t *testing.T) { |
| 120 | + cd := &checkDependencies{ |
| 121 | + cluster: tt.cluster, |
| 122 | + log: testr.New(t), |
| 123 | + } |
| 124 | + |
| 125 | + check := newConfigurationCheck(cd) |
| 126 | + result := check.Run(context.Background()) |
| 127 | + |
| 128 | + assert.Equal(t, tt.expectedResult, result) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
0 commit comments