Repository navigation
Expand file tree
/
Copy pathagentdeployment_builder_test.go
More file actions
373 lines (316 loc) · 13.3 KB
/
Copy pathagentdeployment_builder_test.go
File metadata and controls
373 lines (316 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
Copyright 2026.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"testing"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
agentraxv1alpha1 "github.com/gitcommitankit/agentrax/api/v1alpha1"
)
// testAgentName is the canonical agent name used across builder unit tests.
const testAgentName = "my-agent"
// testDefaultImage is the standard placeholder image used in builder unit tests.
const testDefaultImage = "img:v1"
// testDefaultAgent is the default agent name used for tests not specifically
// testing label or name behaviour.
const testDefaultAgent = "agent"
// testLabelName is the well-known label key for the resource/agent name.
const testLabelName = "app.kubernetes.io/name"
// makeAD is a test helper that constructs a minimal AgentDeployment.
func makeAD(name, image string, port int32, minReplicas int32) *agentraxv1alpha1.AgentDeployment {
return &agentraxv1alpha1.AgentDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "tenant-test",
},
Spec: agentraxv1alpha1.AgentDeploymentSpec{
Image: image,
Port: port,
TenantRef: "team-test",
Replicas: agentraxv1alpha1.ScalingPolicy{
Min: minReplicas,
Max: 3,
Metric: "queueDepth",
Target: 50,
},
},
}
}
// ── desiredDeployment ─────────────────────────────────────────────────────────
// TestDesiredDeployment_Image verifies that the desired Deployment container image matches spec.image.
func TestDesiredDeployment_Image(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD("my-agent", "registry.io/agent:v1", 8080, 1)
dep := r.desiredDeployment(ad)
if dep.Spec.Template.Spec.Containers[0].Image != "registry.io/agent:v1" {
t.Errorf("expected image registry.io/agent:v1, got %s", dep.Spec.Template.Spec.Containers[0].Image)
}
}
// TestDesiredDeployment_Port verifies container port configuration and default fallback.
func TestDesiredDeployment_Port(t *testing.T) {
tests := []struct {
name string
specPort int32
wantPort int32
}{
{"explicit port", 9090, 9090},
{"zero defaults to 8080", 0, 8080},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, tc.specPort, 1)
dep := r.desiredDeployment(ad)
got := dep.Spec.Template.Spec.Containers[0].Ports[0].ContainerPort
if got != tc.wantPort {
t.Errorf("expected port %d, got %d", tc.wantPort, got)
}
})
}
}
// TestDesiredDeployment_Replicas verifies that desired Deployment replicas match spec.replicas.min.
func TestDesiredDeployment_Replicas(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, 8080, 3)
dep := r.desiredDeployment(ad)
if *dep.Spec.Replicas != 3 {
t.Errorf("expected 3 replicas, got %d", *dep.Spec.Replicas)
}
}
// TestDesiredDeployment_EnvAndArgs verifies propagation of environment variables and container arguments.
func TestDesiredDeployment_EnvAndArgs(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, 8080, 1)
ad.Spec.Env = []corev1.EnvVar{{Name: "MODEL_PATH", Value: "/models/v1"}}
ad.Spec.Args = []string{"--serve", "--port=8080"}
dep := r.desiredDeployment(ad)
c := dep.Spec.Template.Spec.Containers[0]
if len(c.Env) != 1 || c.Env[0].Name != "MODEL_PATH" {
t.Errorf("expected env MODEL_PATH, got %v", c.Env)
}
if len(c.Args) != 2 || c.Args[0] != "--serve" {
t.Errorf("expected args [--serve --port=8080], got %v", c.Args)
}
}
// TestDesiredDeployment_Resources verifies container CPU and Memory resource requests/limits propagation.
func TestDesiredDeployment_Resources(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, 8080, 1)
want := corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("4Gi"),
},
}
ad.Spec.Resources = want
dep := r.desiredDeployment(ad)
got := dep.Spec.Template.Spec.Containers[0].Resources
for rsrc, wantQ := range want.Requests {
gotQ := got.Requests[rsrc]
if gotQ.Cmp(wantQ) != 0 {
t.Errorf("Resources.Requests[%s]: want %s, got %s", rsrc, wantQ.String(), gotQ.String())
}
}
for rsrc, wantQ := range want.Limits {
gotQ := got.Limits[rsrc]
if gotQ.Cmp(wantQ) != 0 {
t.Errorf("Resources.Limits[%s]: want %s, got %s", rsrc, wantQ.String(), gotQ.String())
}
}
}
// TestDesiredDeployment_Labels verifies required standard labels on Deployment and Pod template.
func TestDesiredDeployment_Labels(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testAgentName, testDefaultImage, 8080, 1)
dep := r.desiredDeployment(ad)
if dep.Labels[testLabelName] != testAgentName {
t.Errorf("expected label %s=%s, got %s", testLabelName, testAgentName, dep.Labels[testLabelName])
}
if dep.Labels["app.kubernetes.io/managed-by"] != "agentrax" {
t.Errorf("expected label managed-by=agentrax, got %s", dep.Labels["app.kubernetes.io/managed-by"])
}
if dep.Labels["agentrax.io/tenant"] != "team-test" {
t.Errorf("expected label agentrax.io/tenant=team-test, got %s", dep.Labels["agentrax.io/tenant"])
}
// Pod template labels must match selector.
if dep.Spec.Template.Labels[testLabelName] != testAgentName {
t.Errorf("pod template labels missing %s", testLabelName)
}
}
// TestDesiredDeployment_SelectorMatchesPodLabels verifies Deployment selector matches Pod template labels.
func TestDesiredDeployment_SelectorMatchesPodLabels(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, 8080, 1)
dep := r.desiredDeployment(ad)
for k, v := range dep.Spec.Selector.MatchLabels {
if dep.Spec.Template.Labels[k] != v {
t.Errorf("selector key %s=%s not present in pod template labels", k, v)
}
}
}
// ── desiredService ────────────────────────────────────────────────────────────
// TestDesiredService_Port verifies Service port configuration and default fallback.
func TestDesiredService_Port(t *testing.T) {
tests := []struct {
name string
specPort int32
wantPort int32
}{
{"explicit port", 9090, 9090},
{"zero defaults to 8080", 0, 8080},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, tc.specPort, 1)
svc := r.desiredService(ad)
if svc.Spec.Ports[0].Port != tc.wantPort {
t.Errorf("expected port %d, got %d", tc.wantPort, svc.Spec.Ports[0].Port)
}
})
}
}
// TestDesiredService_Selector verifies Service selector targets the agent pod label.
func TestDesiredService_Selector(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testAgentName, testDefaultImage, 8080, 1)
svc := r.desiredService(ad)
if svc.Spec.Selector[testLabelName] != testAgentName {
t.Errorf("service selector missing %s=%s", testLabelName, testAgentName)
}
}
// TestDesiredService_ClusterIPType verifies that the created Service is of type ClusterIP.
func TestDesiredService_ClusterIPType(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, 8080, 1)
svc := r.desiredService(ad)
if svc.Spec.Type != corev1.ServiceTypeClusterIP {
t.Errorf("expected ClusterIP service type, got %s", svc.Spec.Type)
}
}
// ── agentLabels ───────────────────────────────────────────────────────────────
// TestAgentLabels verifies standard label generation for an AgentDeployment,
// including the agentrax.io/agent selector key used by NetworkPolicies.
func TestAgentLabels(t *testing.T) {
ad := &agentraxv1alpha1.AgentDeployment{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: agentraxv1alpha1.AgentDeploymentSpec{TenantRef: "bar"},
}
labels := agentLabels(ad)
expected := map[string]string{
"app.kubernetes.io/name": "foo",
"app.kubernetes.io/managed-by": "agentrax",
"agentrax.io/tenant": "bar",
"agentrax.io/variant": "stable",
"agentrax.io/agent": "true",
}
if len(labels) != len(expected) {
t.Errorf("expected %d labels, got %d: %v", len(expected), len(labels), labels)
}
for k, v := range expected {
if labels[k] != v {
t.Errorf("agentLabels[%s] = %q, want %q", k, labels[k], v)
}
}
}
// ── condition helpers ─────────────────────────────────────────────────────────
// TestSetAndGetCondition verifies setting and reading status conditions on AgentDeployment.
func TestSetAndGetCondition(t *testing.T) {
ad := &agentraxv1alpha1.AgentDeployment{}
SetCondition(ad, agentraxv1alpha1.ConditionReady, metav1.ConditionTrue, "TestReason", "all good")
c := GetCondition(ad, agentraxv1alpha1.ConditionReady)
if c == nil {
t.Fatal("expected condition to be set, got nil")
}
if c.Status != metav1.ConditionTrue {
t.Errorf("expected ConditionTrue, got %s", c.Status)
}
if c.Reason != "TestReason" {
t.Errorf("expected reason TestReason, got %s", c.Reason)
}
}
// TestSetCondition_Overwrite verifies that updating an existing condition updates in-place without duplicates.
func TestSetCondition_Overwrite(t *testing.T) {
ad := &agentraxv1alpha1.AgentDeployment{}
SetCondition(ad, agentraxv1alpha1.ConditionReady, metav1.ConditionFalse, "NotReady", "waiting")
SetCondition(ad, agentraxv1alpha1.ConditionReady, metav1.ConditionTrue, "Ready", "ready now")
c := GetCondition(ad, agentraxv1alpha1.ConditionReady)
if c == nil || c.Status != metav1.ConditionTrue {
t.Errorf("expected overwrite to ConditionTrue, got %v", c)
}
// Slice must not grow with duplicate condition types.
if len(ad.Status.Conditions) != 1 {
t.Errorf("expected 1 condition after overwrite, got %d", len(ad.Status.Conditions))
}
}
// TestRemoveCondition verifies condition removal from the status condition slice.
func TestRemoveCondition(t *testing.T) {
ad := &agentraxv1alpha1.AgentDeployment{}
SetCondition(ad, agentraxv1alpha1.ConditionReady, metav1.ConditionTrue, "R", "m")
SetCondition(ad, agentraxv1alpha1.ConditionReconciled, metav1.ConditionTrue, "R", "m")
RemoveCondition(ad, agentraxv1alpha1.ConditionReady)
if GetCondition(ad, agentraxv1alpha1.ConditionReady) != nil {
t.Error("expected Ready condition to be removed")
}
if GetCondition(ad, agentraxv1alpha1.ConditionReconciled) == nil {
t.Error("expected Reconciled condition to remain after removing Ready")
}
}
// TestRemoveCondition_NonExistent verifies that removing a non-existent condition is a safe no-op.
func TestRemoveCondition_NonExistent(t *testing.T) {
ad := &agentraxv1alpha1.AgentDeployment{}
// Should be a no-op, not panic.
RemoveCondition(ad, agentraxv1alpha1.ConditionReady)
if len(ad.Status.Conditions) != 0 {
t.Errorf("expected 0 conditions after removing non-existent, got %d", len(ad.Status.Conditions))
}
}
// TestGetCondition_Absent verifies that querying an un-set condition returns nil.
func TestGetCondition_Absent(t *testing.T) {
ad := &agentraxv1alpha1.AgentDeployment{}
c := GetCondition(ad, agentraxv1alpha1.ConditionReady)
if c != nil {
t.Errorf("expected nil for absent condition, got %v", c)
}
}
// ── desiredServiceMonitor ─────────────────────────────────────────────────────
// TestDesiredServiceMonitor_Endpoint verifies ServiceMonitor metrics endpoint configuration.
func TestDesiredServiceMonitor_Endpoint(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testDefaultAgent, testDefaultImage, 8080, 1)
sm := r.desiredServiceMonitor(ad)
if len(sm.Spec.Endpoints) != 1 {
t.Fatalf("expected 1 endpoint, got %d", len(sm.Spec.Endpoints))
}
ep := sm.Spec.Endpoints[0]
if ep.Path != "/metrics" {
t.Errorf("expected path /metrics, got %s", ep.Path)
}
if ep.Port != "agent" {
t.Errorf("expected port name 'agent', got %s", ep.Port)
}
}
// TestDesiredServiceMonitor_SelectorMatchesLabels verifies ServiceMonitor selector matches agent labels.
func TestDesiredServiceMonitor_SelectorMatchesLabels(t *testing.T) {
r := &AgentDeploymentReconciler{}
ad := makeAD(testAgentName, testDefaultImage, 8080, 1)
sm := r.desiredServiceMonitor(ad)
if sm.Spec.Selector.MatchLabels[testLabelName] != testAgentName {
t.Errorf("ServiceMonitor selector missing %s=%s", testLabelName, testAgentName)
}
}