From 5c49aab11efd34460a04c38eb09f06b1d428cc6f Mon Sep 17 00:00:00 2001 From: RAJAT SINGH Date: Tue, 8 Jun 2021 18:55:00 +0530 Subject: [PATCH] rbd: add GetMirrorUUID function * Add GetMirrorUUID implementing rbd_mirror_uuid_get This function is be used to get the mirroring uuid for the pool. Basic tests included. Signed-off-by: RAJAT SINGH --- rbd/mirror.go | 28 ++++++++++++++++++++++++++++ rbd/mirror_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/rbd/mirror.go b/rbd/mirror.go index c5a9b0d..84f2f47 100644 --- a/rbd/mirror.go +++ b/rbd/mirror.go @@ -71,6 +71,34 @@ func (imm ImageMirrorMode) String() string { } } +// GetMirrorUUID returns a string naming the mirroring uuid for the pool +// associated with the ioctx. +// +// Implements: +// int rbd_mirror_uuid_get(rados_ioctx_t io_ctx, +// char *uuid, size_t *max_len); +func GetMirrorUUID(ioctx *rados.IOContext) (string, error) { + var ( + err error + buf []byte + cSize C.size_t + ) + retry.WithSizes(1024, 1<<16, func(size int) retry.Hint { + cSize = C.size_t(size) + buf = make([]byte, cSize) + ret := C.rbd_mirror_uuid_get( + cephIoctx(ioctx), + (*C.char)(unsafe.Pointer(&buf[0])), + &cSize) + err = getErrorIfNegative(ret) + return retry.Size(int(cSize)).If(err == errRange) + }) + if err != nil { + return "", err + } + return string(buf[:cSize]), nil +} + // SetMirrorMode is used to enable or disable pool level mirroring with either // an automatic or per-image behavior. // diff --git a/rbd/mirror_test.go b/rbd/mirror_test.go index 5362be5..ad52901 100644 --- a/rbd/mirror_test.go +++ b/rbd/mirror_test.go @@ -17,6 +17,51 @@ import ( "github.com/stretchr/testify/require" ) +func TestGetMirrorUUID(t *testing.T) { + conn := radosConnect(t) + poolName := GetUUID() + err := conn.MakePool(poolName) + require.NoError(t, err) + defer func() { + assert.NoError(t, conn.DeletePool(poolName)) + conn.Shutdown() + }() + + ioctx, err := conn.OpenIOContext(poolName) + assert.NoError(t, err) + defer func() { + ioctx.Destroy() + }() + + // verify that mirroring is not enabled on this new pool + m, err := GetMirrorMode(ioctx) + assert.NoError(t, err) + assert.Equal(t, m, MirrorModeDisabled) + + // enable per-image mirroring for this pool + err = SetMirrorMode(ioctx, MirrorModeImage) + require.NoError(t, err) + + name1 := GetUUID() + options := NewRbdImageOptions() + assert.NoError(t, + options.SetUint64(ImageOptionOrder, uint64(testImageOrder))) + err = CreateImage(ioctx, name1, testImageSize, options) + require.NoError(t, err) + t.Run("getUUID", func(t *testing.T) { + img, err := OpenImage(ioctx, name1, NoSnapshot) + assert.NoError(t, err) + defer func() { + assert.NoError(t, img.Close()) + }() + + err = img.MirrorEnable(ImageMirrorModeSnapshot) + assert.NoError(t, err) + miid, err := GetMirrorUUID(ioctx) + assert.NoError(t, err) + assert.NotEqual(t, miid, "") + }) +} func TestGetMirrorMode(t *testing.T) { conn := radosConnect(t) poolName := GetUUID()