| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "syscall" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | func TestMain(m *testing.M) { |
| 18 | for k, v := range map[string]string{ |
| 19 | "GIT_CONFIG_NOSYSTEM": "1", |
| 20 | "GIT_CONFIG_GLOBAL": os.DevNull, |
| 21 | "GIT_AUTHOR_NAME": "valley-check", |
| 22 | "GIT_AUTHOR_EMAIL": "valley-check@localhost", |
| 23 | "GIT_COMMITTER_NAME": "valley-check", |
| 24 | "GIT_COMMITTER_EMAIL": "valley-check@localhost", |
| 25 | } { |
| 26 | os.Setenv(k, v) |
| 27 | } |
| 28 | os.Exit(m.Run()) |
| 29 | } |
| 30 | |
| 31 | const limit = 10 * time.Second |
| 32 | |
| 33 | func run(t *testing.T, dir string, args ...string) string { |
| 34 | t.Helper() |
| 35 | out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput() |
| 36 | if err != nil { |
| 37 | t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 38 | } |
| 39 | return strings.TrimSpace(string(out)) |
| 40 | } |
| 41 | |
| 42 | // bare makes an empty bare repository, with the ref storage given. |
| 43 | func bare(t *testing.T, format string) string { |
| 44 | t.Helper() |
| 45 | dir := filepath.Join(t.TempDir(), "app.git") |
| 46 | run(t, ".", "init", "--quiet", "--bare", "--ref-format="+format, dir) |
| 47 | return dir |
| 48 | } |
| 49 | |
| 50 | // commit makes a commit in repo that no ref names yet. |
| 51 | func commit(t *testing.T, repo, message string) string { |
| 52 | t.Helper() |
| 53 | tree := run(t, repo, "mktree") |
| 54 | return run(t, repo, "commit-tree", "-m", message, tree) |
| 55 | } |
| 56 | |
| 57 | // land moves main to a new commit, as the integrator's update-ref does. |
| 58 | func land(t *testing.T, repo, message string) string { |
| 59 | t.Helper() |
| 60 | c := commit(t, repo, message) |
| 61 | run(t, repo, "update-ref", "refs/heads/main", c) |
| 62 | return c |
| 63 | } |
| 64 | |
| 65 | // harness runs a bridge and stands in for its bound unit: each request |
| 66 | // the bridge makes is handed to the test, which consumes it as the unit's |
| 67 | // onmain environment would. |
| 68 | type harness struct { |
| 69 | t *testing.T |
| 70 | repo string |
| 71 | file string // the commit file |
| 72 | request string |
| 73 | reqs chan string |
| 74 | done chan error |
| 75 | stop chan struct{} |
| 76 | |
| 77 | mu sync.Mutex |
| 78 | log strings.Builder |
| 79 | requests int |
| 80 | idleAt int // requests when the bridge last began waiting |
| 81 | } |
| 82 | |
| 83 | func (h *harness) Write(p []byte) (int, error) { |
| 84 | h.mu.Lock() |
| 85 | defer h.mu.Unlock() |
| 86 | return h.log.Write(p) |
| 87 | } |
| 88 | |
| 89 | func (h *harness) said() string { |
| 90 | h.mu.Lock() |
| 91 | defer h.mu.Unlock() |
| 92 | return h.log.String() |
| 93 | } |
| 94 | |
| 95 | func startBridge(t *testing.T, repo string, watch func(string) (watcher, error)) *harness { |
| 96 | t.Helper() |
| 97 | dir := t.TempDir() |
| 98 | h := &harness{ |
| 99 | t: t, |
| 100 | repo: repo, |
| 101 | file: filepath.Join(dir, "commit"), |
| 102 | request: filepath.Join(dir, "request"), |
| 103 | reqs: make(chan string), |
| 104 | done: make(chan error, 1), |
| 105 | stop: make(chan struct{}), |
| 106 | idleAt: -1, |
| 107 | } |
| 108 | b := &bridge{ |
| 109 | repo: repo, |
| 110 | commit: h.file, |
| 111 | request: h.request, |
| 112 | log: h, |
| 113 | watch: watch, |
| 114 | stop: h.stop, |
| 115 | requested: func(tip string) { |
| 116 | h.mu.Lock() |
| 117 | h.requests++ |
| 118 | h.mu.Unlock() |
| 119 | h.reqs <- tip |
| 120 | }, |
| 121 | waiting: func() { |
| 122 | h.mu.Lock() |
| 123 | h.idleAt = h.requests |
| 124 | h.mu.Unlock() |
| 125 | }, |
| 126 | } |
| 127 | go func() { h.done <- b.run() }() |
| 128 | t.Cleanup(func() { |
| 129 | close(h.stop) |
| 130 | deadline := time.After(limit) |
| 131 | for { |
| 132 | select { |
| 133 | case <-h.reqs: |
| 134 | case err := <-h.done: |
| 135 | if err != nil { |
| 136 | t.Errorf("the bridge ended with %v", err) |
| 137 | } |
| 138 | return |
| 139 | case <-deadline: |
| 140 | t.Errorf("the bridge did not stop") |
| 141 | return |
| 142 | } |
| 143 | } |
| 144 | }) |
| 145 | return h |
| 146 | } |
| 147 | |
| 148 | // requested waits for the bridge to request a run at want, and consumes |
| 149 | // the request as the unit's onmain environment does: the environment |
| 150 | // file it writes must name want, and no request may be left. |
| 151 | func (h *harness) requested(want string) { |
| 152 | h.t.Helper() |
| 153 | select { |
| 154 | case tip := <-h.reqs: |
| 155 | if tip != want { |
| 156 | h.t.Fatalf("the bridge requested %s, want %s", tip, want) |
| 157 | } |
| 158 | case err := <-h.done: |
| 159 | h.t.Fatalf("the bridge ended with %v; it said:\n%s", err, h.said()) |
| 160 | case <-time.After(limit): |
| 161 | h.t.Fatalf("no run was requested; the bridge said:\n%s", h.said()) |
| 162 | } |
| 163 | if got := environmentCommit(h.t, h.repo, h.file, h.request); got != want { |
| 164 | h.t.Fatalf("the environment names %s, want %s", got, want) |
| 165 | } |
| 166 | if _, err := os.Stat(h.request); err == nil { |
| 167 | h.t.Fatal("the request was left after the environment was written") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // environmentCommit runs onmain environment, and returns the commit the |
| 172 | // environment file then names. |
| 173 | func environmentCommit(t *testing.T, repo, commitFile, request string) string { |
| 174 | t.Helper() |
| 175 | out := filepath.Join(filepath.Dir(commitFile), "environment") |
| 176 | if err := writeEnvironment(repo, commitFile, request, out); err != nil { |
| 177 | t.Fatalf("writing the environment: %v", err) |
| 178 | } |
| 179 | data, err := os.ReadFile(out) |
| 180 | if err != nil { |
| 181 | t.Fatal(err) |
| 182 | } |
| 183 | commit, ok := strings.CutPrefix(string(data), "VALLEY_REPOSITORY="+repo+"\nVALLEY_COMMIT=") |
| 184 | if !ok { |
| 185 | t.Fatalf("the environment file reads %q", data) |
| 186 | } |
| 187 | return strings.TrimSuffix(commit, "\n") |
| 188 | } |
| 189 | |
| 190 | // idle waits until the bridge waits for a change after its last request, |
| 191 | // so what the test moves next can reach it only through the watch, and |
| 192 | // checks it left no request the test has not consumed. |
| 193 | func (h *harness) idle() { |
| 194 | h.t.Helper() |
| 195 | deadline := time.Now().Add(limit) |
| 196 | for { |
| 197 | h.mu.Lock() |
| 198 | idle := h.idleAt == h.requests |
| 199 | h.mu.Unlock() |
| 200 | if idle { |
| 201 | break |
| 202 | } |
| 203 | if time.Now().After(deadline) { |
| 204 | h.t.Fatalf("the bridge did not wait; it said:\n%s", h.said()) |
| 205 | } |
| 206 | time.Sleep(time.Millisecond) |
| 207 | } |
| 208 | if _, err := os.Stat(h.request); err == nil { |
| 209 | h.t.Fatal("a run was requested that the test did not see") |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | func TestAStartRequestsTheTipAndALooseRefMoveTheNext(t *testing.T) { |
| 214 | repo := bare(t, "files") |
| 215 | a := land(t, repo, "a") |
| 216 | h := startBridge(t, repo, watchRefs) |
| 217 | h.requested(a) |
| 218 | h.idle() |
| 219 | b := land(t, repo, "b") |
| 220 | h.requested(b) |
| 221 | h.idle() |
| 222 | } |
| 223 | |
| 224 | func TestARepositoryWithNoMainRequestsItsFirst(t *testing.T) { |
| 225 | repo := bare(t, "files") |
| 226 | h := startBridge(t, repo, watchRefs) |
| 227 | h.idle() |
| 228 | a := land(t, repo, "a") |
| 229 | h.requested(a) |
| 230 | } |
| 231 | |
| 232 | // main packed, with no loose ref, moves when packed-refs is replaced; and |
| 233 | // an update after git pack-refs writes main's loose ref again. |
| 234 | func TestAPackedRefsRewriteRequestsTheTip(t *testing.T) { |
| 235 | repo := bare(t, "files") |
| 236 | a := land(t, repo, "a") |
| 237 | run(t, repo, "pack-refs", "--all") |
| 238 | if _, err := os.Stat(filepath.Join(repo, "refs", "heads", "main")); err == nil { |
| 239 | t.Fatal("pack-refs left main's loose ref") |
| 240 | } |
| 241 | h := startBridge(t, repo, watchRefs) |
| 242 | h.requested(a) |
| 243 | h.idle() |
| 244 | |
| 245 | // Replaced by a rename, as git replaces it. |
| 246 | b := commit(t, repo, "b") |
| 247 | next := filepath.Join(repo, "packed-refs.next") |
| 248 | if err := os.WriteFile(next, []byte("# pack-refs with: peeled fully-peeled sorted \n"+b+" refs/heads/main\n"), 0o644); err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | if err := os.Rename(next, filepath.Join(repo, "packed-refs")); err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | h.requested(b) |
| 255 | h.idle() |
| 256 | |
| 257 | c := land(t, repo, "c") |
| 258 | h.requested(c) |
| 259 | } |
| 260 | |
| 261 | func TestAnUnchangedTipIsNotRequestedAgain(t *testing.T) { |
| 262 | repo := bare(t, "files") |
| 263 | a := land(t, repo, "a") |
| 264 | h := startBridge(t, repo, watchRefs) |
| 265 | h.requested(a) |
| 266 | h.idle() |
| 267 | |
| 268 | run(t, repo, "pack-refs", "--all") |
| 269 | run(t, repo, "update-ref", "refs/heads/main", a) |
| 270 | run(t, repo, "update-ref", "refs/heads/topic", commit(t, repo, "topic")) |
| 271 | run(t, repo, "pack-refs", "--all") |
| 272 | |
| 273 | b := land(t, repo, "b") |
| 274 | h.requested(b) |
| 275 | } |
| 276 | |
| 277 | // pathUnit starts a unit as systemd does for a path unit with PathExists= |
| 278 | // on the request file: whenever the file exists and the unit is not |
| 279 | // running, and so again each time the unit stops while it exists. The |
| 280 | // unit's first command is onmain environment, and command stands for the |
| 281 | // rest, given the run's number and the commit its environment names; how |
| 282 | // a run ends does not change what the path unit does. pathUnit returns |
| 283 | // each run's commit. |
| 284 | func pathUnit(t *testing.T, repo, commitFile, request string, command func(run int, commit string)) []string { |
| 285 | t.Helper() |
| 286 | var runs []string |
| 287 | for len(runs) < 10 { |
| 288 | if _, err := os.Stat(request); err != nil { |
| 289 | return runs |
| 290 | } |
| 291 | c := environmentCommit(t, repo, commitFile, request) |
| 292 | runs = append(runs, c) |
| 293 | command(len(runs)-1, c) |
| 294 | } |
| 295 | t.Fatalf("the unit was started again and again: %v", runs) |
| 296 | return nil |
| 297 | } |
| 298 | |
| 299 | // A commit the bridge requests while the unit runs for an older one, at |
| 300 | // any moment of that run, is run: by the same run, or by one more after |
| 301 | // it. Each case has the bridge request b while the unit's run for a is |
| 302 | // under way, at the moment it names. |
| 303 | func TestACommitRequestedDuringARunIsRun(t *testing.T) { |
| 304 | a := strings.Repeat("a", 40) |
| 305 | b := strings.Repeat("b", 40) |
| 306 | c := strings.Repeat("c", 40) |
| 307 | for _, tc := range []struct { |
| 308 | name string |
| 309 | step string // the step of onmain environment b is requested after |
| 310 | want []string |
| 311 | }{ |
| 312 | // b is in the commit file when this run reads it, and its request |
| 313 | // was made after the removal, so b runs twice. |
| 314 | {"after the request is removed", "removed", []string{b, b}}, |
| 315 | {"after the commit is read", "read", []string{a, b}}, |
| 316 | {"while the command runs", "", []string{a, b}}, |
| 317 | } { |
| 318 | t.Run(tc.name, func(t *testing.T) { |
| 319 | dir := t.TempDir() |
| 320 | repo, commitFile, request := "/srv/git/app.git", filepath.Join(dir, "commit"), filepath.Join(dir, "request") |
| 321 | bridgeRequests := func(c string) { |
| 322 | if err := requestRun(commitFile, request, c); err != nil { |
| 323 | t.Fatal(err) |
| 324 | } |
| 325 | } |
| 326 | bridgeRequests(a) |
| 327 | afterStep = func(step string) { |
| 328 | if step == tc.step { |
| 329 | afterStep = nil |
| 330 | bridgeRequests(b) |
| 331 | } |
| 332 | } |
| 333 | t.Cleanup(func() { afterStep = nil }) |
| 334 | runs := pathUnit(t, repo, commitFile, request, func(run int, _ string) { |
| 335 | if run == 0 && tc.step == "" { |
| 336 | bridgeRequests(b) |
| 337 | } |
| 338 | }) |
| 339 | if !slices.Equal(runs, tc.want) { |
| 340 | t.Errorf("the unit ran for %v, want %v", runs, tc.want) |
| 341 | } |
| 342 | }) |
| 343 | } |
| 344 | |
| 345 | // Moves made during a run are one run at the newest, and a run with no |
| 346 | // request after it, failed or not, is the last. |
| 347 | t.Run("several moves during a run", func(t *testing.T) { |
| 348 | dir := t.TempDir() |
| 349 | repo, commitFile, request := "/srv/git/app.git", filepath.Join(dir, "commit"), filepath.Join(dir, "request") |
| 350 | if err := requestRun(commitFile, request, a); err != nil { |
| 351 | t.Fatal(err) |
| 352 | } |
| 353 | runs := pathUnit(t, repo, commitFile, request, func(run int, _ string) { |
| 354 | if run == 0 { |
| 355 | for _, m := range []string{b, c} { |
| 356 | if err := requestRun(commitFile, request, m); err != nil { |
| 357 | t.Fatal(err) |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | }) |
| 362 | if want := []string{a, c}; !slices.Equal(runs, want) { |
| 363 | t.Errorf("the unit ran for %v, want %v", runs, want) |
| 364 | } |
| 365 | }) |
| 366 | } |
| 367 | |
| 368 | func TestReftableIsRefused(t *testing.T) { |
| 369 | repo := bare(t, "reftable") |
| 370 | land(t, repo, "a") |
| 371 | dir := t.TempDir() |
| 372 | b := &bridge{ |
| 373 | repo: repo, |
| 374 | commit: filepath.Join(dir, "commit"), |
| 375 | request: filepath.Join(dir, "request"), |
| 376 | log: os.Stderr, |
| 377 | watch: watchRefs, |
| 378 | } |
| 379 | err := b.run() |
| 380 | if err == nil || !strings.Contains(err.Error(), "keeps its refs in reftable storage") { |
| 381 | t.Fatalf("got %v, want reftable refused", err) |
| 382 | } |
| 383 | if _, err := os.Stat(b.request); err == nil { |
| 384 | t.Error("a run was requested") |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | func TestTheEnvironmentIsWrittenFromOneCommit(t *testing.T) { |
| 389 | dir := t.TempDir() |
| 390 | commit := strings.Repeat("0123456789abcdef", 4) |
| 391 | for _, c := range []string{commit[:40], commit} { |
| 392 | if err := os.WriteFile(filepath.Join(dir, "commit"), []byte(c+"\n"), 0o644); err != nil { |
| 393 | t.Fatal(err) |
| 394 | } |
| 395 | out := filepath.Join(dir, "environment") |
| 396 | if err := writeEnvironment("/srv/git/app.git", filepath.Join(dir, "commit"), filepath.Join(dir, "request"), out); err != nil { |
| 397 | t.Fatal(err) |
| 398 | } |
| 399 | data, err := os.ReadFile(out) |
| 400 | if err != nil { |
| 401 | t.Fatal(err) |
| 402 | } |
| 403 | if want := "VALLEY_REPOSITORY=/srv/git/app.git\nVALLEY_COMMIT=" + c + "\n"; string(data) != want { |
| 404 | t.Errorf("got %q, want %q", data, want) |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // The commit file is the bridge's user's to write, and the environment |
| 410 | // file is read by systemd for a unit that may run as root, so nothing in |
| 411 | // the commit file but one commit ID reaches it. A refused file still |
| 412 | // consumes the request, so the refusal does not start the unit again. |
| 413 | func TestAnythingButOneCommitIsRefused(t *testing.T) { |
| 414 | commit := strings.Repeat("ab", 20) |
| 415 | for name, content := range map[string]string{ |
| 416 | "an assignment after it": commit + "\nLD_PRELOAD=/tmp/x.so\n", |
| 417 | "an assignment alone": "VALLEY_COMMIT=" + commit + "\n", |
| 418 | "no newline": commit, |
| 419 | "capitals": strings.ToUpper(commit) + "\n", |
| 420 | "a short ID": commit[:39] + "\n", |
| 421 | "empty": "", |
| 422 | "a longer file": commit + "\n" + strings.Repeat("x", 1<<20), |
| 423 | "a carriage return": commit + "\r\n", |
| 424 | } { |
| 425 | t.Run(name, func(t *testing.T) { |
| 426 | dir := t.TempDir() |
| 427 | if err := os.WriteFile(filepath.Join(dir, "commit"), []byte(content), 0o644); err != nil { |
| 428 | t.Fatal(err) |
| 429 | } |
| 430 | refuse(t, dir, "/srv/git/app.git") |
| 431 | }) |
| 432 | } |
| 433 | t.Run("a symbolic link", func(t *testing.T) { |
| 434 | dir := t.TempDir() |
| 435 | if err := os.WriteFile(filepath.Join(dir, "target"), []byte(commit+"\n"), 0o644); err != nil { |
| 436 | t.Fatal(err) |
| 437 | } |
| 438 | if err := os.Symlink("target", filepath.Join(dir, "commit")); err != nil { |
| 439 | t.Fatal(err) |
| 440 | } |
| 441 | refuse(t, dir, "/srv/git/app.git") |
| 442 | }) |
| 443 | t.Run("a FIFO", func(t *testing.T) { |
| 444 | dir := t.TempDir() |
| 445 | if err := syscall.Mkfifo(filepath.Join(dir, "commit"), 0o644); err != nil { |
| 446 | t.Fatal(err) |
| 447 | } |
| 448 | refuse(t, dir, "/srv/git/app.git") |
| 449 | }) |
| 450 | t.Run("no commit file", func(t *testing.T) { |
| 451 | refuse(t, t.TempDir(), "/srv/git/app.git") |
| 452 | }) |
| 453 | t.Run("a repository the file cannot carry", func(t *testing.T) { |
| 454 | dir := t.TempDir() |
| 455 | if err := os.WriteFile(filepath.Join(dir, "commit"), []byte(commit+"\n"), 0o644); err != nil { |
| 456 | t.Fatal(err) |
| 457 | } |
| 458 | refuse(t, dir, "/srv/git/my app.git") |
| 459 | }) |
| 460 | } |
| 461 | |
| 462 | // refuse checks the commit file in dir is refused for repo, no |
| 463 | // environment file written, and the request consumed. |
| 464 | func refuse(t *testing.T, dir, repo string) { |
| 465 | t.Helper() |
| 466 | out, request := filepath.Join(dir, "environment"), filepath.Join(dir, "request") |
| 467 | if err := os.WriteFile(request, nil, 0o644); err != nil { |
| 468 | t.Fatal(err) |
| 469 | } |
| 470 | err := writeEnvironment(repo, filepath.Join(dir, "commit"), request, out) |
| 471 | if err == nil { |
| 472 | t.Fatal("it was accepted") |
| 473 | } |
| 474 | if strings.Contains(err.Error(), "LD_PRELOAD") || strings.Contains(err.Error(), "xxxx") { |
| 475 | t.Errorf("the refusal prints what the file holds: %v", err) |
| 476 | } |
| 477 | if _, err := os.Stat(out); err == nil { |
| 478 | t.Error("an environment file was written") |
| 479 | } |
| 480 | if _, err := os.Stat(request); err == nil { |
| 481 | t.Error("the request was left, to start the unit again") |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // fakeWatch is a watch the test fails by hand. |
| 486 | type fakeWatch struct { |
| 487 | note chan struct{} |
| 488 | end chan error |
| 489 | closed chan struct{} |
| 490 | } |
| 491 | |
| 492 | func newFake() *fakeWatch { |
| 493 | return &fakeWatch{note: make(chan struct{}, 1), end: make(chan error, 1), closed: make(chan struct{})} |
| 494 | } |
| 495 | |
| 496 | func (f *fakeWatch) events() <-chan struct{} { return f.note } |
| 497 | func (f *fakeWatch) failed() <-chan error { return f.end } |
| 498 | func (f *fakeWatch) close() { close(f.closed) } |
| 499 | |
| 500 | // A move made while no watch is held is requested once the watch is back, |
| 501 | // though no event reports it; a watch that cannot be set up again ends |
| 502 | // the bridge. |
| 503 | func TestALostWatchIsSetUpAgainAndMainReadAgain(t *testing.T) { |
| 504 | repo := bare(t, "files") |
| 505 | a := land(t, repo, "a") |
| 506 | b := commit(t, repo, "b") |
| 507 | first, second := newFake(), newFake() |
| 508 | watches := 0 |
| 509 | h := startBridge(t, repo, func(string) (watcher, error) { |
| 510 | watches++ |
| 511 | switch watches { |
| 512 | case 1: |
| 513 | return first, nil |
| 514 | case 2: |
| 515 | if out, err := exec.Command("git", "-C", repo, "update-ref", "refs/heads/main", b).CombinedOutput(); err != nil { |
| 516 | return nil, fmt.Errorf("moving main: %v: %s", err, out) |
| 517 | } |
| 518 | return second, nil |
| 519 | } |
| 520 | return nil, errors.New("no watch") |
| 521 | }) |
| 522 | h.requested(a) |
| 523 | h.idle() |
| 524 | |
| 525 | first.end <- errDropped |
| 526 | h.requested(b) |
| 527 | select { |
| 528 | case <-first.closed: |
| 529 | default: |
| 530 | t.Error("the lost watch was not closed") |
| 531 | } |
| 532 | |
| 533 | h.idle() |
| 534 | second.end <- errDropped |
| 535 | select { |
| 536 | case err := <-h.done: |
| 537 | if err == nil || !strings.Contains(err.Error(), "no watch") { |
| 538 | t.Errorf("the bridge ended with %v", err) |
| 539 | } |
| 540 | h.done <- nil |
| 541 | case <-time.After(limit): |
| 542 | t.Fatal("the bridge went on without a watch") |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // A watched directory moved away, or refs or refs/heads renamed, fails the |
| 547 | // watch: it no longer sees main. |
| 548 | func TestAWatchFailsWhenItsDirectoriesGo(t *testing.T) { |
| 549 | for _, moved := range []string{".", "refs", "refs/heads"} { |
| 550 | t.Run(moved, func(t *testing.T) { |
| 551 | repo := bare(t, "files") |
| 552 | w, err := watchRefs(repo) |
| 553 | if err != nil { |
| 554 | t.Fatal(err) |
| 555 | } |
| 556 | defer w.close() |
| 557 | from := filepath.Join(repo, moved) |
| 558 | if err := os.Rename(from, filepath.Clean(from)+".moved"); err != nil { |
| 559 | t.Fatal(err) |
| 560 | } |
| 561 | select { |
| 562 | case <-w.failed(): |
| 563 | case <-time.After(limit): |
| 564 | t.Fatal("the watch did not fail") |
| 565 | } |
| 566 | }) |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // A repository whose refs are all packed may have no refs/heads. The |
| 571 | // bridge reads main from packed-refs, and follows main into refs/heads |
| 572 | // once git creates it. |
| 573 | func TestAMissingRefsHeadsIsWatchedForAtStart(t *testing.T) { |
| 574 | repo := bare(t, "files") |
| 575 | a := land(t, repo, "a") |
| 576 | run(t, repo, "pack-refs", "--all") |
| 577 | if err := os.RemoveAll(filepath.Join(repo, "refs", "heads")); err != nil { |
| 578 | t.Fatal(err) |
| 579 | } |
| 580 | h := startBridge(t, repo, watchRefs) |
| 581 | h.requested(a) |
| 582 | h.idle() |
| 583 | |
| 584 | b := land(t, repo, "b") |
| 585 | h.requested(b) |
| 586 | h.idle() |
| 587 | c := land(t, repo, "c") |
| 588 | h.requested(c) |
| 589 | } |
| 590 | |
| 591 | // refs/heads going while the bridge watches it, as when the directory is |
| 592 | // replaced, leaves the bridge watching for it, and following main once it |
| 593 | // is back. |
| 594 | func TestARefsHeadsGoneWhileWatchedIsWatchedFor(t *testing.T) { |
| 595 | repo := bare(t, "files") |
| 596 | a := land(t, repo, "a") |
| 597 | heads := filepath.Join(repo, "refs", "heads") |
| 598 | // The second watch is the one set up after refs/heads went. |
| 599 | rearmed := make(chan bool) |
| 600 | watches := 0 |
| 601 | h := startBridge(t, repo, func(gitDir string) (watcher, error) { |
| 602 | watches++ |
| 603 | w, err := watchRefs(gitDir) |
| 604 | if watches == 2 { |
| 605 | _, absent := os.Stat(heads) |
| 606 | rearmed <- absent != nil |
| 607 | } |
| 608 | return w, err |
| 609 | }) |
| 610 | h.requested(a) |
| 611 | h.idle() |
| 612 | |
| 613 | run(t, repo, "pack-refs", "--all") |
| 614 | if err := os.Rename(heads, heads+".old"); err != nil { |
| 615 | t.Fatal(err) |
| 616 | } |
| 617 | select { |
| 618 | case absent := <-rearmed: |
| 619 | if !absent { |
| 620 | t.Fatal("refs/heads was back before the watch was set up again") |
| 621 | } |
| 622 | case <-time.After(limit): |
| 623 | t.Fatalf("the watch was not set up again; the bridge said:\n%s", h.said()) |
| 624 | } |
| 625 | b := land(t, repo, "b") |
| 626 | h.requested(b) |
| 627 | h.idle() |
| 628 | c := land(t, repo, "c") |
| 629 | h.requested(c) |
| 630 | } |