|
| 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" |
| 22 | + "github.com/agent-substrate/substrate/cmd/ateapi/internal/store/ateredis" |
| 23 | + "github.com/agent-substrate/substrate/pkg/proto/ateapipb" |
| 24 | + "github.com/alicebob/miniredis/v2" |
| 25 | + "github.com/redis/go-redis/v9" |
| 26 | +) |
| 27 | + |
| 28 | +// newTestPersistence returns a store backed by a throwaway miniredis. |
| 29 | +func newTestPersistence(t *testing.T) store.Interface { |
| 30 | + t.Helper() |
| 31 | + mr, err := miniredis.Run() |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("failed to start miniredis: %v", err) |
| 34 | + } |
| 35 | + t.Cleanup(mr.Close) |
| 36 | + rdb := redis.NewClusterClient(&redis.ClusterOptions{Addrs: []string{mr.Addr()}}) |
| 37 | + t.Cleanup(func() { rdb.Close() }) //nolint:errcheck // test cleanup |
| 38 | + return ateredis.NewPersistence(rdb) |
| 39 | +} |
| 40 | + |
| 41 | +func TestFinalizeSuspendedStep_ReleasesOnlyOwnWorker(t *testing.T) { |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + assignmentAtespace string |
| 45 | + wantReleased bool |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "frees worker assigned to this actor", |
| 49 | + assignmentAtespace: "team-a", |
| 50 | + wantReleased: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "keeps worker assigned to same-named actor in another atespace", |
| 54 | + assignmentAtespace: "team-b", |
| 55 | + wantReleased: false, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + ctx := context.Background() |
| 62 | + persistence := newTestPersistence(t) |
| 63 | + |
| 64 | + worker := &ateapipb.Worker{ |
| 65 | + WorkerNamespace: "worker-ns", |
| 66 | + WorkerPool: "pool", |
| 67 | + WorkerPod: "pod-1", |
| 68 | + Assignment: &ateapipb.Assignment{ |
| 69 | + Actor: &ateapipb.ActorRef{Atespace: tt.assignmentAtespace, Name: "shared"}, |
| 70 | + }, |
| 71 | + } |
| 72 | + if err := persistence.CreateWorker(ctx, worker); err != nil { |
| 73 | + t.Fatalf("CreateWorker: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + actor := &ateapipb.Actor{ |
| 77 | + Metadata: &ateapipb.ResourceMetadata{Atespace: "team-a", Name: "shared"}, |
| 78 | + Status: ateapipb.Actor_STATUS_SUSPENDING, |
| 79 | + AteomPodNamespace: "worker-ns", |
| 80 | + AteomPodName: "pod-1", |
| 81 | + WorkerPoolName: "pool", |
| 82 | + InProgressSnapshot: "gs://snapshots/shared/1", |
| 83 | + } |
| 84 | + if _, err := persistence.CreateActor(ctx, actor); err != nil { |
| 85 | + t.Fatalf("CreateActor: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + step := &FinalizeSuspendedStep{store: persistence} |
| 89 | + input := &SuspendInput{ActorName: "shared", Atespace: "team-a"} |
| 90 | + if err := step.Execute(ctx, input, &SuspendState{}); err != nil { |
| 91 | + t.Fatalf("Execute: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + stored, err := persistence.GetWorker(ctx, "worker-ns", "pool", "pod-1") |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("GetWorker: %v", err) |
| 97 | + } |
| 98 | + if released := stored.GetAssignment() == nil; released != tt.wantReleased { |
| 99 | + t.Errorf("worker released = %t, want %t (assignment: %v)", released, tt.wantReleased, stored.GetAssignment()) |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments