Skip to content

Commit 17d45e0

Browse files
committed
test(e2e): verify sectionName selects each named port symmetrically
Cover both directions so the test proves the sectionName value drives port selection, not a coincidental attachment: sectionName=http-v2 attaches to the 8080 port only, and sectionName=http attaches to the 80 port only (the other port stays unaffected in each case).
1 parent 4495ec6 commit 17d45e0

1 file changed

Lines changed: 38 additions & 33 deletions

File tree

test/e2e/crds/v1alpha1/backendtrafficpolicy.go

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ spec:
185185
port: 8080
186186
`
187187

188-
var policyScopedToHTTPV2 = `
188+
var policyTmpl = `
189189
apiVersion: apisix.apache.org/v1alpha1
190190
kind: BackendTrafficPolicy
191191
metadata:
@@ -195,9 +195,9 @@ spec:
195195
- name: httpbin-service-e2e-test
196196
kind: Service
197197
group: ""
198-
sectionName: http-v2
198+
sectionName: %s
199199
passHost: rewrite
200-
upstreamHost: httpbin.section-v2.example.com
200+
upstreamHost: %s
201201
`
202202

203203
BeforeEach(func() {
@@ -206,27 +206,24 @@ 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-
211-
It("should only attach to the backend matching sectionName", func() {
212-
s.ResourceApplied("BackendTrafficPolicy", "httpbin", policyScopedToHTTPV2, 1)
213-
214-
// Drive traffic to both hosts so both per-port upstreams are registered
215-
// in the dataplane.
216-
s.RequestAssert(&scaffold.RequestAssert{
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-
})
209+
// assertScopedToPort applies a policy targeting the given port name and
210+
// asserts it attaches to exactly that port: the host routed to the matched
211+
// port gets the rewritten upstreamHost, while the host routed to the other
212+
// port keeps the original host. matchedHost routes to the port named by
213+
// sectionName; otherHost routes to the other port.
214+
assertScopedToPort := func(sectionName, rewriteHost, matchedHost, otherHost string) {
215+
s.ResourceApplied("BackendTrafficPolicy", "httpbin", fmt.Sprintf(policyTmpl, sectionName, rewriteHost), 1)
216+
217+
// Drive traffic to both hosts so both per-port upstreams are registered.
218+
for _, host := range []string{matchedHost, otherHost} {
219+
s.RequestAssert(&scaffold.RequestAssert{
220+
Method: "GET",
221+
Path: "/headers",
222+
Host: host,
223+
Headers: map[string]string{"Host": host},
224+
Checks: []scaffold.ResponseCheckFunc{scaffold.WithExpectedStatus(200)},
225+
})
226+
}
230227

231228
// The Service is split into two per-port upstreams (http/80 and
232229
// http-v2/8080) with identical nodes. The sectionName-scoped policy must
@@ -239,36 +236,44 @@ spec:
239236

240237
var rewritten []*adctypes.Upstream
241238
for _, u := range ups {
242-
if u.PassHost == "rewrite" && u.UpstreamHost == rewrittenHost {
239+
if u.PassHost == "rewrite" && u.UpstreamHost == rewriteHost {
243240
rewritten = append(rewritten, u)
244241
}
245242
}
246243
g.Expect(rewritten).To(HaveLen(1), "policy must attach to exactly the sectionName-matched port, not the whole Service")
247244
}).WithTimeout(scaffold.DefaultTimeout).ProbeEvery(scaffold.DefaultInterval).Should(Succeed())
248245

249-
By("the matched backend (httpbin-v2.org -> 8080) reflects the rewritten host")
246+
By("the matched port reflects the rewritten host")
250247
s.RequestAssert(&scaffold.RequestAssert{
251248
Method: "GET",
252249
Path: "/headers",
253-
Host: "httpbin-v2.org",
254-
Headers: map[string]string{"Host": "httpbin-v2.org"},
250+
Host: matchedHost,
251+
Headers: map[string]string{"Host": matchedHost},
255252
Checks: []scaffold.ResponseCheckFunc{
256253
scaffold.WithExpectedStatus(200),
257-
scaffold.WithExpectedBodyContains(rewrittenHost),
254+
scaffold.WithExpectedBodyContains(rewriteHost),
258255
},
259256
})
260257

261-
By("the unmatched backend (httpbin.org -> 80) keeps the original host")
258+
By("the other port keeps the original host")
262259
s.RequestAssert(&scaffold.RequestAssert{
263260
Method: "GET",
264261
Path: "/headers",
265-
Host: "httpbin.org",
266-
Headers: map[string]string{"Host": "httpbin.org"},
262+
Host: otherHost,
263+
Headers: map[string]string{"Host": otherHost},
267264
Checks: []scaffold.ResponseCheckFunc{
268265
scaffold.WithExpectedStatus(200),
269-
scaffold.WithExpectedBodyNotContains(rewrittenHost),
266+
scaffold.WithExpectedBodyNotContains(rewriteHost),
270267
},
271268
})
269+
}
270+
271+
It("attaches the policy to the http-v2 (8080) port when sectionName is http-v2", func() {
272+
assertScopedToPort("http-v2", "httpbin.section-v2.example.com", "httpbin-v2.org", "httpbin.org")
273+
})
274+
275+
It("attaches the policy to the http (80) port when sectionName is http", func() {
276+
assertScopedToPort("http", "httpbin.section-http.example.com", "httpbin.org", "httpbin-v2.org")
272277
})
273278
})
274279

0 commit comments

Comments
 (0)