Files
substrate/cmd/ateapi/internal/controlapi/workflow_delete_test.go
T
Troy Chiu 07da819da1 controlapi: refactor workflows to typed ensure-steps (#797)
Fixes #554 

Each workflow becomes a plain method orchestrating typed, idempotent
ensureX steps, e.g.:

```go
func (w *ActorWorkflow) ResumeActor(ctx, ref, boot) (*Actor, bool, error) {
    // acquire actor lock, metrics defer — unchanged
    actor, tmpl, snapSrc, err := w.loadActorForResume(ctx, ref, boot)
    if actor.Status == RUNNING { return actor, false, nil }        // explicit no-op
    actor, err         = w.ensureVolumesCreated(ctx, actor, tmpl)
    actor, worker, err = w.ensureWorkerAssigned(ctx, actor, tmpl)
    err                = w.ensureVolumesAttached(ctx, actor, worker, tmpl)
    tele, err          = w.ensureAteletRestored(ctx, actor, worker, tmpl, snapSrc)
    actor, err         = w.finalizeRunning(ctx, ref)
    return actor, true, nil
}
```
Each `ensureX` 
- early-returns if its postcondition already holds, which is derived
from persisted state only, replacing `IsComplete`
- validates the state machine edge internally, replacing
`CheckPrerequisite`
- takes explicit typed inputs and returns what it produced.
- persists what it changed before returning, returning the stored copy
so the optimistic lock version threads forward. No shared
`*ResumeState`.

There is one behavioral change: worker ownership/eligibility validation
for a re-entered resume now runs before volumes are attached (it used to
run in `CallAteletRestoreStep`'s prerequisite, after attach), so a
resume that is about to crash on a reassigned/ineligible worker no
longer attaches volumes to it first.

- [X] Tests pass
- [ ] Appropriate changes to documentation are included in the PR
2026-08-10 14:19:43 -04:00

121 lines
3.6 KiB
Go

// Copyright 2026 Google LLC
//
// 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 controlapi
import (
"context"
"testing"
"github.com/agent-substrate/substrate/cmd/ateapi/internal/store/storetest"
"github.com/agent-substrate/substrate/internal/resources"
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestDeleteActorWorkflow_ExecutionPaths(t *testing.T) {
tests := []struct {
name string
seedStatus ateapipb.Actor_Status
wantErr bool
wantCode codes.Code
}{
{
name: "delete suspended actor succeeds",
seedStatus: ateapipb.Actor_STATUS_SUSPENDED,
wantErr: false,
},
{
name: "delete crashed actor succeeds",
seedStatus: ateapipb.Actor_STATUS_CRASHED,
wantErr: false,
},
{
name: "delete deleting actor succeeds",
seedStatus: ateapipb.Actor_STATUS_DELETING,
wantErr: false,
},
{
name: "delete running actor rejected",
seedStatus: ateapipb.Actor_STATUS_RUNNING,
wantErr: true,
wantCode: codes.FailedPrecondition,
},
{
name: "delete paused actor rejected",
seedStatus: ateapipb.Actor_STATUS_PAUSED,
wantErr: true,
wantCode: codes.FailedPrecondition,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
st, cleanup := storetest.SetupTestStore(t)
defer cleanup()
w := newTestActorWorkflow(t, st, "ns", "tmpl1")
actorRef := resources.ActorRef{Atespace: "team-a", Name: "id1"}
seedWorkflowActor(t, ctx, st, actorRef, "ns", "tmpl1", tc.seedStatus)
deleted, err := w.DeleteActor(ctx, actorRef)
if tc.wantErr {
if got := status.Code(err); got != tc.wantCode {
t.Fatalf("status.Code(err) = %v, want %v (err: %v)", got, tc.wantCode, err)
}
} else {
if err != nil {
t.Fatalf("DeleteActor failed: %v", err)
}
if deleted == nil {
t.Fatalf("expected non-nil deleted actor")
}
if _, err := st.GetActor(ctx, actorRef); err == nil {
t.Errorf("expected actor to be deleted from store, but it still exists")
}
}
})
}
}
func TestEnsureMarkedDeleting_StatusMatrix(t *testing.T) {
allowed := map[ateapipb.Actor_Status]bool{
ateapipb.Actor_STATUS_SUSPENDED: true,
ateapipb.Actor_STATUS_CRASHED: true,
ateapipb.Actor_STATUS_DELETING: true, // skipped, not re-marked
}
for _, seedStatus := range allActorStatuses {
ctx := context.Background()
st, cleanup := storetest.SetupTestStore(t)
w := newTestActorWorkflow(t, st, "ns", "tmpl1")
actorRef := resources.ActorRef{Atespace: "team-a", Name: "id1"}
seedWorkflowActor(t, ctx, st, actorRef, "ns", "tmpl1", seedStatus)
actor, err := st.GetActor(ctx, actorRef)
if err != nil {
t.Fatalf("status %v: get seeded actor: %v", seedStatus, err)
}
updated, err := w.ensureMarkedDeleting(ctx, actorRef, actor)
assertPrerequisiteResult(t, seedStatus, err, allowed[seedStatus])
if err == nil && updated.GetStatus() != ateapipb.Actor_STATUS_DELETING {
t.Errorf("status %v: ensureMarkedDeleting returned actor in %v, want DELETING", seedStatus, updated.GetStatus())
}
cleanup()
}
}