|
15 | 15 | package main |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "errors" |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
18 | 21 | "testing" |
19 | 22 |
|
20 | 23 | corev1 "k8s.io/api/core/v1" |
21 | 24 | "k8s.io/apimachinery/pkg/api/resource" |
22 | 25 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/healthz" |
23 | 27 |
|
24 | 28 | "go.uber.org/zap/zapcore" |
25 | 29 | "gotest.tools/v3/assert" |
@@ -101,3 +105,69 @@ func TestRemoveUnusedNodeFields_NonNodeInput(t *testing.T) { |
101 | 105 | assert.NilError(t, err) |
102 | 106 | assert.Equal(t, any(cm), out, "expected pass-through for non-Node input") |
103 | 107 | } |
| 108 | + |
| 109 | +type healthCheckRecorder struct { |
| 110 | + checks map[string]healthz.Checker |
| 111 | +} |
| 112 | + |
| 113 | +func (r *healthCheckRecorder) AddHealthzCheck(name string, checker healthz.Checker) error { |
| 114 | + r.checks[name] = checker |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func (r *healthCheckRecorder) AddReadyzCheck(name string, checker healthz.Checker) error { |
| 119 | + r.checks[name] = checker |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func TestSetupHealthChecks(t *testing.T) { |
| 124 | + tests := []struct { |
| 125 | + name string |
| 126 | + enableWebhooks bool |
| 127 | + certsReady bool |
| 128 | + webhookErr error |
| 129 | + wantErr string |
| 130 | + }{ |
| 131 | + {name: "webhooks disabled"}, |
| 132 | + {name: "certificates pending", enableWebhooks: true, wantErr: "certs are not ready yet"}, |
| 133 | + { |
| 134 | + name: "webhook starting", |
| 135 | + enableWebhooks: true, |
| 136 | + certsReady: true, |
| 137 | + webhookErr: errors.New("webhook server has not started"), |
| 138 | + wantErr: "webhook server has not started", |
| 139 | + }, |
| 140 | + {name: "webhook ready", enableWebhooks: true, certsReady: true}, |
| 141 | + } |
| 142 | + for _, tt := range tests { |
| 143 | + t.Run(tt.name, func(t *testing.T) { |
| 144 | + certsReady := make(chan struct{}) |
| 145 | + if tt.certsReady { |
| 146 | + close(certsReady) |
| 147 | + } |
| 148 | + recorder := &healthCheckRecorder{checks: make(map[string]healthz.Checker)} |
| 149 | + webhookCalls := 0 |
| 150 | + err := setupHealthChecks(recorder, tt.enableWebhooks, certsReady, func(*http.Request) error { |
| 151 | + webhookCalls++ |
| 152 | + return tt.webhookErr |
| 153 | + }) |
| 154 | + assert.NilError(t, err) |
| 155 | + assert.Assert(t, recorder.checks["healthz"] != nil, "liveness check must be registered") |
| 156 | + assert.Assert(t, recorder.checks["readyz"] != nil, "readiness check must be registered") |
| 157 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/healthz", nil) |
| 158 | + assert.NilError(t, recorder.checks["healthz"](req), "liveness must not depend on certificate or webhook readiness") |
| 159 | + req = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/readyz", nil) |
| 160 | + err = recorder.checks["readyz"](req) |
| 161 | + if tt.wantErr == "" { |
| 162 | + assert.NilError(t, err) |
| 163 | + } else { |
| 164 | + assert.Error(t, err, tt.wantErr) |
| 165 | + } |
| 166 | + wantWebhookCalls := 0 |
| 167 | + if tt.enableWebhooks && tt.certsReady { |
| 168 | + wantWebhookCalls = 1 |
| 169 | + } |
| 170 | + assert.Equal(t, webhookCalls, wantWebhookCalls, "only check webhook startup after certificates are ready") |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
0 commit comments