rados: refactor tests to use testing suite

Signed-off-by: Noah Watkins <nwatkins@redhat.com>
This commit is contained in:
Noah Watkins 2018-08-05 10:41:47 -07:00
parent 3dd9a8e5d4
commit 7b5a27ca9a

View File

@ -1,15 +1,16 @@
package rados_test package rados_test
import ( import (
// "encoding/json" "encoding/json"
"fmt" "fmt"
// "io" "io"
// "io/ioutil" "io/ioutil"
"strconv"
//"net" //"net"
"math/rand"
"os" "os"
"os/exec" "os/exec"
//"sort" "sort"
"math/rand"
"testing" "testing"
"time" "time"
@ -60,10 +61,10 @@ func (suite *RadosTestSuite) SetupTest() {
conn, err := rados.NewConn() conn, err := rados.NewConn()
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
suite.conn = conn suite.conn = conn
suite.conn.ReadDefaultConfigFile()
} }
func (suite *RadosTestSuite) SetupConnection() { func (suite *RadosTestSuite) SetupConnection() {
suite.conn.ReadDefaultConfigFile()
if err := suite.conn.Connect(); assert.NoError(suite.T(), err) { if err := suite.conn.Connect(); assert.NoError(suite.T(), err) {
ioctx, err := suite.conn.OpenIOContext(suite.pool) ioctx, err := suite.conn.OpenIOContext(suite.pool)
if assert.NoError(suite.T(), err) { if assert.NoError(suite.T(), err) {
@ -173,6 +174,41 @@ func (suite *RadosTestSuite) TestParseCmdLineArgs() {
assert.Equal(suite.T(), curr_val, "/dev/null") assert.Equal(suite.T(), curr_val, "/dev/null")
} }
func (suite *RadosTestSuite) TestReadConfigFile() {
// check current log_file value
prev_str, err := suite.conn.GetConfigOption("log_max_new")
assert.NoError(suite.T(), err)
prev_val, err := strconv.Atoi(prev_str)
assert.NoError(suite.T(), err)
// create conf file that changes log_file conf option
file, err := ioutil.TempFile("/tmp", "go-rados")
assert.NoError(suite.T(), err)
next_val := prev_val + 1
conf := fmt.Sprintf("[global]\nlog_max_new = %d\n", next_val)
_, err = io.WriteString(file, conf)
assert.NoError(suite.T(), err)
// parse the config file
err = suite.conn.ReadConfigFile(file.Name())
assert.NoError(suite.T(), err)
// check current log_file value
curr_str, err := suite.conn.GetConfigOption("log_max_new")
assert.NoError(suite.T(), err)
curr_val, err := strconv.Atoi(curr_str)
assert.NoError(suite.T(), err)
assert.NotEqual(suite.T(), prev_str, curr_str)
assert.Equal(suite.T(), curr_val, prev_val+1)
file.Close()
os.Remove(file.Name())
}
func (suite *RadosTestSuite) TestGetClusterStats() { func (suite *RadosTestSuite) TestGetClusterStats() {
suite.SetupConnection() suite.SetupConnection()
@ -283,36 +319,6 @@ func (suite *RadosTestSuite) TestPingMonitor() {
assert.NotEqual(suite.T(), reply, "") assert.NotEqual(suite.T(), reply, "")
} }
//func TestReadConfigFile(t *testing.T) {
// conn, _ := rados.NewConn()
//
// // check current log_file value
// log_file_val, err := conn.GetConfigOption("log_file")
// assert.NoError(t, err)
// assert.NotEqual(t, log_file_val, "/dev/null")
//
// // create a temporary ceph.conf file that changes the log_file conf
// // option.
// file, err := ioutil.TempFile("/tmp", "go-rados")
// assert.NoError(t, err)
//
// _, err = io.WriteString(file, "[global]\nlog_file = /dev/null\n")
// assert.NoError(t, err)
//
// // parse the config file
// err = conn.ReadConfigFile(file.Name())
// assert.NoError(t, err)
//
// // 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())
//}
//
func (suite *RadosTestSuite) TestWaitForLatestOSDMap() { func (suite *RadosTestSuite) TestWaitForLatestOSDMap() {
suite.SetupConnection() suite.SetupConnection()
@ -397,251 +403,212 @@ func (suite *RadosTestSuite) TestDeleteNotFound() {
assert.Equal(suite.T(), err, rados.RadosErrorNotFound) assert.Equal(suite.T(), err, rados.RadosErrorNotFound)
} }
//func TestObjectStat(t *testing.T) { func (suite *RadosTestSuite) TestStatNotFound() {
// conn, _ := rados.NewConn() suite.SetupConnection()
// conn.ReadDefaultConfigFile()
// conn.Connect() oid := suite.GenObjectName()
// _, err := suite.ioctx.Stat(oid)
// pool_name := GetUUID() assert.Equal(suite.T(), err, rados.RadosErrorNotFound)
// err := conn.MakePool(pool_name) }
// assert.NoError(t, err)
// func (suite *RadosTestSuite) TestObjectStat() {
// pool, err := conn.OpenIOContext(pool_name) suite.SetupConnection()
// assert.NoError(t, err)
// oid := suite.GenObjectName()
// bytes_in := []byte("input data") bytes := suite.RandomBytes(234)
// err = pool.Write("obj", bytes_in, 0) err := suite.ioctx.Write(oid, bytes, 0)
// assert.NoError(t, err) assert.NoError(suite.T(), err)
//
// stat, err := pool.Stat("obj") stat, err := suite.ioctx.Stat(oid)
// assert.Equal(t, uint64(len(bytes_in)), stat.Size) assert.Equal(suite.T(), uint64(len(bytes)), stat.Size)
// assert.NotNil(t, stat.ModTime) assert.NotNil(suite.T(), stat.ModTime)
// }
// _, err = pool.Stat("notfound")
// assert.Equal(t, err, rados.RadosErrorNotFound) func (suite *RadosTestSuite) TestGetPoolStats() {
// suite.SetupConnection()
// pool.Destroy()
// conn.Shutdown() // grab current stats
//} prev_stat, err := suite.ioctx.GetPoolStats()
// fmt.Printf("prev_stat: %+v\n", prev_stat)
//func TestGetPoolStats(t *testing.T) { assert.NoError(suite.T(), err)
// conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile() // make some changes to the cluster
// conn.Connect() buf := make([]byte, 1<<20)
// for i := 0; i < 10; i++ {
// poolname := GetUUID() oid := suite.GenObjectName()
// err := conn.MakePool(poolname) suite.ioctx.Write(oid, buf, 0)
// assert.NoError(t, err) }
//
// pool, err := conn.OpenIOContext(poolname) // wait a while for the stats to change
// assert.NoError(t, err) for i := 0; i < 30; i++ {
// stat, err := suite.ioctx.GetPoolStats()
// // grab current stats assert.NoError(suite.T(), err)
// prev_stat, err := pool.GetPoolStats()
// fmt.Printf("prev_stat: %+v\n", prev_stat) // wait for something to change
// assert.NoError(t, err) if stat == prev_stat {
// fmt.Printf("curr_stat: %+v (trying again...)\n", stat)
// // make some changes to the cluster time.Sleep(time.Second)
// buf := make([]byte, 1<<20) } else {
// for i := 0; i < 10; i++ { // success
// objname := GetUUID() fmt.Printf("curr_stat: %+v (change detected)\n", stat)
// pool.Write(objname, buf, 0) return
// } }
// }
// // wait a while for the stats to change
// for i := 0; i < 30; i++ { suite.T().Error("Pool stats aren't changing")
// stat, err := pool.GetPoolStats() }
// assert.NoError(t, err)
// func (suite *RadosTestSuite) TestGetPoolName() {
// // wait for something to change suite.SetupConnection()
// if stat == prev_stat {
// fmt.Printf("curr_stat: %+v (trying again...)\n", stat) name, err := suite.ioctx.GetPoolName()
// time.Sleep(time.Second) assert.NoError(suite.T(), err)
// } else {
// // success assert.Equal(suite.T(), name, suite.pool)
// fmt.Printf("curr_stat: %+v (change detected)\n", stat) }
// conn.Shutdown()
// return func (suite *RadosTestSuite) TestMonCommand() {
// } suite.SetupConnection()
// }
// command, err := json.Marshal(
// pool.Destroy() map[string]string{"prefix": "df", "format": "json"})
// conn.Shutdown() assert.NoError(suite.T(), err)
// t.Error("Pool stats aren't changing")
//} buf, info, err := suite.conn.MonCommand(command)
// assert.NoError(suite.T(), err)
//func TestGetPoolName(t *testing.T) { assert.Equal(suite.T(), info, "")
// conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile() var message map[string]interface{}
// conn.Connect() err = json.Unmarshal(buf, &message)
// assert.NoError(suite.T(), err)
// poolname := GetUUID() }
// err := conn.MakePool(poolname)
// assert.NoError(t, err) func (suite *RadosTestSuite) TestMonCommandWithInputBuffer() {
// suite.SetupConnection()
// ioctx, err := conn.OpenIOContext(poolname)
// assert.NoError(t, err) entity := fmt.Sprintf("client.testMonCmdUser%d", time.Now().UnixNano())
//
// poolname_ret, err := ioctx.GetPoolName() // first add the new test user, specifying its key in the input buffer
// assert.NoError(t, err) command, err := json.Marshal(map[string]interface{}{
// "prefix": "auth add",
// assert.Equal(t, poolname, poolname_ret) "format": "json",
// "entity": entity,
// ioctx.Destroy() })
// conn.Shutdown() assert.NoError(suite.T(), err)
//}
// client_key := fmt.Sprintf(`
//func TestMonCommand(t *testing.T) { [%s]
// conn, _ := rados.NewConn() key = AQD4PGNXBZJNHhAA582iUgxe9DsN+MqFN4Z6Jw==
// conn.ReadDefaultConfigFile() `, entity)
// conn.Connect()
// defer conn.Shutdown() inbuf := []byte(client_key)
//
// command, err := json.Marshal(map[string]string{"prefix": "df", "format": "json"}) buf, info, err := suite.conn.MonCommandWithInputBuffer(command, inbuf)
// assert.NoError(t, err) assert.NoError(suite.T(), err)
// expected_info := fmt.Sprintf("added key for %s", entity)
// buf, info, err := conn.MonCommand(command) assert.Equal(suite.T(), expected_info, info)
// assert.NoError(t, err) assert.Equal(suite.T(), "", string(buf[:]))
// assert.Equal(t, info, "")
// // get the key and verify that it's what we previously set
// var message map[string]interface{} command, err = json.Marshal(map[string]interface{}{
// err = json.Unmarshal(buf, &message) "prefix": "auth get-key",
// assert.NoError(t, err) "format": "json",
//} "entity": entity,
// })
//func TestMonCommandWithInputBuffer(t *testing.T) { assert.NoError(suite.T(), err)
// conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile() buf, info, err = suite.conn.MonCommand(command)
// conn.Connect() assert.NoError(suite.T(), err)
// defer conn.Shutdown() assert.Equal(suite.T(), "", info)
// assert.Equal(suite.T(),
// // first add the new test user, specifying its key in the input buffer `{"key":"AQD4PGNXBZJNHhAA582iUgxe9DsN+MqFN4Z6Jw=="}`,
// command, err := json.Marshal(map[string]interface{}{ string(buf[:]))
// "prefix": "auth add", }
// "format": "json",
// "entity": "client.testMonCommandUser", func (suite *RadosTestSuite) TestObjectListObjects() {
// }) suite.SetupConnection()
// assert.NoError(t, err)
// // objects currently in pool
// inbuf := []byte(`[client.testMonCommandUser] prevObjectList := []string{}
//key = AQD4PGNXBZJNHhAA582iUgxe9DsN+MqFN4Z6Jw== err := suite.ioctx.ListObjects(func(oid string) {
//`) prevObjectList = append(prevObjectList, oid)
// })
// buf, info, err := conn.MonCommandWithInputBuffer(command, inbuf) assert.NoError(suite.T(), err)
// assert.NoError(t, err)
// assert.Equal(t, "added key for client.testMonCommandUser", info) // create some objects
// assert.Equal(t, "", string(buf[:])) createdList := []string{}
// for i := 0; i < 10; i++ {
// // now get the key, and verify it is equal to the key we specified in the input buffer for "auth add" oid := suite.GenObjectName()
// command, err = json.Marshal(map[string]interface{}{ bytes := []byte("input data")
// "prefix": "auth get-key", err := suite.ioctx.Write(oid, bytes, 0)
// "format": "json", assert.NoError(suite.T(), err)
// "entity": "client.testMonCommandUser", createdList = append(createdList, oid)
// }) }
// assert.NoError(t, err)
// // join the lists of objects
// buf, info, err = conn.MonCommand(command) expectedObjectList := prevObjectList
// assert.NoError(t, err) expectedObjectList = append(expectedObjectList, createdList...)
// assert.Equal(t, "", info)
// assert.Equal(t, `{"key":"AQD4PGNXBZJNHhAA582iUgxe9DsN+MqFN4Z6Jw=="}`, string(buf[:])) // now list the current set of objects in the pool
//} currObjectList := []string{}
// err = suite.ioctx.ListObjects(func(oid string) {
//func TestObjectListObjects(t *testing.T) { currObjectList = append(currObjectList, oid)
// conn, _ := rados.NewConn() })
// conn.ReadDefaultConfigFile() assert.NoError(suite.T(), err)
// conn.Connect()
// // lists should be equal
// poolname := GetUUID() sort.Strings(currObjectList)
// err := conn.MakePool(poolname) sort.Strings(expectedObjectList)
// assert.NoError(t, err) assert.Equal(suite.T(), currObjectList, expectedObjectList)
// }
// ioctx, err := conn.OpenIOContext(poolname)
// assert.NoError(t, err) func (suite *RadosTestSuite) TestObjectIterator() {
// suite.SetupConnection()
// objectList := []string{}
// err = ioctx.ListObjects(func(oid string) { prevObjectList := []string{}
// objectList = append(objectList, oid) iter, err := suite.ioctx.Iter()
// }) assert.NoError(suite.T(), err)
// assert.NoError(t, err) for iter.Next() {
// assert.True(t, len(objectList) == 0) prevObjectList = append(prevObjectList, iter.Value())
// }
// createdList := []string{} iter.Close()
// for i := 0; i < 200; i++ { assert.NoError(suite.T(), iter.Err())
// oid := GetUUID()
// bytes_in := []byte("input data") // create an object in a different namespace to verify that
// err = ioctx.Write(oid, bytes_in, 0) // iteration within a namespace does not return it
// assert.NoError(t, err) suite.ioctx.SetNamespace("ns1")
// createdList = append(createdList, oid) bytes_in := []byte("input data")
// } err = suite.ioctx.Write(suite.GenObjectName(), bytes_in, 0)
// assert.True(t, len(createdList) == 200) assert.NoError(suite.T(), err)
//
// err = ioctx.ListObjects(func(oid string) { suite.ioctx.SetNamespace("")
// objectList = append(objectList, oid) createdList := []string{}
// }) for i := 0; i < 10; i++ {
// assert.NoError(t, err) oid := suite.GenObjectName()
// assert.Equal(t, len(objectList), len(createdList)) bytes_in := []byte("input data")
// err = suite.ioctx.Write(oid, bytes_in, 0)
// sort.Strings(objectList) assert.NoError(suite.T(), err)
// sort.Strings(createdList) createdList = append(createdList, oid)
// }
// assert.Equal(t, objectList, createdList)
//} // prev list plus new oids
// expectedObjectList := prevObjectList
//func TestObjectIterator(t *testing.T) { expectedObjectList = append(expectedObjectList, createdList...)
// conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile() currObjectList := []string{}
// conn.Connect() iter, err = suite.ioctx.Iter()
// assert.NoError(suite.T(), err)
// poolname := GetUUID() for iter.Next() {
// err := conn.MakePool(poolname) currObjectList = append(currObjectList, iter.Value())
// assert.NoError(t, err) }
// iter.Close()
// ioctx, err := conn.OpenIOContext(poolname) assert.NoError(suite.T(), iter.Err())
// assert.NoError(t, err)
// sort.Strings(expectedObjectList)
// objectList := []string{} sort.Strings(currObjectList)
// iter, err := ioctx.Iter() assert.Equal(suite.T(), currObjectList, expectedObjectList)
// assert.NoError(t, err) }
// for iter.Next() {
// objectList = append(objectList, iter.Value())
// }
// iter.Close()
// assert.NoError(t, iter.Err())
// assert.True(t, len(objectList) == 0)
//
// //create an object in a different namespace to verify that
// //iteration within a namespace does not return it
// ioctx.SetNamespace("ns1")
// bytes_in := []byte("input data")
// err = ioctx.Write(GetUUID(), bytes_in, 0)
// assert.NoError(t, err)
//
// ioctx.SetNamespace("")
//
// createdList := []string{}
// for i := 0; i < 200; i++ {
// oid := GetUUID()
// bytes_in := []byte("input data")
// err = ioctx.Write(oid, bytes_in, 0)
// assert.NoError(t, err)
// createdList = append(createdList, oid)
// }
// assert.True(t, len(createdList) == 200)
//
// iter, err = ioctx.Iter()
// assert.NoError(t, err)
// for iter.Next() {
// objectList = append(objectList, iter.Value())
// }
// iter.Close()
// assert.NoError(t, iter.Err())
// assert.Equal(t, len(objectList), len(createdList))
//
// sort.Strings(objectList)
// sort.Strings(createdList)
//
// assert.Equal(t, objectList, createdList)
//}
// //
//func TestObjectIteratorAcrossNamespaces(t *testing.T) { //func TestObjectIteratorAcrossNamespaces(t *testing.T) {
// const perNamespace = 100 // const perNamespace = 100
@ -725,122 +692,85 @@ func (suite *RadosTestSuite) TestDeleteNotFound() {
// assert.Equal(t, err, nil) // assert.Equal(t, err, nil)
//} //}
// //
//func TestReadWriteXattr(t *testing.T) {
// conn, _ := rados.NewConn() func (suite *RadosTestSuite) TestReadWriteXattr() {
// conn.ReadDefaultConfigFile() suite.SetupConnection()
// conn.Connect()
// oid := suite.GenObjectName()
// // make pool val := []byte("value")
// pool_name := GetUUID() err := suite.ioctx.SetXattr(oid, "key", val)
// err := conn.MakePool(pool_name) assert.NoError(suite.T(), err)
// assert.NoError(t, err)
// out := make([]byte, len(val))
// pool, err := conn.OpenIOContext(pool_name) n, err := suite.ioctx.GetXattr(oid, "key", out)
// assert.NoError(t, err) assert.NoError(suite.T(), err)
// assert.Equal(suite.T(), n, len(out))
// bytes_in := []byte("input data")
// err = pool.Write("obj", bytes_in, 0) assert.Equal(suite.T(), out, val)
// assert.NoError(t, err) }
//
// my_xattr_in := []byte("my_value") func (suite *RadosTestSuite) TestListXattrs() {
// err = pool.SetXattr("obj", "my_key", my_xattr_in) suite.SetupConnection()
// assert.NoError(t, err)
// oid := suite.GenObjectName()
// my_xattr_out := make([]byte, len(my_xattr_in)) xattrs := make(map[string][]byte)
// n_out, err := pool.GetXattr("obj", "my_key", my_xattr_out) val := []byte("value")
// for i := 0; i < 10; i++ {
// assert.Equal(t, n_out, len(my_xattr_in)) name := fmt.Sprintf("key_%d", i)
// assert.Equal(t, my_xattr_in, my_xattr_out) err := suite.ioctx.SetXattr(oid, name, val)
// assert.NoError(suite.T(), err)
// pool.Destroy() xattrs[name] = val
//} }
//
//func TestListXattrs(t *testing.T) { out, err := suite.ioctx.ListXattrs(oid)
// conn, _ := rados.NewConn() assert.NoError(suite.T(), err)
// conn.ReadDefaultConfigFile() assert.Equal(suite.T(), xattrs, out)
// conn.Connect() }
//
// // make pool func (suite *RadosTestSuite) TestRmXattr() {
// pool_name := GetUUID() suite.SetupConnection()
// err := conn.MakePool(pool_name)
// assert.NoError(t, err) oid := suite.GenObjectName()
//
// pool, err := conn.OpenIOContext(pool_name) // 2 xattrs
// assert.NoError(t, err) xattrs := make(map[string][]byte)
// xattrs["key1"] = []byte("val")
// bytes_in := []byte("input data") xattrs["key2"] = []byte("val")
// err = pool.Write("obj", bytes_in, 0) assert.Equal(suite.T(), len(xattrs), 2)
// assert.NoError(t, err)
// // add them to the object
// input_xattrs := make(map[string][]byte) for key, value := range xattrs {
// for i := 0; i < 200; i++ { err := suite.ioctx.SetXattr(oid, key, value)
// name := fmt.Sprintf("key_%d", i) assert.NoError(suite.T(), err)
// data := []byte(GetUUID()) }
// err = pool.SetXattr("obj", name, data) out, err := suite.ioctx.ListXattrs(oid)
// assert.NoError(t, err) assert.NoError(suite.T(), err)
// input_xattrs[name] = data assert.Equal(suite.T(), len(out), 2)
// } assert.Equal(suite.T(), out, xattrs)
//
// output_xattrs := make(map[string][]byte) // remove key1
// output_xattrs, err = pool.ListXattrs("obj") err = suite.ioctx.RmXattr(oid, "key1")
// assert.NoError(t, err) assert.NoError(suite.T(), err)
// assert.Equal(t, len(input_xattrs), len(output_xattrs)) delete(xattrs, "key1")
// assert.Equal(t, input_xattrs, output_xattrs)
// // verify key1 is gone
// pool.Destroy() out, err = suite.ioctx.ListXattrs(oid)
//} assert.NoError(suite.T(), err)
// assert.Equal(suite.T(), len(out), 1)
//func TestRmXattr(t *testing.T) { assert.Equal(suite.T(), out, xattrs)
// conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile() // remove key2
// conn.Connect() err = suite.ioctx.RmXattr(oid, "key2")
// assert.NoError(suite.T(), err)
// pool_name := GetUUID() delete(xattrs, "key2")
// err := conn.MakePool(pool_name)
// assert.NoError(t, err) // verify key2 is gone
// out, err = suite.ioctx.ListXattrs(oid)
// pool, err := conn.OpenIOContext(pool_name) assert.NoError(suite.T(), err)
// assert.NoError(t, err) assert.Equal(suite.T(), len(out), 0)
// assert.Equal(suite.T(), out, xattrs)
// bytes_in := []byte("input data") }
// err = pool.Write("obj", bytes_in, 0)
// assert.NoError(t, err)
//
// key := "key1"
// val := []byte("val1")
// err = pool.SetXattr("obj", key, val)
// assert.NoError(t, err)
//
// key = "key2"
// val = []byte("val2")
// err = pool.SetXattr("obj", key, val)
// assert.NoError(t, err)
//
// xattr_list := make(map[string][]byte)
// xattr_list, err = pool.ListXattrs("obj")
// assert.NoError(t, err)
// assert.Equal(t, len(xattr_list), 2)
//
// pool.RmXattr("obj", "key2")
// xattr_list, err = pool.ListXattrs("obj")
// assert.NoError(t, err)
// assert.Equal(t, len(xattr_list), 1)
//
// found := false
// for key, _ = range xattr_list {
// if key == "key2" {
// found = true
// }
//
// }
//
// if found {
// t.Error("Deleted pool still exists")
// }
//
// pool.Destroy()
//}
//
//func TestReadWriteOmap(t *testing.T) { //func TestReadWriteOmap(t *testing.T) {
// conn, _ := rados.NewConn() // conn, _ := rados.NewConn()
// conn.ReadDefaultConfigFile() // conn.ReadDefaultConfigFile()