github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/test/compatibility/utils.go (about) 1 // Copyright 2021 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 compatibility 16 17 import ( 18 "fmt" 19 "os" 20 "path/filepath" 21 22 "github.com/matrixorigin/matrixone/pkg/common/moerr" 23 "github.com/matrixorigin/matrixone/pkg/container/types" 24 ) 25 26 var ( 27 PrepareDir = "prepare" 28 ) 29 30 func GetPrepareDirName() string { 31 return filepath.Join("/tmp", "tae-compatibility-test", PrepareDir) 32 } 33 34 func GetPrepareVersionName() string { 35 return filepath.Join(GetPrepareDirName(), "version") 36 } 37 38 func RemovePrepareDir() (string, error) { 39 dir := GetPrepareDirName() 40 return dir, os.RemoveAll(dir) 41 } 42 43 func InitPrepareEnv() error { 44 dir, _ := RemovePrepareDir() 45 if err := os.MkdirAll(dir, 0755); err != nil { 46 return err 47 } 48 file, err := os.OpenFile( 49 GetPrepareVersionName(), 50 os.O_RDWR|os.O_CREATE, 51 os.ModePerm) 52 if err != nil { 53 return err 54 } 55 defer file.Close() 56 ver := int64(version) 57 buf := types.EncodeInt64(&ver) 58 _, err = file.Write(buf) 59 return err 60 } 61 62 func ReadPrepareVersion() (int, error) { 63 versionFileName := GetPrepareVersionName() 64 if _, err := os.Stat(versionFileName); err != nil { 65 return 0, err 66 } 67 file, err := os.OpenFile( 68 versionFileName, 69 os.O_RDONLY, 70 os.ModePerm) 71 if err != nil { 72 return 0, err 73 } 74 defer file.Close() 75 buf := make([]byte, 8) 76 if _, err = file.Read(buf); err != nil { 77 return 0, err 78 } 79 ver := int(types.DecodeInt64(buf)) 80 return ver, nil 81 } 82 83 func EnsurePrepareEnvOK() error { 84 ver, err := ReadPrepareVersion() 85 if err != nil { 86 return err 87 } 88 89 if ver > version { 90 return moerr.NewInternalErrorNoCtx(fmt.Sprintf("prepare env version is %d, but current version is %d", ver, version)) 91 } 92 93 return nil 94 } 95 96 func GetPrepareDirByName(name string) string { 97 return filepath.Join("/tmp", "tae-compatibility-test", PrepareDir, name) 98 } 99 100 func InitPrepareDirByName(name string) (string, error) { 101 dir := GetPrepareDirByName(name) 102 os.RemoveAll(dir) 103 if err := os.MkdirAll(dir, 0755); err != nil { 104 return "", err 105 } 106 return dir, nil 107 } 108 109 func GetExecureDirName() string { 110 return filepath.Join("/tmp", "tae-compatibility-test", "execute") 111 } 112 113 func RemoveExecuteDir() (string, error) { 114 dir := GetExecureDirName() 115 return dir, os.RemoveAll(dir) 116 } 117 118 func InitExecuteEnv() error { 119 dir, _ := RemoveExecuteDir() 120 return os.MkdirAll(dir, 0755) 121 } 122 123 func InitTestCaseExecuteDir(name string) (string, error) { 124 dir := filepath.Join(GetExecureDirName(), name) 125 err := os.RemoveAll(dir) 126 return dir, err 127 } 128 129 func CopyDir(src, dst string) error { 130 return filepath.Walk(src, func(path string, info os.FileInfo, _ error) error { 131 if info.IsDir() { 132 rel, err := filepath.Rel(src, path) 133 if err != nil { 134 return err 135 } 136 return os.Mkdir(filepath.Join(dst, rel), 0755) 137 } 138 rel, err := filepath.Rel(src, path) 139 if err != nil { 140 return err 141 } 142 return CopyFile(path, filepath.Join(dst, rel)) 143 }) 144 } 145 146 // Copy file from src to dst 147 func CopyFile(src, dst string) error { 148 srcFile, err := os.Open(src) 149 if err != nil { 150 return err 151 } 152 defer srcFile.Close() 153 dstFile, err := os.OpenFile( 154 dst, 155 os.O_RDWR|os.O_CREATE, 156 os.ModePerm) 157 if err != nil { 158 return err 159 } 160 defer dstFile.Close() 161 _, err = dstFile.ReadFrom(srcFile) 162 return err 163 }