github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/externalresource/internal/local/file_utils.go (about) 1 // Copyright 2022 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package local 15 16 import ( 17 "context" 18 "encoding/hex" 19 "path/filepath" 20 "testing" 21 22 brStorage "github.com/pingcap/tidb/br/pkg/storage" 23 frameModel "github.com/pingcap/tiflow/engine/framework/model" 24 resModel "github.com/pingcap/tiflow/engine/pkg/externalresource/model" 25 "github.com/pingcap/tiflow/pkg/errors" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func newBrStorageForLocalFile(filePath string) (brStorage.ExternalStorage, error) { 30 backend, err := brStorage.ParseBackend(filePath, nil) 31 if err != nil { 32 return nil, err 33 } 34 ls, err := brStorage.New(context.Background(), backend, nil) 35 if err != nil { 36 retErr := errors.ErrFailToCreateExternalStorage.Wrap(err) 37 return nil, retErr.GenWithStackByArgs("creating ExternalStorage for local file") 38 } 39 return ls, nil 40 } 41 42 // ResourceNameToFilePathName converts a resource name to a file path name. 43 func ResourceNameToFilePathName(resName resModel.ResourceName) string { 44 return hex.EncodeToString([]byte(resName)) 45 } 46 47 func filePathNameToResourceName(filePath string) (resModel.ResourceName, error) { 48 result, err := hex.DecodeString(filePath) 49 if err != nil { 50 return "", errors.Trace(err) 51 } 52 return resModel.ResourceName(result), nil 53 } 54 55 func localPathWithEncoding(baseDir string, 56 creator frameModel.WorkerID, 57 resName resModel.ResourceName, 58 suffixes ...string, 59 ) string { 60 joinSegments := []string{ 61 baseDir, creator, ResourceNameToFilePathName(resName), 62 } 63 joinSegments = append(joinSegments, suffixes...) 64 return filepath.Join(joinSegments...) 65 } 66 67 // AssertLocalFileExists is a test helper. 68 func AssertLocalFileExists( 69 t *testing.T, 70 baseDir string, 71 creator frameModel.WorkerID, 72 resName resModel.ResourceName, 73 suffixes ...string, 74 ) { 75 require.FileExistsf(t, localPathWithEncoding(baseDir, creator, resName, suffixes...), 76 "local file does not exist: baseDir %s, creator %s, resName %s", 77 baseDir, creator, resName) 78 } 79 80 // AssertNoLocalFileExists is a test helper. 81 func AssertNoLocalFileExists( 82 t *testing.T, 83 baseDir string, 84 creator frameModel.WorkerID, 85 resName resModel.ResourceName, 86 suffixes ...string, 87 ) { 88 require.NoFileExists(t, localPathWithEncoding(baseDir, creator, resName, suffixes...), 89 "local file does not exist: baseDir %s, creator %s, resName %s", 90 baseDir, creator, resName) 91 }