rados: add wrapper for rados_ioctx_snap_rollback() function

Implemented RollbackSnapshot() and testcases for the same.

Signed-off-by: Mudit Agarwal <muagarwa@redhat.com>
This commit is contained in:
Mudit Agarwal 2020-07-30 10:07:16 +05:30 committed by John Mulligan
parent 62f5881485
commit 610b92ded2
2 changed files with 86 additions and 0 deletions

View File

@ -149,3 +149,22 @@ func (ioctx *IOContext) ListSnaps() ([]SnapID, error) {
} }
return snapList[:ret], nil return snapList[:ret], nil
} }
// RollbackSnap rollbacks the object with key oID to the pool snapshot.
// The contents of the object will be the same as when the snapshot was taken.
//
// Implements:
// int rados_ioctx_snap_rollback(rados_ioctx_t io, const char *oid, const char *snapname);
func (ioctx *IOContext) RollbackSnap(oid, snapName string) error {
if err := ioctx.validate(); err != nil {
return err
}
coid := C.CString(oid)
defer C.free(unsafe.Pointer(coid))
cSnapName := C.CString(snapName)
defer C.free(unsafe.Pointer(cSnapName))
ret := C.rados_ioctx_snap_rollback(ioctx.ioctx, coid, cSnapName)
return getError(ret)
}

View File

@ -187,3 +187,70 @@ func (suite *RadosTestSuite) TestListSnapshot() {
} }
}) })
} }
func (suite *RadosTestSuite) TestRollbackSnapshot() {
suite.SetupConnection()
suite.T().Run("InvalidArgs", func(t *testing.T) {
ioctx, err := suite.conn.OpenIOContext(suite.pool)
require.NoError(suite.T(), err)
oid := suite.GenObjectName()
defer suite.ioctx.Delete(oid)
err = ioctx.RollbackSnap(oid, "someName")
assert.Error(t, err)
err = ioctx.CreateSnap("")
assert.NoError(t, err)
defer func() {
assert.NoError(t, ioctx.RemoveSnap(""))
}()
err = ioctx.RollbackSnap("someObj", "")
assert.NoError(t, err)
})
suite.T().Run("invalidIOContext", func(t *testing.T) {
ioctx := &IOContext{}
err := ioctx.RollbackSnap("someObj", "someName")
assert.Error(t, err)
assert.Equal(t, err, ErrInvalidIOContext)
})
suite.T().Run("WriteAndRollback", func(t *testing.T) {
ioctx, err := suite.conn.OpenIOContext(suite.pool)
require.NoError(suite.T(), err)
oid := suite.GenObjectName()
defer suite.ioctx.Delete(oid)
bytesIn := []byte("Harry Potter")
err = suite.ioctx.Write(oid, bytesIn, 0)
assert.NoError(t, err)
// Take snap.
err = ioctx.CreateSnap("mySnap")
assert.NoError(t, err)
defer func() {
assert.NoError(t, ioctx.RemoveSnap("mySnap"))
}()
// Overwrite.
bytesOver := []byte("Potter Harry")
err = suite.ioctx.Write(oid, bytesOver, 0)
assert.NoError(t, err)
// Before rollback.
bytesOut := make([]byte, len(bytesOver))
nOut, err := suite.ioctx.Read(oid, bytesOut, 0)
assert.NoError(t, err)
assert.Equal(t, nOut, len(bytesOver))
assert.Equal(t, bytesOver, bytesOut)
// Rollback.
err = ioctx.RollbackSnap(oid, "mySnap")
assert.NoError(t, err)
// After Rollback.
nOut, err = suite.ioctx.Read(oid, bytesOut, 0)
assert.NoError(t, err)
assert.Equal(t, nOut, len(bytesIn))
assert.Equal(t, bytesOut, bytesIn)
})
}