diff options
Diffstat (limited to 'actions/context_test.go')
| -rw-r--r-- | actions/context_test.go | 82 |
1 files changed, 51 insertions, 31 deletions
diff --git a/actions/context_test.go b/actions/context_test.go index 74629a3..79adedf 100644 --- a/actions/context_test.go +++ b/actions/context_test.go @@ -20,57 +20,77 @@ package actions import ( + "fmt" + "fscrypt/util" + "log" "os" + "path/filepath" "testing" - - "fscrypt/filesystem" + "time" ) -var mountpoint = os.Getenv("TEST_FILESYSTEM_ROOT") +const testTime = 10 * time.Millisecond + +// holds the context we will use throughout the actions tests +var testContext *Context // Makes a context using the testing locations for the filesystem and // configuration file. -func makeContext() (*Context, error) { - if err := CreateConfigFile(testTime, true); err != nil { +func setupContext() (ctx *Context, err error) { + mountpoint, err := util.TestPath() + if err != nil { return nil, err } - mnt := filesystem.Mount{Path: mountpoint} - if err := mnt.Setup(); err != nil { - return nil, err - } + ConfigFileLocation = filepath.Join(mountpoint, "test.conf") - return NewContextFromMountpoint(mountpoint) -} + // Should not be able to setup without a config file + if badCtx, badCtxErr := NewContextFromMountpoint(mountpoint); badCtxErr == nil { + badCtx.Mount.RemoveAllMetadata() + return nil, fmt.Errorf("created context at %q without config file", badCtx.Mount.Path) + } -// Cleans up the testing config file and testing filesystem data. -func cleaupContext() { - os.RemoveAll(ConfigFileLocation) - mnt := filesystem.Mount{Path: mountpoint} - mnt.RemoveAllMetadata() -} + if err = CreateConfigFile(testTime, true); err != nil { + return nil, err + } + defer func() { + if err != nil { + os.RemoveAll(ConfigFileLocation) + } + }() -// Tests that we can create a context -func TestSetupContext(t *testing.T) { - _, err := makeContext() - defer cleaupContext() + ctx, err = NewContextFromMountpoint(mountpoint) if err != nil { - t.Fatal(err) + return nil, err } + return ctx, ctx.Mount.Setup() } -// Tests that we cannot create a context without a config file. -func TestNoConfigFile(t *testing.T) { - mnt := filesystem.Mount{Path: mountpoint} - if err := mnt.Setup(); err != nil { - t.Fatal(err) +// Cleans up the testing config file and testing filesystem data. +func cleaupContext(ctx *Context) error { + err1 := os.RemoveAll(ConfigFileLocation) + err2 := ctx.Mount.RemoveAllMetadata() + if err1 != nil { + return err1 } + return err2 +} - _, err := NewContextFromMountpoint(mountpoint) - defer cleaupContext() +func TestMain(m *testing.M) { + log.SetFlags(log.LstdFlags | log.Lmicroseconds) + var err error + testContext, err = setupContext() + if err != nil { + fmt.Printf("setupContext() = %v\n", err) + os.Exit(1) + } - if err == nil { - t.Error("should not be able to create context without config file") + returnCode := m.Run() + err = cleaupContext(testContext) + if err != nil { + fmt.Printf("cleanupContext() = %v\n", err) + os.Exit(1) } + os.Exit(returnCode) } |