From 3767eb2675a12f0d3631610307da7c7721ca0a0c Mon Sep 17 00:00:00 2001 From: Matthieu Rakotojaona Date: Sun, 19 Jul 2015 23:02:48 +0200 Subject: [PATCH] Unmount and remove directory for mount in tests --- cmd/restic/cmd_mount.go | 28 ++++++++++++++++++++++------ cmd/restic/integration_fuse_test.go | 24 ++++++++++++------------ cmd/restic/integration_test.go | 5 ----- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index 01ebe7313..d685af98a 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -15,6 +15,7 @@ import ( type CmdMount struct { global *GlobalOptions ready chan struct{} + done chan struct{} } func init() { @@ -24,6 +25,7 @@ func init() { &CmdMount{ global: &globalOpts, ready: make(chan struct{}, 1), + done: make(chan struct{}), }) if err != nil { panic(err) @@ -78,11 +80,25 @@ func (cmd CmdMount) Execute(args []string) error { cmd.ready <- struct{}{} - err = fs.Serve(c, &root) - if err != nil { - return err - } + errServe := make(chan error) + go func() { + err = fs.Serve(c, &root) + if err != nil { + errServe <- err + } - <-c.Ready - return c.MountError + <-c.Ready + errServe <- c.MountError + }() + + select { + case err := <-errServe: + return err + case <-cmd.done: + err := c.Close() + if err != nil { + cmd.global.Printf("Error closing fuse connection: %s\n", err) + } + return systemFuse.Unmount(mountpoint) + } } diff --git a/cmd/restic/integration_fuse_test.go b/cmd/restic/integration_fuse_test.go index b09e61eb0..1c4cce39f 100644 --- a/cmd/restic/integration_fuse_test.go +++ b/cmd/restic/integration_fuse_test.go @@ -10,8 +10,6 @@ import ( "testing" "time" - "bazil.org/fuse" - "github.com/restic/restic" "github.com/restic/restic/backend" "github.com/restic/restic/repository" @@ -53,6 +51,15 @@ func waitForMount(dir string) error { return fmt.Errorf("subdir %q of dir %s never appeared", mountTestSubdir, dir) } + +func cmdMount(t testing.TB, global GlobalOptions, dir string, ready, done chan struct{}) { + cmd := &CmdMount{global: &global, ready: ready, done: done} + OK(t, cmd.Execute([]string{dir})) + if TestCleanup { + OK(t, os.RemoveAll(dir)) + } +} + func TestMount(t *testing.T) { if !RunFuseTest { t.Skip("Skipping fuse tests") @@ -97,17 +104,10 @@ func TestMount(t *testing.T) { OK(t, os.RemoveAll(mountpoint)) ready := make(chan struct{}, 1) - go cmdMount(t, global, mountpoint, ready) + done := make(chan struct{}) + go cmdMount(t, global, mountpoint, ready, done) <-ready - - defer func() { - err := fuse.Unmount(mountpoint) - OK(t, err) - if TestCleanup { - err = os.RemoveAll(mountpoint) - OK(t, err) - } - }() + defer close(done) OK(t, waitForMount(mountpoint)) mountpointDir, err := os.Open(mountpoint) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 4bbad4861..b06b85745 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -73,11 +73,6 @@ func cmdCheck(t testing.TB, global GlobalOptions) { OK(t, cmd.Execute(nil)) } -func cmdMount(t testing.TB, global GlobalOptions, dir string, ready chan struct{}) { - cmd := &CmdMount{global: &global, ready: ready} - OK(t, cmd.Execute([]string{dir})) -} - func TestBackup(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz")