//go:build hostedlive package main import ( "context" "crypto/ed25519" "errors" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/hostedctl" "amadan.net/oficina/equipo/internal/hostedwire" "amadan.net/oficina/equipo/internal/isolatedjob" "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest" ) var errCrashed = errors.New("crash point reached") // crashCtl is a controller over the store at path and the given runner, // with a passing network audit. func crashCtl(t *testing.T, path string, clk *isolatedjobtest.FakeClock, run isolatedjob.Runner, fake *isolatedjob.Fake) (*hostedctl.Controller, *hostedctl.Store) { t.Helper() ctx := context.Background() store, err := hostedctl.Open(ctx, path, clk) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = store.Close() }) // a second Close does nothing c, err := hostedctl.New(hostedctl.Config{Enabled: true, Images: map[isolatedjob.ImageDigest]bool{testDigest: true}, ProviderNamespace: "test", ProviderFingerprint: func(isolatedjob.ImageDigest, isolatedjob.Size) string { return "" }}, hostedctl.Deps{Store: store, Runner: run, Auditor: fake, Alerter: nopAlerter{}}) if err != nil { t.Fatal(err) } if err := c.RunAudit(ctx); err != nil { t.Fatal(err) } return c, store } // TestCrashAfterLaunch: with EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1 the hook // exits with 137 after the provider created the VM and before transaction 2 // saved its Ref. A controller restarted on the same database without the // variable replays the intent and claims that one VM. func TestCrashAfterLaunch(t *testing.T) { ctx := context.Background() exitCode := 0 saved := crashExit t.Cleanup(func() { crashExit = saved }) crashExit = func(code int) { exitCode = code panic(errCrashed) } clk := isolatedjobtest.NewFakeClock(time.Date(2026, 11, 23, 12, 0, 0, 0, time.UTC)) fake := isolatedjob.NewFake(clk, isolatedjob.SpikeLimits()) path := filepath.Join(t.TempDir(), "hosted.db") key := hostedwire.VerifyKey{Public: instKey.Public().(ed25519.PublicKey), Role: hostedwire.RoleInstance} q := hostedctl.LaunchRequest{AttemptID: "crash-1", TaskID: "task-1", Account: "acct-1", Image: testDigest, Size: isolatedjob.SizeSmall, MaxLifetime: time.Hour, InstanceCode: "code-1"} t.Setenv(crashAfterLaunchEnv, "1") if runnerHook == nil { t.Fatal("runnerHook is nil in a hostedlive build") } c, store := crashCtl(t, path, clk, runnerHook(fake), fake) crashed := func() (r any) { defer func() { r = recover() }() _, _ = c.Launch(ctx, instID, key, q) return nil }() if crashed != errCrashed || exitCode != 137 { t.Fatalf("Launch with the crash point on: recovered %v, exit code %d; want the crash point to exit with 137", crashed, exitCode) } if n := fake.LiveByAttempt()[q.AttemptID]; n != 1 { t.Fatalf("provider VMs for %s at the crash = %d, want 1: the exit must come after the provider call", q.AttemptID, n) } v, err := c.Get(ctx, instID, q.AttemptID) if err != nil { t.Fatalf("Get after the crash = %v", err) } if v.State != hostedctl.JobWaiting || v.VMRef != "" { t.Fatalf("job after the crash = %s with VM %q, want %s with no VM: the exit must come before transaction 2", v.State, v.VMRef, hostedctl.JobWaiting) } if err := store.Close(); err != nil { t.Fatal(err) } // The restart, without the variable: the hook hands back the runner itself. t.Setenv(crashAfterLaunchEnv, "") run := runnerHook(fake) if run != isolatedjob.Runner(fake) { t.Fatalf("runnerHook without %s = %T, want the runner unwrapped", crashAfterLaunchEnv, run) } c, _ = crashCtl(t, path, clk, run, fake) if err := c.Replay(ctx); err != nil { t.Fatalf("Replay after the restart = %v", err) } v, err = c.Get(ctx, instID, q.AttemptID) if err != nil { t.Fatalf("Get after the replay = %v", err) } if v.State != hostedctl.JobStarting || v.VMRef == "" { t.Fatalf("job after the replay = %s with VM %q, want %s with the VM", v.State, v.VMRef, hostedctl.JobStarting) } if n := fake.LiveByAttempt()[q.AttemptID]; n != 1 { t.Fatalf("provider VMs for %s after the replay = %d, want 1", q.AttemptID, n) } }