@@ -25,60 +25,20 @@ var testNs = "test"
25
25
func TestAttachPolicies (t * testing.T ) {
26
26
t .Parallel ()
27
27
policyGVK := schema.GroupVersionKind {Group : "Group" , Version : "Version" , Kind : "Policy" }
28
-
29
- gwPolicyKey := createTestPolicyKey (policyGVK , "gw-policy" )
30
- gwPolicy := & Policy {
31
- Valid : true ,
32
- Source : & policiesfakes.FakePolicy {},
33
- TargetRefs : []PolicyTargetRef {
34
- {
35
- Kind : kinds .Gateway ,
36
- Group : v1 .GroupName ,
37
- Nsname : types.NamespacedName {Namespace : testNs , Name : "gateway" },
38
- },
39
- {
40
- Kind : kinds .Gateway ,
41
- Group : v1 .GroupName ,
42
- Nsname : types.NamespacedName {Namespace : testNs , Name : "gateway2" }, // ignored
43
- },
44
- },
45
- }
46
-
47
- routePolicyKey := createTestPolicyKey (policyGVK , "route-policy" )
48
- routePolicy := & Policy {
49
- Valid : true ,
50
- Source : & policiesfakes.FakePolicy {},
51
- TargetRefs : []PolicyTargetRef {
52
- {
53
- Kind : kinds .HTTPRoute ,
54
- Group : v1 .GroupName ,
55
- Nsname : types.NamespacedName {Namespace : testNs , Name : "hr-route" },
56
- },
57
- {
58
- Kind : kinds .HTTPRoute ,
59
- Group : v1 .GroupName ,
60
- Nsname : types.NamespacedName {Namespace : testNs , Name : "hr2-route" },
61
- },
62
- },
63
- }
64
-
65
- grpcRoutePolicyKey := createTestPolicyKey (policyGVK , "grpc-route-policy" )
66
- grpcRoutePolicy := & Policy {
67
- Valid : true ,
68
- Source : & policiesfakes.FakePolicy {},
69
- TargetRefs : []PolicyTargetRef {
70
- {
71
- Kind : kinds .GRPCRoute ,
28
+ createPolicy := func (targetRefsNames []string , refKind v1.Kind ) * Policy {
29
+ targetRefs := make ([]PolicyTargetRef , 0 , len (targetRefsNames ))
30
+ for _ , name := range targetRefsNames {
31
+ targetRefs = append (targetRefs , PolicyTargetRef {
32
+ Kind : refKind ,
72
33
Group : v1 .GroupName ,
73
- Nsname : types.NamespacedName {Namespace : testNs , Name : "grpc-route" },
74
- },
75
- },
76
- }
77
-
78
- ngfPolicies := map [PolicyKey ]* Policy {
79
- gwPolicyKey : gwPolicy ,
80
- routePolicyKey : routePolicy ,
81
- grpcRoutePolicyKey : grpcRoutePolicy ,
34
+ Nsname : types.NamespacedName {Namespace : testNs , Name : name },
35
+ })
36
+ }
37
+ return & Policy {
38
+ Valid : true ,
39
+ Source : & policiesfakes.FakePolicy {},
40
+ TargetRefs : targetRefs ,
41
+ }
82
42
}
83
43
84
44
createRouteKey := func (name string , routeType RouteType ) RouteKey {
@@ -88,77 +48,40 @@ func TestAttachPolicies(t *testing.T) {
88
48
}
89
49
}
90
50
91
- newGraph := func () * Graph {
92
- return & Graph {
93
- Gateway : & Gateway {
94
- Source : & v1.Gateway {
95
- ObjectMeta : metav1.ObjectMeta {
96
- Name : "gateway" ,
97
- Namespace : testNs ,
98
- },
51
+ createGateway := func (name string ) * Gateway {
52
+ return & Gateway {
53
+ Source : & v1.Gateway {
54
+ ObjectMeta : metav1.ObjectMeta {
55
+ Name : name ,
56
+ Namespace : testNs ,
99
57
},
100
- Valid : true ,
101
58
},
102
- Routes : map [RouteKey ]* L7Route {
103
- createRouteKey ("hr-route" , RouteTypeHTTP ): {
104
- Source : & v1.HTTPRoute {
105
- ObjectMeta : metav1.ObjectMeta {
106
- Name : "hr-route" ,
107
- Namespace : testNs ,
108
- },
109
- },
110
- ParentRefs : []ParentRef {
111
- {
112
- Attachment : & ParentRefAttachmentStatus {
113
- Attached : true ,
114
- },
115
- },
116
- },
117
- Valid : true ,
118
- Attachable : true ,
119
- },
120
- createRouteKey ("hr2-route" , RouteTypeHTTP ): {
121
- Source : & v1.HTTPRoute {
122
- ObjectMeta : metav1.ObjectMeta {
123
- Name : "hr2-route" ,
124
- Namespace : testNs ,
125
- },
126
- },
127
- ParentRefs : []ParentRef {
128
- {
129
- Attachment : & ParentRefAttachmentStatus {
130
- Attached : true ,
131
- },
132
- },
59
+ Valid : true ,
60
+ }
61
+ }
62
+
63
+ createRoutesForGraph := func (routes map [string ]RouteType ) map [RouteKey ]* L7Route {
64
+ routesMap := make (map [RouteKey ]* L7Route , len (routes ))
65
+ for routeName , routeType := range routes {
66
+ routesMap [createRouteKey (routeName , routeType )] = & L7Route {
67
+ Source : & v1.HTTPRoute {
68
+ ObjectMeta : metav1.ObjectMeta {
69
+ Name : routeName ,
70
+ Namespace : testNs ,
133
71
},
134
- Valid : true ,
135
- Attachable : true ,
136
72
},
137
- createRouteKey ("grpc-route" , RouteTypeGRPC ): {
138
- Source : & v1alpha2.GRPCRoute {
139
- ObjectMeta : metav1.ObjectMeta {
140
- Name : "grpc-route" ,
141
- Namespace : testNs ,
142
- },
143
- },
144
- ParentRefs : []ParentRef {
145
- {
146
- Attachment : & ParentRefAttachmentStatus {
147
- Attached : true ,
148
- },
73
+ ParentRefs : []ParentRef {
74
+ {
75
+ Attachment : & ParentRefAttachmentStatus {
76
+ Attached : true ,
149
77
},
150
78
},
151
- Valid : true ,
152
- Attachable : true ,
153
79
},
154
- } ,
155
-
156
- NGFPolicies : ngfPolicies ,
80
+ Valid : true ,
81
+ Attachable : true ,
82
+ }
157
83
}
158
- }
159
-
160
- newModifiedGraph := func (mod func (g * Graph ) * Graph ) * Graph {
161
- return mod (newGraph ())
84
+ return routesMap
162
85
}
163
86
164
87
expectNoPolicyAttachment := func (g * WithT , graph * Graph ) {
@@ -192,30 +115,63 @@ func TestAttachPolicies(t *testing.T) {
192
115
}
193
116
194
117
tests := []struct {
195
- graph * Graph
196
- expect func (g * WithT , graph * Graph )
197
- name string
118
+ gateway * Gateway
119
+ routes map [RouteKey ]* L7Route
120
+ ngfPolicies map [PolicyKey ]* Policy
121
+ expect func (g * WithT , graph * Graph )
122
+ name string
198
123
}{
199
124
{
200
125
name : "nil Gateway" ,
201
- graph : newModifiedGraph (func (g * Graph ) * Graph {
202
- g .Gateway = nil
203
- return g
204
- }),
126
+ routes : createRoutesForGraph (
127
+ map [string ]RouteType {
128
+ "hr1-route" : RouteTypeHTTP ,
129
+ "hr2-route" : RouteTypeHTTP ,
130
+ "grpc-route" : RouteTypeGRPC ,
131
+ },
132
+ ),
133
+ ngfPolicies : map [PolicyKey ]* Policy {
134
+ createTestPolicyKey (policyGVK , "gw-policy" ): createPolicy ([]string {"gateway" , "gateway1" }, kinds .Gateway ),
135
+ createTestPolicyKey (policyGVK , "route-policy" ): createPolicy (
136
+ []string {"hr1-route" , "hr2-route" },
137
+ kinds .HTTPRoute ,
138
+ ),
139
+ createTestPolicyKey (policyGVK , "grpc-route-policy" ): createPolicy ([]string {"grpc-route" }, kinds .GRPCRoute ),
140
+ },
205
141
expect : expectNoPolicyAttachment ,
206
142
},
207
143
{
208
- name : "nil routes" ,
209
- graph : newModifiedGraph (func (g * Graph ) * Graph {
210
- g .Routes = nil
211
- return g
212
- }),
144
+ name : "nil routes" ,
145
+ gateway : createGateway ("gateway" ),
146
+ ngfPolicies : map [PolicyKey ]* Policy {
147
+ createTestPolicyKey (policyGVK , "gw-policy1" ): createPolicy ([]string {"gateway" , "gateway1" }, kinds .Gateway ),
148
+ createTestPolicyKey (policyGVK , "route-policy1" ): createPolicy (
149
+ []string {"hr1-route" , "hr2-route" },
150
+ kinds .HTTPRoute ,
151
+ ),
152
+ createTestPolicyKey (policyGVK , "grpc-route-policy1" ): createPolicy ([]string {"grpc-route" }, kinds .GRPCRoute ),
153
+ },
213
154
expect : expectGatewayPolicyAttachment ,
214
155
},
215
156
{
216
- name : "normal" ,
217
- graph : newGraph (),
218
- expect : expectPolicyAttachment ,
157
+ name : "normal" ,
158
+ routes : createRoutesForGraph (
159
+ map [string ]RouteType {
160
+ "hr-1" : RouteTypeHTTP ,
161
+ "hr-2" : RouteTypeHTTP ,
162
+ "grpc-1" : RouteTypeGRPC ,
163
+ },
164
+ ),
165
+ ngfPolicies : map [PolicyKey ]* Policy {
166
+ createTestPolicyKey (policyGVK , "gw-policy2" ): createPolicy ([]string {"gateway2" , "gateway3" }, kinds .Gateway ),
167
+ createTestPolicyKey (policyGVK , "route-policy2" ): createPolicy (
168
+ []string {"hr-1" , "hr-2" },
169
+ kinds .HTTPRoute ,
170
+ ),
171
+ createTestPolicyKey (policyGVK , "grpc-route-policy2" ): createPolicy ([]string {"grpc-1" }, kinds .GRPCRoute ),
172
+ },
173
+ gateway : createGateway ("gateway2" ),
174
+ expect : expectPolicyAttachment ,
219
175
},
220
176
}
221
177
@@ -224,8 +180,14 @@ func TestAttachPolicies(t *testing.T) {
224
180
t .Parallel ()
225
181
g := NewWithT (t )
226
182
227
- test .graph .attachPolicies ("nginx-gateway" )
228
- test .expect (g , test .graph )
183
+ graph := & Graph {
184
+ Gateway : test .gateway ,
185
+ Routes : test .routes ,
186
+ NGFPolicies : test .ngfPolicies ,
187
+ }
188
+
189
+ graph .attachPolicies ("nginx-gateway" )
190
+ test .expect (g , graph )
229
191
})
230
192
}
231
193
}
0 commit comments