-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rbd: add GetSnapGroupNamespace() to get details about group snapshots
rbd_snap_get_group_namespace() can be used to get details about snapshots of an image, that were created as part of a group. Signed-off-by: Niels de Vos <[email protected]>
- Loading branch information
1 parent
95c9d7a
commit d8f4825
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//go:build ceph_preview | ||
|
||
package rbd | ||
|
||
// #cgo LDFLAGS: -lrbd | ||
// #include <rbd/librbd.h> | ||
import "C" | ||
|
||
// SnapGroupNamespace provides details about a single snapshot that was taken | ||
// as part of an RBG group. | ||
type SnapGroupNamespace struct { | ||
Pool uint64 | ||
GroupName string | ||
GroupSnapName string | ||
} | ||
|
||
// GetSnapGroupNamespace returns the SnapGroupNamespace of the snapshot which | ||
// is part of a group. The caller should make sure that the snapshot ID passed | ||
// in this function belongs to a snapshot that was taken as part of a group | ||
// snapshot. | ||
// | ||
// Implements: | ||
// | ||
// int rbd_snap_get_group_namespace(rbd_image_t image, uint64_t snap_id, | ||
// rbd_snap_group_namespace_t *group_snap, | ||
// size_t group_snap_size) | ||
func (image *Image) GetSnapGroupNamespace(snapID uint64) (*SnapGroupNamespace, error) { | ||
if err := image.validate(imageIsOpen); err != nil { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
err error | ||
sgn C.rbd_snap_group_namespace_t | ||
) | ||
|
||
ret := C.rbd_snap_get_group_namespace(image.image, | ||
C.uint64_t(snapID), | ||
&sgn, | ||
C.sizeof_rbd_snap_group_namespace_t) | ||
err = getError(ret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer C.rbd_snap_group_namespace_cleanup(&sgn, C.sizeof_rbd_snap_group_namespace_t) | ||
|
||
return &SnapGroupNamespace{ | ||
Pool: uint64(sgn.group_pool), | ||
GroupName: C.GoString(sgn.group_name), | ||
GroupSnapName: C.GoString(sgn.group_snap_name), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//go:build ceph_preview | ||
|
||
package rbd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetSnapGroupNamespace(t *testing.T) { | ||
conn := radosConnect(t) | ||
defer conn.Shutdown() | ||
|
||
poolname := GetUUID() | ||
err := conn.MakePool(poolname) | ||
require.NoError(t, err) | ||
defer conn.DeletePool(poolname) | ||
|
||
ioctx, err := conn.OpenIOContext(poolname) | ||
require.NoError(t, err) | ||
defer ioctx.Destroy() | ||
|
||
imageName := "parent" | ||
groupName := "myGroup" | ||
groupSnapName := "myGroupSnap" | ||
options := NewRbdImageOptions() | ||
defer options.Destroy() | ||
err = options.SetUint64(ImageOptionOrder, uint64(testImageOrder)) | ||
assert.NoError(t, err) | ||
err = options.SetUint64(ImageOptionFeatures, 1) | ||
assert.NoError(t, err) | ||
|
||
err = CreateImage(ioctx, imageName, testImageSize, options) | ||
assert.NoError(t, err) | ||
|
||
img, err := OpenImage(ioctx, imageName, NoSnapshot) | ||
assert.NoError(t, err) | ||
defer func() { | ||
assert.NoError(t, img.Close()) | ||
assert.NoError(t, img.Remove()) | ||
}() | ||
|
||
err = GroupCreate(ioctx, groupName) | ||
require.NoError(t, err) | ||
defer func() { | ||
assert.NoError(t, GroupRemove(ioctx, groupName)) | ||
}() | ||
|
||
err = GroupImageAdd(ioctx, groupName, ioctx, imageName) | ||
require.NoError(t, err) | ||
defer func() { | ||
assert.NoError(t, GroupImageRemove(ioctx, groupName, ioctx, imageName)) | ||
}() | ||
|
||
err = GroupSnapCreate(ioctx, groupName, groupSnapName) | ||
require.NoError(t, err) | ||
defer func() { | ||
assert.NoError(t, GroupSnapRemove(ioctx, groupName, groupSnapName)) | ||
}() | ||
|
||
// Take the details of the 1st snapshot of the image. | ||
snapInfoList, err := img.GetSnapshotNames() | ||
assert.NoError(t, err) | ||
snapInfo := snapInfoList[0] | ||
assert.Positive(t, snapInfo.Id) | ||
assert.Regexp(t, "^\\.group\\.", snapInfo.Name) | ||
|
||
// The snapshot is expected to be in the 'group' namespace. | ||
nsType, err := img.GetSnapNamespaceType(snapInfo.Id) | ||
assert.NoError(t, err) | ||
assert.Equal(t, nsType, SnapNamespaceTypeGroup) | ||
|
||
// Get the info from the snapshot in the group. | ||
sgn, err := img.GetSnapGroupNamespace(snapInfo.Id) | ||
assert.NoError(t, err) | ||
require.NotNil(t, sgn) | ||
assert.Equal(t, groupName, sgn.GroupName) | ||
|
||
// Negative testing follows. | ||
invalidSnapID := uint64(22) | ||
|
||
t.Run("InvalidSnapID", func(t *testing.T) { | ||
_, err := img.GetSnapGroupNamespace(invalidSnapID) | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("InvalidImage", func(t *testing.T) { | ||
invalidImgName := GetUUID() | ||
invalidImg := GetImage(ioctx, invalidImgName) | ||
_, err := invalidImg.GetSnapGroupNamespace(invalidSnapID) | ||
assert.Error(t, err) | ||
}) | ||
} |