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
|
package git
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
)
func TestObjectDirs(t *testing.T) {
ctx := testhelper.Context(t)
altObjDirs := []string{
"testdata/objdirs/repo1/objects",
"testdata/objdirs/repo2/objects",
"testdata/objdirs/repo3/objects",
"testdata/objdirs/repo4/objects",
"testdata/objdirs/repo5/objects",
"testdata/objdirs/repoB/objects",
}
repo := "testdata/objdirs/repo0"
objDirs := append([]string{filepath.Join(repo, "objects")}, altObjDirs...)
out, err := ObjectDirectories(ctx, "testdata/objdirs", repo)
require.NoError(t, err)
require.Equal(t, objDirs, out)
out, err = AlternateObjectDirectories(ctx, "testdata/objdirs", repo)
require.NoError(t, err)
require.Equal(t, altObjDirs, out)
}
func TestObjectDirsNoAlternates(t *testing.T) {
ctx := testhelper.Context(t)
repo := "testdata/objdirs/no-alternates"
out, err := ObjectDirectories(ctx, "testdata/objdirs", repo)
require.NoError(t, err)
require.Equal(t, []string{filepath.Join(repo, "objects")}, out)
out, err = AlternateObjectDirectories(ctx, "testdata/objdirs", repo)
require.NoError(t, err)
require.Equal(t, []string{}, out)
}
func TestObjectDirsOutsideStorage(t *testing.T) {
tmp := testhelper.TempDir(t)
storageRoot := filepath.Join(tmp, "storage-root")
repoPath := filepath.Join(storageRoot, "repo")
alternatesFile := filepath.Join(repoPath, "objects", "info", "alternates")
altObjDir := filepath.Join(tmp, "outside-storage-sibling", "objects")
require.NoError(t, os.MkdirAll(filepath.Dir(alternatesFile), perm.PrivateDir))
expectedErr := alternateOutsideStorageError(altObjDir)
for _, tc := range []struct {
desc string
alternates string
}{
{
desc: "relative path",
alternates: "../../../outside-storage-sibling/objects",
},
{
desc: "absolute path",
alternates: altObjDir,
},
} {
t.Run(tc.desc, func(t *testing.T) {
ctx := testhelper.Context(t)
require.NoError(t, os.WriteFile(alternatesFile, []byte(tc.alternates), perm.PrivateFile))
out, err := ObjectDirectories(ctx, storageRoot, repoPath)
require.Equal(t, expectedErr, err)
require.Nil(t, out)
})
}
}
|