| 1 | //go:build unix |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | // What the approval check does when git stops answering, run against a |
| 6 | // stand-in for git on the path. |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "errors" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "slices" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | "syscall" |
| 17 | "testing" |
| 18 | ) |
| 19 | |
| 20 | // standInGit puts script on the path as git, in a directory of its own, |
| 21 | // which the script finds as $STAND_IN. |
| 22 | func standInGit(t *testing.T, script string) string { |
| 23 | dir := t.TempDir() |
| 24 | if err := os.WriteFile(filepath.Join(dir, "git"), []byte("#!/bin/sh\n"+script), 0o755); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | t.Setenv("STAND_IN", dir) |
| 28 | t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 29 | return dir |
| 30 | } |
| 31 | |
| 32 | // A context that ends while a read waits on git stops the read, and git is |
| 33 | // killed and reaped. |
| 34 | func TestAnApprovalCheckStopsWhenItsContextEnds(t *testing.T) { |
| 35 | f := newApprovalFixture(t) |
| 36 | q := f.approved(f.created(f.head()), 1) |
| 37 | dir := standInGit(t, `echo $$ > "$STAND_IN/pid" |
| 38 | read -r line |
| 39 | echo "$line" > "$STAND_IN/asked" |
| 40 | exec sleep 1000 |
| 41 | `) |
| 42 | if err := syscall.Mkfifo(filepath.Join(dir, "asked"), 0o600); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | ctx, cancel := context.WithCancel(context.Background()) |
| 46 | defer cancel() |
| 47 | done := make(chan approvalResult, 1) |
| 48 | go func() { done <- verifyApproval(ctx, f.repo, q) }() |
| 49 | // The stand-in writes the command it read once it holds it, and then |
| 50 | // answers nothing. |
| 51 | asked, err := os.ReadFile(filepath.Join(dir, "asked")) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if !strings.HasPrefix(string(asked), "info ") { |
| 56 | t.Fatalf("git was asked %q", asked) |
| 57 | } |
| 58 | cancel() |
| 59 | if _, refusal, err := outcome(t, <-done); !errors.Is(err, context.Canceled) || refusal != nil { |
| 60 | t.Fatalf("refusal %v, err %v, want the context's end", refusal, err) |
| 61 | } |
| 62 | text, err := os.ReadFile(filepath.Join(dir, "pid")) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | pid, err := strconv.Atoi(strings.TrimSpace(string(text))) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if err := syscall.Kill(pid, 0); !errors.Is(err, syscall.ESRCH) { |
| 71 | t.Errorf("git, process %d, is not reaped: kill answers %v", pid, err) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // A git that exits without answering is an error carrying what it wrote |
| 76 | // to stderr. |
| 77 | func TestAnApprovalCheckSaysWhatAFailingGitSaid(t *testing.T) { |
| 78 | f := newApprovalFixture(t) |
| 79 | q := f.approved(f.created(f.head()), 1) |
| 80 | standInGit(t, `echo "fatal: this git reads no batch commands" >&2 |
| 81 | exit 129 |
| 82 | `) |
| 83 | _, refusal, err := verify(f, q) |
| 84 | if err == nil || refusal != nil || !strings.Contains(err.Error(), "this git reads no batch commands") { |
| 85 | t.Fatalf("refusal %v, err %v, want git's stderr", refusal, err) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Each way of failing to start git closes every descriptor it opened: no |
| 90 | // pipe can be made, one pipe is made and the second cannot be, and git |
| 91 | // does not start. |
| 92 | func TestAFailedStartClosesWhatItOpened(t *testing.T) { |
| 93 | missing := filepath.Join(t.TempDir(), "absent") |
| 94 | var limit syscall.Rlimit |
| 95 | if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | // A first pipe sets up the runtime's poller, which holds descriptors |
| 99 | // of its own from then on. |
| 100 | r, w, err := os.Pipe() |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | _, _ = r.Close(), w.Close() |
| 105 | for _, c := range []struct { |
| 106 | name string |
| 107 | free int // descriptor numbers left below the limit, or -1 for no limit |
| 108 | }{{"no pipe", 0}, {"one pipe of two", 2}, {"git does not start", -1}} { |
| 109 | t.Run(c.name, func(t *testing.T) { |
| 110 | before := openDescriptors(t) |
| 111 | if c.free >= 0 { |
| 112 | lowered := limit |
| 113 | lowered.Cur = uint64(unused(before, c.free)) |
| 114 | if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lowered); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | } |
| 118 | o, err := openObjectReader(context.Background(), missing) |
| 119 | if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if err == nil { |
| 123 | o.close() |
| 124 | t.Fatal("git started") |
| 125 | } |
| 126 | if after := openDescriptors(t); !slices.Equal(after, before) { |
| 127 | t.Errorf("descriptors %v after %v, and %v before", after, err, before) |
| 128 | } |
| 129 | }) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // openDescriptors is the descriptors this process holds, less the one |
| 134 | // that reads them. |
| 135 | func openDescriptors(t *testing.T) []int { |
| 136 | d, err := os.Open("/dev/fd") |
| 137 | if err != nil { |
| 138 | t.Skipf("no /dev/fd: %v", err) |
| 139 | } |
| 140 | self := int(d.Fd()) |
| 141 | names, err := d.Readdirnames(-1) |
| 142 | d.Close() |
| 143 | if err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | var fds []int |
| 147 | for _, n := range names { |
| 148 | if fd, err := strconv.Atoi(n); err == nil && fd != self { |
| 149 | fds = append(fds, fd) |
| 150 | } |
| 151 | } |
| 152 | slices.Sort(fds) |
| 153 | return fds |
| 154 | } |
| 155 | |
| 156 | // unused is the descriptor number with n numbers below it that held does |
| 157 | // not use. |
| 158 | func unused(held []int, n int) int { |
| 159 | for fd := 0; ; fd++ { |
| 160 | if !slices.Contains(held, fd) { |
| 161 | if n == 0 { |
| 162 | return fd |
| 163 | } |
| 164 | n-- |
| 165 | } |
| 166 | } |
| 167 | } |