diff options
| author | Joe Richey joerichey@google.com <joerichey@google.com> | 2017-07-18 21:08:02 -0700 |
|---|---|---|
| committer | Joe Richey joerichey@google.com <joerichey@google.com> | 2017-07-18 21:08:02 -0700 |
| commit | 3afdd84a5c0cea217043e9d32ce61e9f6bccf18b (patch) | |
| tree | 9503e9bb95db4938dababa0bfe2819d7cd7d7e56 | |
| parent | f898a826ab24e03019323ade6a8785e2bf463a41 (diff) | |
tests: Unit tests and Integration tests work
Now the testing functions will skip the integration tests if a testing
filesystem is not specified.
| -rw-r--r-- | actions/context_test.go | 10 | ||||
| -rw-r--r-- | filesystem/filesystem_test.go | 31 | ||||
| -rw-r--r-- | metadata/policy_test.go | 25 | ||||
| -rw-r--r-- | util/errors.go | 24 |
4 files changed, 49 insertions, 41 deletions
diff --git a/actions/context_test.go b/actions/context_test.go index a92ef34..4b38a33 100644 --- a/actions/context_test.go +++ b/actions/context_test.go @@ -28,6 +28,7 @@ import ( "time" "github.com/google/fscrypt/util" + "github.com/pkg/errors" ) const testTime = 10 * time.Millisecond @@ -38,7 +39,7 @@ var testContext *Context // Makes a context using the testing locations for the filesystem and // configuration file. func setupContext() (ctx *Context, err error) { - mountpoint, err := util.TestPath() + mountpoint, err := util.TestRoot() if err != nil { return nil, err } @@ -83,8 +84,11 @@ func TestMain(m *testing.M) { var err error testContext, err = setupContext() if err != nil { - fmt.Printf("setupContext() = %v\n", err) - os.Exit(1) + fmt.Println(err) + if errors.Cause(err) != util.ErrSkipIntegration { + os.Exit(1) + } + os.Exit(0) } returnCode := m.Run() diff --git a/filesystem/filesystem_test.go b/filesystem/filesystem_test.go index 2a6baab..04d5123 100644 --- a/filesystem/filesystem_test.go +++ b/filesystem/filesystem_test.go @@ -40,13 +40,12 @@ var ( ) // Gets the mount corresponding to the integration test path. -func getTestMount() (*Mount, error) { - mountpoint, err := util.TestPath() +func getTestMount(t *testing.T) (*Mount, error) { + mountpoint, err := util.TestRoot() if err != nil { - return nil, err + t.Skip(err) } - mnt, err := GetMount(mountpoint) - return mnt, errors.Wrapf(err, util.TestEnvVarName) + return GetMount(mountpoint) } func getFakeProtector() *metadata.ProtectorData { @@ -72,8 +71,8 @@ func getFakePolicy() *metadata.PolicyData { } // Gets the mount and sets it up -func getSetupMount() (*Mount, error) { - mnt, err := getTestMount() +func getSetupMount(t *testing.T) (*Mount, error) { + mnt, err := getTestMount(t) if err != nil { return nil, err } @@ -82,7 +81,7 @@ func getSetupMount() (*Mount, error) { // Tests that the setup works and creates the correct files func TestSetup(t *testing.T) { - mnt, err := getSetupMount() + mnt, err := getSetupMount(t) if err != nil { t.Fatal(err) } @@ -96,7 +95,7 @@ func TestSetup(t *testing.T) { // Tests that we can remove all of the metadata func TestRemoveAllMetadata(t *testing.T) { - mnt, err := getSetupMount() + mnt, err := getSetupMount(t) if err != nil { t.Fatal(err) } @@ -112,7 +111,7 @@ func TestRemoveAllMetadata(t *testing.T) { // Adding a good Protector should succeed, adding a bad one should fail func TestAddProtector(t *testing.T) { - mnt, err := getSetupMount() + mnt, err := getSetupMount(t) if err != nil { t.Fatal(err) } @@ -151,7 +150,7 @@ func TestAddProtector(t *testing.T) { // Adding a good Policy should succeed, adding a bad one should fail func TestAddPolicy(t *testing.T) { - mnt, err := getSetupMount() + mnt, err := getSetupMount(t) if err != nil { t.Fatal(err) } @@ -190,7 +189,7 @@ func TestAddPolicy(t *testing.T) { // Tests that we can set a policy and get it back func TestSetPolicy(t *testing.T) { - mnt, err := getSetupMount() + mnt, err := getSetupMount(t) if err != nil { t.Fatal(err) } @@ -214,7 +213,7 @@ func TestSetPolicy(t *testing.T) { // Tests that we can set a normal protector and get it back func TestSetProtector(t *testing.T) { - mnt, err := getSetupMount() + mnt, err := getSetupMount(t) if err != nil { t.Fatal(err) } @@ -236,8 +235,8 @@ func TestSetProtector(t *testing.T) { } // Gets a setup mount and a fake second mount -func getTwoSetupMounts() (realMnt, fakeMnt *Mount, err error) { - if realMnt, err = getSetupMount(); err != nil { +func getTwoSetupMounts(t *testing.T) (realMnt, fakeMnt *Mount, err error) { + if realMnt, err = getSetupMount(t); err != nil { return } @@ -259,7 +258,7 @@ func cleanupTwoMounts(realMnt, fakeMnt *Mount) { // Tests that we can set a linked protector and get it back func TestLinkedProtector(t *testing.T) { - realMnt, fakeMnt, err := getTwoSetupMounts() + realMnt, fakeMnt, err := getTwoSetupMounts(t) if err != nil { t.Fatal(err) } diff --git a/metadata/policy_test.go b/metadata/policy_test.go index cc6b36f..783a784 100644 --- a/metadata/policy_test.go +++ b/metadata/policy_test.go @@ -37,14 +37,13 @@ var goodPolicy = &PolicyData{ } // Creates a temporary directory for testing. -func createTestDirectory() (directory string, err error) { - baseDirectory, err := util.TestPath() +func createTestDirectory(t *testing.T) (directory string, err error) { + baseDirectory, err := util.TestRoot() if err != nil { - return + t.Skip(err) } if s, err := os.Stat(baseDirectory); err != nil || !s.IsDir() { - return "", fmt.Errorf("%s: %q is not a valid directory", - util.TestEnvVarName, baseDirectory) + return "", fmt.Errorf("test directory %q is not valid", baseDirectory) } directoryPath := filepath.Join(baseDirectory, "test") @@ -53,8 +52,8 @@ func createTestDirectory() (directory string, err error) { // Makes a test directory, makes a file in the directory, and fills the file // with data. Returns the directory name, file name, and error (if one). -func createTestFile() (directory, file string, err error) { - if directory, err = createTestDirectory(); err != nil { +func createTestFile(t *testing.T) (directory, file string, err error) { + if directory, err = createTestDirectory(t); err != nil { return } // Cleanup if the file creation fails @@ -77,7 +76,7 @@ func createTestFile() (directory, file string, err error) { // Tests that we can set a policy on an empty directory func TestSetPolicyEmptyDirectory(t *testing.T) { - directory, err := createTestDirectory() + directory, err := createTestDirectory(t) if err != nil { t.Fatal(err) } @@ -90,7 +89,7 @@ func TestSetPolicyEmptyDirectory(t *testing.T) { // Tests that we cannot set a policy on a nonempty directory func TestSetPolicyNonemptyDirectory(t *testing.T) { - directory, _, err := createTestFile() + directory, _, err := createTestFile(t) if err != nil { t.Fatal(err) } @@ -103,7 +102,7 @@ func TestSetPolicyNonemptyDirectory(t *testing.T) { // Tests that we cannot set a policy on a file func TestSetPolicyFile(t *testing.T) { - directory, file, err := createTestFile() + directory, file, err := createTestFile(t) if err != nil { t.Fatal(err) } @@ -120,7 +119,7 @@ func TestSetPolicyBadDescriptors(t *testing.T) { badDescriptors := []string{"123456789abcde", "xxxxxxxxxxxxxxxx", "0123456789abcdef00"} for _, badDescriptor := range badDescriptors { badPolicy := &PolicyData{KeyDescriptor: badDescriptor, Options: DefaultOptions} - directory, err := createTestDirectory() + directory, err := createTestDirectory(t) if err != nil { t.Fatal(err) } @@ -134,7 +133,7 @@ func TestSetPolicyBadDescriptors(t *testing.T) { // Tests that we get back the same policy that we set on a directory func TestGetPolicyEmptyDirectory(t *testing.T) { - directory, err := createTestDirectory() + directory, err := createTestDirectory(t) if err != nil { t.Fatal(err) } @@ -155,7 +154,7 @@ func TestGetPolicyEmptyDirectory(t *testing.T) { // Tests that we cannot get a policy on an unencrypted directory func TestGetPolicyUnencrypted(t *testing.T) { - directory, err := createTestDirectory() + directory, err := createTestDirectory(t) if err != nil { t.Fatal(err) } diff --git a/util/errors.go b/util/errors.go index 2a865a3..f10569e 100644 --- a/util/errors.go +++ b/util/errors.go @@ -24,6 +24,8 @@ import ( "io" "log" "os" + + "github.com/pkg/errors" ) // ErrReader wraps an io.Reader, passing along calls to Read() until a read @@ -113,17 +115,21 @@ func NeverError(err error) { } } -// TestEnvVarName is the name on an environment variable that should be set to -// an empty mountpoint. This is only used for integration tests. -var TestEnvVarName = "TEST_FILESYSTEM_ROOT" +var ( + // testEnvVarName is the name on an environment variable that should be + // set to an empty mountpoint. This is only used for integration tests. + // If not set, integration tests are skipped. + testEnvVarName = "TEST_FILESYSTEM_ROOT" + // ErrSkipIntegration indicates integration tests shouldn't be run. + ErrSkipIntegration = errors.New("skipping integration test") +) -// TestPath returns a the path specified by TestEnvVarName. The function -// panics if the environment variable is not set. This function is only used for -// integration tests. -func TestPath() (string, error) { - path := os.Getenv(TestEnvVarName) +// TestRoot returns a the root of a filesystem specified by testEnvVarName. This +// function is only used for integration tests. +func TestRoot() (string, error) { + path := os.Getenv(testEnvVarName) if path == "" { - return "", fmt.Errorf("%s: environment variable not set", TestEnvVarName) + return "", ErrSkipIntegration } return path, nil } |