|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package git |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "runtime" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +// This test mimics a repository having dangling locks. If the locks are older than the threshold, they should be |
| 17 | +// removed. Otherwise, they'll remain and the command will fail. |
| 18 | + |
| 19 | +func TestMaintainExistentLock(t *testing.T) { |
| 20 | + if runtime.GOOS != "linux" { |
| 21 | + // Need to use touch to change the last access time of the lock files |
| 22 | + t.Skip("Skipping test on non-linux OS") |
| 23 | + } |
| 24 | + |
| 25 | + shouldRemainLocked := func(lockFiles []string, err error) { |
| 26 | + assert.Error(t, err) |
| 27 | + for _, lockFile := range lockFiles { |
| 28 | + assert.FileExists(t, lockFile) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + shouldBeUnlocked := func(lockFiles []string, err error) { |
| 33 | + assert.NoError(t, err) |
| 34 | + for _, lockFile := range lockFiles { |
| 35 | + assert.NoFileExists(t, lockFile) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + t.Run("2 days lock file", func(t *testing.T) { |
| 40 | + doTestLockCleanup(t, "2 days", shouldBeUnlocked) |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("1 hour lock file", func(t *testing.T) { |
| 44 | + doTestLockCleanup(t, "1 hour", shouldBeUnlocked) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("1 minutes lock file", func(t *testing.T) { |
| 48 | + doTestLockCleanup(t, "1 minutes", shouldRemainLocked) |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func doTestLockCleanup(t *testing.T, lockAge string, expectedResult func(lockFiles []string, err error)) { |
| 53 | + if tmpDir, err := os.MkdirTemp("", "cleanup-after-crash"); err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } else { |
| 56 | + defer os.RemoveAll(tmpDir) |
| 57 | + |
| 58 | + if err := os.CopyFS(tmpDir, os.DirFS("../../tests/gitea-repositories-meta/org3/repo3.git")); err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + |
| 62 | + lockFiles := lockFilesFor(tmpDir) |
| 63 | + |
| 64 | + os.MkdirAll(tmpDir+"/objects/info/commit-graphs", os.ModeSticky|os.ModePerm) |
| 65 | + |
| 66 | + for _, lockFile := range lockFiles { |
| 67 | + createLockFiles(t, lockFile, lockAge) |
| 68 | + } |
| 69 | + |
| 70 | + cmd := NewCommand(context.Background(), "fetch") |
| 71 | + _, _, cmdErr := cmd.RunStdString(&RunOpts{Dir: tmpDir}) |
| 72 | + |
| 73 | + expectedResult(lockFiles, cmdErr) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func lockFilesFor(path string) []string { |
| 78 | + return []string{ |
| 79 | + path + "/config.lock", |
| 80 | + path + "/HEAD.lock", |
| 81 | + path + "/objects/info/commit-graphs/commit-graph-chain.lock", |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func createLockFiles(t *testing.T, file, lockAge string) { |
| 86 | + cmd := exec.Command("touch", "-m", "-a", "-d", "-"+lockAge, file) |
| 87 | + if err := cmd.Run(); err != nil { |
| 88 | + t.Error(err) |
| 89 | + } |
| 90 | +} |
0 commit comments