Skip to content

Commit dd66a79

Browse files
committed
Update tests to create a ClientSettingsPolicy object during validation
1 parent 7fddffa commit dd66a79

File tree

1 file changed

+93
-66
lines changed

1 file changed

+93
-66
lines changed

tests/cel/clientsettingspolicy_test.go

Lines changed: 93 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package cel
22

33
import (
4+
"context"
5+
"fmt"
6+
"strings"
47
"testing"
58

9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
12+
613
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
714

815
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
916
)
1017

1118
func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
12-
allowedKinds := map[gatewayv1alpha2.LocalPolicyTargetReference]bool{
13-
{
14-
Kind: "Gateway",
15-
Group: "gateway.networking.k8s.io",
16-
}: true,
17-
{
18-
Kind: "HTTPRoute",
19-
Group: "gateway.networking.k8s.io",
20-
}: true,
21-
{
22-
Kind: "GRPCRoute",
23-
Group: "gateway.networking.k8s.io",
24-
}: true,
25-
}
26-
27-
testValidTargetRefKind(t, allowedKinds)
28-
testInvalidTargetRefKind(t, allowedKinds)
19+
// Test valid and invalid TargetRef Kind
20+
// Valid kinds are: Gateway, HTTPRoute, GRPCRoute
21+
testValidTargetRefKind(t)
22+
// Invalid kinds should return an error
23+
// Example of an invalid kind: "InvalidKind", "TCPRoute"
24+
testInvalidTargetRefKind(t)
2925
}
3026

3127
func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
@@ -41,36 +37,39 @@ func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
4137
testInvalidTargetRefGroup(t, allowedGroups)
4238
}
4339

44-
func testValidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
40+
func testValidTargetRefKind(t *testing.T) {
4541
t.Helper()
4642

4743
tests := []struct {
4844
name string
49-
wantErrors string
50-
targetRef gatewayv1alpha2.LocalPolicyTargetReference
45+
wantErrors []string
46+
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
5147
}{
5248
{
53-
name: "Validate TargetRef is of an allowed kind",
54-
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
55-
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
56-
Kind: "Gateway",
57-
Group: "gateway.networking.k8s.io",
49+
name: "Validate TargetRef of kind Gateway is allowed",
50+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
51+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
52+
Kind: "Gateway",
53+
Group: "gateway.networking.k8s.io",
54+
},
5855
},
5956
},
6057
{
61-
name: "Validate TargetRef is of an allowed kind",
62-
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
63-
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
64-
Kind: "HTTPRoute",
65-
Group: "gateway.networking.k8s.io",
58+
name: "Validate TargetRef of kind HTTPRoute is allowed",
59+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
60+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
61+
Kind: "HTTPRoute",
62+
Group: "gateway.networking.k8s.io",
63+
},
6664
},
6765
},
6866
{
69-
name: "Validate TargetRef is of an allowed kind",
70-
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
71-
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
72-
Kind: "GRPCRoute",
73-
Group: "gateway.networking.k8s.io",
67+
name: "Validate TargetRef of kind GRPCRoute is allowed",
68+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
69+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
70+
Kind: "GRPCRoute",
71+
Group: "gateway.networking.k8s.io",
72+
},
7473
},
7574
},
7675
}
@@ -79,44 +78,45 @@ func testValidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.Local
7978
t.Run(tt.name, func(t *testing.T) {
8079
// Create a ClientSettingsPolicy with the targetRef from the test case.
8180
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
81+
ObjectMeta: metav1.ObjectMeta{
82+
Name: "test-policy",
83+
Namespace: "default",
84+
},
8285
Spec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
83-
TargetRef: tt.targetRef,
86+
TargetRef: tt.policySpec.TargetRef,
8487
},
8588
}
86-
87-
if _, ok := allowedKinds[clientSettingsPolicy.Spec.TargetRef]; !ok {
88-
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
89-
90-
if tt.wantErrors == gotError {
91-
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
92-
}
93-
}
89+
validateClientSettingsPolicy(t, clientSettingsPolicy, tt.wantErrors)
9490
})
9591
}
9692
}
9793

