| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/release" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | ) |
| 15 | |
| 16 | // TestRenderKeepsTheCloseError (task 10, the task 6/8 gap): a render that |
| 17 | // failed and whose sandbox also could not be shown to have ended keeps |
| 18 | // both: its code for the reply, and ErrNotEnded for the health, which |
| 19 | // then stops every later render. |
| 20 | func TestRenderKeepsTheCloseError(t *testing.T) { |
| 21 | failed := &Error{Code: CodeCrashed, Detail: "the browser's pipe ended"} |
| 22 | notEnded := fmt.Errorf("closing: %w", ErrNotEnded) |
| 23 | err := withClose(failed, notEnded) |
| 24 | if !errors.Is(err, ErrNotEnded) || ErrorCode(err) != CodeCrashed { |
| 25 | t.Fatalf("a failed render that did not end: %v (code %q)", err, ErrorCode(err)) |
| 26 | } |
| 27 | if err := withClose(nil, notEnded); !errors.Is(err, ErrNotEnded) { |
| 28 | t.Fatalf("a render that did not end: %v", err) |
| 29 | } |
| 30 | if err := withClose(failed, nil); err != failed { |
| 31 | t.Fatalf("a failed render that ended: %v", err) |
| 32 | } |
| 33 | if err := withClose(nil, nil); err != nil { |
| 34 | t.Fatalf("a render that ended: %v", err) |
| 35 | } |
| 36 | r := newHealthRig(t) |
| 37 | r.settle(t) |
| 38 | r.h.Ended(err) |
| 39 | r.h.Ended(withClose(failed, notEnded)) |
| 40 | if tree, health, err := r.h.Ready(context.Background()); tree != "" || health != wire.ToolMissing || !errors.Is(err, ErrNotEnded) { |
| 41 | t.Fatalf("after a failed render that did not end: %q %q %v", tree, health, err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // TestRenderStartupTimeoutKeepsNotEnded (branch review finding 3): a |
| 46 | // launch that fails once the call's deadline has passed is the call's |
| 47 | // timeout, and still carries ErrNotEnded when its sandbox could not be |
| 48 | // shown to have ended, through Render's own startup path, so the health |
| 49 | // stops every later render. A startup timeout whose sandbox ended is the |
| 50 | // timeout alone; a launch refused before the deadline is unchanged. |
| 51 | func TestRenderStartupTimeoutKeepsNotEnded(t *testing.T) { |
| 52 | defer func(l func(context.Context, LaunchConfig) (*Sandbox, error), d time.Duration) { |
| 53 | launch, callTimeout = l, d |
| 54 | }(launch, callTimeout) |
| 55 | callTimeout = 20 * time.Millisecond |
| 56 | pages := []Page{{Path: "/", Viewport: Desktop}} |
| 57 | out := func() string { |
| 58 | dir, err := filepath.EvalSymlinks(t.TempDir()) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if err := os.Chmod(dir, 0o700); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | dir = filepath.Join(dir, "out") |
| 66 | if err := os.Mkdir(dir, 0o700); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | return dir |
| 70 | } |
| 71 | launch = func(ctx context.Context, _ LaunchConfig) (*Sandbox, error) { |
| 72 | <-ctx.Done() |
| 73 | return nil, errors.Join(errors.New("render: bubblewrap did not report its sandbox"), ctx.Err(), ErrNotEnded) |
| 74 | } |
| 75 | report, err := Render(context.Background(), LaunchConfig{}, out(), pages) |
| 76 | if report != nil || ErrorCode(err) != CodeTimeout || !errors.Is(err, ErrNotEnded) { |
| 77 | t.Fatalf("a startup timeout that did not end: %v %v (code %q)", report, err, ErrorCode(err)) |
| 78 | } |
| 79 | r := newHealthRig(t) |
| 80 | r.settle(t) |
| 81 | r.h.Ended(err) |
| 82 | if tree, health, rerr := r.h.Ready(context.Background()); tree != "" || health != wire.ToolMissing || !errors.Is(rerr, ErrNotEnded) { |
| 83 | t.Fatalf("after a startup timeout that did not end: %q %q %v", tree, health, rerr) |
| 84 | } |
| 85 | launch = func(ctx context.Context, _ LaunchConfig) (*Sandbox, error) { |
| 86 | <-ctx.Done() |
| 87 | return nil, errors.Join(errors.New("render: bubblewrap did not start"), ctx.Err()) |
| 88 | } |
| 89 | if _, err := Render(context.Background(), LaunchConfig{}, out(), pages); ErrorCode(err) != CodeTimeout || errors.Is(err, ErrNotEnded) { |
| 90 | t.Fatalf("a startup timeout that ended: %v", err) |
| 91 | } |
| 92 | refused := &release.RendererError{Health: wire.ToolMismatch, Reason: "the tree does not verify"} |
| 93 | launch = func(context.Context, LaunchConfig) (*Sandbox, error) { return nil, refused } |
| 94 | if _, err := Render(context.Background(), LaunchConfig{}, out(), pages); err != refused { |
| 95 | t.Fatalf("a refused launch: %v", err) |
| 96 | } |
| 97 | } |
| 98 | |