|
| 1 | +package nutanix |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "go.uber.org/mock/gomock" |
| 10 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 11 | + |
| 12 | + carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 13 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight" |
| 14 | +) |
| 15 | + |
| 16 | +func TestNutanixChecker_Init(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + nutanixConfig *carenv1.NutanixClusterConfigSpec |
| 20 | + workerNodeConfigs map[string]*carenv1.NutanixWorkerNodeConfigSpec |
| 21 | + expectedCheckCount int |
| 22 | + expectedFirstCheckName string |
| 23 | + expectedSecondCheckName string |
| 24 | + vmImageCheckCount int |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "basic initialization with no configs", |
| 28 | + nutanixConfig: nil, |
| 29 | + workerNodeConfigs: nil, |
| 30 | + expectedCheckCount: 2, // config check and credentials check |
| 31 | + expectedFirstCheckName: "NutanixConfiguration", |
| 32 | + expectedSecondCheckName: "NutanixCredentials", |
| 33 | + vmImageCheckCount: 0, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "initialization with control plane config", |
| 37 | + nutanixConfig: &carenv1.NutanixClusterConfigSpec{ |
| 38 | + ControlPlane: &carenv1.NutanixControlPlaneSpec{ |
| 39 | + Nutanix: &carenv1.NutanixNodeSpec{}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + workerNodeConfigs: nil, |
| 43 | + expectedCheckCount: 3, // config check, credentials check, 1 VM image check |
| 44 | + expectedFirstCheckName: "NutanixConfiguration", |
| 45 | + expectedSecondCheckName: "NutanixCredentials", |
| 46 | + vmImageCheckCount: 1, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "initialization with worker node configs", |
| 50 | + nutanixConfig: nil, |
| 51 | + workerNodeConfigs: map[string]*carenv1.NutanixWorkerNodeConfigSpec{ |
| 52 | + "worker-1": { |
| 53 | + Nutanix: &carenv1.NutanixNodeSpec{}, |
| 54 | + }, |
| 55 | + "worker-2": { |
| 56 | + Nutanix: &carenv1.NutanixNodeSpec{}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + expectedCheckCount: 4, // config check, credentials check, 2 VM image checks |
| 60 | + expectedFirstCheckName: "NutanixConfiguration", |
| 61 | + expectedSecondCheckName: "NutanixCredentials", |
| 62 | + vmImageCheckCount: 2, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "initialization with both control plane and worker node configs", |
| 66 | + nutanixConfig: &carenv1.NutanixClusterConfigSpec{ |
| 67 | + ControlPlane: &carenv1.NutanixControlPlaneSpec{ |
| 68 | + Nutanix: &carenv1.NutanixNodeSpec{}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + workerNodeConfigs: map[string]*carenv1.NutanixWorkerNodeConfigSpec{ |
| 72 | + "worker-1": { |
| 73 | + Nutanix: &carenv1.NutanixNodeSpec{}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + expectedCheckCount: 4, // config check, credentials check, 2 VM image checks (1 CP + 1 worker) |
| 77 | + expectedFirstCheckName: "NutanixConfiguration", |
| 78 | + expectedSecondCheckName: "NutanixCredentials", |
| 79 | + vmImageCheckCount: 2, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + // Create a mock of required dependencies |
| 86 | + ctrl := gomock.NewController(t) |
| 87 | + defer ctrl.Finish() |
| 88 | + |
| 89 | + // Create the checker |
| 90 | + checker := &nutanixChecker{ |
| 91 | + cluster: &clusterv1.Cluster{}, |
| 92 | + nutanixClusterConfigSpec: tt.nutanixConfig, |
| 93 | + nutanixWorkerNodeConfigSpecByMachineDeploymentName: tt.workerNodeConfigs, |
| 94 | + } |
| 95 | + |
| 96 | + // Mock the sub-check functions to track their calls |
| 97 | + configCheckCalled := false |
| 98 | + credsCheckCalled := false |
| 99 | + vmImageCheckCount := 0 |
| 100 | + |
| 101 | + checker.initNutanixConfigurationFunc = func(n *nutanixChecker) preflight.Check { |
| 102 | + configCheckCalled = true |
| 103 | + return func(ctx context.Context) preflight.CheckResult { |
| 104 | + return preflight.CheckResult{ |
| 105 | + Name: tt.expectedFirstCheckName, |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + checker.initCredentialsCheckFunc = func(ctx context.Context, n *nutanixChecker) preflight.Check { |
| 111 | + credsCheckCalled = true |
| 112 | + return func(ctx context.Context) preflight.CheckResult { |
| 113 | + return preflight.CheckResult{ |
| 114 | + Name: tt.expectedSecondCheckName, |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + checker.initVMImageChecksFunc = func(n *nutanixChecker) []preflight.Check { |
| 120 | + checks := []preflight.Check{} |
| 121 | + for i := 0; i < tt.vmImageCheckCount; i++ { |
| 122 | + vmImageCheckCount++ |
| 123 | + checks = append(checks, func(ctx context.Context) preflight.CheckResult { |
| 124 | + return preflight.CheckResult{ |
| 125 | + Name: fmt.Sprintf("VMImageCheck-%d", i), |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | + return checks |
| 130 | + } |
| 131 | + |
| 132 | + // Call Init |
| 133 | + ctx := context.Background() |
| 134 | + checks := checker.Init(ctx) |
| 135 | + |
| 136 | + // Verify the logger was set |
| 137 | + assert.NotNil(t, checker.log) |
| 138 | + |
| 139 | + // Verify correct number of checks |
| 140 | + assert.Len(t, checks, tt.expectedCheckCount) |
| 141 | + |
| 142 | + // Verify the sub-functions were called |
| 143 | + assert.True(t, configCheckCalled, "initNutanixConfiguration should have been called") |
| 144 | + assert.True(t, credsCheckCalled, "initCredentialsCheck should have been called") |
| 145 | + assert.Equal(t, tt.vmImageCheckCount, vmImageCheckCount, "Wrong number of VM image checks") |
| 146 | + |
| 147 | + // Verify the first two checks when we have results |
| 148 | + if len(checks) >= 2 { |
| 149 | + result1 := checks[0](ctx) |
| 150 | + result2 := checks[1](ctx) |
| 151 | + assert.Equal(t, tt.expectedFirstCheckName, result1.Name) |
| 152 | + assert.Equal(t, tt.expectedSecondCheckName, result2.Name) |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
0 commit comments