tests: save gocryptfs process id in test_helpers.MountPID

This will allow to tests to monitor fd usage and maybe other things.
This commit is contained in:
Jakob Unterwurzacher 2019-01-01 19:34:12 +01:00
parent 60e7a0ca9f
commit 817c485bb7
2 changed files with 36 additions and 5 deletions

View File

@ -76,6 +76,8 @@ func TestMain(m *testing.M) {
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, opts...)
before := test_helpers.ListFds()
r := m.Run()
// Catch fd leaks in the tests. NOTE: this does NOT catch leaks in
// the gocryptfs FUSE process, but only in the tests that access it!
after := test_helpers.ListFds()
if len(before) != len(after) {
fmt.Printf("fd leak? before, after:\n%v\n%v\n", before, after)

View File

@ -12,6 +12,7 @@ import (
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"testing"
@ -44,6 +45,9 @@ var DefaultPlainDir string
// DefaultCipherDir is TmpDir + "/default-cipher"
var DefaultCipherDir string
// PID of the running gocryptfs process. Set by Mount().
var MountPID int
// SwitchTMPDIR changes TMPDIR and hence the directory the test are performed in.
// This is used when you want to perform tests on a special filesystem. The
// xattr tests cannot run on tmpfs and use /var/tmp instead of /tmp.
@ -155,13 +159,11 @@ func InitFS(t *testing.T, extraArgs ...string) string {
// Mount CIPHERDIR "c" on PLAINDIR "p"
// Creates "p" if it does not exist.
func Mount(c string, p string, showOutput bool, extraArgs ...string) error {
var args []string
args = append(args, "-q", "-wpanic", "-nosyslog")
args := []string{"-q", "-wpanic", "-nosyslog", "-fg", fmt.Sprintf("-notifypid=%d", os.Getpid())}
args = append(args, extraArgs...)
//args = append(args, "-fusedebug")
//args = append(args, "-d")
args = append(args, c)
args = append(args, p)
args = append(args, c, p)
if _, err := os.Stat(p); err != nil {
err = os.Mkdir(p, 0777)
@ -190,7 +192,34 @@ func Mount(c string, p string, showOutput bool, extraArgs ...string) error {
}()
}
return cmd.Run()
// Two things can happen:
// 1) The mount fails and the process exits
// 2) The mount succeeds and the process sends us USR1
chanExit := make(chan error, 1)
chanUsr1 := make(chan os.Signal, 1)
signal.Notify(chanUsr1, syscall.SIGUSR1)
// Start the process and save the PID
err := cmd.Start()
if err != nil {
return err
}
MountPID = cmd.Process.Pid
// Wait for exit or usr1
go func() {
chanExit <- cmd.Wait()
}()
select {
case err := <-chanExit:
return err
case <-chanUsr1:
return nil
case <-time.After(1 * time.Second):
log.Panicf("Timeout waiting for process %d", MountPID)
}
return nil
}
// MountOrExit calls Mount() and exits on failure.