cephfs: restrict running of tests that need a mounted fs instance

Add a utility function to test cases that expect the cephfs file system
to be mounted locally and accessible.
Add environment vars to configure the location of the mount point as
well whether the function causes the test to be skipped (the default)
or force an early but clear failure.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-03-19 17:38:42 -04:00 committed by John Mulligan
parent 2c249311ef
commit 0b658baebf
1 changed files with 57 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path"
"syscall" "syscall"
"testing" "testing"
"time" "time"
@ -15,9 +16,49 @@ import (
) )
var ( var (
CephMountTest = "/tmp/ceph/mds/mnt/" CephMountDir = "/tmp/ceph/mds/mnt/"
requireCephMount = false
) )
func init() {
mdir := os.Getenv("GO_CEPH_TEST_MOUNT_DIR")
if mdir != "" {
CephMountDir = mdir
}
reqMount := os.Getenv("GO_CEPH_TEST_REQUIRE_MOUNT")
if reqMount == "yes" || reqMount == "true" {
requireCephMount = true
}
}
func useMount(t *testing.T) {
fail := func(m string) {
if requireCephMount {
t.Fatalf("cephfs mount required: %s %s", CephMountDir, m)
} else {
t.Skipf("cephfs mount needed: %s %s", CephMountDir, m)
}
}
s, err := os.Stat(CephMountDir)
if err != nil || !s.IsDir() {
fail("missing or not a directory")
}
if us, ok := s.Sys().(*syscall.Stat_t); ok {
ps, err := os.Stat(path.Dir(path.Clean(CephMountDir)))
if err != nil {
fail("missing parent directory (race condition?)")
}
if ps.Sys().(*syscall.Stat_t).Dev == us.Dev {
fail("not a mount point")
}
} else {
fail("not a unix-like file system? how did you even compile this?" +
"no, seriously please contact us or file an issue and let us know!")
}
}
func TestCreateMount(t *testing.T) { func TestCreateMount(t *testing.T) {
mount := fsConnect(t) mount := fsConnect(t)
mount, err := CreateMount() mount, err := CreateMount()
@ -79,7 +120,10 @@ func TestChangeDir(t *testing.T) {
} }
func TestRemoveDir(t *testing.T) { func TestRemoveDir(t *testing.T) {
useMount(t)
dirname := "one" dirname := "one"
localPath := path.Join(CephMountDir, dirname)
mount := fsConnect(t) mount := fsConnect(t)
err := mount.MakeDir(dirname, 0755) err := mount.MakeDir(dirname, 0755)
@ -89,15 +133,15 @@ func TestRemoveDir(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
// os.Stat the actual mounted location to verify Makedir/RemoveDir // os.Stat the actual mounted location to verify Makedir/RemoveDir
_, err = os.Stat(CephMountTest + dirname) _, err = os.Stat(localPath)
assert.NoError(t, err) assert.NoError(t, err)
err = mount.RemoveDir(dirname) err = mount.RemoveDir(dirname)
assert.NoError(t, err) assert.NoError(t, err)
_, err = os.Stat(CephMountTest + dirname) _, err = os.Stat(localPath)
assert.EqualError(t, err, assert.EqualError(t, err,
fmt.Sprintf("stat %s: no such file or directory", CephMountTest+dirname)) fmt.Sprintf("stat %s: no such file or directory", localPath))
} }
func TestUnmountMount(t *testing.T) { func TestUnmountMount(t *testing.T) {
@ -127,6 +171,8 @@ func TestReleaseMount(t *testing.T) {
} }
func TestChmodDir(t *testing.T) { func TestChmodDir(t *testing.T) {
useMount(t)
dirname := "two" dirname := "two"
var stats_before uint32 = 0755 var stats_before uint32 = 0755
var stats_after uint32 = 0700 var stats_after uint32 = 0700
@ -139,7 +185,7 @@ func TestChmodDir(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
// os.Stat the actual mounted location to verify Makedir/RemoveDir // os.Stat the actual mounted location to verify Makedir/RemoveDir
stats, err := os.Stat(CephMountTest + dirname) stats, err := os.Stat(path.Join(CephMountDir, dirname))
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, uint32(stats.Mode().Perm()), stats_before) assert.Equal(t, uint32(stats.Mode().Perm()), stats_before)
@ -147,12 +193,15 @@ func TestChmodDir(t *testing.T) {
err = mount.Chmod(dirname, stats_after) err = mount.Chmod(dirname, stats_after)
assert.NoError(t, err) assert.NoError(t, err)
stats, err = os.Stat(CephMountTest + dirname) stats, err = os.Stat(path.Join(CephMountDir, dirname))
assert.NoError(t, err)
assert.Equal(t, uint32(stats.Mode().Perm()), stats_after) assert.Equal(t, uint32(stats.Mode().Perm()), stats_after)
} }
// Not cross-platform, go's os does not specifiy Sys return type // Not cross-platform, go's os does not specifiy Sys return type
func TestChown(t *testing.T) { func TestChown(t *testing.T) {
useMount(t)
dirname := "three" dirname := "three"
// dockerfile creates bob user account // dockerfile creates bob user account
var bob uint32 = 1010 var bob uint32 = 1010
@ -167,7 +216,7 @@ func TestChown(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
// os.Stat the actual mounted location to verify Makedir/RemoveDir // os.Stat the actual mounted location to verify Makedir/RemoveDir
stats, err := os.Stat(CephMountTest + dirname) stats, err := os.Stat(path.Join(CephMountDir, dirname))
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, uint32(stats.Sys().(*syscall.Stat_t).Uid), root) assert.Equal(t, uint32(stats.Sys().(*syscall.Stat_t).Uid), root)
@ -176,7 +225,7 @@ func TestChown(t *testing.T) {
err = mount.Chown(dirname, bob, bob) err = mount.Chown(dirname, bob, bob)
assert.NoError(t, err) assert.NoError(t, err)
stats, err = os.Stat(CephMountTest + dirname) stats, err = os.Stat(path.Join(CephMountDir, dirname))
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, uint32(stats.Sys().(*syscall.Stat_t).Uid), bob) assert.Equal(t, uint32(stats.Sys().(*syscall.Stat_t).Uid), bob)
assert.Equal(t, uint32(stats.Sys().(*syscall.Stat_t).Gid), bob) assert.Equal(t, uint32(stats.Sys().(*syscall.Stat_t).Gid), bob)