rbd: add error checking in OpenImage*() functions

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-06-10 10:38:27 +02:00 committed by John Mulligan
parent a2c1c2bb34
commit 9e7a1c444f
2 changed files with 51 additions and 0 deletions

View File

@ -1028,6 +1028,13 @@ func TrashRestore(ioctx *rados.IOContext, id, name string) error {
// int rbd_open(rados_ioctx_t io, const char *name,
// rbd_image_t *image, const char *snap_name);
func OpenImage(ioctx *rados.IOContext, name, snapName string) (*Image, error) {
if ioctx == nil {
return nil, ErrNoIOContext
}
if name == "" {
return nil, ErrNoName
}
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
@ -1064,6 +1071,13 @@ func OpenImage(ioctx *rados.IOContext, name, snapName string) (*Image, error) {
// int rbd_open_read_only(rados_ioctx_t io, const char *name,
// rbd_image_t *image, const char *snap_name);
func OpenImageReadOnly(ioctx *rados.IOContext, name, snapName string) (*Image, error) {
if ioctx == nil {
return nil, ErrNoIOContext
}
if name == "" {
return nil, ErrNoName
}
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
@ -1101,6 +1115,13 @@ func OpenImageReadOnly(ioctx *rados.IOContext, name, snapName string) (*Image, e
// int rbd_open_by_id(rados_ioctx_t io, const char *id,
// rbd_image_t *image, const char *snap_name);
func OpenImageById(ioctx *rados.IOContext, id, snapName string) (*Image, error) {
if ioctx == nil {
return nil, ErrNoIOContext
}
if id == "" {
return nil, ErrNoName
}
cid := C.CString(id)
defer C.free(unsafe.Pointer(cid))
@ -1138,6 +1159,13 @@ func OpenImageById(ioctx *rados.IOContext, id, snapName string) (*Image, error)
// int rbd_open_by_id_read_only(rados_ioctx_t io, const char *id,
// rbd_image_t *image, const char *snap_name);
func OpenImageByIdReadOnly(ioctx *rados.IOContext, id, snapName string) (*Image, error) {
if ioctx == nil {
return nil, ErrNoIOContext
}
if id == "" {
return nil, ErrNoName
}
cid := C.CString(id)
defer C.free(unsafe.Pointer(cid))

View File

@ -1242,6 +1242,13 @@ func TestOpenImage(t *testing.T) {
name := GetUUID()
// pass invalid arguments
_, err = OpenImage(nil, "some-image", NoSnapshot)
require.Error(t, err)
_, err = OpenImage(ioctx, "", NoSnapshot)
require.Error(t, err)
// image does not exist yet
_, err = OpenImage(ioctx, name, NoSnapshot)
assert.Error(t, err)
@ -1255,6 +1262,12 @@ func TestOpenImage(t *testing.T) {
assert.NoError(t, err)
// open read-only
// pass invalid parameters
_, err = OpenImageReadOnly(nil, "some-image", NoSnapshot)
require.Error(t, err)
_, err = OpenImageReadOnly(ioctx, "", NoSnapshot)
require.Error(t, err)
oImage, err = OpenImageReadOnly(ioctx, name, NoSnapshot)
assert.NoError(t, err)
@ -1495,6 +1508,16 @@ func TestOpenImageById(t *testing.T) {
err = workingImage.Close()
assert.NoError(t, err)
t.Run("InvalidArguments", func(t *testing.T) {
_, err = OpenImageById(nil, "some-id", NoSnapshot)
require.Error(t, err)
_, err = OpenImageById(ioctx, "", NoSnapshot)
require.Error(t, err)
_, err = OpenImageByIdReadOnly(nil, "some-id", NoSnapshot)
require.Error(t, err)
_, err = OpenImageByIdReadOnly(ioctx, "", NoSnapshot)
require.Error(t, err)
})
t.Run("ReadWriteBadId", func(t *testing.T) {
t.Skip("segfaults due to https://tracker.ceph.com/issues/43178")
// phony id