go-ceph/rbd/rbd_test.go

278 lines
5.7 KiB
Go
Raw Normal View History

package rbd_test
import (
"bytes"
2015-02-11 21:21:05 +00:00
"encoding/json"
"github.com/ceph/go-ceph/rados"
"github.com/ceph/go-ceph/rbd"
2015-02-11 21:21:05 +00:00
"github.com/stretchr/testify/assert"
"os/exec"
"sort"
"testing"
)
func GetUUID() string {
2015-02-11 21:21:05 +00:00
out, _ := exec.Command("uuidgen").Output()
return string(out[:36])
}
func TestVersion(t *testing.T) {
2015-02-11 21:21:05 +00:00
var major, minor, patch = rbd.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")
}
func TestGetImageNames(t *testing.T) {
2015-02-11 21:21:05 +00:00
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
poolname := GetUUID()
err := conn.MakePool(poolname)
assert.NoError(t, err)
ioctx, err := conn.OpenIOContext(poolname)
assert.NoError(t, err)
createdList := []string{}
for i := 0; i < 10; i++ {
name := GetUUID()
_, err := rbd.Create(ioctx, name, 1<<22, 22)
2015-02-11 21:21:05 +00:00
assert.NoError(t, err)
createdList = append(createdList, name)
}
imageNames, err := rbd.GetImageNames(ioctx)
assert.NoError(t, err)
sort.Strings(createdList)
sort.Strings(imageNames)
assert.Equal(t, createdList, imageNames)
for _, name := range createdList {
img := rbd.GetImage(ioctx, name)
err := img.Remove()
assert.NoError(t, err)
}
ioctx.Destroy()
conn.DeletePool(poolname)
conn.Shutdown()
}
func TestIOReaderWriter(t *testing.T) {
2015-02-11 21:21:05 +00:00
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
2015-02-11 21:21:05 +00:00
poolname := GetUUID()
err := conn.MakePool(poolname)
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
ioctx, err := conn.OpenIOContext(poolname)
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
name := GetUUID()
img, err := rbd.Create(ioctx, name, 1<<22, 22)
2015-02-11 21:21:05 +00:00
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
err = img.Open()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
stats, err := img.Stat()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
encoder := json.NewEncoder(img)
encoder.Encode(stats)
2015-02-11 21:21:05 +00:00
err = img.Flush()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
_, err = img.Seek(0, 0)
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
var stats2 *rbd.ImageInfo
decoder := json.NewDecoder(img)
decoder.Decode(&stats2)
2015-02-11 21:21:05 +00:00
assert.Equal(t, &stats, &stats2)
2015-02-11 21:21:05 +00:00
_, err = img.Seek(0, 0)
bytes_in := []byte("input data")
_, err = img.Write(bytes_in)
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
_, err = img.Seek(0, 0)
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
bytes_out := make([]byte, len(bytes_in))
n_out, err := img.Read(bytes_out)
2015-02-11 21:21:05 +00:00
assert.Equal(t, n_out, len(bytes_in))
assert.Equal(t, bytes_in, bytes_out)
2015-02-11 21:21:05 +00:00
err = img.Close()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
img.Remove()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
ioctx.Destroy()
conn.DeletePool(poolname)
conn.Shutdown()
}
func TestCreateSnapshot(t *testing.T) {
2015-02-11 21:21:05 +00:00
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
2015-02-11 21:21:05 +00:00
poolname := GetUUID()
err := conn.MakePool(poolname)
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
ioctx, err := conn.OpenIOContext(poolname)
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
name := GetUUID()
img, err := rbd.Create(ioctx, name, 1<<22, 22)
2015-02-11 21:21:05 +00:00
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
err = img.Open()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
snapshot, err := img.CreateSnapshot("mysnap")
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
err = img.Close()
err = img.Open("mysnap")
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
snapshot.Remove()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
err = img.Close()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
img.Remove()
assert.NoError(t, err)
2015-02-11 21:21:05 +00:00
ioctx.Destroy()
conn.DeletePool(poolname)
conn.Shutdown()
}
func TestParentInfo(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
poolname := GetUUID()
err := conn.MakePool(poolname)
assert.NoError(t, err)
ioctx, err := conn.OpenIOContext(poolname)
assert.NoError(t, err)
name := "parent"
img, err := rbd.Create(ioctx, name, 1<<22, 22, 1)
assert.NoError(t, err)
err = img.Open()
assert.NoError(t, err)
snapshot, err := img.CreateSnapshot("mysnap")
assert.NoError(t, err)
err = snapshot.Protect()
assert.NoError(t, err)
// create an image context with the parent+snapshot
snapImg := rbd.GetImage(ioctx, "parent")
err = snapImg.Open("mysnap")
assert.NoError(t, err)
// ensure no children prior to clone
pools, images, err := snapImg.ListChildren()
assert.NoError(t, err)
assert.Equal(t, len(pools), 0, "pools equal")
assert.Equal(t, len(images), 0, "children length equal")
imgNew, err := img.Clone("mysnap", ioctx, "child", 1, 22)
assert.NoError(t, err)
err = imgNew.Open()
assert.NoError(t, err)
parentPool := make([]byte, 128)
parentName := make([]byte, 128)
parentSnapname := make([]byte, 128)
err = imgNew.GetParentInfo(parentPool, parentName, parentSnapname)
assert.NoError(t, err)
n := bytes.Index(parentName, []byte{0})
pName := string(parentName[:n])
n = bytes.Index(parentSnapname, []byte{0})
pSnapname := string(parentSnapname[:n])
assert.Equal(t, pName, "parent", "they should be equal")
assert.Equal(t, pSnapname, "mysnap", "they should be equal")
pools, images, err = snapImg.ListChildren()
assert.NoError(t, err)
assert.Equal(t, len(pools), 1, "pools equal")
assert.Equal(t, len(images), 1, "children length equal")
err = imgNew.Close()
assert.NoError(t, err)
err = imgNew.Remove()
assert.NoError(t, err)
err = snapshot.Unprotect()
assert.NoError(t, err)
err = snapshot.Remove()
assert.NoError(t, err)
err = img.Close()
assert.NoError(t, err)
err = snapImg.Close()
assert.NoError(t, err)
err = img.Remove()
assert.NoError(t, err)
ioctx.Destroy()
conn.DeletePool(poolname)
conn.Shutdown()
}
func TestNotFound(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()
poolname := GetUUID()
err := conn.MakePool(poolname)
assert.NoError(t, err)
ioctx, err := conn.OpenIOContext(poolname)
assert.NoError(t, err)
name := GetUUID()
img := rbd.GetImage(ioctx, name)
err = img.Open()
assert.Equal(t, err, rbd.RbdErrorNotFound)
img.Remove()
assert.Equal(t, err, rbd.RbdErrorNotFound)
ioctx.Destroy()
conn.DeletePool(poolname)
conn.Shutdown()
}