feat: compute and fleet storage interfaces with fakes
Add provider-neutral contracts, identity-checked compute fakes, bounded credential rotation, and direct-import boundary tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+555
−0
internal/compute/fake.go+141 −0internal/compute/fake_test.go+156 −0internal/compute/imports_test.go+40 −0internal/compute/provisioner.go+47 −0internal/fleetstore/fake.go+72 −0internal/fleetstore/fake_test.go+85 −0internal/fleetstore/provisioner.go+14 −0
diff --git a/internal/compute/fake.go b/internal/compute/fake.go| new file mode 100644 |
| index 000000000..370315799 |
| --- /dev/null |
| +++ b/internal/compute/fake.go |
| @@ -0,0 +1,141 @@ |
| +package compute |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "reflect" |
| + "slices" |
| + "sort" |
| + "sync" |
| + "time" |
| +) |
| + |
| +type Fake struct { |
| + LoseLaunchResponse bool |
| + FailLaunch error |
| + FailLaunchNumber int |
| + InitialState State |
| + TerminateIdentityMismatch bool |
| + mu sync.Mutex |
| + instances map[string]Instance |
| + tokens map[string]string |
| + specs map[string]LaunchSpec |
| + calls map[string]int |
| +} |
| + |
| +var _ Provisioner = (*Fake)(nil) |
| + |
| +func NewFake() *Fake { |
| + return &Fake{instances: make(map[string]Instance), tokens: make(map[string]string), specs: make(map[string]LaunchSpec), calls: make(map[string]int)} |
| +} |
| +func (f *Fake) Launch(ctx context.Context, s LaunchSpec) (Instance, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.calls["Launch"]++ |
| + if err := ctx.Err(); err != nil { |
| + return Instance{}, err |
| + } |
| + if cloudID, ok := f.tokens[s.ClientToken]; ok && s.ClientToken != "" { |
| + if !reflect.DeepEqual(f.specs[cloudID], s) { |
| + return Instance{}, fmt.Errorf("%w: client token reused with different launch spec", ErrLaunchRejected) |
| + } |
| + return f.instances[cloudID], nil |
| + } |
| + if (f.FailLaunch != nil && f.FailLaunchNumber == 0) || (f.FailLaunchNumber > 0 && f.calls["Launch"] == f.FailLaunchNumber) { |
| + err := f.FailLaunch |
| + if err == nil { |
| + err = ErrLaunchRejected |
| + } |
| + return Instance{}, err |
| + } |
| + state := f.InitialState |
| + if state == "" { |
| + state = StatePending |
| + } |
| + cloudID := fmt.Sprintf("fake-instance-%d", len(f.instances)+1) |
| + a := Instance{CloudID: cloudID, Region: s.Region, Identity: s.Identity, State: state, LaunchedAt: time.Now().UTC()} |
| + f.instances[cloudID] = a |
| + s.SecurityGroups = slices.Clone(s.SecurityGroups) |
| + f.specs[cloudID] = s |
| + if s.ClientToken != "" { |
| + f.tokens[s.ClientToken] = cloudID |
| + } |
| + if f.LoseLaunchResponse { |
| + return Instance{}, errors.New("launch response lost") |
| + } |
| + return a, nil |
| +} |
| +func (f *Fake) Find(ctx context.Context, region string, id Identity) ([]Instance, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.calls["Find"]++ |
| + if err := ctx.Err(); err != nil { |
| + return nil, err |
| + } |
| + return f.match(func(a Instance) bool { return a.Region == region && a.Identity == id }), nil |
| +} |
| +func (f *Fake) Terminate(ctx context.Context, region, cloudID string, want Identity) error { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.calls["Terminate"]++ |
| + if err := ctx.Err(); err != nil { |
| + return err |
| + } |
| + a, ok := f.instances[cloudID] |
| + if !ok || a.Region != region || a.Identity != want || f.TerminateIdentityMismatch { |
| + return ErrIdentityMismatch |
| + } |
| + a.State = StateTerminated |
| + f.instances[cloudID] = a |
| + return nil |
| +} |
| +func (f *Fake) ListDeployment(ctx context.Context, region, deployment string) ([]Instance, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.calls["ListDeployment"]++ |
| + if err := ctx.Err(); err != nil { |
| + return nil, err |
| + } |
| + return f.match(func(a Instance) bool { return a.Region == region && a.Identity.Deployment == deployment }), nil |
| +} |
| +func (f *Fake) ConsoleOutput(ctx context.Context, region, cloudID string) (string, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.calls["ConsoleOutput"]++ |
| + if err := ctx.Err(); err != nil { |
| + return "", err |
| + } |
| + a, ok := f.instances[cloudID] |
| + if !ok || a.Region != region { |
| + return "", errors.New("instance not found") |
| + } |
| + return "", nil |
| +} |
| +func (f *Fake) SetState(cloudID string, state State) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + if a, ok := f.instances[cloudID]; ok { |
| + a.State = state |
| + f.instances[cloudID] = a |
| + } |
| +} |
| +func (f *Fake) Calls() map[string]int { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + out := make(map[string]int, len(f.calls)) |
| + for k, v := range f.calls { |
| + out[k] = v |
| + } |
| + return out |
| +} |
| +func (f *Fake) match(accept func(Instance) bool) []Instance { |
| + var out []Instance |
| + for _, a := range f.instances { |
| + if accept(a) { |
| + out = append(out, a) |
| + } |
| + } |
| + sort.Slice(out, func(i, j int) bool { return out[i].CloudID < out[j].CloudID }) |
| + return out |
| +} |
diff --git a/internal/compute/fake_test.go b/internal/compute/fake_test.go| new file mode 100644 |
| index 000000000..4eb02fbb9 |
| --- /dev/null |
| +++ b/internal/compute/fake_test.go |
| @@ -0,0 +1,156 @@ |
| +package compute |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "sync" |
| + "testing" |
| +) |
| + |
| +func spec(token string) LaunchSpec { |
| + return LaunchSpec{Identity: Identity{"deployment", "fleet", "box", "operation"}, Region: "region", InstanceType: "small", Image: "image", Subnet: "subnet", SecurityGroups: []string{"group"}, RootGB: 20, UserData: "data", ClientToken: token} |
| +} |
| +func TestFakeClientToken(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + s := spec("one") |
| + a, e := f.Launch(ctx, s) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + b, e := f.Launch(ctx, s) |
| + if e != nil || a.CloudID != b.CloudID { |
| + t.Fatalf("retry: %v %v", b, e) |
| + } |
| + s.RootGB++ |
| + if _, e = f.Launch(ctx, s); e == nil { |
| + t.Fatal("changed spec accepted") |
| + } |
| + s.RootGB-- |
| + s.ClientToken = "two" |
| + b, e = f.Launch(ctx, s) |
| + if e != nil || b.CloudID == a.CloudID { |
| + t.Fatalf("new token: %v %v", b, e) |
| + } |
| + got, _ := f.Find(ctx, s.Region, s.Identity) |
| + if len(got) != 2 { |
| + t.Fatal(got) |
| + } |
| + s.Identity.Deployment = "other" |
| + got, _ = f.Find(ctx, s.Region, s.Identity) |
| + if len(got) != 0 { |
| + t.Fatal(got) |
| + } |
| + got, _ = f.ListDeployment(ctx, "elsewhere", "deployment") |
| + if len(got) != 0 { |
| + t.Fatal(got) |
| + } |
| + got, _ = f.ListDeployment(ctx, "region", "deployment") |
| + if len(got) != 2 { |
| + t.Fatal(got) |
| + } |
| +} |
| +func TestFakeLostLaunchResponse(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + f.LoseLaunchResponse = true |
| + s := spec("one") |
| + if _, err := f.Launch(ctx, s); err == nil || errors.Is(err, ErrLaunchRejected) { |
| + t.Fatalf("expected ambiguous error: %v", err) |
| + } |
| + got, _ := f.Find(ctx, s.Region, s.Identity) |
| + if len(got) != 1 { |
| + t.Fatal(got) |
| + } |
| + f.SetState(got[0].CloudID, StateTerminated) |
| + got, _ = f.Find(ctx, s.Region, s.Identity) |
| + if len(got) != 1 || got[0].State != StateTerminated { |
| + t.Fatal(got) |
| + } |
| + a, err := f.Launch(ctx, s) |
| + if err != nil || a.CloudID != got[0].CloudID { |
| + t.Fatalf("retry: %v %v", a, err) |
| + } |
| +} |
| +func TestFakeIdentityTerminate(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + s := spec("one") |
| + a, _ := f.Launch(ctx, s) |
| + for i := 0; i < 4; i++ { |
| + want := s.Identity |
| + fields := []*string{&want.Deployment, &want.Fleet, &want.Label, &want.Operation} |
| + *fields[i] = "other" |
| + if err := f.Terminate(ctx, s.Region, a.CloudID, want); !errors.Is(err, ErrIdentityMismatch) { |
| + t.Fatal(err) |
| + } |
| + got, _ := f.Find(ctx, s.Region, s.Identity) |
| + if got[0].State != a.State { |
| + t.Fatal(got) |
| + } |
| + } |
| + if err := f.Terminate(ctx, "other", a.CloudID, s.Identity); !errors.Is(err, ErrIdentityMismatch) { |
| + t.Fatal(err) |
| + } |
| + if err := f.Terminate(ctx, s.Region, a.CloudID, s.Identity); err != nil { |
| + t.Fatal(err) |
| + } |
| + got, _ := f.Find(ctx, s.Region, s.Identity) |
| + if got[0].State != StateTerminated { |
| + t.Fatal(got) |
| + } |
| + f = NewFake() |
| + f.TerminateIdentityMismatch = true |
| + a, _ = f.Launch(ctx, s) |
| + if err := f.Terminate(ctx, s.Region, a.CloudID, s.Identity); !errors.Is(err, ErrIdentityMismatch) { |
| + t.Fatal(err) |
| + } |
| + got, _ = f.Find(ctx, s.Region, s.Identity) |
| + if got[0].State != a.State { |
| + t.Fatal(got) |
| + } |
| +} |
| +func TestFakeNthCapacityFailure(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + f.FailLaunchNumber = 3 |
| + for _, token := range []string{"one", "two", "three"} { |
| + _, err := f.Launch(ctx, spec(token)) |
| + if (token == "three") != errors.Is(err, ErrLaunchRejected) { |
| + t.Fatal(token, err) |
| + } |
| + } |
| + got, _ := f.ListDeployment(ctx, "region", "deployment") |
| + if len(got) != 2 { |
| + t.Fatal(got) |
| + } |
| +} |
| +func TestFakeConcurrentCalls(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + f.InitialState = StateRunning |
| + var wg sync.WaitGroup |
| + for i := 0; i < 20; i++ { |
| + wg.Go(func() { |
| + a, err := f.Launch(ctx, spec("one")) |
| + if err != nil { |
| + t.Error(err) |
| + return |
| + } |
| + f.SetState(a.CloudID, StateRunning) |
| + f.Find(ctx, "region", spec("one").Identity) |
| + f.ListDeployment(ctx, "region", "deployment") |
| + f.ConsoleOutput(ctx, "region", a.CloudID) |
| + f.Calls() |
| + }) |
| + } |
| + wg.Wait() |
| + calls := f.Calls() |
| + if calls["Launch"] != 20 { |
| + t.Fatal(calls) |
| + } |
| + calls["Launch"] = 0 |
| + if f.Calls()["Launch"] != 20 { |
| + t.Fatal("aliased counters") |
| + } |
| +} |
diff --git a/internal/compute/imports_test.go b/internal/compute/imports_test.go| new file mode 100644 |
| index 000000000..e10b86819 |
| --- /dev/null |
| +++ b/internal/compute/imports_test.go |
| @@ -0,0 +1,40 @@ |
| +package compute |
| + |
| +import ( |
| + "go/parser" |
| + "go/token" |
| + "os" |
| + "path/filepath" |
| + "strconv" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func TestManagedCoreImports(t *testing.T) { |
| + for _, dir := range []string{".", "../fleetstore", "../console"} { |
| + entries, err := os.ReadDir(dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, entry := range entries { |
| + name := entry.Name() |
| + if entry.IsDir() || !strings.HasSuffix(name, ".go") || (dir == "../console" && !strings.HasPrefix(name, "managed")) { |
| + continue |
| + } |
| + path := filepath.Join(dir, name) |
| + f, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, imp := range f.Imports { |
| + value, err := strconv.Unquote(imp.Path.Value) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if strings.HasPrefix(value, "github.com/aws/") { |
| + t.Errorf("%s imports %s", path, value) |
| + } |
| + } |
| + } |
| + } |
| +} |
diff --git a/internal/compute/provisioner.go b/internal/compute/provisioner.go| new file mode 100644 |
| index 000000000..15d2c1784 |
| --- /dev/null |
| +++ b/internal/compute/provisioner.go |
| @@ -0,0 +1,47 @@ |
| +package compute |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "time" |
| +) |
| + |
| +var ( |
| + ErrLaunchRejected = errors.New("launch rejected") |
| + ErrIdentityMismatch = errors.New("instance identity mismatch") |
| +) |
| + |
| +type Identity struct{ Deployment, Fleet, Label, Operation string } |
| +type LaunchSpec struct { |
| + Identity |
| + Region, InstanceType, Image, Subnet string |
| + SecurityGroups []string |
| + RootGB int |
| + UserData string |
| + ClientToken string |
| +} |
| +type State string |
| + |
| +const ( |
| + StatePending State = "pending" |
| + StateRunning State = "running" |
| + StateStopping State = "stopping" |
| + StateStopped State = "stopped" |
| + StateShuttingDown State = "shutting-down" |
| + StateTerminated State = "terminated" |
| +) |
| + |
| +type Instance struct { |
| + CloudID, Region string |
| + Identity Identity |
| + State State |
| + Reason string |
| + LaunchedAt time.Time |
| +} |
| +type Provisioner interface { |
| + Launch(ctx context.Context, s LaunchSpec) (Instance, error) |
| + Find(ctx context.Context, region string, id Identity) ([]Instance, error) |
| + Terminate(ctx context.Context, region, cloudID string, want Identity) error |
| + ListDeployment(ctx context.Context, region, deployment string) ([]Instance, error) |
| + ConsoleOutput(ctx context.Context, region, cloudID string) (string, error) |
| +} |
diff --git a/internal/fleetstore/fake.go b/internal/fleetstore/fake.go| new file mode 100644 |
| index 000000000..cc419c595 |
| --- /dev/null |
| +++ b/internal/fleetstore/fake.go |
| @@ -0,0 +1,72 @@ |
| +package fleetstore |
| + |
| +import ( |
| + "context" |
| + "crypto/sha256" |
| + "errors" |
| + "fmt" |
| + "sync" |
| +) |
| + |
| +type Fake struct { |
| + mu sync.Mutex |
| + storage map[string]FleetStorage |
| + keys map[string]map[string]bool |
| + latest map[string]string |
| + nextKey uint64 |
| +} |
| + |
| +var _ Provisioner = (*Fake)(nil) |
| + |
| +func NewFake() *Fake { |
| + return &Fake{storage: make(map[string]FleetStorage), keys: make(map[string]map[string]bool), latest: make(map[string]string)} |
| +} |
| +func (f *Fake) EnsureFleetStorage(ctx context.Context, fleet, region string) (FleetStorage, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + if err := ctx.Err(); err != nil { |
| + return FleetStorage{}, err |
| + } |
| + if s, ok := f.storage[fleet]; ok { |
| + if s.Region != region { |
| + return FleetStorage{}, errors.New("fleet storage region mismatch") |
| + } |
| + return s, nil |
| + } |
| + s := FleetStorage{Bucket: fmt.Sprintf("fake-fleet-%x", sha256.Sum256([]byte(fleet))), Region: region} |
| + f.storage[fleet] = s |
| + return s, nil |
| +} |
| +func (f *Fake) FleetCredentials(ctx context.Context, fleet string) (Credentials, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + if err := ctx.Err(); err != nil { |
| + return Credentials{}, err |
| + } |
| + return Credentials{ID: f.latest[fleet]}, nil |
| +} |
| +func (f *Fake) RotateFleetCredentials(ctx context.Context, fleet, keep string) (Credentials, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + if err := ctx.Err(); err != nil { |
| + return Credentials{}, err |
| + } |
| + keys := f.keys[fleet] |
| + if keep != "" && !keys[keep] { |
| + return Credentials{}, errors.New("fleet credential to keep not found") |
| + } |
| + if keys == nil { |
| + keys = make(map[string]bool) |
| + f.keys[fleet] = keys |
| + } |
| + for id := range keys { |
| + if id != keep { |
| + delete(keys, id) |
| + } |
| + } |
| + f.nextKey++ |
| + id := fmt.Sprintf("fake-key-%d", f.nextKey) |
| + keys[id] = true |
| + f.latest[fleet] = id |
| + return Credentials{ID: id, Secret: []byte(fmt.Sprintf("fake-secret-%d", f.nextKey))}, nil |
| +} |
diff --git a/internal/fleetstore/fake_test.go b/internal/fleetstore/fake_test.go| new file mode 100644 |
| index 000000000..d5e00b5ce |
| --- /dev/null |
| +++ b/internal/fleetstore/fake_test.go |
| @@ -0,0 +1,85 @@ |
| +package fleetstore |
| + |
| +import ( |
| + "context" |
| + "reflect" |
| + "testing" |
| +) |
| + |
| +func TestFleetStorageFake(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + a, err := f.EnsureFleetStorage(ctx, "fleet", "region") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + b, err := f.EnsureFleetStorage(ctx, "fleet", "region") |
| + if err != nil || a != b { |
| + t.Fatal(b, err) |
| + } |
| + c, err := f.EnsureFleetStorage(ctx, "other", "region") |
| + if err != nil || a.Bucket == c.Bucket { |
| + t.Fatal(c, err) |
| + } |
| + if _, err = f.EnsureFleetStorage(ctx, "fleet", "other-region"); err == nil { |
| + t.Fatal("fleet region changed") |
| + } |
| + creds, err := f.FleetCredentials(ctx, "fleet") |
| + if err != nil || creds.ID != "" || len(creds.Secret) != 0 { |
| + t.Fatal(creds, err) |
| + } |
| + typ := reflect.TypeOf(a) |
| + if typ.NumField() != 2 { |
| + t.Fatal("storage metadata must contain only bucket and region") |
| + } |
| +} |
| +func TestFleetStorageFakeRotation(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + other, _ := f.RotateFleetCredentials(ctx, "other", "") |
| + first, err := f.RotateFleetCredentials(ctx, "fleet", "") |
| + if err != nil || first.ID == "" || len(first.Secret) == 0 { |
| + t.Fatal(first, err) |
| + } |
| + got, err := f.FleetCredentials(ctx, "fleet") |
| + if err != nil || got.ID != first.ID || len(got.Secret) != 0 { |
| + t.Fatal(got, err) |
| + } |
| + second, err := f.RotateFleetCredentials(ctx, "fleet", first.ID) |
| + if err != nil || second.ID == first.ID || reflect.DeepEqual(second.Secret, first.Secret) { |
| + t.Fatal(second, err) |
| + } |
| + third, err := f.RotateFleetCredentials(ctx, "fleet", first.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(f.keys["fleet"]) != 2 || !f.keys["fleet"][first.ID] || !f.keys["fleet"][third.ID] || f.keys["fleet"][second.ID] { |
| + t.Fatal(f.keys) |
| + } |
| + if len(f.keys["other"]) != 1 || !f.keys["other"][other.ID] { |
| + t.Fatal(f.keys) |
| + } |
| + if _, err = f.RotateFleetCredentials(ctx, "fleet", "unknown"); err == nil { |
| + t.Fatal("unknown keep accepted") |
| + } |
| + if len(f.keys["fleet"]) != 2 || !f.keys["fleet"][first.ID] { |
| + t.Fatal(f.keys) |
| + } |
| +} |
| +func TestFleetCredentialsDoubleLostRotation(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + delivered, _ := f.RotateFleetCredentials(ctx, "fleet", "") |
| + for i := 0; i < 2; i++ { |
| + if _, err := f.RotateFleetCredentials(ctx, "fleet", delivered.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(f.keys["fleet"]) > 2 || !f.keys["fleet"][delivered.ID] { |
| + t.Fatal(f.keys) |
| + } |
| + } |
| + got, _ := f.FleetCredentials(ctx, "fleet") |
| + if len(got.Secret) != 0 { |
| + t.Fatal("secret recovered") |
| + } |
| +} |
diff --git a/internal/fleetstore/provisioner.go b/internal/fleetstore/provisioner.go| new file mode 100644 |
| index 000000000..0955ad258 |
| --- /dev/null |
| +++ b/internal/fleetstore/provisioner.go |
| @@ -0,0 +1,14 @@ |
| +package fleetstore |
| + |
| +import "context" |
| + |
| +type Credentials struct { |
| + ID string |
| + Secret []byte |
| +} |
| +type FleetStorage struct{ Bucket, Region string } |
| +type Provisioner interface { |
| + EnsureFleetStorage(ctx context.Context, fleet, region string) (FleetStorage, error) |
| + FleetCredentials(ctx context.Context, fleet string) (Credentials, error) |
| + RotateFleetCredentials(ctx context.Context, fleet, keep string) (Credentials, error) |
| +} |