| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "sync" |
| 10 | "sync/atomic" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/release" |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // healthRig is a Health over a real tree directory, with VerifyRenderer |
| 19 | // and the fixture render faked: verify returns verifyErr, the fixture |
| 20 | // fixtureErr (after waiting on gate when it is set). |
| 21 | type healthRig struct { |
| 22 | h *Health |
| 23 | tree string |
| 24 | mu sync.Mutex |
| 25 | verifyErr error |
| 26 | fixtureErr error |
| 27 | gate chan struct{} |
| 28 | verifies atomic.Int32 |
| 29 | fixtures atomic.Int32 |
| 30 | changed atomic.Int32 |
| 31 | configs []LaunchConfig |
| 32 | } |
| 33 | |
| 34 | const healthPin = "8f27c7adf358cd4c9f31fb16a06449ecf67696864f35b54005574b0a9ce8dae8" |
| 35 | |
| 36 | func newHealthRig(t *testing.T) *healthRig { |
| 37 | t.Helper() |
| 38 | exe := filepath.Join(t.TempDir(), "equipo-runner") |
| 39 | cfg, scratch := Installed("/usr/bin/bwrap", exe, healthPin) |
| 40 | r := &healthRig{tree: cfg.Tree} |
| 41 | if err := os.MkdirAll(cfg.Tree, 0o755); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | r.h = &Health{Config: cfg, Scratch: scratch, |
| 45 | Verify: func(tree, pin string) error { |
| 46 | r.verifies.Add(1) |
| 47 | if tree != cfg.Tree || pin != healthPin { |
| 48 | return fmt.Errorf("verified %s against %s", tree, pin) |
| 49 | } |
| 50 | r.mu.Lock() |
| 51 | defer r.mu.Unlock() |
| 52 | return r.verifyErr |
| 53 | }, |
| 54 | Fixture: func(ctx context.Context, c LaunchConfig, s string) error { |
| 55 | r.fixtures.Add(1) |
| 56 | r.mu.Lock() |
| 57 | r.configs = append(r.configs, c) |
| 58 | gate, err := r.gate, r.fixtureErr |
| 59 | r.mu.Unlock() |
| 60 | if s != scratch { |
| 61 | return fmt.Errorf("scratch %s", s) |
| 62 | } |
| 63 | if gate != nil { |
| 64 | select { |
| 65 | case <-gate: |
| 66 | case <-ctx.Done(): |
| 67 | return ctx.Err() |
| 68 | } |
| 69 | } |
| 70 | return err |
| 71 | }, |
| 72 | Changed: func() { r.changed.Add(1) }, |
| 73 | } |
| 74 | return r |
| 75 | } |
| 76 | |
| 77 | func (r *healthRig) set(verify, fixture error) { |
| 78 | r.mu.Lock() |
| 79 | r.verifyErr, r.fixtureErr = verify, fixture |
| 80 | r.mu.Unlock() |
| 81 | } |
| 82 | |
| 83 | // settle starts the check and waits for its end (Ready waits on a |
| 84 | // running check before its own). |
| 85 | func (r *healthRig) settle(t *testing.T) { |
| 86 | t.Helper() |
| 87 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 88 | defer cancel() |
| 89 | r.h.Start(ctx) |
| 90 | r.h.wait(ctx) |
| 91 | } |
| 92 | |
| 93 | // TestHealthReportsEachState (task 8): the renderer's health is ok only |
| 94 | // when the tree verifies and has rendered the fixture page; otherwise |
| 95 | // the state VerifyRenderer's error gives (anything unexpected is a |
| 96 | // mismatch), never ok. |
| 97 | func TestHealthReportsEachState(t *testing.T) { |
| 98 | for _, c := range []struct { |
| 99 | name string |
| 100 | verify error |
| 101 | want string |
| 102 | }{ |
| 103 | {"ok", nil, wire.ToolHealthy}, |
| 104 | {"missing", &release.RendererError{Health: wire.ToolMissing, Reason: "absent"}, wire.ToolMissing}, |
| 105 | {"mismatch", &release.RendererError{Health: wire.ToolMismatch, Path: "usr/lib/x", Reason: "hash"}, wire.ToolMismatch}, |
| 106 | {"unpinned", &release.RendererError{Health: wire.ToolUnpinned, Reason: "no pin"}, wire.ToolUnpinned}, |
| 107 | {"unexpected", errors.New("disk on fire"), wire.ToolMismatch}, |
| 108 | } { |
| 109 | t.Run(c.name, func(t *testing.T) { |
| 110 | r := newHealthRig(t) |
| 111 | r.set(c.verify, nil) |
| 112 | r.settle(t) |
| 113 | if got := r.h.Report(context.Background()); got != c.want { |
| 114 | t.Fatalf("reported %q, want %q", got, c.want) |
| 115 | } |
| 116 | tree, health, err := r.h.Ready(context.Background()) |
| 117 | if health != c.want || (c.want == wire.ToolHealthy) != (err == nil) || (c.want == wire.ToolHealthy) != (tree == r.tree) { |
| 118 | t.Fatalf("ready: %q %q %v", tree, health, err) |
| 119 | } |
| 120 | if c.verify != nil && r.fixtures.Load() != 0 { |
| 121 | t.Fatalf("a tree that does not verify was rendered") |
| 122 | } |
| 123 | if c.want == wire.ToolHealthy && (len(r.configs) != 1 || r.configs[0] != r.h.Config) { |
| 124 | t.Fatalf("fixture rendered with %+v", r.configs) |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // TestHealthVerifiedButNotRenderedIsNotOK: a tree that verifies but |
| 131 | // cannot render the fixture page reports missing (as a host that cannot |
| 132 | // nest the browser's sandbox does) and Ready refuses it. The render is |
| 133 | // not tried again for the same tree: the runner needs updating. |
| 134 | func TestHealthVerifiedButNotRenderedIsNotOK(t *testing.T) { |
| 135 | r := newHealthRig(t) |
| 136 | r.set(nil, errors.New("the browser's sandbox did not start")) |
| 137 | r.settle(t) |
| 138 | if got := r.h.Report(context.Background()); got != wire.ToolMissing { |
| 139 | t.Fatalf("reported %q", got) |
| 140 | } |
| 141 | r.h.wait(context.Background()) |
| 142 | tree, health, err := r.h.Ready(context.Background()) |
| 143 | if tree != "" || health != wire.ToolMissing || !errors.Is(err, ErrNotChecked) { |
| 144 | t.Fatalf("ready: %q %q %v", tree, health, err) |
| 145 | } |
| 146 | // Even once it would render, the same tree is not tried again. |
| 147 | r.set(nil, nil) |
| 148 | if _, health, _ := r.h.Ready(context.Background()); health != wire.ToolMissing || r.fixtures.Load() != 1 { |
| 149 | t.Fatalf("tried again: %q after %d renders", health, r.fixtures.Load()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // TestHealthNotEndedNeedsAttention: a fixture whose sandbox could not be |
| 154 | // shown to have ended is never ok and never tried again, not even with |
| 155 | // another tree. |
| 156 | func TestHealthNotEndedNeedsAttention(t *testing.T) { |
| 157 | r := newHealthRig(t) |
| 158 | r.set(nil, fmt.Errorf("closing: %w", ErrNotEnded)) |
| 159 | r.settle(t) |
| 160 | replaceTree(t, r.tree) |
| 161 | r.set(nil, nil) |
| 162 | tree, health, err := r.h.Ready(context.Background()) |
| 163 | if tree != "" || health != wire.ToolMissing || !errors.Is(err, ErrNotEnded) || r.fixtures.Load() != 1 { |
| 164 | t.Fatalf("ready: %q %q %v after %d renders", tree, health, err, r.fixtures.Load()) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // TestHealthRenderNotEndedNeedsAttention: a render after the fixture |
| 169 | // whose sandbox could not be shown to have ended stops every later one; |
| 170 | // another render's error does not. |
| 171 | func TestHealthRenderNotEndedNeedsAttention(t *testing.T) { |
| 172 | r := newHealthRig(t) |
| 173 | r.settle(t) |
| 174 | r.h.Ended(nil) |
| 175 | r.h.Ended(&Error{Code: CodeTimeout}) |
| 176 | if _, health, err := r.h.Ready(context.Background()); health != wire.ToolHealthy || err != nil { |
| 177 | t.Fatalf("after a timeout: %q %v", health, err) |
| 178 | } |
| 179 | r.h.Ended(fmt.Errorf("render: %w", ErrNotEnded)) |
| 180 | if got := r.h.Report(context.Background()); got != wire.ToolMissing || r.changed.Load() != 2 { |
| 181 | t.Fatalf("reported %q, changed %d", got, r.changed.Load()) |
| 182 | } |
| 183 | r.h.wait(context.Background()) |
| 184 | if tree, health, err := r.h.Ready(context.Background()); tree != "" || health != wire.ToolMissing || !errors.Is(err, ErrNotEnded) { |
| 185 | t.Fatalf("after a render that did not end: %q %q %v", tree, health, err) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // TestHealthStartsInTheBackground: Start does not wait for the check; |
| 190 | // until the fixture has rendered the report is not ok, and once it has, |
| 191 | // Changed hears it (the next hello says ok). |
| 192 | func TestHealthStartsInTheBackground(t *testing.T) { |
| 193 | r := newHealthRig(t) |
| 194 | r.gate = make(chan struct{}) |
| 195 | done := make(chan struct{}) |
| 196 | go func() { |
| 197 | r.h.Start(context.Background()) |
| 198 | close(done) |
| 199 | }() |
| 200 | select { |
| 201 | case <-done: |
| 202 | case <-time.After(5 * time.Second): |
| 203 | t.Fatal("Start waited for the fixture render") |
| 204 | } |
| 205 | if got := r.h.Report(context.Background()); got != wire.ToolMissing { |
| 206 | t.Fatalf("reported %q before the fixture rendered", got) |
| 207 | } |
| 208 | close(r.gate) |
| 209 | r.h.wait(context.Background()) |
| 210 | if r.changed.Load() != 1 { |
| 211 | t.Fatalf("changed %d times", r.changed.Load()) |
| 212 | } |
| 213 | if got := r.h.Report(context.Background()); got != wire.ToolHealthy { |
| 214 | t.Fatalf("reported %q", got) |
| 215 | } |
| 216 | // Report checks again in the background; the same state says nothing. |
| 217 | r.h.wait(context.Background()) |
| 218 | if r.changed.Load() != 1 || r.verifies.Load() < 2 { |
| 219 | t.Fatalf("changed %d times, verified %d", r.changed.Load(), r.verifies.Load()) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // TestHealthRechecksTheFilesBeforeEachRender: Ready verifies the tree |
| 224 | // every time; once it no longer verifies it is not ok, and the report |
| 225 | // changes with it. |
| 226 | func TestHealthRechecksTheFilesBeforeEachRender(t *testing.T) { |
| 227 | r := newHealthRig(t) |
| 228 | r.settle(t) |
| 229 | for range 3 { |
| 230 | if _, health, err := r.h.Ready(context.Background()); health != wire.ToolHealthy || err != nil { |
| 231 | t.Fatalf("ready %q %v", health, err) |
| 232 | } |
| 233 | } |
| 234 | if r.verifies.Load() != 4 || r.fixtures.Load() != 1 { |
| 235 | t.Fatalf("verified %d times, rendered %d", r.verifies.Load(), r.fixtures.Load()) |
| 236 | } |
| 237 | r.set(&release.RendererError{Health: wire.ToolMismatch, Path: "MANIFEST", Reason: "tampered"}, nil) |
| 238 | tree, health, err := r.h.Ready(context.Background()) |
| 239 | if tree != "" || health != wire.ToolMismatch || !errors.Is(err, release.ErrRendererMismatch) { |
| 240 | t.Fatalf("ready: %q %q %v", tree, health, err) |
| 241 | } |
| 242 | if got := r.h.Report(context.Background()); got != wire.ToolMismatch || r.changed.Load() != 2 { |
| 243 | t.Fatalf("reported %q, changed %d", got, r.changed.Load()) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // TestHealthIsPerTree: the fixture rendered with one tree says nothing |
| 248 | // about another at the same path (an update put it there): that one is |
| 249 | // not ok until it has rendered too. |
| 250 | func TestHealthIsPerTree(t *testing.T) { |
| 251 | r := newHealthRig(t) |
| 252 | r.settle(t) |
| 253 | replaceTree(t, r.tree) |
| 254 | r.set(nil, errors.New("crashed")) |
| 255 | if _, health, _ := r.h.Ready(context.Background()); health != wire.ToolMissing || r.fixtures.Load() != 2 { |
| 256 | t.Fatalf("another tree: %q after %d renders", health, r.fixtures.Load()) |
| 257 | } |
| 258 | // A tree replaced while the fixture renders is not the one rendered. |
| 259 | r = newHealthRig(t) |
| 260 | var once sync.Once |
| 261 | r.h.Fixture = func(context.Context, LaunchConfig, string) error { |
| 262 | once.Do(func() { replaceTree(t, r.tree) }) |
| 263 | return nil |
| 264 | } |
| 265 | r.settle(t) |
| 266 | if got := r.h.Report(context.Background()); got == wire.ToolHealthy { |
| 267 | t.Fatal("a tree replaced during its fixture render is ok") |
| 268 | } |
| 269 | // The tree now there renders once on its own, and then it is ok. |
| 270 | r.h.wait(context.Background()) |
| 271 | if _, health, _ := r.h.Ready(context.Background()); health != wire.ToolHealthy { |
| 272 | t.Fatalf("the new tree: %q", health) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // TestHealthTreeReplacedWhileVerified: a tree replaced while it is |
| 277 | // verified is not the one verified: a mismatch, never rendered. |
| 278 | func TestHealthTreeReplacedWhileVerified(t *testing.T) { |
| 279 | r := newHealthRig(t) |
| 280 | r.h.Verify = func(string, string) error { |
| 281 | replaceTree(t, r.tree) |
| 282 | return nil |
| 283 | } |
| 284 | if _, health, err := r.h.Ready(context.Background()); health != wire.ToolMismatch || err == nil || r.fixtures.Load() != 0 { |
| 285 | t.Fatalf("ready %q %v after %d renders", health, err, r.fixtures.Load()) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // TestHealthLaunchRefusalKeepsItsState: a fixture render that Launch |
| 290 | // refuses because the tree no longer verifies says that state. |
| 291 | func TestHealthLaunchRefusalKeepsItsState(t *testing.T) { |
| 292 | r := newHealthRig(t) |
| 293 | r.set(nil, &release.RendererError{Health: wire.ToolMismatch, Path: "usr/bin/x", Reason: "hash"}) |
| 294 | if _, health, err := r.h.Ready(context.Background()); health != wire.ToolMismatch || !errors.Is(err, ErrNotChecked) { |
| 295 | t.Fatalf("ready %q %v", health, err) |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // TestHealthCancelledRenderIsTriedAgain: a fixture render cut short (the |
| 300 | // runner stopping, a render call cancelled) was not the tree's try. |
| 301 | func TestHealthCancelledRenderIsTriedAgain(t *testing.T) { |
| 302 | r := newHealthRig(t) |
| 303 | r.gate = make(chan struct{}) |
| 304 | ctx, cancel := context.WithCancel(context.Background()) |
| 305 | cancel() |
| 306 | if health, _ := r.h.run(ctx); health != wire.ToolMissing { |
| 307 | t.Fatalf("cancelled: %q", health) |
| 308 | } |
| 309 | close(r.gate) |
| 310 | if _, health, err := r.h.Ready(context.Background()); health != wire.ToolHealthy || err != nil || r.fixtures.Load() != 2 { |
| 311 | t.Fatalf("after a cancelled render: %q %v, %d renders", health, err, r.fixtures.Load()) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // TestHealthOnlyOnLinuxAmd64: the renderer is Linux amd64's; a nil |
| 316 | // Health (a Mac or arm64 runner has none) reports nothing and is never |
| 317 | // ready. |
| 318 | func TestHealthOnlyOnLinuxAmd64(t *testing.T) { |
| 319 | for _, p := range [][2]string{{"darwin", "arm64"}, {"linux", "arm64"}, {"darwin", "amd64"}} { |
| 320 | if Platform(p[0], p[1]) { |
| 321 | t.Fatalf("%s/%s has a renderer", p[0], p[1]) |
| 322 | } |
| 323 | } |
| 324 | if !Platform("linux", "amd64") { |
| 325 | t.Fatal("linux/amd64 has no renderer") |
| 326 | } |
| 327 | var h *Health |
| 328 | h.Start(context.Background()) |
| 329 | if got := h.Report(context.Background()); got != "" { |
| 330 | t.Fatalf("no renderer reported %q", got) |
| 331 | } |
| 332 | if tree, health, err := h.Ready(context.Background()); tree != "" || health != wire.ToolMissing || err == nil { |
| 333 | t.Fatalf("no renderer ready: %q %q %v", tree, health, err) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // replaceTree puts another directory at tree, as an install's rename |
| 338 | // does. |
| 339 | func replaceTree(t *testing.T, tree string) { |
| 340 | t.Helper() |
| 341 | next := tree + ".next" |
| 342 | if err := os.Mkdir(next, 0o755); err != nil { |
| 343 | t.Fatal(err) |
| 344 | } |
| 345 | if err := os.Rename(tree, tree+".old"); err != nil { |
| 346 | t.Fatal(err) |
| 347 | } |
| 348 | if err := os.Rename(next, tree); err != nil { |
| 349 | t.Fatal(err) |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // TestHealthTriesAFailedFixtureAgainAfterTenMinutes (task 10): a failed |
| 354 | // fixture render (a loaded box timing it out, say) does not stick until |
| 355 | // the runner restarts. Once the failure is more than FixtureRetry old, |
| 356 | // the next check, Ready's or a report's, renders it once more; within |
| 357 | // that it is not tried again. A sandbox that did not end still never is. |
| 358 | func TestHealthTriesAFailedFixtureAgainAfterTenMinutes(t *testing.T) { |
| 359 | r := newHealthRig(t) |
| 360 | var mu sync.Mutex |
| 361 | clock := time.Date(2026, 11, 28, 9, 0, 0, 0, time.UTC) |
| 362 | r.h.Now = func() time.Time { mu.Lock(); defer mu.Unlock(); return clock } |
| 363 | advance := func(d time.Duration) { mu.Lock(); clock = clock.Add(d); mu.Unlock() } |
| 364 | r.set(nil, errors.New("the render ran out of time")) |
| 365 | r.settle(t) |
| 366 | ready := func() (string, error) { |
| 367 | t.Helper() |
| 368 | _, health, err := r.h.Ready(context.Background()) |
| 369 | return health, err |
| 370 | } |
| 371 | advance(FixtureRetry - time.Second) |
| 372 | if health, err := ready(); health != wire.ToolMissing || !errors.Is(err, ErrNotChecked) || r.fixtures.Load() != 1 { |
| 373 | t.Fatalf("within ten minutes: %q %v after %d renders", health, err, r.fixtures.Load()) |
| 374 | } |
| 375 | advance(2 * time.Second) |
| 376 | if health, _ := ready(); health != wire.ToolMissing || r.fixtures.Load() != 2 { |
| 377 | t.Fatalf("past ten minutes: %q after %d renders", health, r.fixtures.Load()) |
| 378 | } |
| 379 | // The failure just now starts the wait again. |
| 380 | if health, _ := ready(); health != wire.ToolMissing || r.fixtures.Load() != 2 { |
| 381 | t.Fatalf("right after the second try: %q after %d renders", health, r.fixtures.Load()) |
| 382 | } |
| 383 | // A report's background check tries too, once the wait is over. |
| 384 | r.set(nil, nil) |
| 385 | advance(FixtureRetry + time.Second) |
| 386 | r.h.Start(context.Background()) // settle's context has ended |
| 387 | r.h.wait(context.Background()) |
| 388 | if got := r.h.Report(context.Background()); got != wire.ToolHealthy || r.fixtures.Load() != 3 { |
| 389 | t.Fatalf("a report's check past ten minutes: %q after %d renders", got, r.fixtures.Load()) |
| 390 | } |
| 391 | // A sandbox that did not end is never tried again, however long ago. |
| 392 | r2 := newHealthRig(t) |
| 393 | r2.h.Now = r.h.Now |
| 394 | r2.set(nil, fmt.Errorf("closing: %w", ErrNotEnded)) |
| 395 | r2.settle(t) |
| 396 | r2.set(nil, nil) |
| 397 | advance(10 * FixtureRetry) |
| 398 | if _, health, err := r2.h.Ready(context.Background()); health != wire.ToolMissing || !errors.Is(err, ErrNotEnded) || r2.fixtures.Load() != 1 { |
| 399 | t.Fatalf("not ended, much later: %q %v after %d renders", health, err, r2.fixtures.Load()) |
| 400 | } |
| 401 | } |
| 402 | |