rados: add snapshot create/remove functions

Added wrappers for pool-wide snapshot create and remove functions.

Signed-off-by: Mudit Agarwal <muagarwa@redhat.com>
This commit is contained in:
Mudit Agarwal 2020-07-30 08:31:23 +05:30 committed by John Mulligan
parent 558b366fbb
commit 8e5a05c1a4
2 changed files with 106 additions and 0 deletions

40
rados/snapshot.go Normal file
View File

@ -0,0 +1,40 @@
package rados
// #cgo LDFLAGS: -lrados
// #include <stdlib.h>
// #include <rados/librados.h>
import "C"
import "unsafe"
// CreateSnap creates a pool-wide snapshot.
//
// Implements:
// int rados_ioctx_snap_create(rados_ioctx_t io, const char *snapname)
func (ioctx *IOContext) CreateSnap(snapName string) error {
if err := ioctx.validate(); err != nil {
return err
}
cSnapName := C.CString(snapName)
defer C.free(unsafe.Pointer(cSnapName))
ret := C.rados_ioctx_snap_create(ioctx.ioctx, cSnapName)
return getError(ret)
}
// RemoveSnap deletes the pool snapshot.
//
// Implements:
// int rados_ioctx_snap_remove(rados_ioctx_t io, const char *snapname)
func (ioctx *IOContext) RemoveSnap(snapName string) error {
if err := ioctx.validate(); err != nil {
return err
}
cSnapName := C.CString(snapName)
defer C.free(unsafe.Pointer(cSnapName))
ret := C.rados_ioctx_snap_remove(ioctx.ioctx, cSnapName)
return getError(ret)
}

66
rados/snapshot_test.go Normal file
View File

@ -0,0 +1,66 @@
package rados
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func (suite *RadosTestSuite) TestCreateRemoveSnapshot() {
suite.SetupConnection()
suite.T().Run("invalidIOCtx", func(t *testing.T) {
ioctx := &IOContext{}
err := ioctx.CreateSnap("someSnap")
assert.Error(t, err)
err = ioctx.RemoveSnap("someSnap")
assert.Error(t, err)
assert.Equal(t, err, ErrInvalidIOContext)
})
suite.T().Run("NewSnap", func(t *testing.T) {
ioctx, err := suite.conn.OpenIOContext(suite.pool)
require.NoError(suite.T(), err)
snapName := "mySnap"
err = ioctx.CreateSnap(snapName)
assert.NoError(t, err)
err = ioctx.RemoveSnap(snapName)
assert.NoError(t, err)
})
suite.T().Run("ExistingSnap", func(t *testing.T) {
ioctx, err := suite.conn.OpenIOContext(suite.pool)
require.NoError(suite.T(), err)
snapName := "mySnap"
err = ioctx.CreateSnap(snapName)
assert.NoError(t, err)
defer func() {
assert.NoError(t, ioctx.RemoveSnap(snapName))
}()
err = ioctx.CreateSnap(snapName)
assert.Error(t, err)
})
suite.T().Run("NonExistingSnap", func(t *testing.T) {
ioctx, err := suite.conn.OpenIOContext(suite.pool)
require.NoError(suite.T(), err)
err = ioctx.RemoveSnap("someSnapName")
assert.Error(t, err)
})
// Strangely, this works!!
suite.T().Run("EmptySnapNameString", func(t *testing.T) {
ioctx, err := suite.conn.OpenIOContext(suite.pool)
require.NoError(suite.T(), err)
err = ioctx.CreateSnap("")
assert.NoError(t, err)
err = ioctx.RemoveSnap("")
assert.NoError(t, err)
})
}