Skip to content

Commit b8478c1

Browse files
committed
test(gateway): expand gwexp test coverage — aiservice, tokenpolicy, wasmplugin
- aiservice: 13.6% -> 79.5% (Auth, Observability, Retry, List) - tokenpolicy: 17.0% -> 83.0% (FullConfig, NoTargetRefs) - wasmplugin: 15.5% -> 84.5% (FullConfig, InlineWasm, ConfigMap) - DeepCopy roundtrip + nil safety for all three CRD types
1 parent 833d55b commit b8478c1

3 files changed

Lines changed: 419 additions & 0 deletions

File tree

internal/gwexp/aiservice/types_test.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
)
99

10+
func TestAIServiceDeepCopy_Nil(t *testing.T) {
11+
var svc *AIService
12+
assert.Nil(t, svc.DeepCopy())
13+
assert.Nil(t, svc.DeepCopyObject())
14+
}
15+
1016
func TestDeepCopyRoundtrip(t *testing.T) {
1117
original := &AIService{
1218
ObjectMeta: metav1.ObjectMeta{
@@ -32,4 +38,142 @@ func TestDeepCopyRoundtrip(t *testing.T) {
3238
copied := original.DeepCopy()
3339
assert.Equal(t, original, copied)
3440
assert.NotSame(t, original, copied)
41+
}
42+
43+
func TestAIServiceDeepCopy_WithAuth(t *testing.T) {
44+
svc := &AIService{
45+
ObjectMeta: metav1.ObjectMeta{Name: "openai-gpt4", Namespace: "default"},
46+
Spec: AIServiceSpec{
47+
Provider: "openai",
48+
Format: "openai",
49+
Model: "gpt-4o",
50+
Auth: AIServiceAuth{
51+
Type: "apiKey",
52+
Secret: "openai-secret",
53+
Key: "Authorization",
54+
Header: "api.openai.com",
55+
},
56+
Timeout: "30s",
57+
},
58+
}
59+
copied := svc.DeepCopy()
60+
assert.Equal(t, svc.Spec.Auth.Type, copied.Spec.Auth.Type)
61+
assert.Equal(t, svc.Spec.Provider, copied.Spec.Provider)
62+
assert.NotSame(t, svc, copied)
63+
}
64+
65+
func TestAIServiceDeepCopy_WithObservability(t *testing.T) {
66+
svc := &AIService{
67+
ObjectMeta: metav1.ObjectMeta{Name: "with-obs", Namespace: "default"},
68+
Spec: AIServiceSpec{
69+
Provider: "anthropic",
70+
Model: "claude-3",
71+
Observability: AIObservabilityConfig{
72+
Langfuse: LangfuseConfig{
73+
Host: "https://cloud.langfuse.com",
74+
PublicKey: "pk-xxx",
75+
SecretKey: "sk-xxx",
76+
},
77+
OTel: OTelConfig{
78+
Endpoint: "http://otel-collector:4317",
79+
ServiceName: "ai-gateway",
80+
},
81+
},
82+
},
83+
}
84+
copied := svc.DeepCopy()
85+
assert.Equal(t, svc.Spec.Observability.Langfuse.Host, copied.Spec.Observability.Langfuse.Host)
86+
assert.Equal(t, svc.Spec.Observability.OTel.Endpoint, copied.Spec.Observability.OTel.Endpoint)
87+
}
88+
89+
func TestAIServiceDeepCopy_WithRetry(t *testing.T) {
90+
svc := &AIService{
91+
ObjectMeta: metav1.ObjectMeta{Name: "with-retry", Namespace: "default"},
92+
Spec: AIServiceSpec{
93+
Provider: "openai",
94+
Model: "gpt-4o",
95+
Retry: AIRetryConfig{
96+
MaxRetries: 3,
97+
Backoff: "exponential",
98+
},
99+
},
100+
}
101+
copied := svc.DeepCopy()
102+
assert.Equal(t, uint32(3), copied.Spec.Retry.MaxRetries)
103+
}
104+
105+
func TestAIServiceListDeepCopy(t *testing.T) {
106+
list := &AIServiceList{
107+
Items: []AIService{
108+
{ObjectMeta: metav1.ObjectMeta{Name: "svc1", Namespace: "ns1"}, Spec: AIServiceSpec{Provider: "openai", Model: "gpt-4o"}},
109+
{ObjectMeta: metav1.ObjectMeta{Name: "svc2", Namespace: "ns2"}, Spec: AIServiceSpec{Provider: "anthropic", Model: "claude-3"}},
110+
},
111+
}
112+
copied := list.DeepCopy()
113+
assert.Equal(t, 2, len(copied.Items))
114+
assert.Equal(t, list.Items[0].Name, copied.Items[0].Name)
115+
}
116+
117+
func TestAIServiceListDeepCopy_Nil(t *testing.T) {
118+
var list *AIServiceList
119+
assert.Nil(t, list.DeepCopy())
120+
assert.Nil(t, list.DeepCopyObject())
121+
}
122+
123+
func TestAIServiceDeepCopyInto(t *testing.T) {
124+
src := &AIService{
125+
ObjectMeta: metav1.ObjectMeta{Name: "src", Namespace: "ns"},
126+
Spec: AIServiceSpec{
127+
Provider: "openai",
128+
Model: "gpt-4o",
129+
Auth: AIServiceAuth{Type: "apiKey", Secret: "s", Key: "k", Header: "h"},
130+
Timeout: "60s",
131+
Retry: AIRetryConfig{MaxRetries: 5, Backoff: "linear"},
132+
},
133+
}
134+
dst := &AIService{}
135+
src.DeepCopyInto(dst)
136+
assert.Equal(t, "src", dst.Name)
137+
assert.Equal(t, "openai", dst.Spec.Provider)
138+
assert.Equal(t, uint32(5), dst.Spec.Retry.MaxRetries)
139+
}
140+
141+
func TestAIServiceSpecDeepCopy(t *testing.T) {
142+
spec := &AIServiceSpec{
143+
Provider: "anthropic",
144+
Format: "anthropic",
145+
Model: "claude-3",
146+
Auth: AIServiceAuth{Type: "apiKey", Secret: "sec"},
147+
Timeout: "30s",
148+
Retry: AIRetryConfig{MaxRetries: 3, Backoff: "exponential"},
149+
Observability: AIObservabilityConfig{
150+
Langfuse: LangfuseConfig{Host: "host", PublicKey: "pk", SecretKey: "sk"},
151+
OTel: OTelConfig{Endpoint: "ep", ServiceName: "sn"},
152+
},
153+
}
154+
copied := spec.DeepCopy()
155+
assert.Equal(t, spec.Provider, copied.Provider)
156+
assert.Equal(t, spec.Observability.Langfuse.Host, copied.Observability.Langfuse.Host)
157+
}
158+
159+
func TestAIServiceSpecDeepCopy_Nil(t *testing.T) {
160+
var spec *AIServiceSpec
161+
assert.Nil(t, spec.DeepCopy())
162+
}
163+
164+
func TestAIServiceStatusDeepCopy(t *testing.T) {
165+
status := &AIServiceStatus{
166+
Conditions: []metav1.Condition{
167+
{Type: "Accepted", Status: "True", Reason: "Valid", Message: "ok"},
168+
},
169+
}
170+
copied := status.DeepCopy()
171+
assert.Equal(t, 1, len(copied.Conditions))
172+
assert.Equal(t, "Accepted", copied.Conditions[0].Type)
173+
assert.NotSame(t, &status.Conditions, &copied.Conditions)
174+
}
175+
176+
func TestAIServiceStatusDeepCopy_Nil(t *testing.T) {
177+
var status *AIServiceStatus
178+
assert.Nil(t, status.DeepCopy())
35179
}

internal/gwexp/tokenpolicy/types_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,123 @@ func TestDeepCopyRoundtrip(t *testing.T) {
2727
copied := original.DeepCopy()
2828
assert.Equal(t, original, copied)
2929
assert.NotSame(t, original, copied)
30+
}
31+
32+
func TestTokenPolicyDeepCopy_FullConfig(t *testing.T) {
33+
policy := &TokenPolicy{
34+
ObjectMeta: metav1.ObjectMeta{Name: "gpt4-limit", Namespace: "default"},
35+
Spec: TokenPolicySpec{
36+
TargetRefs: []gatewayv1.LocalPolicyTargetReference{
37+
{Group: "gateway.nantian.dev", Kind: "AIService", Name: "gpt4-svc"},
38+
},
39+
TokensPerMinute: 1000,
40+
TokensPerHour: 50000,
41+
RequestsPerMinute: 100,
42+
Scope: "model",
43+
Burst: 2.0,
44+
OnLimit: "block",
45+
},
46+
}
47+
copied := policy.DeepCopy()
48+
assert.Equal(t, uint64(1000), copied.Spec.TokensPerMinute)
49+
assert.Equal(t, "model", copied.Spec.Scope)
50+
assert.Equal(t, 1, len(copied.Spec.TargetRefs))
51+
assert.Equal(t, "gpt4-svc", string(copied.Spec.TargetRefs[0].Name))
52+
assert.NotSame(t, &policy.Spec.TargetRefs, &copied.Spec.TargetRefs)
53+
}
54+
55+
func TestTokenPolicyDeepCopy_NoTargetRefs(t *testing.T) {
56+
policy := &TokenPolicy{
57+
ObjectMeta: metav1.ObjectMeta{Name: "global-limit", Namespace: "default"},
58+
Spec: TokenPolicySpec{
59+
TokensPerMinute: 500,
60+
},
61+
}
62+
copied := policy.DeepCopy()
63+
assert.Nil(t, copied.Spec.TargetRefs)
64+
assert.Equal(t, uint64(500), copied.Spec.TokensPerMinute)
65+
}
66+
67+
func TestTokenPolicyDeepCopy_Nil(t *testing.T) {
68+
var p *TokenPolicy
69+
assert.Nil(t, p.DeepCopy())
70+
assert.Nil(t, p.DeepCopyObject())
71+
}
72+
73+
func TestTokenPolicyDeepCopyInto(t *testing.T) {
74+
src := &TokenPolicy{
75+
ObjectMeta: metav1.ObjectMeta{Name: "src", Namespace: "ns"},
76+
Spec: TokenPolicySpec{
77+
TargetRefs: []gatewayv1.LocalPolicyTargetReference{
78+
{Group: "gateway.nantian.dev", Kind: "AIService", Name: "gpt4"},
79+
},
80+
TokensPerMinute: 2000,
81+
Scope: "global",
82+
OnLimit: "block",
83+
},
84+
}
85+
dst := &TokenPolicy{}
86+
src.DeepCopyInto(dst)
87+
assert.Equal(t, "src", dst.Name)
88+
assert.Equal(t, uint64(2000), dst.Spec.TokensPerMinute)
89+
assert.NotSame(t, &src.Spec.TargetRefs, &dst.Spec.TargetRefs)
90+
}
91+
92+
func TestTokenPolicyListDeepCopy(t *testing.T) {
93+
list := &TokenPolicyList{
94+
Items: []TokenPolicy{
95+
{ObjectMeta: metav1.ObjectMeta{Name: "tp1", Namespace: "ns1"}, Spec: TokenPolicySpec{TokensPerMinute: 100}},
96+
{ObjectMeta: metav1.ObjectMeta{Name: "tp2", Namespace: "ns2"}, Spec: TokenPolicySpec{TokensPerMinute: 200, Scope: "model"}},
97+
},
98+
}
99+
copied := list.DeepCopy()
100+
assert.Equal(t, 2, len(copied.Items))
101+
assert.Equal(t, "tp1", copied.Items[0].Name)
102+
assert.Equal(t, uint64(200), copied.Items[1].Spec.TokensPerMinute)
103+
}
104+
105+
func TestTokenPolicyListDeepCopy_Nil(t *testing.T) {
106+
var list *TokenPolicyList
107+
assert.Nil(t, list.DeepCopy())
108+
assert.Nil(t, list.DeepCopyObject())
109+
}
110+
111+
func TestTokenPolicySpecDeepCopy(t *testing.T) {
112+
spec := &TokenPolicySpec{
113+
TargetRefs: []gatewayv1.LocalPolicyTargetReference{
114+
{Group: "gateway.nantian.dev", Kind: "AIService", Name: "svc1"},
115+
},
116+
TokensPerMinute: 500,
117+
TokensPerHour: 10000,
118+
RequestsPerMinute: 50,
119+
Scope: "user",
120+
Burst: 1.5,
121+
OnLimit: "block",
122+
}
123+
copied := spec.DeepCopy()
124+
assert.Equal(t, spec.TokensPerMinute, copied.TokensPerMinute)
125+
assert.Equal(t, spec.TokensPerHour, copied.TokensPerHour)
126+
assert.NotSame(t, &spec.TargetRefs, &copied.TargetRefs)
127+
}
128+
129+
func TestTokenPolicySpecDeepCopy_Nil(t *testing.T) {
130+
var spec *TokenPolicySpec
131+
assert.Nil(t, spec.DeepCopy())
132+
}
133+
134+
func TestTokenPolicyStatusDeepCopy(t *testing.T) {
135+
status := &TokenPolicyStatus{
136+
Conditions: []metav1.Condition{
137+
{Type: "Accepted", Status: "True", Reason: "Valid", Message: "ok"},
138+
},
139+
}
140+
copied := status.DeepCopy()
141+
assert.Equal(t, 1, len(copied.Conditions))
142+
assert.Equal(t, "Accepted", copied.Conditions[0].Type)
143+
assert.NotSame(t, &status.Conditions, &copied.Conditions)
144+
}
145+
146+
func TestTokenPolicyStatusDeepCopy_Nil(t *testing.T) {
147+
var status *TokenPolicyStatus
148+
assert.Nil(t, status.DeepCopy())
30149
}

0 commit comments

Comments
 (0)