Skip to content

Commit 5705266

Browse files
committed
test(gateway): add tests for toProtoCircuitBreaker and ApplyPartialSnapshotWithSecrets
- Test nil input, valid config, and zero value for CB proto conversion - Test nil current snapshot, backend replacement, and field preservation - Covers recent optimizer changes in xds and translator packages
1 parent cc5ef8a commit 5705266

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package translator
2+
3+
import (
4+
"testing"
5+
6+
"github.com/nantian-gw/gateway/internal/ir"
7+
)
8+
9+
func TestApplyPartialSnapshotWithSecrets_NilCurrent(t *testing.T) {
10+
backends := []ir.BackendCluster{{Name: "svc1", Namespace: "ns1"}}
11+
listeners := []ir.Listener{{Name: "l1"}}
12+
secrets := []ir.SecretMaterial{{Name: "s1", Namespace: "ns1"}}
13+
14+
result := ApplyPartialSnapshotWithSecrets(nil, backends, listeners, secrets)
15+
if result == nil {
16+
t.Fatal("expected non-nil result")
17+
}
18+
if len(result.Backends) != 1 || result.Backends[0].Name != "svc1" {
19+
t.Error("backends not set correctly")
20+
}
21+
if len(result.Listeners) != 1 || result.Listeners[0].Name != "l1" {
22+
t.Error("listeners not set correctly")
23+
}
24+
if len(result.Secrets) != 1 || result.Secrets[0].Name != "s1" {
25+
t.Error("secrets not set correctly")
26+
}
27+
}
28+
29+
func TestApplyPartialSnapshotWithSecrets_ReplaceBackends(t *testing.T) {
30+
current := &ir.Snapshot{
31+
ID: "v1",
32+
Backends: []ir.BackendCluster{{Name: "old-svc", Namespace: "ns1"}},
33+
Listeners: []ir.Listener{{Name: "l1", Port: 80}},
34+
HTTPRoutes: []ir.HTTPRoute{{Name: "r1", Namespace: "ns1"}},
35+
}
36+
newBackends := []ir.BackendCluster{{Name: "new-svc", Namespace: "ns1"}}
37+
38+
result := ApplyPartialSnapshotWithSecrets(current, newBackends, nil, nil)
39+
if result.ID != "v1" {
40+
t.Errorf("ID = %s, want v1", result.ID)
41+
}
42+
if len(result.Backends) != 1 || result.Backends[0].Name != "new-svc" {
43+
t.Error("backends not replaced")
44+
}
45+
if len(result.Listeners) != 1 || result.Listeners[0].Port != 80 {
46+
t.Error("listeners not preserved")
47+
}
48+
if len(result.HTTPRoutes) != 1 || result.HTTPRoutes[0].Name != "r1" {
49+
t.Error("routes not preserved")
50+
}
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package xds
2+
3+
import (
4+
"testing"
5+
6+
"github.com/nantian-gw/gateway/internal/ir"
7+
controlv1 "github.com/nantian-gw/proto/gateway/control/v1"
8+
)
9+
10+
func TestToProtoCircuitBreaker(t *testing.T) {
11+
// nil input returns nil
12+
if got := toProtoCircuitBreaker(nil); got != nil {
13+
t.Errorf("toProtoCircuitBreaker(nil) = %v, want nil", got)
14+
}
15+
16+
// valid input
17+
input := &ir.CircuitBreakerConfig{MaxInflightRequests: 100}
18+
got := toProtoCircuitBreaker(input)
19+
if got == nil {
20+
t.Fatal("expected non-nil result")
21+
}
22+
if got.MaxInflightRequests != 100 {
23+
t.Errorf("MaxInflightRequests = %d, want 100", got.MaxInflightRequests)
24+
}
25+
if _, ok := any(got).(*controlv1.CircuitBreakerConfig); !ok {
26+
t.Errorf("expected *controlv1.CircuitBreakerConfig, got %T", got)
27+
}
28+
29+
// zero value
30+
input2 := &ir.CircuitBreakerConfig{MaxInflightRequests: 0}
31+
got2 := toProtoCircuitBreaker(input2)
32+
if got2 == nil {
33+
t.Fatal("expected non-nil result for zero value")
34+
}
35+
if got2.MaxInflightRequests != 0 {
36+
t.Errorf("MaxInflightRequests = %d, want 0", got2.MaxInflightRequests)
37+
}
38+
}

0 commit comments

Comments
 (0)