2019-12-12 20:00:51 +00:00
|
|
|
package cephfs
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2018-10-09 04:26:51 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-08 14:44:09 +00:00
|
|
|
"io"
|
2018-10-09 04:26:51 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2019-12-20 12:51:53 +00:00
|
|
|
"time"
|
2019-12-05 16:53:41 +00:00
|
|
|
|
2019-12-18 22:28:06 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
|
|
|
|
2019-12-05 16:53:41 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-09 04:26:51 +00:00
|
|
|
)
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2018-10-10 22:06:47 +00:00
|
|
|
var (
|
2021-03-12 20:50:15 +00:00
|
|
|
testMdsName = "Z"
|
2018-10-10 22:06:47 +00:00
|
|
|
)
|
|
|
|
|
2020-03-19 21:38:42 +00:00
|
|
|
func init() {
|
2020-03-23 18:41:05 +00:00
|
|
|
mdsName := os.Getenv("GO_CEPH_TEST_MDS_NAME")
|
|
|
|
if mdsName != "" {
|
|
|
|
testMdsName = mdsName
|
|
|
|
}
|
2020-03-19 21:38:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 19:35:40 +00:00
|
|
|
func TestCreateMount(t *testing.T) {
|
2019-12-12 20:00:51 +00:00
|
|
|
mount, err := CreateMount()
|
2015-07-08 08:34:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, mount)
|
2020-03-23 20:55:42 +00:00
|
|
|
assert.NoError(t, mount.Release())
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 19:46:37 +00:00
|
|
|
func fsConnect(t require.TestingT) *MountInfo {
|
2019-12-12 20:00:51 +00:00
|
|
|
mount, err := CreateMount()
|
2019-12-20 12:51:53 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-05 16:53:41 +00:00
|
|
|
require.NotNil(t, mount)
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2015-07-08 08:34:54 +00:00
|
|
|
err = mount.ReadDefaultConfigFile()
|
2019-12-20 12:51:53 +00:00
|
|
|
require.NoError(t, err)
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2019-12-20 12:51:53 +00:00
|
|
|
timeout := time.After(time.Second * 5)
|
|
|
|
ch := make(chan error)
|
|
|
|
go func(mount *MountInfo) {
|
|
|
|
ch <- mount.Mount()
|
|
|
|
}(mount)
|
|
|
|
select {
|
|
|
|
case err = <-ch:
|
|
|
|
case <-timeout:
|
|
|
|
err = fmt.Errorf("timed out waiting for connect")
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
return mount
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 19:46:37 +00:00
|
|
|
func fsDisconnect(t assert.TestingT, mount *MountInfo) {
|
2020-03-23 21:11:12 +00:00
|
|
|
assert.NoError(t, mount.Unmount())
|
|
|
|
assert.NoError(t, mount.Release())
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:51:53 +00:00
|
|
|
func TestMountRoot(t *testing.T) {
|
2020-03-23 21:11:12 +00:00
|
|
|
mount := fsConnect(t)
|
|
|
|
fsDisconnect(t, mount)
|
2019-12-20 12:51:53 +00:00
|
|
|
}
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2019-12-20 12:51:53 +00:00
|
|
|
func TestSyncFs(t *testing.T) {
|
|
|
|
mount := fsConnect(t)
|
2020-03-23 21:11:12 +00:00
|
|
|
defer fsDisconnect(t, mount)
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2019-12-20 12:51:53 +00:00
|
|
|
err := mount.SyncFs()
|
2015-07-08 08:34:54 +00:00
|
|
|
assert.NoError(t, err)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:26:51 +00:00
|
|
|
func TestUnmountMount(t *testing.T) {
|
2019-12-20 12:51:53 +00:00
|
|
|
t.Run("neverMounted", func(t *testing.T) {
|
|
|
|
mount, err := CreateMount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, mount)
|
|
|
|
assert.False(t, mount.IsMounted())
|
2020-03-23 20:58:15 +00:00
|
|
|
assert.NoError(t, mount.Release())
|
2019-12-20 12:51:53 +00:00
|
|
|
})
|
|
|
|
t.Run("mountUnmount", func(t *testing.T) {
|
|
|
|
mount := fsConnect(t)
|
2020-03-23 20:58:15 +00:00
|
|
|
defer func() { assert.NoError(t, mount.Release()) }()
|
2019-12-20 12:51:53 +00:00
|
|
|
assert.True(t, mount.IsMounted())
|
|
|
|
|
|
|
|
err := mount.Unmount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, mount.IsMounted())
|
|
|
|
})
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReleaseMount(t *testing.T) {
|
2019-12-12 20:00:51 +00:00
|
|
|
mount, err := CreateMount()
|
2018-10-09 04:26:51 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-05 16:53:41 +00:00
|
|
|
require.NotNil(t, mount)
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2020-03-23 21:00:29 +00:00
|
|
|
assert.NoError(t, mount.Release())
|
|
|
|
// call release again to ensure idempotency of the func
|
|
|
|
assert.NoError(t, mount.Release())
|
2018-10-10 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:28:06 +00:00
|
|
|
func radosConnect(t *testing.T) *rados.Conn {
|
|
|
|
conn, err := rados.NewConn()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = conn.ReadDefaultConfigFile()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
timeout := time.After(time.Second * 5)
|
|
|
|
ch := make(chan error)
|
|
|
|
go func(conn *rados.Conn) {
|
|
|
|
ch <- conn.Connect()
|
|
|
|
}(conn)
|
|
|
|
select {
|
|
|
|
case err = <-ch:
|
|
|
|
case <-timeout:
|
|
|
|
err = fmt.Errorf("timed out waiting for connect")
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFromRados(t *testing.T) {
|
|
|
|
conn := radosConnect(t)
|
|
|
|
mount, err := CreateFromRados(conn)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, mount)
|
|
|
|
}
|
2019-12-19 20:11:50 +00:00
|
|
|
|
|
|
|
func TestCreateMountWithId(t *testing.T) {
|
2020-01-28 18:12:33 +00:00
|
|
|
mount, err := CreateMountWithId("bobolink")
|
2019-12-19 20:11:50 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, mount)
|
2020-03-23 20:58:15 +00:00
|
|
|
defer func() { assert.NoError(t, mount.Release()) }()
|
2019-12-19 20:11:50 +00:00
|
|
|
|
|
|
|
err = mount.ReadDefaultConfigFile()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = mount.Mount()
|
|
|
|
assert.NoError(t, err)
|
2020-03-23 20:58:15 +00:00
|
|
|
defer func() { assert.NoError(t, mount.Unmount()) }()
|
2020-01-28 18:12:33 +00:00
|
|
|
|
|
|
|
// verify the custom entity_id is visible in the 'session ls' output
|
|
|
|
// of mds.
|
|
|
|
cmd := []byte(`{"prefix": "session ls"}`)
|
|
|
|
buf, info, err := mount.MdsCommand(
|
2020-03-23 18:41:05 +00:00
|
|
|
testMdsName,
|
2020-01-28 18:12:33 +00:00
|
|
|
[][]byte{cmd})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, "", string(buf))
|
|
|
|
assert.Equal(t, "", string(info))
|
|
|
|
assert.Contains(t, string(buf), `"bobolink"`)
|
2019-12-19 20:11:50 +00:00
|
|
|
}
|
2020-01-28 18:09:29 +00:00
|
|
|
|
2020-02-13 22:18:01 +00:00
|
|
|
func TestMountWithRoot(t *testing.T) {
|
|
|
|
bMount := fsConnect(t)
|
2020-03-23 21:11:12 +00:00
|
|
|
defer fsDisconnect(t, bMount)
|
2020-02-13 22:18:01 +00:00
|
|
|
|
|
|
|
dir1 := "/test-mount-with-root"
|
|
|
|
err := bMount.MakeDir(dir1, 0755)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer bMount.RemoveDir(dir1)
|
|
|
|
|
|
|
|
sub1 := "/i.was.here"
|
|
|
|
dir2 := dir1 + sub1
|
|
|
|
err = bMount.MakeDir(dir2, 0755)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer bMount.RemoveDir(dir2)
|
|
|
|
|
|
|
|
t.Run("withRoot", func(t *testing.T) {
|
|
|
|
mount, err := CreateMount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, mount)
|
|
|
|
defer func() {
|
|
|
|
assert.NoError(t, mount.Release())
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = mount.ReadDefaultConfigFile()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = mount.MountWithRoot(dir1)
|
|
|
|
assert.NoError(t, err)
|
2020-03-23 21:11:21 +00:00
|
|
|
defer func() {
|
|
|
|
assert.NoError(t, mount.Unmount())
|
|
|
|
}()
|
2020-02-13 22:18:01 +00:00
|
|
|
|
|
|
|
err = mount.ChangeDir(sub1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("badRoot", func(t *testing.T) {
|
|
|
|
mount, err := CreateMount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, mount)
|
|
|
|
defer func() {
|
|
|
|
assert.NoError(t, mount.Release())
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = mount.ReadDefaultConfigFile()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = mount.MountWithRoot("/i-yam-what-i-yam")
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2020-03-04 18:44:42 +00:00
|
|
|
|
|
|
|
func TestGetSetConfigOption(t *testing.T) {
|
|
|
|
// we don't need an active connection for this, just the handle
|
|
|
|
mount, err := CreateMount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, mount)
|
2020-03-23 20:58:15 +00:00
|
|
|
defer func() { assert.NoError(t, mount.Release()) }()
|
2020-03-04 18:44:42 +00:00
|
|
|
|
|
|
|
err = mount.SetConfigOption("__dne__", "value")
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = mount.GetConfigOption("__dne__")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
origVal, err := mount.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = mount.SetConfigOption("log_file", "/dev/null")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
currVal, err := mount.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dev/null", currVal)
|
|
|
|
|
|
|
|
err = mount.SetConfigOption("log_file", origVal)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
currVal, err = mount.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, origVal, currVal)
|
|
|
|
}
|
2020-05-04 07:42:08 +00:00
|
|
|
|
2020-06-11 18:37:51 +00:00
|
|
|
func TestParseConfigArgv(t *testing.T) {
|
|
|
|
mount, err := CreateMount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, mount)
|
|
|
|
defer func() { assert.NoError(t, mount.Release()) }()
|
|
|
|
|
|
|
|
origVal, err := mount.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = mount.ParseConfigArgv(
|
|
|
|
[]string{"cephfs.test", "--log_file", "/dev/null"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
currVal, err := mount.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dev/null", currVal)
|
|
|
|
assert.NotEqual(t, "/dev/null", origVal)
|
|
|
|
|
|
|
|
// ensure that an empty slice triggers an error (not a crash)
|
|
|
|
err = mount.ParseConfigArgv([]string{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// ensure we get an error for an invalid mount value
|
|
|
|
badMount := &MountInfo{}
|
|
|
|
err = badMount.ParseConfigArgv(
|
|
|
|
[]string{"cephfs.test", "--log_file", "/dev/null"})
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:46:25 +00:00
|
|
|
func TestParseDefaultConfigEnv(t *testing.T) {
|
|
|
|
mount, err := CreateMount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, mount)
|
|
|
|
defer func() { assert.NoError(t, mount.Release()) }()
|
|
|
|
|
|
|
|
origVal, err := mount.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Setenv("CEPH_ARGS", "--log_file /dev/null")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = mount.ParseDefaultConfigEnv()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
currVal, err := mount.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/dev/null", currVal)
|
|
|
|
assert.NotEqual(t, "/dev/null", origVal)
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:42:08 +00:00
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
mount, err := CreateMount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, mount)
|
|
|
|
defer assert.NoError(t, mount.Release())
|
|
|
|
|
|
|
|
t.Run("mountCurrentDir", func(t *testing.T) {
|
|
|
|
path := mount.CurrentDir()
|
|
|
|
assert.Equal(t, path, "")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mountChangeDir", func(t *testing.T) {
|
|
|
|
err := mount.ChangeDir("someDir")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, ErrNotConnected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mountMakeDir", func(t *testing.T) {
|
|
|
|
err := mount.MakeDir("someName", 0444)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, ErrNotConnected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mountRemoveDir", func(t *testing.T) {
|
|
|
|
err := mount.RemoveDir("someDir")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, ErrNotConnected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mountLink", func(t *testing.T) {
|
|
|
|
err := mount.Link("/", "/")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, ErrNotConnected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mountUnlink", func(t *testing.T) {
|
|
|
|
err := mount.Unlink("someFile")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, ErrNotConnected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mountSymlink", func(t *testing.T) {
|
|
|
|
err := mount.Symlink("/", "/")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, ErrNotConnected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mountReadlink", func(t *testing.T) {
|
|
|
|
_, err := mount.Readlink("somePath")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, ErrNotConnected)
|
|
|
|
})
|
|
|
|
}
|
2021-07-08 14:44:09 +00:00
|
|
|
|
|
|
|
func TestReadConfigFile(t *testing.T) {
|
2023-07-08 05:47:44 +00:00
|
|
|
file, err := os.CreateTemp("/tmp", "cephfs.conf")
|
2021-07-08 14:44:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
assert.NoError(t, file.Close())
|
|
|
|
assert.NoError(t, os.Remove(file.Name()))
|
|
|
|
}()
|
|
|
|
_, err = io.WriteString(
|
|
|
|
file, "[global]\nfsid = 04862775-14d5-46e0-a015-000000000000\n")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mount, err := CreateMount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, mount)
|
|
|
|
|
|
|
|
err = mount.ReadConfigFile(file.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v, err := mount.GetConfigOption("fsid")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "04862775-14d5-46e0-a015-000000000000", v)
|
|
|
|
}
|
2021-09-07 00:02:52 +00:00
|
|
|
|
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
var major, minor, patch = Version()
|
|
|
|
assert.False(t, major < 0 || major > 1000, "invalid major")
|
|
|
|
assert.False(t, minor < 0 || minor > 1000, "invalid minor")
|
|
|
|
assert.False(t, patch < 0 || patch > 1000, "invalid patch")
|
|
|
|
}
|