2014-05-24 17:36:12 +00:00
|
|
|
package rados_test
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
//import "bytes"
|
2014-11-27 19:21:47 +00:00
|
|
|
import "github.com/noahdesu/go-rados"
|
2014-05-24 17:36:12 +00:00
|
|
|
import "github.com/stretchr/testify/assert"
|
2014-08-29 16:21:10 +00:00
|
|
|
import "os"
|
2014-08-30 17:28:24 +00:00
|
|
|
import "os/exec"
|
2014-08-30 17:36:10 +00:00
|
|
|
import "io"
|
|
|
|
import "io/ioutil"
|
|
|
|
import "time"
|
|
|
|
import "net"
|
|
|
|
import "fmt"
|
2014-08-30 17:28:24 +00:00
|
|
|
|
|
|
|
func GetUUID() string {
|
|
|
|
out, _ := exec.Command("uuidgen").Output()
|
|
|
|
return string(out[:36])
|
|
|
|
}
|
2014-05-24 17:36:12 +00:00
|
|
|
|
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
var major, minor, patch = rados.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")
|
|
|
|
}
|
|
|
|
|
2014-08-29 16:21:10 +00:00
|
|
|
func TestGetSetConfigOption(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
|
|
|
|
|
|
|
// rejects invalid options
|
|
|
|
err := conn.SetConfigOption("wefoijweojfiw", "welfkwjelkfj")
|
|
|
|
assert.Error(t, err, "Invalid option")
|
|
|
|
|
|
|
|
// verify SetConfigOption changes a values
|
|
|
|
log_file_val, err := conn.GetConfigOption("log_file")
|
|
|
|
assert.NotEqual(t, log_file_val, "/dev/null")
|
|
|
|
|
|
|
|
err = conn.SetConfigOption("log_file", "/dev/null")
|
|
|
|
assert.NoError(t, err, "Invalid option")
|
|
|
|
|
|
|
|
log_file_val, err = conn.GetConfigOption("log_file")
|
|
|
|
assert.Equal(t, log_file_val, "/dev/null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDefaultConfigEnv(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
|
|
|
|
|
|
|
log_file_val, _ := conn.GetConfigOption("log_file")
|
|
|
|
assert.NotEqual(t, log_file_val, "/dev/null")
|
|
|
|
|
|
|
|
err := os.Setenv("CEPH_ARGS", "--log-file /dev/null")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = conn.ParseDefaultConfigEnv()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
log_file_val, _ = conn.GetConfigOption("log_file")
|
|
|
|
assert.Equal(t, log_file_val, "/dev/null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCmdLineArgs(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
2014-08-30 17:30:16 +00:00
|
|
|
conn.ReadDefaultConfigFile()
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:30:16 +00:00
|
|
|
mon_host_val, _ := conn.GetConfigOption("mon_host")
|
|
|
|
assert.NotEqual(t, mon_host_val, "1.1.1.1")
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:30:16 +00:00
|
|
|
args := []string{ "--mon-host", "1.1.1.1" }
|
2014-08-29 16:21:10 +00:00
|
|
|
err := conn.ParseCmdLineArgs(args)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2014-08-30 17:30:16 +00:00
|
|
|
mon_host_val, _ = conn.GetConfigOption("mon_host")
|
|
|
|
assert.Equal(t, mon_host_val, "1.1.1.1")
|
2014-08-29 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetClusterStats(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
|
|
|
conn.ReadDefaultConfigFile()
|
|
|
|
conn.Connect()
|
|
|
|
|
2014-08-30 17:33:09 +00:00
|
|
|
poolname := GetUUID()
|
|
|
|
err := conn.MakePool(poolname)
|
2014-08-29 16:21:10 +00:00
|
|
|
assert.NoError(t, err)
|
2014-08-30 17:33:09 +00:00
|
|
|
|
|
|
|
pool, err := conn.OpenPool(poolname)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2014-11-28 00:16:39 +00:00
|
|
|
// grab current stats
|
|
|
|
prev_stat, err := conn.GetClusterStats()
|
2014-11-28 00:36:01 +00:00
|
|
|
fmt.Printf("prev_stat: %+v\n", prev_stat)
|
2014-11-28 00:16:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// make some changes to the cluster
|
|
|
|
buf := make([]byte, 1<<20)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
objname := GetUUID()
|
|
|
|
pool.Write(objname, buf, 0)
|
|
|
|
}
|
2014-08-30 17:33:09 +00:00
|
|
|
|
2014-11-28 00:16:39 +00:00
|
|
|
// wait a while for the stats to change
|
2014-08-30 17:33:09 +00:00
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
stat, err := conn.GetClusterStats()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2014-11-28 00:16:39 +00:00
|
|
|
// wait for something to change
|
|
|
|
if stat == prev_stat {
|
2014-11-28 00:36:01 +00:00
|
|
|
fmt.Printf("curr_stat: %+v (trying again...)\n", stat)
|
2014-08-30 17:33:09 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
} else {
|
|
|
|
// success
|
2014-11-28 00:36:01 +00:00
|
|
|
fmt.Printf("curr_stat: %+v (change detected)\n", stat)
|
2014-08-30 17:33:09 +00:00
|
|
|
conn.Shutdown()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Shutdown()
|
2014-11-28 00:16:39 +00:00
|
|
|
t.Error("Cluster stats aren't changing")
|
2014-05-24 17:36:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 22:56:17 +00:00
|
|
|
func TestGetFSID(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
|
|
|
conn.ReadDefaultConfigFile()
|
|
|
|
conn.Connect()
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-29 22:56:17 +00:00
|
|
|
fsid, err := conn.GetFSID()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, fsid, "")
|
2014-08-30 17:34:53 +00:00
|
|
|
|
|
|
|
conn.Shutdown()
|
2014-08-29 22:56:17 +00:00
|
|
|
}
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-29 22:56:17 +00:00
|
|
|
func TestGetInstanceID(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
|
|
|
conn.ReadDefaultConfigFile()
|
|
|
|
conn.Connect()
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-29 22:56:17 +00:00
|
|
|
id := conn.GetInstanceID()
|
|
|
|
assert.NotEqual(t, id, 0)
|
2014-08-30 17:34:53 +00:00
|
|
|
|
|
|
|
conn.Shutdown()
|
2014-08-29 22:56:17 +00:00
|
|
|
}
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:28:24 +00:00
|
|
|
func TestMakeDeletePool(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
|
|
|
conn.ReadDefaultConfigFile()
|
|
|
|
conn.Connect()
|
|
|
|
|
|
|
|
// get current list of pool
|
|
|
|
pools, err := conn.ListPools()
|
|
|
|
assert.NoError(t, err)
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:28:24 +00:00
|
|
|
// check that new pool name is unique
|
|
|
|
new_name := GetUUID()
|
|
|
|
for _, poolname := range pools {
|
|
|
|
if new_name == poolname {
|
|
|
|
t.Error("Random pool name exists!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create pool
|
|
|
|
err = conn.MakePool(new_name)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// get updated list of pools
|
|
|
|
pools, err = conn.ListPools()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// verify that the new pool name exists
|
|
|
|
found := false
|
|
|
|
for _, poolname := range pools {
|
|
|
|
if new_name == poolname {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.Error("Cannot find newly created pool")
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the pool
|
|
|
|
err = conn.DeletePool(new_name)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// verify that it is gone
|
|
|
|
|
|
|
|
// get updated list of pools
|
|
|
|
pools, err = conn.ListPools()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// verify that the new pool name exists
|
|
|
|
found = false
|
|
|
|
for _, poolname := range pools {
|
|
|
|
if new_name == poolname {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
t.Error("Deleted pool still exists")
|
|
|
|
}
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
conn.Shutdown()
|
|
|
|
}
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
func TestPingMonitor(t *testing.T) {
|
2014-05-24 18:42:18 +00:00
|
|
|
conn, _ := rados.NewConn()
|
2014-05-24 17:36:12 +00:00
|
|
|
conn.ReadDefaultConfigFile()
|
2014-08-29 16:21:10 +00:00
|
|
|
conn.Connect()
|
2014-08-30 17:36:10 +00:00
|
|
|
|
|
|
|
// mon id that should work with vstart.sh
|
|
|
|
reply, err := conn.PingMonitor("a")
|
|
|
|
if err == nil {
|
|
|
|
assert.NotEqual(t, reply, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:43:35 +00:00
|
|
|
// mon id that should work with micro-osd.sh
|
|
|
|
reply, err = conn.PingMonitor("0")
|
|
|
|
if err == nil {
|
|
|
|
assert.NotEqual(t, reply, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
// try to use a hostname as the monitor id
|
|
|
|
mon_addr, _ := conn.GetConfigOption("mon_host")
|
|
|
|
hosts, _ := net.LookupAddr(mon_addr)
|
|
|
|
for _, host := range hosts {
|
|
|
|
reply, err := conn.PingMonitor(host)
|
|
|
|
if err == nil {
|
|
|
|
assert.NotEqual(t, reply, "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Error("Could not find a valid monitor id")
|
|
|
|
|
|
|
|
conn.Shutdown()
|
2014-05-24 17:36:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
func TestReadConfigFile(t *testing.T) {
|
2014-05-24 18:42:18 +00:00
|
|
|
conn, _ := rados.NewConn()
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
// check current log_file value
|
|
|
|
log_file_val, err := conn.GetConfigOption("log_file")
|
2014-08-29 16:21:10 +00:00
|
|
|
assert.NoError(t, err)
|
2014-08-30 17:36:10 +00:00
|
|
|
assert.NotEqual(t, log_file_val, "/dev/null")
|
2014-08-29 16:21:10 +00:00
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
// create a temporary ceph.conf file that changes the log_file conf
|
|
|
|
// option.
|
|
|
|
file, err := ioutil.TempFile("/tmp", "go-rados")
|
2014-08-29 16:21:10 +00:00
|
|
|
assert.NoError(t, err)
|
2014-08-30 17:36:10 +00:00
|
|
|
|
|
|
|
_, err = io.WriteString(file, "[global]\nlog_file = /dev/null\n")
|
2014-08-29 16:21:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
// parse the config file
|
|
|
|
err = conn.ReadConfigFile(file.Name())
|
2014-08-29 16:21:10 +00:00
|
|
|
assert.NoError(t, err)
|
2014-08-30 17:36:10 +00:00
|
|
|
|
|
|
|
// check current log_file value
|
|
|
|
log_file_val, err = conn.GetConfigOption("log_file")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, log_file_val, "/dev/null")
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
file.Close()
|
|
|
|
os.Remove(file.Name())
|
2014-05-24 17:36:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 17:36:10 +00:00
|
|
|
func TestWaitForLatestOSDMap(t *testing.T) {
|
|
|
|
conn, _ := rados.NewConn()
|
|
|
|
conn.ReadDefaultConfigFile()
|
|
|
|
conn.Connect()
|
|
|
|
|
|
|
|
err := conn.WaitForLatestOSDMap()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
conn.Shutdown()
|
|
|
|
}
|