lib: more conf_* wrappers and test clean-ups

Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
This commit is contained in:
Noah Watkins 2014-08-29 09:21:10 -07:00
parent c995a1c1ec
commit 130cb56e1c
2 changed files with 142 additions and 13 deletions

26
conn.go
View File

@ -126,6 +126,23 @@ func (c *Conn) SetConfigOption(option, value string) error {
}
}
func (c *Conn) GetConfigOption(name string) (value string, err error) {
buf := make([]byte, 4096)
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
ret := int(C.rados_conf_get(c.cluster, c_name,
(*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))))
// FIXME: ret may be -ENAMETOOLONG if the buffer is not large enough. We
// can handle this case, but we need a reliable way to test for
// -ENAMETOOLONG constant. Will the syscall/Errno stuff in Go help?
if ret == 0 {
value = C.GoString((*C.char)(unsafe.Pointer(&buf[0])))
return value, nil
} else {
return "", RadosError(ret)
}
}
// WaitForLatestOSDMap blocks the caller until the latest OSD map has been
// retrieved. It returns an error, if any.
func (c *Conn) WaitForLatestOSDMap() error {
@ -168,3 +185,12 @@ func (c *Conn) ParseCmdLineArgs(args []string) error {
return nil
}
}
func (c *Conn) ParseDefaultConfigEnv() error {
ret := C.rados_conf_parse_env(c.cluster, nil)
if ret == 0 {
return nil
} else {
return RadosError(int(ret))
}
}

View File

@ -4,6 +4,8 @@ import "testing"
//import "bytes"
import "github.com/noahdesu/rados"
import "github.com/stretchr/testify/assert"
import "fmt"
import "os"
func TestVersion(t *testing.T) {
var major, minor, patch = rados.Version()
@ -12,25 +14,126 @@ func TestVersion(t *testing.T) {
assert.False(t, patch < 0 || patch > 1000, "invalid patch")
}
func TestOpen(t *testing.T) {
_, err := rados.NewConn()
assert.Equal(t, err, nil, "error")
}
func TestConnect(t *testing.T) {
func TestGetSetConfigOption(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
err := conn.Connect()
assert.Equal(t, err, nil)
// 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 TestPingMonitor(t *testing.T) {
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()
log_file_val, _ := conn.GetConfigOption("mon_host")
assert.NotEqual(t, log_file_val, "127.0.0.1")
args := []string{ "--mon-host 127.0.0.1" }
err := conn.ParseCmdLineArgs(args)
assert.NoError(t, err)
log_file_val, _ = conn.GetConfigOption("mon_host")
assert.Equal(t, log_file_val, "127.0.0.1")
}
func TestGetClusterStats(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
reply, err := conn.PingMonitor("kyoto")
assert.Equal(t, err, nil)
assert.True(t, len(reply) > 0)
stat, err := conn.GetClusterStats()
assert.NoError(t, err)
assert.True(t, stat.Kb > 0)
assert.True(t, stat.Kb_used > 0)
assert.True(t, stat.Kb_avail > 0)
assert.True(t, stat.Num_objects > 0)
}
//func TestOpen(t *testing.T) {
// _, err := rados.NewConn()
// assert.Equal(t, err, nil, "error")
//}
//
//func TestConnect(t *testing.T) {
// conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile()
// err := conn.Connect()
// assert.Equal(t, err, nil)
//}
//
//func TestPingMonitor(t *testing.T) {
// conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile()
// conn.Connect()
// reply, err := conn.PingMonitor("kyoto")
// assert.Equal(t, err, nil)
// assert.True(t, len(reply) > 0)
//}
func TestListPools(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
pools, _ := conn.ListPools()
fmt.Println(len(pools), pools)
}
func TestSetConfigOption(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
err := conn.WaitForLatestOSDMap()
assert.NoError(t, err)
stat, err := conn.GetClusterStats()
assert.NoError(t, err)
assert.True(t, stat.Kb > 0)
assert.True(t, stat.Kb_used > 0)
assert.True(t, stat.Kb_avail > 0)
assert.True(t, stat.Num_objects > 0)
args := []string{ "--mon-host 127.0.0.1" }
conn2, _ := rados.NewConn()
err = conn2.ParseCmdLineArgs(args)
assert.NoError(t, err)
args = []string{ "--mmm-host 127.0.0.1" }
err = conn2.ParseCmdLineArgs(args)
assert.NoError(t, err)
}
//func TestConnect(t *testing.T) {