github.com/matrixorigin/matrixone@v1.2.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, 0, 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  		ctx := context.Background()
    48  		dir := t.TempDir()
    49  
    50  		aPath := filepath.Join(dir, "a")
    51  		err := os.Mkdir(aPath, 0755)
    52  		assert.Nil(t, err)
    53  
    54  		bPath := filepath.Join(dir, "b")
    55  		err = os.Symlink(aPath, bPath)
    56  		assert.Nil(t, err)
    57  
    58  		filePathInA := filepath.Join(aPath, "foo")
    59  		err = os.WriteFile(
    60  			filePathInA,
    61  			[]byte("foo"),
    62  			0644,
    63  		)
    64  		assert.Nil(t, err)
    65  
    66  		filePathInB := filepath.Join(bPath, "foo")
    67  		fs, readPath, err := GetForETL(ctx, nil, filePathInB)
    68  		assert.Nil(t, err)
    69  
    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  }
   121  
   122  func TestLocalETLFSEmptyRootPath(t *testing.T) {
   123  	fs, err := NewLocalETLFS(
   124  		"test",
   125  		"",
   126  	)
   127  	assert.Nil(t, err)
   128  	assert.NotNil(t, fs)
   129  }
   130  
   131  func TestLocalETLFSWithBadSymlink(t *testing.T) {
   132  	ctx := context.Background()
   133  	dir := t.TempDir()
   134  	err := os.Symlink(
   135  		"file-not-exists-fdsafdsafdsa",
   136  		filepath.Join(dir, "foo"),
   137  	)
   138  	assert.Nil(t, err)
   139  	fs, err := NewLocalETLFS("test", dir)
   140  	assert.Nil(t, err)
   141  	_, err = fs.List(ctx, "")
   142  	assert.Nil(t, err)
   143  }