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