|
| 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 | + "errors" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/agent-substrate/substrate/cmd/ateapi/internal/store" |
| 24 | + "github.com/agent-substrate/substrate/cmd/ateapi/internal/store/storetest" |
| 25 | + "github.com/agent-substrate/substrate/internal/ateerrors" |
| 26 | + "github.com/agent-substrate/substrate/pkg/proto/ateapipb" |
| 27 | + "google.golang.org/grpc/codes" |
| 28 | + "google.golang.org/grpc/status" |
| 29 | +) |
| 30 | + |
| 31 | +// seedActor stores a running actor with all worker-binding fields populated, so |
| 32 | +// tests can assert they are cleared when the actor crashes. |
| 33 | +func seedActor(t *testing.T, ctx context.Context, st store.Interface, atespace, id string) { |
| 34 | + t.Helper() |
| 35 | + if err := st.CreateActor(ctx, &ateapipb.Actor{ |
| 36 | + ActorId: id, |
| 37 | + Atespace: atespace, |
| 38 | + Status: ateapipb.Actor_STATUS_RUNNING, |
| 39 | + AteomPodNamespace: "ns", |
| 40 | + AteomPodName: "pod", |
| 41 | + AteomPodIp: "1.2.3.4", |
| 42 | + AteomPodUid: "uid", |
| 43 | + WorkerPoolName: "pool", |
| 44 | + }); err != nil { |
| 45 | + t.Fatalf("seed actor: %v", err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// assertCrashed reloads the actor and verifies it is CRASHED. |
| 50 | +func assertCrashed(t *testing.T, ctx context.Context, st store.Interface, atespace, id string) { |
| 51 | + t.Helper() |
| 52 | + got, err := st.GetActor(ctx, atespace, id) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("GetActor(%q, %q) = %v, want nil", atespace, id, err) |
| 55 | + } |
| 56 | + if got.GetStatus() != ateapipb.Actor_STATUS_CRASHED { |
| 57 | + t.Errorf("status = %v, want %v", got.GetStatus(), ateapipb.Actor_STATUS_CRASHED) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestCrashActor(t *testing.T) { |
| 62 | + const ( |
| 63 | + atespace = "team-a" |
| 64 | + actorID = "actor-1" |
| 65 | + ) |
| 66 | + |
| 67 | + tests := []struct { |
| 68 | + name string |
| 69 | + seed bool |
| 70 | + // check inspects the returned error; nil-safe. |
| 71 | + check func(t *testing.T, ctx context.Context, st store.Interface, err error) |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "crashes running actor", |
| 75 | + seed: true, |
| 76 | + check: func(t *testing.T, ctx context.Context, st store.Interface, err error) { |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("crashActor() = %v, want nil", err) |
| 79 | + } |
| 80 | + assertCrashed(t, ctx, st, atespace, actorID) |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "actor not found", |
| 85 | + seed: false, |
| 86 | + check: func(t *testing.T, ctx context.Context, st store.Interface, err error) { |
| 87 | + if err == nil { |
| 88 | + t.Fatal("crashActor() = nil, want error") |
| 89 | + } |
| 90 | + if !errors.Is(err, store.ErrNotFound) { |
| 91 | + t.Errorf("crashActor() error = %v, want errors.Is(store.ErrNotFound)", err) |
| 92 | + } |
| 93 | + if !strings.Contains(err.Error(), "while loading actor to crash") { |
| 94 | + t.Errorf("crashActor() error = %q, want it to contain %q", err, "while loading actor to crash") |
| 95 | + } |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tt := range tests { |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + ctx := context.Background() |
| 103 | + st, cleanup := storetest.SetupTestStore(t) |
| 104 | + defer cleanup() |
| 105 | + |
| 106 | + if tt.seed { |
| 107 | + seedActor(t, ctx, st, atespace, actorID) |
| 108 | + } |
| 109 | + |
| 110 | + err := crashActor(ctx, st, atespace, actorID) |
| 111 | + tt.check(t, ctx, st, err) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestMaybeCrashActor(t *testing.T) { |
| 117 | + const ( |
| 118 | + atespace = "team-a" |
| 119 | + actorID = "actor-1" |
| 120 | + wrapMsg = "calling atelet" |
| 121 | + ) |
| 122 | + |
| 123 | + crashErr := ateerrors.NewGRPCError(context.Background(), codes.NotFound, ateerrors.ReasonTerminalFileSystemError, ateerrors.ActorCrashedMetadata(), errors.New("boom")) |
| 124 | + // A structured error carrying a reason but no actorCrashed directive must be |
| 125 | + // wrapped, not crash the actor. |
| 126 | + noCrashErr := ateerrors.NewGRPCError(context.Background(), codes.NotFound, ateerrors.ReasonFailedGetExternalObject, nil, errors.New("infra")) |
| 127 | + plainErr := errors.New("transient") |
| 128 | + |
| 129 | + tests := []struct { |
| 130 | + name string |
| 131 | + seed bool |
| 132 | + err error |
| 133 | + // check inspects the returned error and store state. |
| 134 | + check func(t *testing.T, ctx context.Context, st store.Interface, err error) |
| 135 | + }{ |
| 136 | + { |
| 137 | + name: "nil error returns nil", |
| 138 | + seed: false, |
| 139 | + err: nil, |
| 140 | + check: func(t *testing.T, ctx context.Context, st store.Interface, err error) { |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("maybeCrashActor() = %v, want nil", err) |
| 143 | + } |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "crash reason crashes actor", |
| 148 | + seed: true, |
| 149 | + err: crashErr, |
| 150 | + check: func(t *testing.T, ctx context.Context, st store.Interface, err error) { |
| 151 | + if err == nil { |
| 152 | + t.Fatal("maybeCrashActor() = nil, want error") |
| 153 | + } |
| 154 | + if got := status.Code(err); got != codes.DataLoss { |
| 155 | + t.Errorf("status code = %v, want %v", got, codes.DataLoss) |
| 156 | + } |
| 157 | + assertCrashed(t, ctx, st, atespace, actorID) |
| 158 | + }, |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "crash reason but actor missing returns load error", |
| 162 | + seed: false, |
| 163 | + err: crashErr, |
| 164 | + check: func(t *testing.T, ctx context.Context, st store.Interface, err error) { |
| 165 | + if err == nil { |
| 166 | + t.Fatal("maybeCrashActor() = nil, want error") |
| 167 | + } |
| 168 | + if got := status.Code(err); got == codes.DataLoss { |
| 169 | + t.Errorf("status code = %v, want it not to be DataLoss", got) |
| 170 | + } |
| 171 | + if !errors.Is(err, store.ErrNotFound) { |
| 172 | + t.Errorf("maybeCrashActor() error = %v, want errors.Is(store.ErrNotFound)", err) |
| 173 | + } |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "status error without crash directive is wrapped", |
| 178 | + seed: true, |
| 179 | + err: noCrashErr, |
| 180 | + check: func(t *testing.T, ctx context.Context, st store.Interface, err error) { |
| 181 | + if err == nil { |
| 182 | + t.Fatal("maybeCrashActor() = nil, want error") |
| 183 | + } |
| 184 | + if !errors.Is(err, noCrashErr) { |
| 185 | + t.Errorf("maybeCrashActor() error = %v, want errors.Is(noCrashErr)", err) |
| 186 | + } |
| 187 | + if !strings.HasPrefix(err.Error(), wrapMsg) { |
| 188 | + t.Errorf("maybeCrashActor() error = %q, want prefix %q", err, wrapMsg) |
| 189 | + } |
| 190 | + // The actor must not have been crashed. |
| 191 | + got, gerr := st.GetActor(ctx, atespace, actorID) |
| 192 | + if gerr != nil { |
| 193 | + t.Fatalf("GetActor() = %v, want nil", gerr) |
| 194 | + } |
| 195 | + if got.GetStatus() == ateapipb.Actor_STATUS_CRASHED { |
| 196 | + t.Errorf("status = CRASHED, want it unchanged") |
| 197 | + } |
| 198 | + }, |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "non-crash error is wrapped", |
| 202 | + seed: true, |
| 203 | + err: plainErr, |
| 204 | + check: func(t *testing.T, ctx context.Context, st store.Interface, err error) { |
| 205 | + if err == nil { |
| 206 | + t.Fatal("maybeCrashActor() = nil, want error") |
| 207 | + } |
| 208 | + if !errors.Is(err, plainErr) { |
| 209 | + t.Errorf("maybeCrashActor() error = %v, want errors.Is(plainErr)", err) |
| 210 | + } |
| 211 | + if !strings.HasPrefix(err.Error(), wrapMsg) { |
| 212 | + t.Errorf("maybeCrashActor() error = %q, want prefix %q", err, wrapMsg) |
| 213 | + } |
| 214 | + // The actor must not have been crashed. |
| 215 | + got, gerr := st.GetActor(ctx, atespace, actorID) |
| 216 | + if gerr != nil { |
| 217 | + t.Fatalf("GetActor() = %v, want nil", gerr) |
| 218 | + } |
| 219 | + if got.GetStatus() == ateapipb.Actor_STATUS_CRASHED { |
| 220 | + t.Errorf("status = CRASHED, want it unchanged") |
| 221 | + } |
| 222 | + }, |
| 223 | + }, |
| 224 | + } |
| 225 | + |
| 226 | + for _, tt := range tests { |
| 227 | + t.Run(tt.name, func(t *testing.T) { |
| 228 | + ctx := context.Background() |
| 229 | + st, cleanup := storetest.SetupTestStore(t) |
| 230 | + defer cleanup() |
| 231 | + |
| 232 | + if tt.seed { |
| 233 | + seedActor(t, ctx, st, atespace, actorID) |
| 234 | + } |
| 235 | + |
| 236 | + err := maybeCrashActor(ctx, st, atespace, actorID, tt.err, wrapMsg) |
| 237 | + tt.check(t, ctx, st, err) |
| 238 | + }) |
| 239 | + } |
| 240 | +} |
0 commit comments