Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit 29b7209

Browse files
committed
add-go-fuse-to-inject-filesystem-error
Signed-off-by: qiffang <[email protected]>
1 parent b97c06f commit 29b7209

File tree

3 files changed

+89
-77
lines changed

3 files changed

+89
-77
lines changed

compact_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ package tsdb
1616
import (
1717
"context"
1818
"fmt"
19+
"github.com/hanwen/go-fuse/fuse"
20+
testutil2 "github.com/qiffang/tsdb/testutil"
1921
"io/ioutil"
2022
"math"
2123
"os"
2224
"path"
2325
"path/filepath"
26+
"strings"
27+
"syscall"
2428
"testing"
2529
"time"
2630

@@ -1058,3 +1062,87 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) {
10581062
})
10591063
}
10601064
}
1065+
1066+
func TestCreateBlockWithHook(t *testing.T) {
1067+
//init acgtion
1068+
original := filepath.Join(string(filepath.Separator), "tmp", fmt.Sprintf("dev-%d", time.Now().Unix()))
1069+
mountpoint := filepath.Join(string(filepath.Separator), "tmp", fmt.Sprintf("mountpoint-%d", time.Now().Unix()))
1070+
server := newFuseServer(t, original, mountpoint)
1071+
//remember to call unmount after you do not use it
1072+
defer cleanUp(server, mountpoint, original)
1073+
1074+
//normal logic
1075+
createBlock(t, mountpoint, genSeries(1, 1, 200, 300))
1076+
1077+
dir, _ := ioutil.ReadDir(mountpoint)
1078+
testutil.Equals(t, 0, len(dir))
1079+
}
1080+
1081+
func TestOpenBlockWithHook(t *testing.T) {
1082+
//init action
1083+
original := filepath.Join(string(filepath.Separator), "tmp", fmt.Sprintf("dev-%d", time.Now().Unix()))
1084+
mountpoint := filepath.Join(string(filepath.Separator), "tmp", fmt.Sprintf("mountpoint-%d", time.Now().Unix()))
1085+
path := createBlock(t, original, genSeries(1, 1, 200, 300))
1086+
_, file := filepath.Split(path)
1087+
server := newFuseServer(t, original, mountpoint)
1088+
//remember to call unmount after you do not use it
1089+
defer cleanUp(server, mountpoint, original)
1090+
1091+
//normal logic
1092+
OpenBlock(nil, filepath.Join(mountpoint, file), nil)
1093+
dir, _ := ioutil.ReadDir(filepath.Join(mountpoint, file))
1094+
1095+
testutil.Equals(t, true, len(dir) > 0)
1096+
hasTempFile := false
1097+
for _, info := range dir {
1098+
if strings.HasSuffix(info.Name(), "tmp") {
1099+
hasTempFile = true
1100+
break
1101+
}
1102+
}
1103+
1104+
testutil.Equals(t, true, hasTempFile)
1105+
}
1106+
1107+
func newFuseServer(t *testing.T, original, mountpoint string)(*fuse.Server){
1108+
createDirIfAbsent(original)
1109+
createDirIfAbsent(mountpoint)
1110+
fs, err := testutil2.NewHookFs(original, mountpoint, &TestRenameHook{})
1111+
testutil.Ok(t, err)
1112+
server, err := fs.NewServe()
1113+
if err != nil {
1114+
fmt.Printf("start server failed, %v \n", err)
1115+
}
1116+
testutil.Ok(t, err)
1117+
go func(){
1118+
fs.Start(server)
1119+
}()
1120+
1121+
return server
1122+
}
1123+
1124+
func cleanUp(server *fuse.Server, mountpoint string, original string) {
1125+
server.Unmount()
1126+
syscall.Unmount(mountpoint, -1)
1127+
1128+
os.RemoveAll(mountpoint)
1129+
os.RemoveAll(original)
1130+
fmt.Println("Done")
1131+
}
1132+
1133+
func createDirIfAbsent(name string) {
1134+
_, err := os.Stat(name)
1135+
if err != nil {
1136+
os.Mkdir(name, os.ModePerm)
1137+
}
1138+
}
1139+
1140+
type TestRenameHook struct {}
1141+
1142+
func (h *TestRenameHook) PreRename(oldPatgh string, newPath string) (hooked bool, err error) {
1143+
fmt.Printf("renamed file from %s to %s \n", oldPatgh, newPath)
1144+
return true, syscall.EIO
1145+
}
1146+
func (h *TestRenameHook) PostRename(oldPatgh string, newPath string) (hooked bool, err error) {
1147+
return false, nil
1148+
}

fileutil/preallocate_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func preallocFixed(f *os.File, sizeInBytes int64) error {
3434
Length: sizeInBytes}
3535
p := unsafe.Pointer(fstore)
3636
_, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_PREALLOCATE), uintptr(p))
37-
if errno == 0 || errno == syscall.ENOTSUP {
37+
if errno == 0 || errno == syscall.ENOTSUP || errno == syscall.EINVAL {
3838
return nil
3939
}
4040
return errno

testutil/hook_test.go

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)