Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/workloadmanager/sandbox_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ import (
)

func buildSandboxPlaceHolder(sandboxCR *sandboxv1alpha1.Sandbox, entry *sandboxEntry) *types.SandboxInfo {
var expiresAt time.Time
if sandboxCR.Spec.Lifecycle.ShutdownTime != nil {
expiresAt = sandboxCR.Spec.Lifecycle.ShutdownTime.Time
} else {
expiresAt = time.Now().Add(DefaultSandboxTTL)
}
return &types.SandboxInfo{
Kind: entry.Kind,
SessionID: entry.SessionID,
SandboxNamespace: sandboxCR.GetNamespace(),
Name: sandboxCR.GetName(),
ExpiresAt: time.Now().Add(DefaultSandboxTTL),
ExpiresAt: expiresAt,
Status: "creating",
}
}
Expand Down
129 changes: 126 additions & 3 deletions pkg/workloadmanager/sandbox_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,132 @@ import (

const sandboxHelperTestPodIP = "10.0.0.1"

// Note: TestBuildSandboxPlaceHolder and TestBuildSandboxPlaceHolder_CodeInterpreter
// removed - they only verified that struct fields match input parameters, which is
// trivial field copying behavior.
func TestBuildSandboxPlaceHolder_TableDriven(t *testing.T) {
now := time.Now()

tests := []struct {
name string
setupSandbox func() *sandboxv1alpha1.Sandbox
entry *sandboxEntry
validate func(t *testing.T, result *types.SandboxInfo)
}{
{
name: "no ShutdownTime falls back to DefaultSandboxTTL",
setupSandbox: func() *sandboxv1alpha1.Sandbox {
return &sandboxv1alpha1.Sandbox{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sandbox",
Namespace: "default",
},
}
},
entry: &sandboxEntry{
Kind: types.SandboxKind,
SessionID: "session-123",
},
validate: func(t *testing.T, result *types.SandboxInfo) {
expected := now.Add(DefaultSandboxTTL)
assert.WithinDuration(t, expected, result.ExpiresAt, 2*time.Second)
assert.Equal(t, "creating", result.Status)
assert.Equal(t, "session-123", result.SessionID)
},
},
{
name: "ShutdownTime set to 24h is used as ExpiresAt",
setupSandbox: func() *sandboxv1alpha1.Sandbox {
shutdownTime := now.Add(24 * time.Hour)
return &sandboxv1alpha1.Sandbox{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sandbox",
Namespace: "default",
},
Spec: sandboxv1alpha1.SandboxSpec{
Lifecycle: sandboxv1alpha1.Lifecycle{
ShutdownTime: &metav1.Time{Time: shutdownTime},
},
},
}
},
entry: &sandboxEntry{
Kind: types.SandboxKind,
SessionID: "session-456",
},
validate: func(t *testing.T, result *types.SandboxInfo) {
expected := now.Add(24 * time.Hour)
assert.Equal(t, expected, result.ExpiresAt)
},
},
{
name: "ShutdownTime set to 30m overrides DefaultSandboxTTL",
setupSandbox: func() *sandboxv1alpha1.Sandbox {
shutdownTime := now.Add(30 * time.Minute)
return &sandboxv1alpha1.Sandbox{
ObjectMeta: metav1.ObjectMeta{
Name: "short-sandbox",
Namespace: "default",
},
Spec: sandboxv1alpha1.SandboxSpec{
Lifecycle: sandboxv1alpha1.Lifecycle{
ShutdownTime: &metav1.Time{Time: shutdownTime},
},
},
}
},
entry: &sandboxEntry{
Kind: types.SandboxClaimsKind,
SessionID: "session-789",
},
validate: func(t *testing.T, result *types.SandboxInfo) {
expected := now.Add(30 * time.Minute)
assert.Equal(t, expected, result.ExpiresAt)
// Must NOT be 8h (DefaultSandboxTTL)
assert.True(t, result.ExpiresAt.Before(now.Add(DefaultSandboxTTL)),
"ExpiresAt should be 30m, not the 8h default")
},
},
{
name: "warm-pool path: ShutdownTime set on simpleSandbox reflects MaxSessionDuration",
setupSandbox: func() *sandboxv1alpha1.Sandbox {
// Simulates the simpleSandbox built by the warm-pool CodeInterpreter path
// after the fix in workload_builder.go sets ShutdownTime from MaxSessionDuration.
shutdownTime := now.Add(24 * time.Hour)
return &sandboxv1alpha1.Sandbox{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "ci-warmpool-abc",
Labels: map[string]string{
SessionIdLabelKey: "session-wp-001",
},
},
Spec: sandboxv1alpha1.SandboxSpec{
Lifecycle: sandboxv1alpha1.Lifecycle{
ShutdownTime: &metav1.Time{Time: shutdownTime},
},
},
}
},
entry: &sandboxEntry{
Kind: types.SandboxClaimsKind,
SessionID: "session-wp-001",
},
validate: func(t *testing.T, result *types.SandboxInfo) {
expected := now.Add(24 * time.Hour)
assert.Equal(t, expected, result.ExpiresAt,
"warm-pool placeholder ExpiresAt must reflect MaxSessionDuration, not the 8h default")
assert.Equal(t, "creating", result.Status)
assert.Equal(t, types.SandboxClaimsKind, result.Kind)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sandbox := tt.setupSandbox()
result := buildSandboxPlaceHolder(sandbox, tt.entry)
tt.validate(t, result)
})
}
}
Comment on lines +33 to +158
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new table-driven tests validate buildSandboxPlaceHolder behavior when ShutdownTime is present/absent on the Sandbox object, but they don't cover the warm-pool CodeInterpreter flow where the placeholder Sandbox is constructed without Spec.Lifecycle.ShutdownTime. Adding a regression test for that warm-pool path (ensuring ShutdownTime/expiry reflects MaxSessionDuration) would help prevent this bug from recurring.

Copilot generated this review using guidance from repository custom instructions.

func TestBuildSandboxInfo_TableDriven(t *testing.T) {
now := time.Now()
Expand Down
4 changes: 4 additions & 0 deletions pkg/workloadmanager/workload_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ func buildSandboxByCodeInterpreter(namespace string, codeInterpreterName string,
},
},
}
if codeInterpreterObj.Spec.MaxSessionDuration != nil {
shutdownTime := metav1.NewTime(time.Now().Add(codeInterpreterObj.Spec.MaxSessionDuration.Duration))
simpleSandbox.Spec.Lifecycle.ShutdownTime = &shutdownTime
}
sandboxEntry.Kind = types.SandboxClaimsKind
return simpleSandbox, sandboxClaim, sandboxEntry, nil
}
Expand Down
Loading