Skip to content

Commit 2114de8

Browse files
committed
Refactor server test
1 parent f49dc00 commit 2114de8

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

internal/controller/nginx/config/servers_test.go

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,21 +2469,36 @@ func TestCreateLocations_InferenceBackends(t *testing.T) {
24692469
multiInferencePrimaryBackend := createInferenceBackend("test_primary_pool_80", 70, "primary-pool")
24702470
multiInferenceSecondaryBackend := createInferenceBackend("test_secondary_pool_80", 30, "secondary-pool")
24712471

2472-
createBackendGroup := func(backends []dataplane.Backend, ruleIdx int, pathRuleIdx int) dataplane.BackendGroup {
2472+
createBackendGroup := func(backends []dataplane.Backend) dataplane.BackendGroup {
24732473
return dataplane.BackendGroup{
2474-
Source: hrNsName,
2475-
RuleIdx: ruleIdx,
2476-
PathRuleIdx: pathRuleIdx,
2477-
Backends: backends,
2474+
Source: hrNsName,
2475+
Backends: backends,
24782476
}
24792477
}
24802478

2481-
singleInferenceGroup := createBackendGroup([]dataplane.Backend{singleInferenceBackend}, 0, 0)
2482-
multipleInferenceGroup := createBackendGroup(
2483-
[]dataplane.Backend{multiInferencePrimaryBackend, multiInferenceSecondaryBackend},
2484-
0,
2485-
0,
2486-
)
2479+
singleInferenceGroup := createBackendGroup([]dataplane.Backend{singleInferenceBackend})
2480+
multipleInferenceGroup := createBackendGroup([]dataplane.Backend{
2481+
multiInferencePrimaryBackend,
2482+
multiInferenceSecondaryBackend,
2483+
})
2484+
2485+
// setBackendGroupIndices returns a new PathRule with updated RuleIdx and PathRuleIdx in all BackendGroups
2486+
setBackendGroupIndices := func(pathRule dataplane.PathRule, ruleIdx int, pathRuleIdx int) dataplane.PathRule {
2487+
// Create a copy of the PathRule
2488+
newPathRule := pathRule
2489+
2490+
// Create a copy of the MatchRules slice
2491+
newPathRule.MatchRules = make([]dataplane.MatchRule, len(pathRule.MatchRules))
2492+
copy(newPathRule.MatchRules, pathRule.MatchRules)
2493+
2494+
// Update the BackendGroup indices in the copied MatchRules
2495+
for matchRuleIdx := range newPathRule.MatchRules {
2496+
newPathRule.MatchRules[matchRuleIdx].BackendGroup.RuleIdx = ruleIdx
2497+
newPathRule.MatchRules[matchRuleIdx].BackendGroup.PathRuleIdx = pathRuleIdx
2498+
}
2499+
2500+
return newPathRule
2501+
}
24872502

24882503
// Helper function to create path rules
24892504
createPathRule := func(path string, matchRules []dataplane.MatchRule) dataplane.PathRule {
@@ -2553,45 +2568,6 @@ func TestCreateLocations_InferenceBackends(t *testing.T) {
25532568
},
25542569
})
25552570

2556-
combinedPathRules := []dataplane.PathRule{
2557-
createPathRule("/inference", []dataplane.MatchRule{
2558-
{
2559-
Match: dataplane.Match{},
2560-
BackendGroup: createBackendGroup([]dataplane.Backend{singleInferenceBackend}, 0, 0),
2561-
},
2562-
}),
2563-
createPathRule("/inference-match", []dataplane.MatchRule{
2564-
{
2565-
Match: dataplane.Match{
2566-
Method: helpers.GetPointer("POST"),
2567-
},
2568-
BackendGroup: createBackendGroup([]dataplane.Backend{singleInferenceBackend}, 1, 1),
2569-
},
2570-
}),
2571-
createPathRule("/weighted-inference", []dataplane.MatchRule{
2572-
{
2573-
Match: dataplane.Match{},
2574-
BackendGroup: createBackendGroup(
2575-
[]dataplane.Backend{multiInferencePrimaryBackend, multiInferenceSecondaryBackend},
2576-
2,
2577-
2,
2578-
),
2579-
},
2580-
}),
2581-
createPathRule("/weighted-inference-match", []dataplane.MatchRule{
2582-
{
2583-
Match: dataplane.Match{
2584-
Method: helpers.GetPointer("GET"),
2585-
},
2586-
BackendGroup: createBackendGroup(
2587-
[]dataplane.Backend{multiInferencePrimaryBackend, multiInferenceSecondaryBackend},
2588-
3,
2589-
3,
2590-
),
2591-
},
2592-
}),
2593-
}
2594-
25952571
proxySetHeaders := []http.Header{
25962572
{Name: "Host", Value: "$gw_api_compliant_host"},
25972573
{Name: "X-Forwarded-For", Value: "$proxy_add_x_forwarded_for"},
@@ -2826,8 +2802,13 @@ func TestCreateLocations_InferenceBackends(t *testing.T) {
28262802
},
28272803
},
28282804
{
2829-
name: "mixed multiple path rules with different inference configurations",
2830-
pathRules: combinedPathRules,
2805+
name: "mixed multiple path rules with different inference configurations",
2806+
pathRules: []dataplane.PathRule{
2807+
setBackendGroupIndices(pathRuleInferenceOnly, 0, 0),
2808+
setBackendGroupIndices(pathRuleInferenceWithMatch, 1, 1),
2809+
setBackendGroupIndices(pathRuleMultipleInferenceBackends, 2, 2),
2810+
setBackendGroupIndices(pathRuleMultipleInferenceWithMatch, 3, 3),
2811+
},
28312812
expLocs: []http.Location{
28322813
// 1. Single inference pool locations (rule index 0)
28332814
{

0 commit comments

Comments
 (0)