github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/local/index_test.go (about) 1 package local_test 2 3 import ( 4 "fmt" 5 "io/fs" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 "github.com/treeverse/lakefs/pkg/local" 12 "github.com/treeverse/lakefs/pkg/uri" 13 ) 14 15 const ( 16 repo = "foo" 17 ref = "bar" 18 uPath = "baz" 19 head = "head" 20 ) 21 22 var ( 23 testPath = uPath 24 testUri = &uri.URI{ 25 Repository: repo, 26 Ref: ref, 27 Path: &testPath, 28 } 29 ) 30 31 func writeIndex(t *testing.T, dir string) { 32 _, err := local.WriteIndex(dir, testUri, head, "") 33 require.NoError(t, err) 34 } 35 36 func TestWriteIndex(t *testing.T) { 37 expectedContent := fmt.Sprintf("src: lakefs://%s/%s/%s\nat_head: %s\nactive_operation: \"\"\n", repo, ref, uPath, head) 38 tmpDir := t.TempDir() 39 writeIndex(t, tmpDir) 40 buf, err := os.ReadFile(filepath.Join(tmpDir, local.IndexFileName)) 41 require.NoError(t, err) 42 require.Equal(t, expectedContent, string(buf)) 43 } 44 45 func TestReadIndex(t *testing.T) { 46 tmpDir := t.TempDir() 47 indexPath := filepath.Join(tmpDir, "path", "to", "index") 48 require.NoError(t, os.MkdirAll(indexPath, os.ModePerm)) 49 writeIndex(t, indexPath) 50 51 // Verify error on no index 52 _, err := local.ReadIndex(tmpDir) 53 require.ErrorIs(t, err, fs.ErrNotExist) 54 55 // Read index from path 56 res, err := local.ReadIndex(indexPath) 57 require.NoError(t, err) 58 require.Equal(t, indexPath, res.LocalPath()) 59 require.Equal(t, head, res.AtHead) 60 require.Equal(t, testUri.String(), res.PathURI) 61 } 62 63 func TestFindIndices(t *testing.T) { 64 root := t.TempDir() 65 indicesFound := []string{ 66 filepath.Join(root, "path", "one"), 67 filepath.Join(root, "path", "two"), 68 filepath.Join(root, "path", "three", "four"), 69 filepath.Join(root, "path", "three", "five", "six"), 70 filepath.Join(root, "path2"), 71 } 72 indicesNotFound := []string{ 73 filepath.Join(root, "path", "one", "shouldNotFind"), 74 filepath.Join(root, "path", "three", "four", "five", "shouldNotFind"), 75 } 76 for _, dir := range append(indicesFound, indicesNotFound...) { 77 require.NoError(t, os.MkdirAll(dir, os.ModePerm)) 78 writeIndex(t, dir) 79 } 80 // Create some files 81 for i, dir := range indicesFound { 82 _, err := os.Create(filepath.Join(dir, fmt.Sprintf("file_%d", i))) 83 require.NoError(t, err) 84 } 85 86 // Check on root 87 dirs, err := local.FindIndices(root) 88 require.NoError(t, err) 89 require.Equal(t, len(indicesFound), len(dirs)) 90 for _, dir := range indicesFound { 91 rel, err := filepath.Rel(root, dir) 92 require.NoError(t, err) 93 require.Contains(t, dirs, rel) 94 } 95 96 // Check on different sub path that was not supposed to be found from root 97 dirs, err = local.FindIndices(filepath.Join(root, "path", "three", "four", "five")) 98 require.NoError(t, err) 99 require.Equal(t, 1, len(dirs)) 100 require.Equal(t, "shouldNotFind", dirs[0]) 101 102 // Create file on root and check only one result 103 writeIndex(t, root) 104 dirs, err = local.FindIndices(filepath.Join(root)) 105 require.NoError(t, err) 106 require.Equal(t, 1, len(dirs)) 107 require.Equal(t, ".", dirs[0]) 108 }