98-
func testInvalidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.LocalPolicyTargetReference]bool) {
94+
func testInvalidTargetRefKind(t *testing.T) {
9995
t.Helper()
10096

10197
tests := []struct {
10298
name string
103-
wantErrors string
104-
targetRef gatewayv1alpha2.LocalPolicyTargetReference
99+
wantErrors []string
100+
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
105101
}{
106102
{
107-
name: "Validate TargetRef is of an allowed kind",
108-
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
109-
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
110-
Kind: "InvalidKind",
111-
Group: "gateway.networking.k8s.io",
103+
name: "Validate Invalid TargetRef Kind is not allowed",
104+
wantErrors: []string{"TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"},
105+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
106+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
107+
Kind: "InvalidKind",
108+
Group: "gateway.networking.k8s.io",
109+
},
112110
},
113111
},
114112
{
115-
name: "Validate TargetRef is of an allowed kind",
116-
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'",
117-
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{
118-
Kind: "TCPRoute",
119-
Group: "gateway.networking.k8s.io",
113+
name: "Validate TCPRoute TargetRef Kind is not allowed",
114+
wantErrors: []string{"TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"},
115+
policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
116+
TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{
117+
Kind: "TCPRoute",
118+
Group: "gateway.networking.k8s.io",
119+
},
120120
},
121121
},
122122
}
@@ -125,18 +125,15 @@ func testInvalidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.Loc
125125
t.Run(tt.name, func(t *testing.T) {
126126
// Create a ClientSettingsPolicy with the targetRef from the test case.
127127
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
128+
ObjectMeta: metav1.ObjectMeta{
129+
Name: "test-policy",
130+
Namespace: "default",
131+
},
128132
Spec: ngfAPIv1alpha1.ClientSettingsPolicySpec{
129-
TargetRef: tt.targetRef,
133+
TargetRef: tt.policySpec.TargetRef,
130134
},
131135
}
132-
133-
if _, ok := allowedKinds[clientSettingsPolicy.Spec.TargetRef]; !ok {
134-
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'"
135-
136-
if tt.wantErrors != gotError {
137-
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors)
138-
}
139-
}
136+
validateClientSettingsPolicy(t, clientSettingsPolicy, tt.wantErrors)
140137
})
141138
}
142139
}
@@ -223,3 +220,33 @@ func testInvalidTargetRefGroup(t *testing.T, allowedGroups map[gatewayv1alpha2.L
223220
})
224221
}
225222
}
223+
224+
func validateClientSettingsPolicy(t *testing.T, clientSettingsPolicy *ngfAPIv1alpha1.ClientSettingsPolicy, wantErrors []string) {
225+
t.Helper()
226+
227+
// Register API types with the runtime scheme
228+
// This is necessary to create a fake client that can handle the custom resource.
229+
scheme := runtime.NewScheme()
230+
_ = ngfAPIv1alpha1.AddToScheme(scheme)
231+
232+
// Create a fake client with the scheme
233+
// This is used to simulate interactions with the Kubernetes API.
234+
k8sClient := fake.NewClientBuilder().WithScheme(scheme).Build()
235+
236+
err := k8sClient.Create(context.Background(), clientSettingsPolicy)
237+
if err != nil {
238+
t.Logf("Error creating ClientSettingsPolicy %q: %v", fmt.Sprintf("%v/%v", clientSettingsPolicy.Namespace, clientSettingsPolicy.Name), err)
239+
}
240+
241+
// if (len(wantErrors) != 0) != (err != nil) {
242+
// t.Fatalf("Unexpected response while creating ClientSettingsPolicy %q; got err=\n%v\n;want error=%v", fmt.Sprintf("%v/%v", clientSettingsPolicy.Namespace, clientSettingsPolicy.Name), err, wantErrors)
243+
// }
244+
245+
if err != nil {
246+
for _, wantError := range wantErrors {
247+
if !strings.Contains(err.Error(), wantError) {
248+
t.Errorf("missing expected error %q in %v", wantError, err)
249+
}
250+
}
251+
}
252+
}

0 commit comments

Comments
 (0)