| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "encoding/hex" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 13 | ) |
| 14 | |
| 15 | // Astra round 2: streaming the candidate into the checks tree's .git |
| 16 | // never holds the serial step worker. If index-pack fails early (a full |
| 17 | // disk) the upstream commands are cancelled and reaped instead of |
| 18 | // blocking on an undrained pipe; if it stalls, the preparation deadline |
| 19 | // ends it. Either way checkRepository returns an error promptly. |
| 20 | func TestCheckRepositoryPackFailureNeverHangs(t *testing.T) { |
| 21 | root := t.TempDir() |
| 22 | work := filepath.Join(root, "work") |
| 23 | gitT(t, root, "init", "-q", "-b", "main", work) |
| 24 | // 4 MiB that does not compress: far beyond any pipe buffer. |
| 25 | raw := make([]byte, 2<<20) |
| 26 | rand.Read(raw) |
| 27 | os.WriteFile(filepath.Join(work, "noise.txt"), []byte(hex.EncodeToString(raw)), 0644) |
| 28 | gitT(t, work, "add", "-A") |
| 29 | gitT(t, work, "commit", "-qm", "noise") |
| 30 | cand := gitT(t, work, "rev-parse", "HEAD") |
| 31 | for _, tc := range []struct{ name, indexPack string }{ |
| 32 | {"fails", "exit 1"}, |
| 33 | {"stalls", "exec sleep 600"}, |
| 34 | } { |
| 35 | t.Run(tc.name, func(t *testing.T) { |
| 36 | dir := t.TempDir() |
| 37 | wrapper := filepath.Join(dir, "git") |
| 38 | script := "#!/bin/sh\nfor a in \"$@\"; do [ \"$a\" = index-pack ] && { " + tc.indexPack + "; }; done\nexec /usr/bin/git \"$@\"\n" |
| 39 | if err := os.WriteFile(wrapper, []byte(script), 0700); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | old := prepTimeout |
| 43 | prepTimeout = 3 * time.Second |
| 44 | t.Cleanup(func() { prepTimeout = old }) |
| 45 | tree := workspace.TaskTree{Dir: filepath.Join(dir, "tree"), GitDir: filepath.Join(work, ".git"), Git: wrapper, Branch: "equipo/tk1-x", Base: cand} |
| 46 | os.MkdirAll(tree.Dir, 0700) |
| 47 | e := &Engineer{} |
| 48 | e.O.Runner.GitPath = wrapper |
| 49 | done := make(chan error, 1) |
| 50 | start := time.Now() |
| 51 | go func() { done <- e.checkRepository(context.Background(), tree, cand) }() |
| 52 | select { |
| 53 | case err := <-done: |
| 54 | if err == nil { |
| 55 | t.Fatal("a failed index-pack was taken for a checks repository") |
| 56 | } |
| 57 | t.Logf("refused after %s: %v", time.Since(start).Round(time.Millisecond), err) |
| 58 | case <-time.After(30 * time.Second): |
| 59 | t.Fatal("checkRepository hung on a failed index-pack") |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |