github.com/jstaf/onedriver@v0.14.2-0.20240420231225-f07678f9e6ef/fs/offline/setup_test.go (about)

     1  package offline
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"os/signal"
     8  	"path/filepath"
     9  	"strings"
    10  	"syscall"
    11  	"testing"
    12  
    13  	"github.com/hanwen/go-fuse/v2/fuse"
    14  	"github.com/jstaf/onedriver/fs"
    15  	"github.com/jstaf/onedriver/fs/graph"
    16  	"github.com/rs/zerolog"
    17  	"github.com/rs/zerolog/log"
    18  )
    19  
    20  const (
    21  	mountLoc  = "mount"
    22  	testDBLoc = "tmp"
    23  	TestDir   = mountLoc + "/onedriver_tests"
    24  )
    25  
    26  var auth *graph.Auth
    27  
    28  // Like the graph package, but designed for running tests offline.
    29  func TestMain(m *testing.M) {
    30  	if wd, _ := os.Getwd(); strings.HasSuffix(wd, "/offline") {
    31  		// depending on how this test gets launched, the working directory can be wrong
    32  		os.Chdir("../..")
    33  	}
    34  
    35  	// attempt to unmount regardless of what happens (in case previous tests
    36  	// failed and didn't clean themselves up)
    37  	exec.Command("fusermount3", "-uz", mountLoc).Run()
    38  	os.Mkdir(mountLoc, 0755)
    39  
    40  	auth = graph.Authenticate(graph.AuthConfig{}, ".auth_tokens.json", false)
    41  	inode, err := graph.GetItem("root", auth)
    42  	if inode != nil || !graph.IsOffline(err) {
    43  		fmt.Println("These tests must be run offline.")
    44  		os.Exit(1)
    45  	}
    46  
    47  	f, _ := os.OpenFile("fusefs_tests.log", os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0644)
    48  	zerolog.SetGlobalLevel(zerolog.TraceLevel)
    49  	log.Logger = log.Output(zerolog.ConsoleWriter{Out: f, TimeFormat: "15:04:05"})
    50  	defer f.Close()
    51  	log.Info().Msg("Setup offline tests ------------------------------")
    52  
    53  	// reuses the cached data from the previous tests
    54  	server, _ := fuse.NewServer(
    55  		fs.NewFilesystem(auth, filepath.Join(testDBLoc, "test")),
    56  		mountLoc,
    57  		&fuse.MountOptions{
    58  			Name:          "onedriver",
    59  			FsName:        "onedriver",
    60  			DisableXAttrs: true,
    61  			MaxBackground: 1024,
    62  		},
    63  	)
    64  
    65  	// setup sigint handler for graceful unmount on interrupt/terminate
    66  	sigChan := make(chan os.Signal, 1)
    67  	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT)
    68  	go fs.UnmountHandler(sigChan, server)
    69  
    70  	// mount fs in background thread
    71  	go server.Serve()
    72  
    73  	log.Info().Msg("Start offline tests ------------------------------")
    74  	code := m.Run()
    75  	log.Info().Msg("Finish offline tests ------------------------------")
    76  
    77  	if server.Unmount() != nil {
    78  		log.Error().Msg("Failed to unmount test fuse server, attempting lazy unmount")
    79  		exec.Command("fusermount3", "-zu", "mount").Run()
    80  	}
    81  	fmt.Println("Successfully unmounted fuse server!")
    82  	os.Exit(code)
    83  }