@@ -12,6 +12,7 @@ import (
12
12
"github.com/kong/go-kong/kong"
13
13
"github.com/kong/kubernetes-testing-framework/pkg/clusters"
14
14
"github.com/kong/kubernetes-testing-framework/pkg/utils/kubernetes/generators"
15
+ "github.com/stretchr/testify/assert"
15
16
"github.com/stretchr/testify/require"
16
17
appsv1 "k8s.io/api/apps/v1"
17
18
corev1 "k8s.io/api/core/v1"
@@ -36,11 +37,16 @@ func TestConsumerGroup(t *testing.T) {
36
37
ctx := context .Background ()
37
38
ns , cleaner := helpers .Setup (ctx , t , env )
38
39
39
- d , s , i , p := deployMinimalSvcWithKeyAuth (ctx , t , ns .Name )
40
- cleaner .Add (d )
41
- cleaner .Add (s )
42
- cleaner .Add (i )
43
- cleaner .Add (p )
40
+ // path is the basic path used for most of the test
41
+ path := "/test-consumer-group/basic"
42
+ // multiPath is the path used to test consumer group + route plugins
43
+ multiPath := "/test-consumer-group/multi"
44
+
45
+ deployment , service , ingress , keyauthPlugin := deployMinimalSvcWithKeyAuth (ctx , t , ns .Name , path )
46
+ cleaner .Add (deployment )
47
+ cleaner .Add (service )
48
+ cleaner .Add (ingress )
49
+ cleaner .Add (keyauthPlugin )
44
50
45
51
addedHeader := header {
46
52
K : "X-Test-Header" ,
@@ -89,6 +95,15 @@ func TestConsumerGroup(t *testing.T) {
89
95
ctx , t , ns .Name , "test-consumer-group-2" , pluginRateLimit .Name ,
90
96
)
91
97
cleaner .Add (rateLimitGroup )
98
+ // 3 has consumers but no plugins
99
+ nothingGroup := configureConsumerGroupWithPlugins (
100
+ ctx , t , ns .Name , "test-consumer-group-3" ,
101
+ )
102
+ cleaner .Add (nothingGroup )
103
+ addHeaderRouteGroup := configureConsumerGroupWithPlugins (
104
+ ctx , t , ns .Name , "test-consumer-group-4" , pluginRespTrans .Name ,
105
+ )
106
+ cleaner .Add (addHeaderRouteGroup )
92
107
93
108
rateLimitHeader := header {
94
109
K : "RateLimit-Limit" ,
@@ -130,7 +145,7 @@ func TestConsumerGroup(t *testing.T) {
130
145
t .Log ("checking if consumer has plugin configured correctly based on consumer group membership" )
131
146
for _ , consumer := range consumers {
132
147
require .Eventually (t , func () bool {
133
- req := helpers .MustHTTPRequest (t , http .MethodGet , proxyURL .Host , "/" , map [string ]string {
148
+ req := helpers .MustHTTPRequest (t , http .MethodGet , proxyURL .Host , path , map [string ]string {
134
149
"apikey" : consumer .Name ,
135
150
})
136
151
resp , err := helpers .DefaultHTTPClientWithProxy (proxyURL ).Do (req )
@@ -158,10 +173,77 @@ func TestConsumerGroup(t *testing.T) {
158
173
return true
159
174
}, ingressWait , waitTick )
160
175
}
176
+
177
+ t .Log ("checking plugins attached to a consumer group and route only apply when request matches both" )
178
+ four , fourSecret := configureConsumerWithAPIKey (ctx , t , ns .Name , "test-consumer-4" , "test-consumer-group-4" )
179
+ cleaner .Add (four )
180
+ cleaner .Add (fourSecret )
181
+
182
+ multiIngress := generators .NewIngressForService (multiPath , map [string ]string {
183
+ annotations .AnnotationPrefix + annotations .StripPathKey : "true" ,
184
+ annotations .AnnotationPrefix + annotations .PluginsKey : strings .Join ([]string {keyauthPlugin .Name , pluginRespTrans .Name }, "," ),
185
+ }, service )
186
+ multiIngress .Spec .IngressClassName = kong .String (consts .IngressClass )
187
+ multiIngress .Name = "multi"
188
+ require .NoError (t , clusters .DeployIngress (ctx , env .Cluster (), ns .Name , multiIngress ))
189
+ cleaner .Add (multiIngress )
190
+
191
+ require .EventuallyWithT (t , func (c * assert.CollectT ) {
192
+ // this should see the header, it uses a consumer in the group on the associated route
193
+ req := helpers .MustHTTPRequest (t , http .MethodGet , proxyURL .Host , multiPath , map [string ]string {
194
+ "apikey" : four .Name ,
195
+ })
196
+ resp , err := helpers .DefaultHTTPClientWithProxy (proxyURL ).Do (req )
197
+ if ! assert .NoError (c , err ) {
198
+ return
199
+ }
200
+ defer resp .Body .Close ()
201
+ if ! assert .Equal (c , resp .StatusCode , http .StatusOK ) {
202
+ return
203
+ }
204
+ hv := resp .Header .Get (addedHeader .K )
205
+ if ! assert .Equal (c , addedHeader .V , hv ) {
206
+ return
207
+ }
208
+
209
+ // this should not see the header, it uses a consumer in the group on another route
210
+ clear := helpers .MustHTTPRequest (t , http .MethodGet , proxyURL .Host , path , map [string ]string {
211
+ "apikey" : four .Name ,
212
+ })
213
+ clearResp , err := helpers .DefaultHTTPClientWithProxy (proxyURL ).Do (clear )
214
+ if ! assert .NoError (c , err ) {
215
+ return
216
+ }
217
+ defer clearResp .Body .Close ()
218
+ if ! assert .Equal (c , clearResp .StatusCode , http .StatusOK ) {
219
+ return
220
+ }
221
+ hv = clearResp .Header .Get (addedHeader .K )
222
+ if ! assert .NotEqual (c , addedHeader .V , hv ) {
223
+ return
224
+ }
225
+
226
+ // this should not see the header, it uses a consumer outside the group on the associated route
227
+ empty := helpers .MustHTTPRequest (t , http .MethodGet , proxyURL .Host , multiPath , map [string ]string {
228
+ "apikey" : "test-consumer-3" ,
229
+ })
230
+ emptyResp , err := helpers .DefaultHTTPClientWithProxy (proxyURL ).Do (empty )
231
+ if ! assert .NoError (c , err ) {
232
+ return
233
+ }
234
+ defer emptyResp .Body .Close ()
235
+ if ! assert .Equal (c , emptyResp .StatusCode , http .StatusOK ) {
236
+ return
237
+ }
238
+ hv = emptyResp .Header .Get (addedHeader .K )
239
+ if ! assert .NotEqual (c , addedHeader .V , hv ) {
240
+ return
241
+ }
242
+ }, ingressWait , waitTick )
161
243
}
162
244
163
245
func deployMinimalSvcWithKeyAuth (
164
- ctx context.Context , t * testing.T , namespace string ,
246
+ ctx context.Context , t * testing.T , namespace , path string ,
165
247
) (* appsv1.Deployment , * corev1.Service , * netv1.Ingress , * kongv1.KongPlugin ) {
166
248
const pluginKeyAuthName = "key-auth"
167
249
t .Logf ("configuring plugin %q (to give consumers an identity)" , pluginKeyAuthName )
@@ -194,7 +276,7 @@ func deployMinimalSvcWithKeyAuth(
194
276
require .NoError (t , err )
195
277
196
278
t .Logf ("creating an ingress for service %q with plugin %q attached" , service .Name , pluginKeyAuthName )
197
- ingress := generators .NewIngressForService ("/" , map [string ]string {
279
+ ingress := generators .NewIngressForService (path , map [string ]string {
198
280
annotations .AnnotationPrefix + annotations .StripPathKey : "true" ,
199
281
annotations .AnnotationPrefix + annotations .PluginsKey : pluginKeyAuthName ,
200
282
}, service )
0 commit comments