github.com/matrixorigin/matrixone@v1.2.0/pkg/backup/fs_test.go (about) 1 // Copyright 2023 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 backup 16 17 import ( 18 "context" 19 "fmt" 20 "github.com/matrixorigin/matrixone/pkg/fileservice" 21 "github.com/stretchr/testify/assert" 22 "math/rand" 23 "os" 24 "testing" 25 "time" 26 ) 27 28 func Test_writeFile(t *testing.T) { 29 type args struct { 30 ctx context.Context 31 fs fileservice.FileService 32 path string 33 data []byte 34 } 35 36 tDir := os.TempDir() 37 tPath := tDir + "/" + "test" 38 err := os.RemoveAll(tPath) 39 assert.NoError(t, err) 40 etlFS, _, err := setupFilesystem(context.Background(), tPath, true) 41 assert.NoError(t, err) 42 43 etlFS2, _, err := setupFilesystem(context.Background(), tPath, true) 44 assert.NoError(t, err) 45 46 data1 := []byte("abc") 47 data2 := []byte("abc") 48 49 tests := []struct { 50 name string 51 args args 52 wantErr assert.ErrorAssertionFunc 53 }{ 54 { 55 name: "t1", 56 args: args{ 57 ctx: context.Background(), 58 fs: etlFS, 59 path: "t1", 60 data: data1, 61 }, 62 wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { 63 wantData := i[0].([]byte) 64 assert.NoError(t, err) 65 data, err2 := readFileAndCheck(context.Background(), etlFS, "t1") 66 assert.NoError(t, err2) 67 assert.Equal(t, data, wantData) 68 return true 69 }, 70 }, 71 { 72 name: "t2", 73 args: args{ 74 ctx: context.Background(), 75 fs: etlFS2, 76 path: "t2", 77 data: data1, 78 }, 79 wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { 80 wantData := i[0].([]byte) 81 assert.NoError(t, err) 82 data, err2 := readFileAndCheck(context.Background(), etlFS, "t2") 83 assert.NoError(t, err2) 84 assert.Equal(t, data, wantData) 85 return true 86 }, 87 }, 88 } 89 for _, tt := range tests { 90 t.Run(tt.name, func(t *testing.T) { 91 tt.wantErr(t, writeFile(tt.args.ctx, tt.args.fs, tt.args.path, tt.args.data), data2, fmt.Sprintf("writeFile(%v, %v, %v, %v)", tt.args.ctx, tt.args.fs, tt.args.path, tt.args.data)) 92 }) 93 } 94 } 95 96 func getTestFs(t *testing.T, forEtl bool) fileservice.FileService { 97 tPath := getTempDir(t, "test") 98 etlFS, _, err := setupFilesystem(context.Background(), tPath, forEtl) 99 assert.NoError(t, err) 100 return etlFS 101 } 102 103 func getTempDir(t *testing.T, name string) string { 104 tDir := os.TempDir() 105 src := rand.NewSource(time.Now().UnixNano()) 106 tag := fmt.Sprintf("%x", src.Int63()) 107 tPath := tDir + "/" + tag + "/" + name 108 //fmt.Println(tPath) 109 return tPath 110 } 111 112 func getTempFile(t *testing.T, dir, name, content string) *os.File { 113 file, err := os.CreateTemp(dir, name) 114 assert.NoError(t, err) 115 _, err = file.WriteString(content) 116 assert.NoError(t, err) 117 err = file.Sync() 118 assert.NoError(t, err) 119 err = file.Close() 120 assert.NoError(t, err) 121 return file 122 }