rados: add GetNamespace implementing rados_ioctx_get_namespace

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-10-09 11:10:48 -04:00 committed by mergify[bot]
parent bd77bccc68
commit b756c84956
2 changed files with 77 additions and 0 deletions

43
rados/ioctx_nautilus.go Normal file
View File

@ -0,0 +1,43 @@
// +build !luminous,!mimic
package rados
// #cgo LDFLAGS: -lrados
// #include <rados/librados.h>
//
import "C"
import (
"unsafe"
"github.com/ceph/go-ceph/internal/retry"
)
// GetNamespace gets the namespace used for objects within this IO context.
//
// Implements:
// int rados_ioctx_get_namespace(rados_ioctx_t io, char *buf,
// unsigned maxlen);
func (ioctx *IOContext) GetNamespace() (string, error) {
if err := ioctx.validate(); err != nil {
return "", err
}
var (
err error
buf []byte
ret C.int
)
retry.WithSizes(128, 8192, func(size int) retry.Hint {
buf = make([]byte, size)
ret = C.rados_ioctx_get_namespace(
ioctx.ioctx,
(*C.char)(unsafe.Pointer(&buf[0])),
C.unsigned(len(buf)))
err = getErrorIfNegative(ret)
return retry.DoubleSize.If(err == errRange)
})
if err != nil {
return "", err
}
return string(buf[:ret]), nil
}

View File

@ -0,0 +1,34 @@
// +build !luminous,!mimic
package rados
import (
"testing"
"github.com/stretchr/testify/assert"
)
func (suite *RadosTestSuite) TestSetGetNamespace() {
suite.SetupConnection()
suite.T().Run("validNS", func(t *testing.T) {
suite.ioctx.SetNamespace("space1")
ns, err := suite.ioctx.GetNamespace()
assert.NoError(t, err)
assert.Equal(t, "space1", ns)
})
suite.T().Run("allNamespaces", func(t *testing.T) {
suite.ioctx.SetNamespace(AllNamespaces)
ns, err := suite.ioctx.GetNamespace()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), AllNamespaces, ns)
})
suite.T().Run("invalidIoctx", func(t *testing.T) {
i := &IOContext{}
ns, err := i.GetNamespace()
assert.Error(suite.T(), err)
assert.Equal(suite.T(), "", ns)
})
}