@@ -3,6 +3,7 @@ package webhook
33import (
44 "os"
55 "path/filepath"
6+ "strings"
67 "testing"
78
89 "github.com/google/go-cmp/cmp"
@@ -215,3 +216,93 @@ func TestNewWebhookConfig(t *testing.T) {
215216 })
216217 }
217218}
219+ func TestLoadCertManagerCA_NotFound (t * testing.T ) {
220+ config := & Config {}
221+ _ , err := config .loadCertManagerCA ("/nonexistent/path" )
222+ if err == nil {
223+ t .Error ("Expected error when certificate files don't exist" )
224+ }
225+ }
226+
227+ func TestLoadCertManagerCA_EmptyFile (t * testing.T ) {
228+ dir := t .TempDir ()
229+ // Create empty files
230+ if err := os .WriteFile (filepath .Join (dir , "tls.crt" ), []byte {}, 0600 ); err != nil {
231+ t .Fatalf ("failed to create empty tls.crt: %v" , err )
232+ }
233+ if err := os .WriteFile (filepath .Join (dir , "ca.crt" ), []byte {}, 0600 ); err != nil {
234+ t .Fatalf ("failed to create empty ca.crt: %v" , err )
235+ }
236+
237+ config := & Config {}
238+ _ , err := config .loadCertManagerCA (dir )
239+ if err == nil {
240+ t .Error ("Expected error for empty certificate files" )
241+ }
242+ if ! strings .Contains (err .Error (), "empty" ) {
243+ t .Errorf ("Expected error message to contain 'empty', got: %v" , err )
244+ }
245+ }
246+
247+ func TestLoadCertManagerCA_Success (t * testing.T ) {
248+ t .Run ("loads ca.crt successfully" , func (t * testing.T ) {
249+ dir := t .TempDir ()
250+ caContent := []byte ("test-ca-content" )
251+ if err := os .WriteFile (filepath .Join (dir , "ca.crt" ), caContent , 0600 ); err != nil {
252+ t .Fatalf ("failed to create ca.crt: %v" , err )
253+ }
254+
255+ config := & Config {}
256+ result , err := config .loadCertManagerCA (dir )
257+ if err != nil {
258+ t .Errorf ("Unexpected error: %v" , err )
259+ }
260+ if string (result ) != string (caContent ) {
261+ t .Errorf ("Expected %s, got %s" , caContent , result )
262+ }
263+ })
264+ }
265+
266+ func TestNewWebhookConfig_CertManagerNotMounted (t * testing.T ) {
267+ t .Setenv ("POD_NAMESPACE" , "test-namespace" )
268+
269+ dir := t .TempDir ()
270+ // Don't create any certificate files to simulate cert-manager not ready
271+
272+ _ , err := NewWebhookConfig (nil , "test-webhook" , 8080 , nil , dir , true , true , false , true )
273+ if err == nil {
274+ t .Error ("Expected error when cert-manager certificates not mounted" )
275+ }
276+ if ! strings .Contains (err .Error (), "failed to load cert-manager CA certificate" ) {
277+ t .Errorf ("Expected error about loading cert-manager CA, got: %v" , err )
278+ }
279+ }
280+
281+ func TestNewWebhookConfig_SelfSignedCertError (t * testing.T ) {
282+ t .Setenv ("POD_NAMESPACE" , "test-namespace" )
283+
284+ // Use an invalid certDir (read-only location) to force genCertificate to fail
285+ invalidCertDir := "/proc/invalid-cert-dir"
286+
287+ clientConnectionType := options .Service
288+ _ , err := NewWebhookConfig (
289+ nil ,
290+ "test-service" ,
291+ 443 ,
292+ & clientConnectionType ,
293+ invalidCertDir ,
294+ false , // enableGuardRail
295+ false , // denyModifyMemberClusterLabels
296+ false , // enableWorkload
297+ false , // useCertManager = false to trigger self-signed path
298+ )
299+
300+ if err == nil {
301+ t .Fatal ("Expected error when genCertificate fails, got nil" )
302+ }
303+
304+ expectedErrMsg := "failed to generate self-signed certificate"
305+ if ! strings .Contains (err .Error (), expectedErrMsg ) {
306+ t .Errorf ("Expected error to contain '%s', got: %v" , expectedErrMsg , err )
307+ }
308+ }
0 commit comments