oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build hostedlive
2
3package main
4
5import (
6 "context"
7 "crypto/ed25519"
8 "errors"
9 "path/filepath"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/hostedctl"
14 "amadan.net/oficina/equipo/internal/hostedwire"
15 "amadan.net/oficina/equipo/internal/isolatedjob"
16 "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest"
17)
18
19var errCrashed = errors.New("crash point reached")
20
21// crashCtl is a controller over the store at path and the given runner,
22// with a passing network audit.
23func crashCtl(t *testing.T, path string, clk *isolatedjobtest.FakeClock, run isolatedjob.Runner, fake *isolatedjob.Fake) (*hostedctl.Controller, *hostedctl.Store) {
24 t.Helper()
25 ctx := context.Background()
26 store, err := hostedctl.Open(ctx, path, clk)
27 if err != nil {
28 t.Fatal(err)
29 }
30 t.Cleanup(func() { _ = store.Close() }) // a second Close does nothing
31 c, err := hostedctl.New(hostedctl.Config{Enabled: true, Images: map[isolatedjob.ImageDigest]bool{testDigest: true},
32 ProviderNamespace: "test", ProviderFingerprint: func(isolatedjob.ImageDigest, isolatedjob.Size) string { return "" }},
33 hostedctl.Deps{Store: store, Runner: run, Auditor: fake, Alerter: nopAlerter{}})
34 if err != nil {
35 t.Fatal(err)
36 }
37 if err := c.RunAudit(ctx); err != nil {
38 t.Fatal(err)
39 }
40 return c, store
41}
42
43// TestCrashAfterLaunch: with EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1 the hook
44// exits with 137 after the provider created the VM and before transaction 2
45// saved its Ref. A controller restarted on the same database without the
46// variable replays the intent and claims that one VM.
47func TestCrashAfterLaunch(t *testing.T) {
48 ctx := context.Background()
49 exitCode := 0
50 saved := crashExit
51 t.Cleanup(func() { crashExit = saved })
52 crashExit = func(code int) {
53 exitCode = code
54 panic(errCrashed)
55 }
56
57 clk := isolatedjobtest.NewFakeClock(time.Date(2026, 11, 23, 12, 0, 0, 0, time.UTC))
58 fake := isolatedjob.NewFake(clk, isolatedjob.SpikeLimits())
59 path := filepath.Join(t.TempDir(), "hosted.db")
60 key := hostedwire.VerifyKey{Public: instKey.Public().(ed25519.PublicKey), Role: hostedwire.RoleInstance}
61 q := hostedctl.LaunchRequest{AttemptID: "crash-1", TaskID: "task-1", Account: "acct-1", Image: testDigest,
62 Size: isolatedjob.SizeSmall, MaxLifetime: time.Hour, InstanceCode: "code-1"}
63
64 t.Setenv(crashAfterLaunchEnv, "1")
65 if runnerHook == nil {
66 t.Fatal("runnerHook is nil in a hostedlive build")
67 }
68 c, store := crashCtl(t, path, clk, runnerHook(fake), fake)
69 crashed := func() (r any) {
70 defer func() { r = recover() }()
71 _, _ = c.Launch(ctx, instID, key, q)
72 return nil
73 }()
74 if crashed != errCrashed || exitCode != 137 {
75 t.Fatalf("Launch with the crash point on: recovered %v, exit code %d; want the crash point to exit with 137", crashed, exitCode)
76 }
77 if n := fake.LiveByAttempt()[q.AttemptID]; n != 1 {
78 t.Fatalf("provider VMs for %s at the crash = %d, want 1: the exit must come after the provider call", q.AttemptID, n)
79 }
80 v, err := c.Get(ctx, instID, q.AttemptID)
81 if err != nil {
82 t.Fatalf("Get after the crash = %v", err)
83 }
84 if v.State != hostedctl.JobWaiting || v.VMRef != "" {
85 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)
86 }
87 if err := store.Close(); err != nil {
88 t.Fatal(err)
89 }
90
91 // The restart, without the variable: the hook hands back the runner itself.
92 t.Setenv(crashAfterLaunchEnv, "")
93 run := runnerHook(fake)
94 if run != isolatedjob.Runner(fake) {
95 t.Fatalf("runnerHook without %s = %T, want the runner unwrapped", crashAfterLaunchEnv, run)
96 }
97 c, _ = crashCtl(t, path, clk, run, fake)
98 if err := c.Replay(ctx); err != nil {
99 t.Fatalf("Replay after the restart = %v", err)
100 }
101 v, err = c.Get(ctx, instID, q.AttemptID)
102 if err != nil {
103 t.Fatalf("Get after the replay = %v", err)
104 }
105 if v.State != hostedctl.JobStarting || v.VMRef == "" {
106 t.Fatalf("job after the replay = %s with VM %q, want %s with the VM", v.State, v.VMRef, hostedctl.JobStarting)
107 }
108 if n := fake.LiveByAttempt()[q.AttemptID]; n != 1 {
109 t.Fatalf("provider VMs for %s after the replay = %d, want 1", q.AttemptID, n)
110 }
111}
112