@@ -16,11 +16,15 @@ package tsdb
1616import (
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+ }
0 commit comments