@@ -2,6 +2,7 @@ package gatewayapi
22
33import (
44 "fmt"
5+ "net/http"
56 "time"
67
78 . "github.com/onsi/ginkgo/v2"
8081 headers:
8182 X-Proxy-Test: "disabled"
8283`
84+ var (
85+ gatewayProxyWithPluginMetadata0 = `
86+ apiVersion: gateway.apisix.io/v1alpha1
87+ kind: GatewayProxy
88+ metadata:
89+ name: api7-proxy-config
90+ spec:
91+ plugins:
92+ - name: error-page
93+ enabled: true
94+ config: {}
95+ pluginMetadata:
96+ error-page: {
97+ "enable": true,
98+ "error_404": {
99+ "body": "404 from plugin metadata",
100+ "content-type": "text/plain"
101+ }
102+ }
103+ `
104+ gatewayProxyWithPluginMetadata1 = `
105+ apiVersion: gateway.apisix.io/v1alpha1
106+ kind: GatewayProxy
107+ metadata:
108+ name: api7-proxy-config
109+ spec:
110+ plugins:
111+ - name: error-page
112+ enabled: true
113+ config: {}
114+ pluginMetadata:
115+ error-page: {
116+ "enable": false,
117+ "error_404": {
118+ "body": "404 from plugin metadata",
119+ "content-type": "text/plain"
120+ }
121+ }
122+ `
123+ )
83124
84125 var httpRouteForTest = `
85126apiVersion: gateway.networking.k8s.io/v1
@@ -101,15 +142,15 @@ spec:
101142 port: 80
102143`
103144
104- var ResourceApplied = func (resourceType , resourceName , resourceRaw string , observedGeneration int ) {
145+ var resourceApplied = func (resourceType , resourceName , resourceRaw string , observedGeneration int ) {
105146 Expect (s .CreateResourceFromString (resourceRaw )).
106147 NotTo (HaveOccurred (), fmt .Sprintf ("creating %s" , resourceType ))
107148
108149 Eventually (func () string {
109150 hryaml , err := s .GetResourceYaml (resourceType , resourceName )
110151 Expect (err ).NotTo (HaveOccurred (), fmt .Sprintf ("getting %s yaml" , resourceType ))
111152 return hryaml
112- }, "8s" , "2s" ).
153+ }). WithTimeout ( 8 * time . Second ). ProbeEvery ( 2 * time . Second ).
113154 Should (
114155 SatisfyAll (
115156 ContainSubstring (`status: "True"` ),
@@ -163,7 +204,7 @@ spec:
163204 Context ("Test Gateway with enabled GatewayProxy plugin" , func () {
164205 It ("Should apply plugin configuration when enabled" , func () {
165206 By ("Create HTTPRoute for Gateway with GatewayProxy" )
166- ResourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
207+ resourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
167208
168209 By ("Check if the plugin is applied" )
169210 resp := s .NewAPISIXClient ().
@@ -180,7 +221,7 @@ spec:
180221 time .Sleep (5 * time .Second )
181222
182223 By ("Create HTTPRoute for Gateway with GatewayProxy" )
183- ResourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
224+ resourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
184225
185226 By ("Check if the plugin is not applied" )
186227 resp = s .NewAPISIXClient ().
@@ -194,7 +235,7 @@ spec:
194235
195236 It ("Should work normally without GatewayProxy" , func () {
196237 By ("Create HTTPRoute for Gateway with GatewayProxy" )
197- ResourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
238+ resourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
198239
199240 By ("Check if the plugin is applied" )
200241 resp := s .NewAPISIXClient ().
@@ -211,7 +252,7 @@ spec:
211252 time .Sleep (5 * time .Second )
212253
213254 By ("Create HTTPRoute for Gateway without GatewayProxy" )
214- ResourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
255+ resourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
215256
216257 By ("Check if the route works without plugin" )
217258 resp = s .NewAPISIXClient ().
@@ -224,4 +265,53 @@ spec:
224265 })
225266 })
226267
268+ Context ("Test Gateway with PluginMetadata" , func () {
269+ var (
270+ err error
271+ )
272+
273+ It ("Should work OK with error-page" , func () {
274+ By ("Update GatewayProxy with PluginMetadata" )
275+ err = s .CreateResourceFromString (gatewayProxyWithPluginMetadata0 )
276+ Expect (err ).ShouldNot (HaveOccurred ())
277+ time .Sleep (5 * time .Second )
278+
279+ By ("Create HTTPRoute for Gateway with GatewayProxy" )
280+ resourceApplied ("HTTPRoute" , "test-route" , fmt .Sprintf (httpRouteForTest , "api7" ), 1 )
281+
282+ By ("Check PluginMetadata working" )
283+ s .NewAPISIXClient ().
284+ GET ("/not-found" ).
285+ WithHost ("example.com" ).
286+ Expect ().
287+ Status (http .StatusNotFound ).
288+ Body ().Contains ("404 from plugin metadata" )
289+
290+ By ("Update GatewayProxy with PluginMetadata" )
291+ err = s .CreateResourceFromString (gatewayProxyWithPluginMetadata1 )
292+ Expect (err ).ShouldNot (HaveOccurred ())
293+ time .Sleep (5 * time .Second )
294+
295+ By ("Check PluginMetadata working" )
296+ s .NewAPISIXClient ().
297+ GET ("/not-found" ).
298+ WithHost ("example.com" ).
299+ Expect ().
300+ Status (http .StatusNotFound ).
301+ Body ().Contains (`{"error_msg":"404 Route Not Found"}` )
302+
303+ By ("Delete GatewayProxy" )
304+ err = s .DeleteResourceFromString (gatewayProxyWithPluginMetadata0 )
305+ Expect (err ).ShouldNot (HaveOccurred ())
306+ time .Sleep (5 * time .Second )
307+
308+ By ("Check PluginMetadata is not working" )
309+ s .NewAPISIXClient ().
310+ GET ("/not-found" ).
311+ WithHost ("example.com" ).
312+ Expect ().
313+ Status (http .StatusNotFound ).
314+ Body ().Contains (`{"error_msg":"404 Route Not Found"}` )
315+ })
316+ })
227317})
0 commit comments