@@ -111,6 +111,115 @@ func TestGetListenAndServeFunc_WithoutKubeConfig(t *testing.T) {
111111 assert .NoError (t , err , "GetListenAndServeFunc should succeed without kubeConfig" )
112112}
113113
114+ // TestGetListenAndServeFunc_WithEmptyClientCA tests that the server
115+ // starts successfully when TLS is enabled but client-ca is empty
116+ func TestGetListenAndServeFunc_WithEmptyClientCA (t * testing.T ) {
117+ // Generate test certificates dynamically
118+ caCert , caKey , err := generateCA ()
119+ require .NoError (t , err )
120+
121+ serverCert , serverKey , err := generateServerCert (caCert , caKey , "localhost" )
122+ require .NoError (t , err )
123+
124+ tmpDir , err := os .MkdirTemp ("" , "server-test-*" )
125+ require .NoError (t , err )
126+ defer os .RemoveAll (tmpDir )
127+
128+ tlsCertPath := filepath .Join (tmpDir , "tls.crt" )
129+ tlsKeyPath := filepath .Join (tmpDir , "tls.key" )
130+ emptyClientCAPath := "" // Empty client CA path
131+
132+ err = os .WriteFile (tlsCertPath , serverCert , 0644 )
133+ require .NoError (t , err )
134+ err = os .WriteFile (tlsKeyPath , serverKey , 0600 )
135+ require .NoError (t , err )
136+
137+ logger := logrus .New ()
138+ logger .SetOutput (io .Discard )
139+
140+ // Test with TLS enabled but empty client CA - should succeed
141+ _ , err = GetListenAndServeFunc (
142+ WithLogger (logger ),
143+ WithTLS (& tlsCertPath , & tlsKeyPath , & emptyClientCAPath ),
144+ WithDebug (false ),
145+ )
146+
147+ assert .NoError (t , err , "GetListenAndServeFunc should succeed with empty client-ca" )
148+ }
149+
150+ // TestGetListenAndServeFunc_WithNilClientCA tests that the server
151+ // starts successfully when TLS is enabled but client-ca pointer is nil
152+ func TestGetListenAndServeFunc_WithNilClientCA (t * testing.T ) {
153+ // Generate test certificates dynamically
154+ caCert , caKey , err := generateCA ()
155+ require .NoError (t , err )
156+
157+ serverCert , serverKey , err := generateServerCert (caCert , caKey , "localhost" )
158+ require .NoError (t , err )
159+
160+ tmpDir , err := os .MkdirTemp ("" , "server-test-*" )
161+ require .NoError (t , err )
162+ defer os .RemoveAll (tmpDir )
163+
164+ tlsCertPath := filepath .Join (tmpDir , "tls.crt" )
165+ tlsKeyPath := filepath .Join (tmpDir , "tls.key" )
166+
167+ err = os .WriteFile (tlsCertPath , serverCert , 0644 )
168+ require .NoError (t , err )
169+ err = os .WriteFile (tlsKeyPath , serverKey , 0600 )
170+ require .NoError (t , err )
171+
172+ logger := logrus .New ()
173+ logger .SetOutput (io .Discard )
174+
175+ // Test with TLS enabled but nil client CA pointer - should succeed
176+ _ , err = GetListenAndServeFunc (
177+ WithLogger (logger ),
178+ WithTLS (& tlsCertPath , & tlsKeyPath , nil ),
179+ WithDebug (false ),
180+ )
181+
182+ assert .NoError (t , err , "GetListenAndServeFunc should succeed with nil client-ca pointer" )
183+ }
184+
185+ // TestClientCAEnabled tests the clientCAEnabled helper function
186+ func TestClientCAEnabled (t * testing.T ) {
187+ tests := []struct {
188+ name string
189+ clientCAPath * string
190+ expected bool
191+ }{
192+ {
193+ name : "nil pointer" ,
194+ clientCAPath : nil ,
195+ expected : false ,
196+ },
197+ {
198+ name : "empty string" ,
199+ clientCAPath : strPtr ("" ),
200+ expected : false ,
201+ },
202+ {
203+ name : "valid path" ,
204+ clientCAPath : strPtr ("/path/to/ca.crt" ),
205+ expected : true ,
206+ },
207+ }
208+
209+ for _ , tt := range tests {
210+ t .Run (tt .name , func (t * testing.T ) {
211+ sc := & serverConfig {
212+ clientCAPath : tt .clientCAPath ,
213+ }
214+ assert .Equal (t , tt .expected , sc .clientCAEnabled (), "clientCAEnabled result should match expected" )
215+ })
216+ }
217+ }
218+
219+ func strPtr (s string ) * string {
220+ return & s
221+ }
222+
114223// TestHTTPClientHasTLSConfig verifies that rest.HTTPClientFor creates a client
115224// with proper TLS configuration including CA certificates
116225func TestHTTPClientHasTLSConfig (t * testing.T ) {
0 commit comments