github.com/matrixorigin/matrixone@v0.7.0/pkg/fileservice/local_etl_fs_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package fileservice 16 17 import ( 18 "context" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestLocalETLFS(t *testing.T) { 27 28 t.Run("file service", func(t *testing.T) { 29 testFileService(t, func(name string) FileService { 30 dir := t.TempDir() 31 fs, err := NewLocalETLFS(name, dir) 32 assert.Nil(t, err) 33 return fs 34 }) 35 }) 36 37 t.Run("mutable file service", func(t *testing.T) { 38 testMutableFileService(t, func() MutableFileService { 39 dir := t.TempDir() 40 fs, err := NewLocalETLFS("etl", dir) 41 assert.Nil(t, err) 42 return fs 43 }) 44 }) 45 46 t.Run("symlink to dir", func(t *testing.T) { 47 dir := t.TempDir() 48 49 aPath := filepath.Join(dir, "a") 50 err := os.Mkdir(aPath, 0755) 51 assert.Nil(t, err) 52 53 bPath := filepath.Join(dir, "b") 54 err = os.Symlink(aPath, bPath) 55 assert.Nil(t, err) 56 57 filePathInA := filepath.Join(aPath, "foo") 58 err = os.WriteFile( 59 filePathInA, 60 []byte("foo"), 61 0644, 62 ) 63 assert.Nil(t, err) 64 65 filePathInB := filepath.Join(bPath, "foo") 66 fs, readPath, err := GetForETL(nil, filePathInB) 67 assert.Nil(t, err) 68 69 ctx := context.Background() 70 vec := IOVector{ 71 FilePath: readPath, 72 Entries: []IOEntry{ 73 { 74 Size: -1, 75 }, 76 }, 77 } 78 err = fs.Read(ctx, &vec) 79 assert.Nil(t, err) 80 assert.Equal(t, []byte("foo"), vec.Entries[0].Data) 81 82 entries, err := fs.List(ctx, "") 83 assert.Nil(t, err) 84 assert.Equal(t, 1, len(entries)) 85 assert.Equal(t, "foo", entries[0].Name) 86 87 }) 88 89 t.Run("deref symlink", func(t *testing.T) { 90 dir := t.TempDir() 91 92 aPath := filepath.Join(dir, "a") 93 err := os.Mkdir(aPath, 0755) 94 assert.Nil(t, err) 95 96 bPath := filepath.Join(dir, "b") 97 err = os.Symlink(aPath, bPath) 98 assert.Nil(t, err) 99 100 filePathInA := filepath.Join(aPath, "foo") 101 err = os.WriteFile( 102 filePathInA, 103 []byte("foo"), 104 0644, 105 ) 106 assert.Nil(t, err) 107 108 fs, err := NewLocalETLFS("foo", dir) 109 assert.Nil(t, err) 110 111 ctx := context.Background() 112 entries, err := fs.List(ctx, "") 113 assert.Nil(t, err) 114 assert.Equal(t, 2, len(entries)) 115 assert.True(t, entries[0].IsDir) 116 assert.True(t, entries[1].IsDir) 117 118 }) 119 120 }