@@ -5,10 +5,12 @@ import (
5
5
"fmt"
6
6
"strings"
7
7
"testing"
8
+ "time"
8
9
9
10
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
11
"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"
12
14
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
13
15
14
16
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
@@ -167,26 +169,65 @@ func validateClientSettingsPolicy(t *testing.T,
167
169
) {
168
170
t .Helper ()
169
171
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 )
174
175
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 ())
178
178
179
179
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 ()
180
216
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 )
183
218
}
184
219
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 })
185
228
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 )
191
230
}
231
+
232
+ return k8sClient
192
233
}
0 commit comments