@@ -5,10 +5,12 @@ import (
55 "fmt"
66 "strings"
77 "testing"
8+ "time"
89
910 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011 "k8s.io/apimachinery/pkg/runtime"
11- "sigs.k8s.io/controller-runtime/pkg/client/fake"
12+ controllerruntime "sigs.k8s.io/controller-runtime"
13+ "sigs.k8s.io/controller-runtime/pkg/client"
1214 gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1315
1416 ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
@@ -167,26 +169,65 @@ func validateClientSettingsPolicy(t *testing.T,
167169) {
168170 t .Helper ()
169171
170- // Register API types with the runtime scheme
171- // This is necessary to create a fake client that can handle the custom resource.
172- scheme := runtime .NewScheme ()
173- _ = ngfAPIv1alpha1 .AddToScheme (scheme )
172+ // Get Kubernetes client from test framework
173+ // This should be set up by your test framework to connect to a real cluster
174+ k8sClient := getKubernetesClient (t )
174175
175- // Create a fake client with the scheme
176- // This is used to simulate interactions with the Kubernetes API.
177- k8sClient := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
176+ // Make policy name unique to avoid conflicts
177+ clientSettingsPolicy .Name = fmt .Sprintf ("%s-%d" , clientSettingsPolicy .Name , time .Now ().UnixNano ())
178178
179179 err := k8sClient .Create (context .Background (), clientSettingsPolicy )
180+
181+ // Clean up after test
182+ defer func () {
183+ _ = k8sClient .Delete (context .Background (), clientSettingsPolicy )
184+ }()
185+
186+ // Check if we expected errors
187+ if len (wantErrors ) == 0 {
188+ if err != nil {
189+ t .Errorf ("expected no error but got: %v" , err )
190+ }
191+ return
192+ }
193+
194+ // We expected errors - validation should have failed
195+ if err == nil {
196+ t .Errorf ("expected validation error but policy was accepted" )
197+ return
198+ }
199+
200+ // Check that we got the expected error messages
201+ var missingErrors []string
202+ for _ , wantError := range wantErrors {
203+ if ! strings .Contains (err .Error (), wantError ) {
204+ missingErrors = append (missingErrors , wantError )
205+ }
206+ }
207+ if len (missingErrors ) != 0 {
208+ t .Errorf ("missing expected errors: %v, got: %v" , missingErrors , err )
209+ }
210+ }
211+
212+ // getKubernetesClient returns a client connected to a real Kubernetes cluster
213+ func getKubernetesClient (t * testing.T ) client.Client {
214+ // Use controller-runtime to get cluster connection
215+ k8sConfig , err := controllerruntime .GetConfig ()
180216 if err != nil {
181- t .Logf ("Error creating ClientSettingsPolicy %q: %v" ,
182- fmt .Sprintf ("%v/%v" , clientSettingsPolicy .Namespace , clientSettingsPolicy .Name ), err )
217+ t .Skipf ("Cannot connect to Kubernetes cluster: %v" , err )
183218 }
184219
220+ // Set up scheme with NGF types
221+ scheme := runtime .NewScheme ()
222+ if err := ngfAPIv1alpha1 .AddToScheme (scheme ); err != nil {
223+ t .Fatalf ("Failed to add NGF schema: %v" , err )
224+ }
225+
226+ // Create client
227+ k8sClient , err := client .New (k8sConfig , client.Options {Scheme : scheme })
185228 if err != nil {
186- for _ , wantError := range wantErrors {
187- if ! strings .Contains (err .Error (), wantError ) {
188- t .Errorf ("missing expected error %q in %v" , wantError , err )
189- }
190- }
229+ t .Skipf ("Cannot create k8s client: %v" , err )
191230 }
231+
232+ return k8sClient
192233}
0 commit comments