@@ -206,34 +206,67 @@ spec:
206206 s .ApplyHTTPRoute (types.NamespacedName {Namespace : s .Namespace (), Name : "httpbin-v2" }, fmt .Sprintf (routeToHTTPV2Port , s .Namespace (), s .Namespace ()))
207207 })
208208
209+ const rewrittenHost = "httpbin.section-v2.example.com"
210+
209211 It ("should only attach to the backend matching sectionName" , func () {
210212 s .ResourceApplied ("BackendTrafficPolicy" , "httpbin" , policyScopedToHTTPV2 , 1 )
211213
212- By ("policy applies to the http-v2 (8080) backend" )
214+ // Drive traffic to both hosts so both per-port upstreams are registered
215+ // in the dataplane.
213216 s .RequestAssert (& scaffold.RequestAssert {
214- Method : "GET" ,
215- Path : "/headers" ,
216- Host : "httpbin-v2.org" ,
217- Headers : map [string ]string {
218- "Host" : "httpbin-v2.org" ,
219- },
217+ Method : "GET" ,
218+ Path : "/headers" ,
219+ Host : "httpbin-v2.org" ,
220+ Headers : map [string ]string {"Host" : "httpbin-v2.org" },
221+ Checks : []scaffold.ResponseCheckFunc {scaffold .WithExpectedStatus (200 )},
222+ })
223+ s .RequestAssert (& scaffold.RequestAssert {
224+ Method : "GET" ,
225+ Path : "/headers" ,
226+ Host : "httpbin.org" ,
227+ Headers : map [string ]string {"Host" : "httpbin.org" },
228+ Checks : []scaffold.ResponseCheckFunc {scaffold .WithExpectedStatus (200 )},
229+ })
230+
231+ // The Service is split into two per-port upstreams (http/80 and
232+ // http-v2/8080) with identical nodes. The sectionName-scoped policy must
233+ // attach to exactly one of them; under name-only matching it would attach
234+ // to both (the whole Service), which this assertion rejects.
235+ Eventually (func (g Gomega ) {
236+ ups , err := s .DefaultDataplaneResource ().Upstream ().List (context .Background ())
237+ g .Expect (err ).ToNot (HaveOccurred (), "listing upstreams" )
238+ g .Expect (ups ).To (HaveLen (2 ), "two per-port upstreams should exist" )
239+
240+ var rewritten []* adctypes.Upstream
241+ for _ , u := range ups {
242+ if u .PassHost == "rewrite" && u .UpstreamHost == rewrittenHost {
243+ rewritten = append (rewritten , u )
244+ }
245+ }
246+ g .Expect (rewritten ).To (HaveLen (1 ), "policy must attach to exactly the sectionName-matched port, not the whole Service" )
247+ }).WithTimeout (scaffold .DefaultTimeout ).ProbeEvery (scaffold .DefaultInterval ).Should (Succeed ())
248+
249+ By ("the matched backend (httpbin-v2.org -> 8080) reflects the rewritten host" )
250+ s .RequestAssert (& scaffold.RequestAssert {
251+ Method : "GET" ,
252+ Path : "/headers" ,
253+ Host : "httpbin-v2.org" ,
254+ Headers : map [string ]string {"Host" : "httpbin-v2.org" },
220255 Checks : []scaffold.ResponseCheckFunc {
221256 scaffold .WithExpectedStatus (200 ),
222- scaffold .WithExpectedBodyContains ("httpbin.section-v2.example.com" ),
257+ scaffold .WithExpectedBodyContains (rewrittenHost ),
223258 },
224259 })
225260
226- By ("policy does not apply to the http ( 80) backend " )
261+ By ("the unmatched backend (httpbin.org -> 80) keeps the original host " )
227262 s .RequestAssert (& scaffold.RequestAssert {
228- Method : "GET" ,
229- Path : "/headers" ,
230- Host : "httpbin.org" ,
231- Headers : map [string ]string {
232- "Host" : "httpbin.org" ,
233- },
263+ Method : "GET" ,
264+ Path : "/headers" ,
265+ Host : "httpbin.org" ,
266+ Headers : map [string ]string {"Host" : "httpbin.org" },
234267 Checks : []scaffold.ResponseCheckFunc {
235268 scaffold .WithExpectedStatus (200 ),
236- scaffold .WithExpectedBodyNotContains ("httpbin.section-v2.example.com" ),
269+ scaffold .WithExpectedBodyNotContains (rewrittenHost ),
237270 },
238271 })
239272 })
0 commit comments