Skip to content

Commit de7ee44

Browse files
Merge pull request #323 from HarshitPal25/test/workload-builder-coverage
test: add coverage for buildSandboxObject and buildSandboxClaimObject
2 parents d6664a2 + 1a1d535 commit de7ee44

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

pkg/workloadmanager/workload_builder_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package workloadmanager
1919
import (
2020
"testing"
2121
"time"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
)
2325

2426
// TestBuildSandboxObject_DoesNotMutateCallerLabels verifies that buildSandboxObject
@@ -102,3 +104,107 @@ func TestBuildSandboxObject_NilLabels(t *testing.T) {
102104
t.Errorf("expected %s=sandbox-xyz, got %q", SandboxNameLabelKey, podLabels[SandboxNameLabelKey])
103105
}
104106
}
107+
108+
// TestBuildSandboxObject_WorkloadNameLabel verifies that the WorkloadNameLabelKey
109+
// label is correctly set on the Sandbox object metadata from the workloadName param.
110+
// This covers the bug where CodeInterpreter sandboxes were missing this label.
111+
func TestBuildSandboxObject_WorkloadNameLabel(t *testing.T) {
112+
tests := []struct {
113+
name string
114+
workloadName string
115+
wantLabel string
116+
}{
117+
{
118+
name: "workloadName is set",
119+
workloadName: "my-code-interpreter",
120+
wantLabel: "my-code-interpreter",
121+
},
122+
{
123+
name: "workloadName is empty",
124+
workloadName: "",
125+
wantLabel: "",
126+
},
127+
}
128+
129+
for _, tt := range tests {
130+
t.Run(tt.name, func(t *testing.T) {
131+
params := &buildSandboxParams{
132+
namespace: "default",
133+
workloadName: tt.workloadName,
134+
sandboxName: "sandbox-wl-test",
135+
sessionID: "session-wl-test",
136+
ttl: time.Hour,
137+
idleTimeout: 15 * time.Minute,
138+
}
139+
sandbox := buildSandboxObject(params)
140+
141+
got := sandbox.ObjectMeta.Labels[WorkloadNameLabelKey]
142+
if got != tt.wantLabel {
143+
t.Errorf("expected %s=%q, got %q", WorkloadNameLabelKey, tt.wantLabel, got)
144+
}
145+
})
146+
}
147+
}
148+
149+
// TestBuildSandboxClaimObject verifies that buildSandboxClaimObject correctly
150+
// populates all fields including labels, annotations, and owner references.
151+
func TestBuildSandboxClaimObject(t *testing.T) {
152+
t.Run("with owner reference", func(t *testing.T) {
153+
ownerRef := &metav1.OwnerReference{
154+
APIVersion: "runtime.agentcube.volcano.sh/v1alpha1",
155+
Kind: "CodeInterpreter",
156+
Name: "my-ci",
157+
UID: "test-uid-123",
158+
}
159+
params := &buildSandboxClaimParams{
160+
namespace: "test-ns",
161+
name: "claim-abc",
162+
sandboxTemplateName: "my-ci",
163+
sessionID: "session-claim-test",
164+
idleTimeout: 10 * time.Minute,
165+
ownerReference: ownerRef,
166+
}
167+
claim := buildSandboxClaimObject(params)
168+
169+
if claim.Namespace != "test-ns" {
170+
t.Errorf("expected namespace test-ns, got %q", claim.Namespace)
171+
}
172+
if claim.Name != "claim-abc" {
173+
t.Errorf("expected name claim-abc, got %q", claim.Name)
174+
}
175+
if claim.Spec.TemplateRef.Name != "my-ci" {
176+
t.Errorf("expected templateRef name my-ci, got %q", claim.Spec.TemplateRef.Name)
177+
}
178+
if claim.Labels[SessionIdLabelKey] != "session-claim-test" {
179+
t.Errorf("expected label %s=session-claim-test, got %q", SessionIdLabelKey, claim.Labels[SessionIdLabelKey])
180+
}
181+
if claim.Annotations[IdleTimeoutAnnotationKey] != "10m0s" {
182+
t.Errorf("expected annotation %s=10m0s, got %q", IdleTimeoutAnnotationKey, claim.Annotations[IdleTimeoutAnnotationKey])
183+
}
184+
if len(claim.OwnerReferences) != 1 {
185+
t.Fatalf("expected 1 owner reference, got %d", len(claim.OwnerReferences))
186+
}
187+
if claim.OwnerReferences[0].Name != "my-ci" {
188+
t.Errorf("expected owner ref name my-ci, got %q", claim.OwnerReferences[0].Name)
189+
}
190+
})
191+
192+
t.Run("without owner reference", func(t *testing.T) {
193+
params := &buildSandboxClaimParams{
194+
namespace: "default",
195+
name: "claim-no-owner",
196+
sandboxTemplateName: "template-1",
197+
sessionID: "session-no-owner",
198+
idleTimeout: 0,
199+
}
200+
claim := buildSandboxClaimObject(params)
201+
202+
if len(claim.OwnerReferences) != 0 {
203+
t.Errorf("expected 0 owner references, got %d", len(claim.OwnerReferences))
204+
}
205+
if claim.Annotations[IdleTimeoutAnnotationKey] != DefaultSandboxIdleTimeout.String() {
206+
t.Errorf("expected default idle timeout annotation %s, got %q",
207+
DefaultSandboxIdleTimeout.String(), claim.Annotations[IdleTimeoutAnnotationKey])
208+
}
209+
})
210+
}

0 commit comments

Comments
 (0)