feat: 持久化清理 VM 执行器
This commit is contained in:
@@ -33,6 +33,23 @@ func (f *fakeLifecycle) GetSandbox(_ context.Context, id string) (*opensandbox.S
|
||||
}
|
||||
return &opensandbox.SandboxInfo{ID: id, Metadata: map[string]string{"ci.ddupan.top/spiffe-id": assignment().Identity.SPIFFEID}}, nil
|
||||
}
|
||||
func (f *fakeLifecycle) PatchSandboxMetadata(_ context.Context, id string, patch opensandbox.MetadataPatch) (*opensandbox.SandboxInfo, error) {
|
||||
for index := range f.items {
|
||||
if f.items[index].ID != id {
|
||||
continue
|
||||
}
|
||||
if f.items[index].Metadata == nil {
|
||||
f.items[index].Metadata = map[string]string{}
|
||||
}
|
||||
for key, value := range patch {
|
||||
if value != nil {
|
||||
f.items[index].Metadata[key] = *value
|
||||
}
|
||||
}
|
||||
return &f.items[index], nil
|
||||
}
|
||||
return &opensandbox.SandboxInfo{ID: id}, nil
|
||||
}
|
||||
func (f *fakeLifecycle) DeleteSandbox(_ context.Context, id string) error { f.deleted = id; return nil }
|
||||
|
||||
func backend(lifecycle Lifecycle) Backend {
|
||||
@@ -86,3 +103,21 @@ func TestBindIdentityVerifiesPersistedMetadata(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTerminalMarkerDrivesDurableCleanup(t *testing.T) {
|
||||
lifecycle := &fakeLifecycle{items: []opensandbox.SandboxInfo{{
|
||||
ID: "sandbox-42",
|
||||
Metadata: map[string]string{assignmentMetadata: assignment().ID},
|
||||
}}}
|
||||
backend := backend(lifecycle)
|
||||
if err := backend.MarkTerminal(context.Background(), assignment().ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if lifecycle.items[0].Metadata[terminalMetadata] != "true" {
|
||||
t.Fatalf("metadata = %#v", lifecycle.items[0].Metadata)
|
||||
}
|
||||
cleaned, err := backend.CleanupTerminated(context.Background())
|
||||
if err != nil || cleaned != 1 || lifecycle.deleted != "sandbox-42" {
|
||||
t.Fatalf("cleaned=%d deleted=%q err=%v", cleaned, lifecycle.deleted, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user