@@ -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,65 @@ 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
+ // graph *Graph
119
+ gateway * Gateway
120
+ routes map [RouteKey ]* L7Route
121
+ ngfPolicies map [PolicyKey ]* Policy
122
+ expect func (g * WithT , graph * Graph )
123
+ controllerName string
124
+ name string
198
125
}{
199
126
{
200
127
name : "nil Gateway" ,
201
- graph : newModifiedGraph (func (g * Graph ) * Graph {
202
- g .Gateway = nil
203
- return g
204
- }),
128
+ routes : createRoutesForGraph (
129
+ map [string ]RouteType {
130
+ "hr1-route" : RouteTypeHTTP ,
131
+ "hr2-route" : RouteTypeHTTP ,
132
+ "grpc-route" : RouteTypeGRPC ,
133
+ },
134
+ ),
135
+ ngfPolicies : map [PolicyKey ]* Policy {
136
+ createTestPolicyKey (policyGVK , "gw-policy" ): createPolicy ([]string {"gateway" , "gateway1" }, kinds .Gateway ),
137
+ createTestPolicyKey (policyGVK , "route-policy" ): createPolicy (
138
+ []string {"hr1-route" , "hr2-route" },
139
+ kinds .HTTPRoute ,
140
+ ),
141
+ createTestPolicyKey (policyGVK , "grpc-route-policy" ): createPolicy ([]string {"grpc-route" }, kinds .GRPCRoute ),
142
+ },
205
143
expect : expectNoPolicyAttachment ,
206
144
},
207
145
{
208
- name : "nil routes" ,
209
- graph : newModifiedGraph (func (g * Graph ) * Graph {
210
- g .Routes = nil
211
- return g
212
- }),
146
+ name : "nil routes" ,
147
+ gateway : createGateway ("gateway" ),
148
+ ngfPolicies : map [PolicyKey ]* Policy {
149
+ createTestPolicyKey (policyGVK , "gw-policy1" ): createPolicy ([]string {"gateway" , "gateway1" }, kinds .Gateway ),
150
+ createTestPolicyKey (policyGVK , "route-policy1" ): createPolicy (
151
+ []string {"hr1-route" , "hr2-route" },
152
+ kinds .HTTPRoute ,
153
+ ),
154
+ createTestPolicyKey (policyGVK , "grpc-route-policy1" ): createPolicy ([]string {"grpc-route" }, kinds .GRPCRoute ),
155
+ },
213
156
expect : expectGatewayPolicyAttachment ,
214
157
},
215
158
{
216
- name : "normal" ,
217
- graph : newGraph (),
218
- expect : expectPolicyAttachment ,
159
+ name : "normal" ,
160
+ routes : createRoutesForGraph (
161
+ map [string ]RouteType {
162
+ "hr-1" : RouteTypeHTTP ,
163
+ "hr-2" : RouteTypeHTTP ,
164
+ "grpc-1" : RouteTypeGRPC ,
165
+ },
166
+ ),
167
+ ngfPolicies : map [PolicyKey ]* Policy {
168
+ createTestPolicyKey (policyGVK , "gw-policy2" ): createPolicy ([]string {"gateway2" , "gateway3" }, kinds .Gateway ),
169
+ createTestPolicyKey (policyGVK , "route-policy2" ): createPolicy (
170
+ []string {"hr-1" , "hr-2" },
171
+ kinds .HTTPRoute ,
172
+ ),
173
+ createTestPolicyKey (policyGVK , "grpc-route-policy2" ): createPolicy ([]string {"grpc-1" }, kinds .GRPCRoute ),
174
+ },
175
+ gateway : createGateway ("gateway2" ),
176
+ expect : expectPolicyAttachment ,
219
177
},
220
178
}
221
179
@@ -224,8 +182,14 @@ func TestAttachPolicies(t *testing.T) {
224
182
t .Parallel ()
225
183
g := NewWithT (t )
226
184
227
- test .graph .attachPolicies ("nginx-gateway" )
228
- test .expect (g , test .graph )
185
+ graph := & Graph {
186
+ Gateway : test .gateway ,
187
+ Routes : test .routes ,
188
+ NGFPolicies : test .ngfPolicies ,
189
+ }
190
+
191
+ graph .attachPolicies ("nginx-gateway" )
192
+ test .expect (g , graph )
229
193
})
230
194
}
231
195
}
0 commit comments