|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controlapi |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/agent-substrate/substrate/cmd/ateapi/internal/store/storetest" |
| 22 | + "github.com/agent-substrate/substrate/pkg/proto/ateapipb" |
| 23 | + "google.golang.org/grpc/codes" |
| 24 | + "google.golang.org/grpc/status" |
| 25 | +) |
| 26 | + |
| 27 | +// TestPauseActorWorkflow exercises the pause workflow end-to-end against |
| 28 | +// seeded actor statuses, covering both the rejected and the idempotent-success |
| 29 | +// paths. The atelet dialer is nil, so any step that unexpectedly reaches it |
| 30 | +// panics. |
| 31 | +func TestPauseActorWorkflow(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + seedStatus ateapipb.Actor_Status |
| 35 | + // wantErr true means PauseActor must fail with FailedPrecondition. |
| 36 | + wantErr bool |
| 37 | + // wantStatus is the stored status after the call. |
| 38 | + wantStatus ateapipb.Actor_Status |
| 39 | + }{ |
| 40 | + { |
| 41 | + // Pausing a SUSPENDED actor is rejected by MarkPausingStep's |
| 42 | + // CheckPrerequisite and the actor's status is left untouched. |
| 43 | + name: "not running rejected", |
| 44 | + seedStatus: ateapipb.Actor_STATUS_SUSPENDED, |
| 45 | + wantErr: true, |
| 46 | + wantStatus: ateapipb.Actor_STATUS_SUSPENDED, |
| 47 | + }, |
| 48 | + { |
| 49 | + // Pausing a PAUSED actor succeeds idempotently via IsComplete |
| 50 | + // fast-forward without calling atelet. |
| 51 | + name: "already paused succeeds", |
| 52 | + seedStatus: ateapipb.Actor_STATUS_PAUSED, |
| 53 | + wantStatus: ateapipb.Actor_STATUS_PAUSED, |
| 54 | + }, |
| 55 | + } |
| 56 | + for _, tc := range tests { |
| 57 | + t.Run(tc.name, func(t *testing.T) { |
| 58 | + ctx := context.Background() |
| 59 | + st, cleanup := storetest.SetupTestStore(t) |
| 60 | + defer cleanup() |
| 61 | + w := newTestActorWorkflow(t, st, "ns", "tmpl1") |
| 62 | + |
| 63 | + seedWorkflowActor(t, ctx, st, "team-a", "id1", "ns", "tmpl1", tc.seedStatus) |
| 64 | + |
| 65 | + actor, err := w.PauseActor(ctx, "team-a", "id1") |
| 66 | + if tc.wantErr { |
| 67 | + if got := status.Code(err); got != codes.FailedPrecondition { |
| 68 | + t.Fatalf("status.Code(err) = %v, want %v (err: %v)", got, codes.FailedPrecondition, err) |
| 69 | + } |
| 70 | + } else { |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("PauseActor failed: %v", err) |
| 73 | + } |
| 74 | + if actor.GetStatus() != tc.wantStatus { |
| 75 | + t.Errorf("returned status = %v, want %v", actor.GetStatus(), tc.wantStatus) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + got, err := st.GetActor(ctx, "team-a", "id1") |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("GetActor failed: %v", err) |
| 82 | + } |
| 83 | + if got.GetStatus() != tc.wantStatus { |
| 84 | + t.Errorf("stored status = %v, want %v", got.GetStatus(), tc.wantStatus) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TestPauseSteps_CheckPrerequisite verifies each pause step's CheckPrerequisite |
| 91 | +// against every actor status: nil for the step's allowed statuses, |
| 92 | +// FailedPrecondition for all others. |
| 93 | +func TestPauseSteps_CheckPrerequisite(t *testing.T) { |
| 94 | + tests := []struct { |
| 95 | + name string |
| 96 | + step WorkflowStep[*PauseInput, *PauseState] |
| 97 | + // allowed lists the statuses CheckPrerequisite accepts; nil means |
| 98 | + // every status is accepted. |
| 99 | + allowed map[ateapipb.Actor_Status]bool |
| 100 | + }{ |
| 101 | + { |
| 102 | + // Loading has no prerequisite: it is allowed from every status. |
| 103 | + name: "LoadActorForPauseStep", |
| 104 | + step: &LoadActorForPauseStep{}, |
| 105 | + allowed: nil, |
| 106 | + }, |
| 107 | + { |
| 108 | + // Pausing is allowed only from RUNNING. |
| 109 | + name: "MarkPausingStep", |
| 110 | + step: &MarkPausingStep{}, |
| 111 | + allowed: map[ateapipb.Actor_Status]bool{ |
| 112 | + ateapipb.Actor_STATUS_RUNNING: true, |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + // The checkpoint call is allowed only from PAUSING (PAUSED is |
| 117 | + // fast-forwarded by IsComplete). |
| 118 | + name: "CallAteletPauseStep", |
| 119 | + step: &CallAteletPauseStep{}, |
| 120 | + allowed: map[ateapipb.Actor_Status]bool{ |
| 121 | + ateapipb.Actor_STATUS_PAUSING: true, |
| 122 | + }, |
| 123 | + }, |
| 124 | + { |
| 125 | + // Finalizing is allowed only from PAUSING: a persisted PAUSED |
| 126 | + // actor always has its worker pod fields cleared and is |
| 127 | + // fast-forwarded by IsComplete. |
| 128 | + name: "FinalizePausedStep", |
| 129 | + step: &FinalizePausedStep{}, |
| 130 | + allowed: map[ateapipb.Actor_Status]bool{ |
| 131 | + ateapipb.Actor_STATUS_PAUSING: true, |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + for _, tc := range tests { |
| 136 | + t.Run(tc.name, func(t *testing.T) { |
| 137 | + ctx := context.Background() |
| 138 | + for _, st := range allActorStatuses { |
| 139 | + err := tc.step.CheckPrerequisite(ctx, &PauseInput{ActorID: "id1"}, &PauseState{Actor: &ateapipb.Actor{Status: st}}) |
| 140 | + assertPrerequisiteResult(t, st, err, tc.allowed == nil || tc.allowed[st]) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments