1
1
package cel
2
2
3
3
import (
4
+ "context"
5
+ "fmt"
6
+ "strings"
4
7
"testing"
5
8
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
+
6
13
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
7
14
8
15
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1"
9
16
)
10
17
11
18
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 )
29
25
}
30
26
31
27
func TestClientSettingsPoliciesTargetRefGroup (t * testing.T ) {
@@ -41,36 +37,39 @@ func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
41
37
testInvalidTargetRefGroup (t , allowedGroups )
42
38
}
43
39
44
- func testValidTargetRefKind (t * testing.T , allowedKinds map [gatewayv1alpha2. LocalPolicyTargetReference ] bool ) {
40
+ func testValidTargetRefKind (t * testing.T ) {
45
41
t .Helper ()
46
42
47
43
tests := []struct {
48
44
name string
49
- wantErrors string
50
- targetRef gatewayv1alpha2. LocalPolicyTargetReference
45
+ wantErrors [] string
46
+ policySpec ngfAPIv1alpha1. ClientSettingsPolicySpec
51
47
}{
52
48
{
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
+ },
58
55
},
59
56
},
60
57
{
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
+ },
66
64
},
67
65
},
68
66
{
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
+ },
74
73
},
75
74
},
76
75
}
@@ -79,44 +78,45 @@ func testValidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.Local
79
78
t .Run (tt .name , func (t * testing.T ) {
80
79
// Create a ClientSettingsPolicy with the targetRef from the test case.
81
80
clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
81
+ ObjectMeta : metav1.ObjectMeta {
82
+ Name : "test-policy" ,
83
+ Namespace : "default" ,
84
+ },
82
85
Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
83
- TargetRef : tt .targetRef ,
86
+ TargetRef : tt .policySpec . TargetRef ,
84
87
},
85
88
}
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 )
94
90
})
95
91
}
96
92
}
97
93
98
- func testInvalidTargetRefKind (t * testing.T , allowedKinds map [gatewayv1alpha2. LocalPolicyTargetReference ] bool ) {
94
+ func testInvalidTargetRefKind (t * testing.T ) {
99
95
t .Helper ()
100
96
101
97
tests := []struct {
102
98
name string
103
- wantErrors string
104
- targetRef gatewayv1alpha2. LocalPolicyTargetReference
99
+ wantErrors [] string
100
+ policySpec ngfAPIv1alpha1. ClientSettingsPolicySpec
105
101
}{
106
102
{
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
+ },
112
110
},
113
111
},
114
112
{
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
+ },
120
120
},
121
121
},
122
122
}
@@ -125,18 +125,15 @@ func testInvalidTargetRefKind(t *testing.T, allowedKinds map[gatewayv1alpha2.Loc
125
125
t .Run (tt .name , func (t * testing.T ) {
126
126
// Create a ClientSettingsPolicy with the targetRef from the test case.
127
127
clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
128
+ ObjectMeta : metav1.ObjectMeta {
129
+ Name : "test-policy" ,
130
+ Namespace : "default" ,
131
+ },
128
132
Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
129
- TargetRef : tt .targetRef ,
133
+ TargetRef : tt .policySpec . TargetRef ,
130
134
},
131
135
}
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 )
140
137
})
141
138
}
142
139
}
@@ -223,3 +220,33 @@ func testInvalidTargetRefGroup(t *testing.T, allowedGroups map[gatewayv1alpha2.L
223
220
})
224
221
}
225
222
}
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