| 1 | //go:build linux |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | // Which controller serves a repository, over real directories, flock and |
| 6 | // inotify, with the competing controller a second process. The hooks |
| 7 | // ownLocked and watchOpened stop a start at the boundaries where a |
| 8 | // directory can be replaced under it. |
| 9 | |
| 10 | import ( |
| 11 | "bufio" |
| 12 | "errors" |
| 13 | "io" |
| 14 | "os" |
| 15 | "os/exec" |
| 16 | "path/filepath" |
| 17 | "strings" |
| 18 | "sync" |
| 19 | "testing" |
| 20 | "time" |
| 21 | ) |
| 22 | |
| 23 | // TestHelperHoldsARepository is not a test. Run as a second process, with |
| 24 | // VALLEY_TEST_HOLD naming a repository, it takes the repository's lock the |
| 25 | // way a controller does, says "held" or "refused", and holds the lock |
| 26 | // until its standard input closes. |
| 27 | func TestHelperHoldsARepository(t *testing.T) { |
| 28 | repo := os.Getenv("VALLEY_TEST_HOLD") |
| 29 | if repo == "" { |
| 30 | t.Skip("run by the tests below as a second controller") |
| 31 | } |
| 32 | r, err := ownRepo(repo) |
| 33 | if err != nil { |
| 34 | os.Stdout.WriteString("refused: " + err.Error() + "\n") |
| 35 | os.Exit(3) |
| 36 | } |
| 37 | os.Stdout.WriteString("held\n") |
| 38 | io.Copy(io.Discard, os.Stdin) |
| 39 | r.Close() |
| 40 | os.Exit(0) |
| 41 | } |
| 42 | |
| 43 | // otherController is a second process taking repo's lock. |
| 44 | type otherController struct { |
| 45 | cmd *exec.Cmd |
| 46 | stdin io.WriteCloser |
| 47 | said string |
| 48 | } |
| 49 | |
| 50 | // startOther starts a second controller over repo and waits for what it |
| 51 | // says about the lock. |
| 52 | func startOther(t *testing.T, repo string) *otherController { |
| 53 | t.Helper() |
| 54 | cmd := exec.Command(os.Args[0], "-test.run=^TestHelperHoldsARepository$") |
| 55 | cmd.Env = append(os.Environ(), "VALLEY_TEST_HOLD="+repo) |
| 56 | stdin, err := cmd.StdinPipe() |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | stdout, err := cmd.StdoutPipe() |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if err := cmd.Start(); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | o := &otherController{cmd: cmd, stdin: stdin} |
| 68 | line, _ := bufio.NewReader(stdout).ReadString('\n') |
| 69 | o.said = strings.TrimSpace(line) |
| 70 | t.Cleanup(o.stop) |
| 71 | return o |
| 72 | } |
| 73 | |
| 74 | func (o *otherController) stop() { |
| 75 | o.stdin.Close() |
| 76 | o.cmd.Wait() |
| 77 | } |
| 78 | |
| 79 | func repoDir(t *testing.T) string { |
| 80 | repo := filepath.Join(t.TempDir(), "app.git") |
| 81 | if err := os.Mkdir(repo, 0o750); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | return repo |
| 85 | } |
| 86 | |
| 87 | // serveRecorded starts a controller over repo whose sweep and loop record |
| 88 | // that they ran. |
| 89 | func serveRecorded(repo string) (err error, swept, looped bool) { |
| 90 | err = serve(repo, func(string) { swept = true }, func(func() error) error { looped = true; return nil }) |
| 91 | return err, swept, looped |
| 92 | } |
| 93 | |
| 94 | // A second process over a repository a controller serves is refused, and a |
| 95 | // controller started over a repository a second process holds neither |
| 96 | // sweeps nor passes. |
| 97 | func TestOneControllerServesARepository(t *testing.T) { |
| 98 | repo := repoDir(t) |
| 99 | held, err := ownRepo(repo) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | if o := startOther(t, repo); !strings.HasPrefix(o.said, "refused: ") || !strings.Contains(o.said, "another controller holds its lock") { |
| 104 | t.Fatalf("a second process over a served repository said %q", o.said) |
| 105 | } |
| 106 | held.Close() |
| 107 | |
| 108 | o := startOther(t, repo) |
| 109 | if o.said != "held" { |
| 110 | t.Fatalf("the second process said %q over a repository nobody served", o.said) |
| 111 | } |
| 112 | err, swept, looped := serveRecorded(repo) |
| 113 | if !errors.Is(err, errLockHeld) || swept || looped { |
| 114 | t.Fatalf("a controller refused the repository returned %v, swept %v, looped %v", err, swept, looped) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // A repository replaced between its open and the check is opened again, |
| 119 | // and the lock that is kept is on the directory the path names. |
| 120 | func TestARepositoryReplacedAsItIsLockedIsLockedAgain(t *testing.T) { |
| 121 | repo := repoDir(t) |
| 122 | moved := repo + ".moved" |
| 123 | once := sync.Once{} |
| 124 | ownLocked = func() { |
| 125 | once.Do(func() { |
| 126 | if err := os.Rename(repo, moved); err != nil { |
| 127 | t.Error(err) |
| 128 | } |
| 129 | if err := os.Mkdir(repo, 0o750); err != nil { |
| 130 | t.Error(err) |
| 131 | } |
| 132 | }) |
| 133 | } |
| 134 | t.Cleanup(func() { ownLocked = nil }) |
| 135 | held, err := ownRepo(repo) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | defer held.Close() |
| 140 | ownLocked = nil |
| 141 | if o := startOther(t, repo); !strings.HasPrefix(o.said, "refused: ") { |
| 142 | t.Fatalf("the directory at the path was not the one locked: a second process said %q", o.said) |
| 143 | } |
| 144 | if o := startOther(t, moved); o.said != "held" { |
| 145 | t.Fatalf("the directory moved away is still locked: a second process said %q", o.said) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // The first lock is on a directory another takes the place of, and by the |
| 150 | // next try a second process holds the new one. The controller is refused, |
| 151 | // sweeps nothing, passes nothing, and holds nothing. |
| 152 | func TestAStartWhoseSecondTryFindsAnOwnerNeitherSweepsNorPasses(t *testing.T) { |
| 153 | repo := repoDir(t) |
| 154 | var other *otherController |
| 155 | ownLocked = func() { |
| 156 | if other != nil { |
| 157 | return |
| 158 | } |
| 159 | if err := os.Rename(repo, repo+".moved"); err != nil { |
| 160 | t.Error(err) |
| 161 | } |
| 162 | if err := os.Mkdir(repo, 0o750); err != nil { |
| 163 | t.Error(err) |
| 164 | } |
| 165 | other = startOther(t, repo) |
| 166 | } |
| 167 | t.Cleanup(func() { ownLocked = nil }) |
| 168 | err, swept, looped := serveRecorded(repo) |
| 169 | ownLocked = nil |
| 170 | if other == nil || other.said != "held" { |
| 171 | t.Fatalf("the second process did not take the new directory: %+v", other) |
| 172 | } |
| 173 | if !errors.Is(err, errLockHeld) || swept || looped { |
| 174 | t.Fatalf("the start returned %v, swept %v, looped %v", err, swept, looped) |
| 175 | } |
| 176 | if o := startOther(t, repo+".moved"); o.said != "held" { |
| 177 | t.Fatalf("the refused start kept the first directory locked: %q", o.said) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // A path that names another directory at every try is no repository to |
| 182 | // serve, and the refusal is bounded. |
| 183 | func TestARepositoryReplacedEveryTimeIsNotServed(t *testing.T) { |
| 184 | repo := repoDir(t) |
| 185 | n := 0 |
| 186 | ownLocked = func() { |
| 187 | n++ |
| 188 | os.Rename(repo, repo+".moved"+string(rune('0'+n))) |
| 189 | os.Mkdir(repo, 0o750) |
| 190 | } |
| 191 | t.Cleanup(func() { ownLocked = nil }) |
| 192 | err, swept, looped := serveRecorded(repo) |
| 193 | if err == nil || swept || looped || n != ownTries { |
| 194 | t.Fatalf("the start returned %v after %d tries, swept %v, looped %v", err, n, swept, looped) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // A repository whose path comes to name another directory while the |
| 199 | // controller runs stops the loop before its next pass. |
| 200 | func TestAControllerWhoseRepositoryIsReplacedStops(t *testing.T) { |
| 201 | repo := repoDir(t) |
| 202 | dir := wakeDir(t) |
| 203 | passes := 0 |
| 204 | err := serve(repo, func(string) {}, func(owned func() error) error { |
| 205 | l := &wakeLoop{dir: dir, interval: time.Hour, stderr: io.Discard, watch: watchDir, owned: owned, |
| 206 | pass: func(passReason) (bool, error) { |
| 207 | passes++ |
| 208 | os.Rename(repo, repo+".moved") |
| 209 | os.Mkdir(repo, 0o750) |
| 210 | markWake(t, dir) |
| 211 | return false, nil |
| 212 | }} |
| 213 | return l.run() |
| 214 | }) |
| 215 | if err == nil || !strings.Contains(err.Error(), "no longer the directory this controller locked") || passes != 1 { |
| 216 | t.Fatalf("a controller whose repository was replaced returned %v after %d passes", err, passes) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // failingWatch is a watch that ends at once, for a reason other than the |
| 221 | // directory leaving, as an inotify read error would end one. |
| 222 | type failingWatch struct{ end chan error } |
| 223 | |
| 224 | func (f failingWatch) events() <-chan struct{} { return nil } |
| 225 | func (f failingWatch) failed() <-chan error { return f.end } |
| 226 | func (f failingWatch) removeDue() error { return nil } |
| 227 | func (f failingWatch) hasDue() bool { return false } |
| 228 | func (f failingWatch) close() {} |
| 229 | |
| 230 | // A watch that fails leaves the controller passing on its interval, still |
| 231 | // serving the repository: a second process is refused it meanwhile. |
| 232 | func TestAFailedWatchKeepsTheRepository(t *testing.T) { |
| 233 | repo := repoDir(t) |
| 234 | dir := wakeDir(t) |
| 235 | stop := make(chan struct{}) |
| 236 | done := make(chan error, 1) |
| 237 | var stderr strings.Builder |
| 238 | var mu sync.Mutex |
| 239 | go func() { |
| 240 | done <- serve(repo, func(string) {}, func(owned func() error) error { |
| 241 | l := &wakeLoop{dir: dir, interval: time.Hour, stderr: lockedWriter{&mu, &stderr}, stop: stop, owned: owned, |
| 242 | watch: func(string) (watcher, error) { |
| 243 | end := make(chan error, 1) |
| 244 | end <- errors.New("reading its events: input/output error") |
| 245 | return failingWatch{end}, nil |
| 246 | }, |
| 247 | pass: func(passReason) (bool, error) { return false, nil }} |
| 248 | return l.run() |
| 249 | }) |
| 250 | }() |
| 251 | time.Sleep(300 * time.Millisecond) |
| 252 | if o := startOther(t, repo); !strings.HasPrefix(o.said, "refused: ") { |
| 253 | t.Fatalf("while its watch had failed, a second process could take the repository: %q", o.said) |
| 254 | } |
| 255 | close(stop) |
| 256 | if err := <-done; err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | mu.Lock() |
| 260 | defer mu.Unlock() |
| 261 | if !strings.Contains(stderr.String(), "input/output error") { |
| 262 | t.Fatalf("the failed watch was not said: %s", stderr.String()) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // A wake directory replaced between its open and its watch is not watched: |
| 267 | // the path names another directory. The next try watches the directory at |
| 268 | // the path, and a due left in the one moved away is not consumed. |
| 269 | func TestAWakeDirectoryReplacedAsItIsWatchedIsWatchedAgain(t *testing.T) { |
| 270 | dir := wakeDir(t) |
| 271 | moved := dir + ".moved" |
| 272 | once := sync.Once{} |
| 273 | watchOpened = func() { |
| 274 | once.Do(func() { |
| 275 | if err := os.Rename(dir, moved); err != nil { |
| 276 | t.Error(err) |
| 277 | } |
| 278 | if err := os.Mkdir(dir, 0o770); err != nil { |
| 279 | t.Error(err) |
| 280 | } |
| 281 | markWake(t, moved) |
| 282 | }) |
| 283 | } |
| 284 | t.Cleanup(func() { watchOpened = nil }) |
| 285 | r := startLoop(t, dir, time.Hour, nil) |
| 286 | if p := r.waitPasses(1, 5*time.Second); p[0].reason != reasonStartup { |
| 287 | t.Fatalf("the first pass was %v", p[0].reason) |
| 288 | } |
| 289 | if !strings.Contains(r.said(), "another directory took its place") { |
| 290 | t.Fatalf("the replacement was not said: %s", r.said()) |
| 291 | } |
| 292 | if p := r.waitPasses(2, 2*watchRetry); p[1].reason != reasonWatcher { |
| 293 | t.Fatalf("the pass once the directory at the path was watched was %v", p[1].reason) |
| 294 | } |
| 295 | markWake(t, dir) |
| 296 | if p := r.waitPasses(3, 5*time.Second); p[2].reason != reasonDue { |
| 297 | t.Fatalf("a mark in the directory at the path started a %v pass", p[2].reason) |
| 298 | } |
| 299 | if _, err := os.Lstat(filepath.Join(moved, "due")); err != nil { |
| 300 | t.Fatalf("the due in the directory moved away was consumed: %v", err) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // A second controller given the same wake directory consumes none of its |
| 305 | // marks, and passes at its interval. The first goes on being woken. |
| 306 | func TestASecondConsumerOfAWakeDirectoryConsumesNothing(t *testing.T) { |
| 307 | dir := wakeDir(t) |
| 308 | first := startLoop(t, dir, time.Hour, nil) |
| 309 | first.waitPasses(1, 5*time.Second) |
| 310 | second := startLoop(t, dir, time.Hour, nil) |
| 311 | second.waitPasses(1, 5*time.Second) |
| 312 | if !strings.Contains(second.said(), "another controller consumes its marks") { |
| 313 | t.Fatalf("the second consumer said: %s", second.said()) |
| 314 | } |
| 315 | markWake(t, dir) |
| 316 | if p := first.waitPasses(2, 5*time.Second); p[1].reason != reasonDue { |
| 317 | t.Fatalf("the first consumer passed for %v", p[1].reason) |
| 318 | } |
| 319 | second.still(1, 300*time.Millisecond) |
| 320 | } |