1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
package tempdir
import (
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg"
)
func TestCleanSuccess(t *testing.T) {
cfg := testcfg.Build(t)
locator := config.NewLocator(cfg)
cleanRoot, err := locator.TempDir(cfg.Storages[0].Name)
require.NoError(t, err)
require.NoError(t, os.MkdirAll(cleanRoot, perm.SharedDir), "create clean root before setup")
testhelper.MustRunCommand(t, nil, "chmod", "-R", "0700", cleanRoot)
require.NoError(t, os.RemoveAll(cleanRoot), "clean up test clean root")
old := time.Unix(0, 0)
recent := time.Now()
makeDir(t, locator, cfg.Storages[0], "a", old)
makeDir(t, locator, cfg.Storages[0], "a/b", recent) // Messes up mtime of "a", we fix that below
makeDir(t, locator, cfg.Storages[0], "c", recent)
makeDir(t, locator, cfg.Storages[0], "f", old)
makeFile(t, locator, cfg.Storages[0], "a/b/g", old)
makeFile(t, locator, cfg.Storages[0], "c/d", old)
makeFile(t, locator, cfg.Storages[0], "e", recent)
// This is really evil and even breaks 'rm -rf'
chmod(t, locator, cfg.Storages[0], "a/b", 0)
chmod(t, locator, cfg.Storages[0], "a", 0)
chtimes(t, locator, cfg.Storages[0], "a", old)
assertEntries(t, locator, cfg.Storages[0], "a", "c", "e", "f")
require.NoError(t, clean(locator, cfg.Storages[0]), "walk first pass")
assertEntries(t, locator, cfg.Storages[0], "c", "e")
}
func TestCleanTempDir(t *testing.T) {
ctx := testhelper.Context(t)
cfg := testcfg.Build(t, testcfg.WithStorages("first", "second"))
locator := config.NewLocator(cfg)
_, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
SkipCreationViaService: true,
})
gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"))
logrus.SetLevel(logrus.InfoLevel)
logrus.SetOutput(io.Discard)
hook := test.NewGlobal()
cleanTempDir(locator, cfg.Storages)
require.Equal(t, 2, len(hook.Entries), hook.Entries)
require.Equal(t, "finished tempdir cleaner walk", hook.LastEntry().Message)
}
func TestCleanNoTmpExists(t *testing.T) {
cfg := testcfg.Build(t)
locator := config.NewLocator(cfg)
require.NoError(t, clean(locator, cfg.Storages[0]))
}
func TestCleanNoStorageExists(t *testing.T) {
cfg := testcfg.Build(t, testcfg.WithStorages("first"))
locator := config.NewLocator(cfg)
err := clean(locator, config.Storage{Name: "does-not-exist", Path: "/something"})
require.EqualError(t, err, "temporary dir: tmp dir: no such storage: \"does-not-exist\"")
}
type mockLocator struct {
storage.Locator
}
func (m mockLocator) TempDir(storageName string) (string, error) {
return "something", nil
}
func TestCleanerSafety(t *testing.T) {
defer func() {
if p := recover(); p != nil {
if _, ok := p.(invalidCleanRoot); !ok {
t.Fatalf("expected invalidCleanRoot panic, got %v", p)
}
}
}()
// We need to set up a mock locator which returns an invalid temporary directory path.
require.NoError(t, clean(mockLocator{}, config.Storage{}))
t.Fatal("expected panic")
}
func chmod(t *testing.T, locator storage.Locator, storage config.Storage, p string, mode os.FileMode) {
root, err := locator.TempDir(storage.Name)
require.NoError(t, err)
require.NoError(t, os.Chmod(filepath.Join(root, p), mode))
}
func chtimes(t *testing.T, locator storage.Locator, storage config.Storage, p string, date time.Time) {
root, err := locator.TempDir(storage.Name)
require.NoError(t, err)
require.NoError(t, os.Chtimes(filepath.Join(root, p), date, date))
}
func assertEntries(t *testing.T, locator storage.Locator, storage config.Storage, entries ...string) {
root, err := locator.TempDir(storage.Name)
require.NoError(t, err)
foundEntries, err := os.ReadDir(root)
require.NoError(t, err)
require.Len(t, foundEntries, len(entries))
for i, name := range entries {
require.Equal(t, name, foundEntries[i].Name())
}
}
func makeFile(t *testing.T, locator storage.Locator, storage config.Storage, filePath string, mtime time.Time) {
root, err := locator.TempDir(storage.Name)
require.NoError(t, err)
fullPath := filepath.Join(root, filePath)
require.NoError(t, os.WriteFile(fullPath, nil, perm.SharedFile))
require.NoError(t, os.Chtimes(fullPath, mtime, mtime))
}
func makeDir(t *testing.T, locator storage.Locator, storage config.Storage, dirPath string, mtime time.Time) {
root, err := locator.TempDir(storage.Name)
require.NoError(t, err)
fullPath := filepath.Join(root, dirPath)
require.NoError(t, os.MkdirAll(fullPath, perm.PrivateDir))
require.NoError(t, os.Chtimes(fullPath, mtime, mtime))
}
|