| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 18 | "golang.org/x/sys/unix" |
| 19 | ) |
| 20 | |
| 21 | func TestNamespaceRecoveryIdentity(t *testing.T) { |
| 22 | m, l := fixture(t) |
| 23 | p := launch(t, m, l, "import time; print('ready',flush=True); time.sleep(60)") |
| 24 | if _, e := bufio.NewReader(p.Stdout()).ReadString('\n'); e != nil { |
| 25 | t.Fatal(e) |
| 26 | } |
| 27 | r := record(t, m, p) |
| 28 | fd := descriptor(t, r.HostPID) |
| 29 | for _, tc := range []struct { |
| 30 | name string |
| 31 | edit func(*Record) |
| 32 | }{ |
| 33 | {"reused pid", func(r *Record) { r.StartTicks++ }}, |
| 34 | {"changed manifest", func(r *Record) { r.ManifestHash += "-changed" }}, |
| 35 | {"changed namespace", func(r *Record) { r.NamespaceID = "pid:[0]" }}, |
| 36 | {"missing handshake", func(r *Record) { r.HostPID = 0; r.StartTicks = 0; r.NamespaceID = "" }}, |
| 37 | {"unavailable proof", func(r *Record) { r.ManifestHash = "" }}, |
| 38 | } { |
| 39 | t.Run(tc.name, func(t *testing.T) { |
| 40 | bad := r |
| 41 | tc.edit(&bad) |
| 42 | proof, e := m.Recover(context.Background(), bad) |
| 43 | if e == nil || proof.Verified { |
| 44 | t.Fatalf("uncertain identity accepted: %+v %v", proof, e) |
| 45 | } |
| 46 | alive(t, fd) |
| 47 | if _, e := os.Stat(filepath.Join(m.cfg.ScratchRoot, "attempt-"+r.ID)); e != nil { |
| 48 | t.Fatalf("unproven recovery removed scratch: %v", e) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | changed := r |
| 53 | changed.BootID = "different-boot" |
| 54 | proof, e := m.Recover(context.Background(), changed) |
| 55 | if e != nil || !proof.Verified || proof.Exit.Code != -1 || proof.Exit.Signal != 0 { |
| 56 | t.Fatalf("changed boot fabricated outcome: %+v %v", proof, e) |
| 57 | } |
| 58 | alive(t, fd) |
| 59 | // A fresh manager reacquires only the durable, exact namespace init. |
| 60 | fresh, e := New(m.cfg) |
| 61 | if e != nil { |
| 62 | t.Fatal(e) |
| 63 | } |
| 64 | proof, e = fresh.Recover(context.Background(), r) |
| 65 | if e != nil || !proof.Verified || proof.Record != r { |
| 66 | t.Fatalf("recover exact identity: %+v %v", proof, e) |
| 67 | } |
| 68 | exited(t, fd) |
| 69 | } |
| 70 | func TestNamespaceMountEscape(t *testing.T) { |
| 71 | home, e := os.UserHomeDir() |
| 72 | if e != nil { |
| 73 | t.Fatal(e) |
| 74 | } |
| 75 | m, l := fixture(t) |
| 76 | outsideHome := t.TempDir() |
| 77 | if e := os.Chmod(outsideHome, 0700); e != nil { |
| 78 | t.Fatal(e) |
| 79 | } |
| 80 | for _, tc := range []struct { |
| 81 | name string |
| 82 | edit func(*provider.Launch) |
| 83 | }{ |
| 84 | {"traversal", func(l *provider.Launch) { l.Workspace += "/../workspace" }}, |
| 85 | {"outside private home", func(l *provider.Launch) { l.Binding.Home = outsideHome }}, |
| 86 | {"broad home", func(l *provider.Launch) { l.Binding.Home = home }}, |
| 87 | {"protected state", func(l *provider.Launch) { l.Workspace = m.cfg.ScratchRoot }}, |
| 88 | {"parent of protected state", func(l *provider.Launch) { l.Workspace = filepath.Dir(m.cfg.ScratchRoot) }}, |
| 89 | {"host runtime", func(l *provider.Launch) { |
| 90 | l.Policy.Mounts = []provider.Mount{{Source: "/run", Target: "/opt/run", ReadOnly: true}} |
| 91 | }}, |
| 92 | {"host cgroup", func(l *provider.Launch) { |
| 93 | l.Policy.Mounts = []provider.Mount{{Source: "/sys/fs/cgroup", Target: "/opt/cgroup", ReadOnly: true}} |
| 94 | }}, |
| 95 | {"writable runtime", func(l *provider.Launch) { |
| 96 | l.Policy.Mounts = []provider.Mount{{Source: "/etc/hostname", Target: "/opt/name"}} |
| 97 | }}, |
| 98 | {"reserved target", func(l *provider.Launch) { |
| 99 | l.Policy.Mounts = []provider.Mount{{Source: "/etc/hostname", Target: "/home/runner/hostname", ReadOnly: true}} |
| 100 | }}, |
| 101 | {"home environment traversal", func(l *provider.Launch) { l.Policy.Env = map[string]string{"CODEX_HOME": "/home/runner/../../etc"} }}, |
| 102 | {"tmp environment traversal", func(l *provider.Launch) { l.Policy.Env = map[string]string{"CLAUDE_TMPDIR": "/tmp/../workspace"} }}, |
| 103 | {"environment", func(l *provider.Launch) { l.Policy.Env = map[string]string{"LD_PRELOAD": "/workspace/a.so"} }}, |
| 104 | } { |
| 105 | t.Run(tc.name, func(t *testing.T) { |
| 106 | bad := l |
| 107 | tc.edit(&bad) |
| 108 | p, e := m.Start(context.Background(), bad, []string{"/usr/bin/true"}) |
| 109 | if e == nil { |
| 110 | m.Stop(context.Background(), p.ID()) |
| 111 | t.Fatal("unsafe mount/environment accepted") |
| 112 | } |
| 113 | }) |
| 114 | } |
| 115 | t.Run("symlink swap", func(t *testing.T) { |
| 116 | path := filepath.Join(filepath.Dir(l.Workspace), "link") |
| 117 | if e := os.Symlink(l.Workspace, path); e != nil { |
| 118 | t.Fatal(e) |
| 119 | } |
| 120 | bad := l |
| 121 | bad.Workspace = path |
| 122 | if p, e := m.Start(context.Background(), bad, []string{"/usr/bin/true"}); e == nil { |
| 123 | m.Stop(context.Background(), p.ID()) |
| 124 | t.Fatal("symlink accepted") |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | func TestNamespaceRecoverySavedExit(t *testing.T) { |
| 130 | m, l := fixture(t) |
| 131 | p := launch(t, m, l, "pass") |
| 132 | r := record(t, m, p) |
| 133 | exit, e := p.Wait(context.Background()) |
| 134 | if e != nil { |
| 135 | t.Fatal(e) |
| 136 | } |
| 137 | fresh, e := New(m.cfg) |
| 138 | if e != nil { |
| 139 | t.Fatal(e) |
| 140 | } |
| 141 | proof, e := fresh.Recover(context.Background(), r) |
| 142 | if e != nil || !proof.Verified || proof.Exit != exit { |
| 143 | t.Fatalf("durable exit recovery: %+v %v", proof, e) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestNamespaceRecoveryRejectsNonInit(t *testing.T) { |
| 148 | m, l := fixture(t) |
| 149 | p := launch(t, m, l, "import time; print('ready',flush=True); time.sleep(60)") |
| 150 | r := record(t, m, p) |
| 151 | // Both candidate PIDs are owned fixtures. Neither is namespace init. |
| 152 | control, controlFD := ownedControl(t) |
| 153 | bad := r |
| 154 | bad.HostPID = control.Process.Pid |
| 155 | bad.StartTicks, bad.NamespaceID, _ = identity(bad.HostPID) |
| 156 | if e := m.persist(bad); e != nil { |
| 157 | t.Fatal(e) |
| 158 | } |
| 159 | defer m.persist(r) |
| 160 | if proof, e := m.Recover(context.Background(), bad); e == nil || proof.Verified { |
| 161 | t.Fatal("host process accepted as namespace init") |
| 162 | } |
| 163 | alive(t, controlFD) |
| 164 | } |
| 165 | |
| 166 | func TestNamespaceRecoveryStaleDurableTicks(t *testing.T) { |
| 167 | m, l := fixture(t) |
| 168 | p := launch(t, m, l, "import time; time.sleep(60)") |
| 169 | r := record(t, m, p) |
| 170 | fd := descriptor(t, r.HostPID) |
| 171 | bad := r |
| 172 | bad.StartTicks++ |
| 173 | if e := m.persist(bad); e != nil { |
| 174 | t.Fatal(e) |
| 175 | } |
| 176 | defer m.persist(r) |
| 177 | if proof, e := m.Recover(context.Background(), bad); e != nil || !proof.Verified || proof.Exit.Code != -1 || proof.Exit.Signal != 0 { |
| 178 | t.Fatalf("absent recorded identity not recovered: %+v %v", proof, e) |
| 179 | } |
| 180 | alive(t, fd) |
| 181 | } |
| 182 | |
| 183 | func TestNamespaceRecoveryUnavailableProof(t *testing.T) { |
| 184 | for _, kind := range []string{"corrupt", "record", "unverified", "timestamp"} { |
| 185 | t.Run(kind, func(t *testing.T) { |
| 186 | m, l := fixture(t) |
| 187 | p := launch(t, m, l, "pass") |
| 188 | r := record(t, m, p) |
| 189 | if _, e := p.Wait(context.Background()); e != nil { |
| 190 | t.Fatal(e) |
| 191 | } |
| 192 | path := filepath.Join(m.cfg.ScratchRoot, p.ID()+".exit.json") |
| 193 | b, e := os.ReadFile(path) |
| 194 | if e != nil { |
| 195 | t.Fatal(e) |
| 196 | } |
| 197 | var proof Proof |
| 198 | if e = json.Unmarshal(b, &proof); e != nil { |
| 199 | t.Fatal(e) |
| 200 | } |
| 201 | switch kind { |
| 202 | case "corrupt": |
| 203 | e = os.WriteFile(path, []byte("{"), 0600) |
| 204 | case "record": |
| 205 | proof.Record.ManifestHash += "-changed" |
| 206 | e = durableJSON(m.cfg.ScratchRoot, p.ID()+".exit.json", proof) |
| 207 | case "unverified": |
| 208 | proof.Verified = false |
| 209 | e = durableJSON(m.cfg.ScratchRoot, p.ID()+".exit.json", proof) |
| 210 | case "timestamp": |
| 211 | proof.Exit.ObservedAt = time.Time{} |
| 212 | e = durableJSON(m.cfg.ScratchRoot, p.ID()+".exit.json", proof) |
| 213 | } |
| 214 | if e != nil { |
| 215 | t.Fatal(e) |
| 216 | } |
| 217 | if recovered, e := m.Recover(context.Background(), r); e == nil || recovered.Verified { |
| 218 | t.Fatalf("unavailable proof accepted: %+v %v", recovered, e) |
| 219 | } |
| 220 | }) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | func TestNamespaceRecoveryCleansScratch(t *testing.T) { |
| 225 | for _, kind := range []string{"saved-exit", "new-boot"} { |
| 226 | t.Run(kind, func(t *testing.T) { |
| 227 | m, l := fixture(t) |
| 228 | p := launch(t, m, l, "pass") |
| 229 | r := record(t, m, p) |
| 230 | if _, e := p.Wait(context.Background()); e != nil { |
| 231 | t.Fatal(e) |
| 232 | } |
| 233 | // Recreate the crash window after proof fsync but before scratch removal. |
| 234 | dir := filepath.Join(m.cfg.ScratchRoot, "attempt-"+r.ID) |
| 235 | if e := os.Mkdir(dir, 0700); e != nil { |
| 236 | t.Fatal(e) |
| 237 | } |
| 238 | if e := os.WriteFile(filepath.Join(dir, "leftover"), []byte("owned"), 0600); e != nil { |
| 239 | t.Fatal(e) |
| 240 | } |
| 241 | if kind == "new-boot" { |
| 242 | r.BootID = "previous-boot" |
| 243 | } |
| 244 | fresh, e := New(m.cfg) |
| 245 | if e != nil { |
| 246 | t.Fatal(e) |
| 247 | } |
| 248 | if proof, e := fresh.Recover(context.Background(), r); e != nil || !proof.Verified { |
| 249 | t.Fatalf("recover: %+v %v", proof, e) |
| 250 | } |
| 251 | if _, e := os.Stat(dir); !os.IsNotExist(e) { |
| 252 | t.Fatalf("recovery left owned scratch: %v", e) |
| 253 | } |
| 254 | }) |
| 255 | } |
| 256 | } |
| 257 | func TestNamespaceScratchIdentity(t *testing.T) { |
| 258 | m, l := fixture(t) |
| 259 | p := launch(t, m, l, "import time; time.sleep(60)") |
| 260 | if _, e := os.Stat(filepath.Join(m.cfg.ScratchRoot, "attempt-"+p.ID(), "tmp")); e != nil { |
| 261 | t.Fatalf("scratch not bound to durable ID: %v", e) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Restore fixture permissions even when testing the unfixed implementation. |
| 266 | func writableFixture(t *testing.T, dir string) { |
| 267 | t.Helper() |
| 268 | t.Cleanup(func() { |
| 269 | _ = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error { |
| 270 | if err == nil && d.IsDir() { |
| 271 | return os.Chmod(path, 0700) |
| 272 | } |
| 273 | return err |
| 274 | }) |
| 275 | }) |
| 276 | } |
| 277 | |
| 278 | func TestNamespaceReadOnlyScratch(t *testing.T) { |
| 279 | for _, mode := range []int{0555, 0500, 0000} { |
| 280 | for _, stop := range []bool{false, true} { |
| 281 | t.Run(fmt.Sprintf("%04o/stop=%t", mode, stop), func(t *testing.T) { |
| 282 | m, l := fixture(t) |
| 283 | script := fmt.Sprintf(`import os,sys |
| 284 | for base in ['/tmp', '/home/runner']: |
| 285 | os.makedirs(base+'/cache/nested') |
| 286 | open(base+'/cache/nested/file','w').write('owned') |
| 287 | os.chmod(base+'/cache/nested',%d) |
| 288 | os.chmod(base+'/cache',%d) |
| 289 | os.chmod(base,%d) |
| 290 | print('ready',flush=True) |
| 291 | sys.stdin.readline() |
| 292 | `, mode, mode, mode) |
| 293 | p := launch(t, m, l, script) |
| 294 | r := record(t, m, p) |
| 295 | dir := filepath.Join(m.cfg.ScratchRoot, "attempt-"+r.ID) |
| 296 | writableFixture(t, dir) |
| 297 | if _, e := bufio.NewReader(p.Stdout()).ReadString('\n'); e != nil { |
| 298 | t.Fatal(e) |
| 299 | } |
| 300 | if !stop { |
| 301 | p.Stdin().Close() |
| 302 | _, _ = p.Wait(context.Background()) |
| 303 | } |
| 304 | proof, e := m.Stop(context.Background(), r.ID) |
| 305 | if e != nil || !proof.Verified { |
| 306 | t.Fatalf("read-only scratch invalidated exit: %+v %v", proof, e) |
| 307 | } |
| 308 | if _, e := os.Stat(dir); !os.IsNotExist(e) { |
| 309 | t.Fatalf("scratch remains: %v", e) |
| 310 | } |
| 311 | recovered, e := m.Recover(context.Background(), r) |
| 312 | if e != nil || recovered != proof { |
| 313 | t.Fatalf("saved proof changed: %+v %v", recovered, e) |
| 314 | } |
| 315 | }) |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | func TestNamespaceRecoveryReadOnlyScratch(t *testing.T) { |
| 321 | m, l := fixture(t) |
| 322 | p := launch(t, m, l, "pass") |
| 323 | r := record(t, m, p) |
| 324 | if _, e := p.Wait(context.Background()); e != nil { |
| 325 | t.Fatal(e) |
| 326 | } |
| 327 | dir := filepath.Join(m.cfg.ScratchRoot, "attempt-"+r.ID) |
| 328 | nested := filepath.Join(dir, "cache", "nested") |
| 329 | if e := os.MkdirAll(nested, 0700); e != nil { |
| 330 | t.Fatal(e) |
| 331 | } |
| 332 | writableFixture(t, dir) |
| 333 | if e := os.WriteFile(filepath.Join(nested, "file"), []byte("owned"), 0400); e != nil { |
| 334 | t.Fatal(e) |
| 335 | } |
| 336 | outside := t.TempDir() |
| 337 | marker := filepath.Join(outside, "keep") |
| 338 | if e := os.WriteFile(marker, []byte("outside"), 0400); e != nil { |
| 339 | t.Fatal(e) |
| 340 | } |
| 341 | if e := os.Symlink(outside, filepath.Join(nested, "link")); e != nil { |
| 342 | t.Fatal(e) |
| 343 | } |
| 344 | if e := os.Chmod(outside, 0500); e != nil { |
| 345 | t.Fatal(e) |
| 346 | } |
| 347 | defer os.Chmod(outside, 0700) |
| 348 | for _, path := range []string{nested, filepath.Dir(nested), dir} { |
| 349 | if e := os.Chmod(path, 0500); e != nil { |
| 350 | t.Fatal(e) |
| 351 | } |
| 352 | } |
| 353 | proof, e := m.Recover(context.Background(), r) |
| 354 | if e != nil || !proof.Verified { |
| 355 | t.Fatalf("read-only recovery invalidated exit: %+v %v", proof, e) |
| 356 | } |
| 357 | if _, e := os.Stat(dir); !os.IsNotExist(e) { |
| 358 | t.Fatalf("scratch remains: %v", e) |
| 359 | } |
| 360 | info, e := os.Stat(outside) |
| 361 | if e != nil || info.Mode().Perm() != 0500 { |
| 362 | t.Fatalf("symlink target permissions changed: %v %v", info, e) |
| 363 | } |
| 364 | if b, e := os.ReadFile(marker); e != nil || string(b) != "outside" { |
| 365 | t.Fatalf("symlink target changed: %q %v", b, e) |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | func TestNamespaceCleanupFailureKeepsProof(t *testing.T) { |
| 370 | m, l := fixture(t) |
| 371 | p := launch(t, m, l, "pass") |
| 372 | r := record(t, m, p) |
| 373 | if _, e := p.Wait(context.Background()); e != nil { |
| 374 | t.Fatal(e) |
| 375 | } |
| 376 | want, e := m.Stop(context.Background(), r.ID) |
| 377 | if e != nil { |
| 378 | t.Fatal(e) |
| 379 | } |
| 380 | dir := filepath.Join(m.cfg.ScratchRoot, "attempt-"+r.ID) |
| 381 | if e := os.Mkdir(dir, 0700); e != nil { |
| 382 | t.Fatal(e) |
| 383 | } |
| 384 | if e := os.Chmod(m.cfg.ScratchRoot, 0500); e != nil { |
| 385 | t.Fatal(e) |
| 386 | } |
| 387 | defer os.Chmod(m.cfg.ScratchRoot, 0700) |
| 388 | for range 2 { |
| 389 | proof, e := m.Recover(context.Background(), r) |
| 390 | if !errors.Is(e, os.ErrPermission) || proof != want { |
| 391 | t.Fatalf("cleanup failure changed proof: %+v %v", proof, e) |
| 392 | } |
| 393 | } |
| 394 | if e := os.Chmod(m.cfg.ScratchRoot, 0700); e != nil { |
| 395 | t.Fatal(e) |
| 396 | } |
| 397 | proof, e := m.Recover(context.Background(), r) |
| 398 | if e != nil || proof != want { |
| 399 | t.Fatalf("cleanup retry: %+v %v", proof, e) |
| 400 | } |
| 401 | if _, e := os.Stat(dir); !os.IsNotExist(e) { |
| 402 | t.Fatalf("scratch remains: %v", e) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | func TestNamespaceRecoveryCrashBeforeExit(t *testing.T) { |
| 407 | m, l := fixture(t) |
| 408 | c := exec.Command(os.Args[0]) |
| 409 | c.Env = append(os.Environ(), "EQUIPO_NAMESPACE_PARENT_TEST=1", "EQUIPO_TEST_STATE="+m.cfg.ScratchRoot, "EQUIPO_TEST_WORKSPACE="+l.Workspace) |
| 410 | stdout, e := c.StdoutPipe() |
| 411 | if e != nil { |
| 412 | t.Fatal(e) |
| 413 | } |
| 414 | c.Stderr = os.Stderr |
| 415 | if e = c.Start(); e != nil { |
| 416 | t.Fatal(e) |
| 417 | } |
| 418 | t.Cleanup(func() { c.Process.Kill(); c.Wait() }) |
| 419 | var r Record |
| 420 | if e = json.NewDecoder(stdout).Decode(&r); e != nil { |
| 421 | t.Fatal(e) |
| 422 | } |
| 423 | fd := descriptor(t, r.HostPID) |
| 424 | t.Cleanup(func() { unix.PidfdSendSignal(fd, unix.SIGKILL, nil, 0) }) |
| 425 | if e = c.Process.Kill(); e != nil { |
| 426 | t.Fatal(e) |
| 427 | } |
| 428 | c.Wait() |
| 429 | exited(t, fd) |
| 430 | deadline := time.Now().Add(5 * time.Second) |
| 431 | for { |
| 432 | probe, err := unix.PidfdOpen(r.HostPID, 0) |
| 433 | if err == unix.ESRCH { |
| 434 | break |
| 435 | } |
| 436 | // EINVAL is the kernel part way through the reap: the process is |
| 437 | // unhashed and its number not yet free. Soon it is ESRCH. |
| 438 | if err != nil && err != unix.EINVAL { |
| 439 | t.Fatal(err) |
| 440 | } |
| 441 | if err == nil { |
| 442 | unix.Close(probe) |
| 443 | } |
| 444 | if time.Now().After(deadline) { |
| 445 | t.Fatalf("namespace init was not reaped: %v", err) |
| 446 | } |
| 447 | time.Sleep(time.Millisecond) |
| 448 | } |
| 449 | if _, e := os.Stat(filepath.Join(m.cfg.ScratchRoot, r.ID+".exit.json")); !os.IsNotExist(e) { |
| 450 | t.Fatalf("crash fixture has exit proof: %v", e) |
| 451 | } |
| 452 | assertUnknownRecovery(t, m, r) |
| 453 | } |
| 454 | |
| 455 | func TestNamespaceRecoveryReusedPID(t *testing.T) { |
| 456 | for _, kind := range []string{"ticks", "namespace"} { |
| 457 | t.Run(kind, func(t *testing.T) { |
| 458 | m, l := fixture(t) |
| 459 | p := launch(t, m, l, "pass") |
| 460 | r := record(t, m, p) |
| 461 | if _, e := p.Wait(context.Background()); e != nil { |
| 462 | t.Fatal(e) |
| 463 | } |
| 464 | if e := os.Remove(filepath.Join(m.cfg.ScratchRoot, r.ID+".exit.json")); e != nil { |
| 465 | t.Fatal(e) |
| 466 | } |
| 467 | control, fd := ownedControl(t) |
| 468 | r.HostPID = control.Process.Pid |
| 469 | ticks, ns, e := identity(r.HostPID) |
| 470 | if e != nil { |
| 471 | t.Fatal(e) |
| 472 | } |
| 473 | r.StartTicks, r.NamespaceID = ticks, ns |
| 474 | if kind == "ticks" { |
| 475 | r.StartTicks++ |
| 476 | } else { |
| 477 | r.NamespaceID = "pid:[1]" |
| 478 | } |
| 479 | if e := m.persist(r); e != nil { |
| 480 | t.Fatal(e) |
| 481 | } |
| 482 | assertUnknownRecovery(t, m, r) |
| 483 | alive(t, fd) |
| 484 | }) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | func assertUnknownRecovery(t *testing.T, m *Manager, r Record) { |
| 489 | t.Helper() |
| 490 | proof, e := m.Recover(context.Background(), r) |
| 491 | if e != nil || !proof.Verified || proof.Record != r || proof.Exit.Code != -1 || proof.Exit.Signal != 0 || proof.Exit.ObservedAt.IsZero() { |
| 492 | t.Fatalf("absent init not recovered with unknown outcome: %+v %v", proof, e) |
| 493 | } |
| 494 | again, e := m.Recover(context.Background(), r) |
| 495 | if e != nil || again != proof { |
| 496 | t.Fatalf("recovery proof not durable: %+v %v", again, e) |
| 497 | } |
| 498 | if _, e := os.Stat(filepath.Join(m.cfg.ScratchRoot, "attempt-"+r.ID)); !os.IsNotExist(e) { |
| 499 | t.Fatalf("scratch remains: %v", e) |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | func TestNamespaceRecoveryMalformedRecord(t *testing.T) { |
| 504 | for _, kind := range []string{"namespace", "id"} { |
| 505 | t.Run(kind, func(t *testing.T) { |
| 506 | m, l := fixture(t) |
| 507 | p := launch(t, m, l, "pass") |
| 508 | r := record(t, m, p) |
| 509 | if _, e := p.Wait(context.Background()); e != nil { |
| 510 | t.Fatal(e) |
| 511 | } |
| 512 | if e := os.Remove(filepath.Join(m.cfg.ScratchRoot, r.ID+".exit.json")); e != nil { |
| 513 | t.Fatal(e) |
| 514 | } |
| 515 | if kind == "namespace" { |
| 516 | r.NamespaceID = "invalid" |
| 517 | } else { |
| 518 | r.ID = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
| 519 | } |
| 520 | if e := m.persist(r); e != nil { |
| 521 | t.Fatal(e) |
| 522 | } |
| 523 | if proof, e := m.Recover(context.Background(), r); !errors.Is(e, errIdentity) || proof.Verified { |
| 524 | t.Fatalf("malformed record accepted: %+v %v", proof, e) |
| 525 | } |
| 526 | }) |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | func TestDirectInterruptedIntentOnly(t *testing.T) { |
| 531 | root := t.TempDir() |
| 532 | boot, e := bootID() |
| 533 | if e != nil { |
| 534 | t.Fatal(e) |
| 535 | } |
| 536 | m := &Manager{cfg: Config{ScratchRoot: root}, processes: map[string]*process{}} |
| 537 | r := Record{ID: "abcdefabcdefabcdefabcdefabcdefab", AttemptKey: "intent", BootID: boot, ManifestHash: "manifest", PolicyHash: "policy"} |
| 538 | if e = m.persist(r); e != nil { |
| 539 | t.Fatal(e) |
| 540 | } |
| 541 | dir := filepath.Join(root, "attempt-"+r.ID) |
| 542 | if e = os.Mkdir(dir, 0700); e != nil { |
| 543 | t.Fatal(e) |
| 544 | } |
| 545 | proof, e := m.Recover(context.Background(), r) |
| 546 | if e != nil || !proof.Verified || proof.Exit.Code != -1 { |
| 547 | t.Fatalf("intent-only restart blocked: %+v %v", proof, e) |
| 548 | } |
| 549 | if _, e = os.Stat(dir); !os.IsNotExist(e) { |
| 550 | t.Fatal("intent scratch retained", e) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | func TestDirectInterruptedParentLifecycle(t *testing.T) { |
| 555 | root := t.TempDir() |
| 556 | boot, e := bootID() |
| 557 | if e != nil { |
| 558 | t.Fatal(e) |
| 559 | } |
| 560 | parent := exec.Command("/bin/sleep", "60") |
| 561 | if e = parent.Start(); e != nil { |
| 562 | t.Fatal(e) |
| 563 | } |
| 564 | defer parent.Process.Kill() |
| 565 | defer parent.Wait() |
| 566 | ticks, _, e := identity(parent.Process.Pid) |
| 567 | if e != nil { |
| 568 | t.Fatal(e) |
| 569 | } |
| 570 | m := &Manager{cfg: Config{ScratchRoot: root}, processes: map[string]*process{}} |
| 571 | r := Record{ID: "abcdefabcdefabcdefabcdefabcdefab", AttemptKey: "intent", BootID: boot, ManifestHash: "manifest", PolicyHash: "policy", ParentPID: parent.Process.Pid, ParentTicks: ticks} |
| 572 | if e = m.persist(r); e != nil { |
| 573 | t.Fatal(e) |
| 574 | } |
| 575 | dir := filepath.Join(root, "attempt-"+r.ID) |
| 576 | if e = os.Mkdir(dir, 0700); e != nil { |
| 577 | t.Fatal(e) |
| 578 | } |
| 579 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond) |
| 580 | defer cancel() |
| 581 | if proof, e := m.Recover(ctx, r); e == nil || proof.Verified { |
| 582 | t.Fatal("cleanup preceded parent death") |
| 583 | } |
| 584 | if _, e = os.Stat(dir); e != nil { |
| 585 | t.Fatal("live lifecycle scratch deleted") |
| 586 | } |
| 587 | parent.Process.Kill() |
| 588 | parent.Wait() |
| 589 | if proof, e := m.Recover(context.Background(), r); e != nil || !proof.Verified { |
| 590 | t.Fatalf("dead parent blocks recovery: %+v %v", proof, e) |
| 591 | } |
| 592 | } |
| 593 | |