From bcf44fc782ae343ed8edee348ead2c243aa1b5cc Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Mon, 14 Oct 2019 14:09:30 +0200 Subject: [PATCH] Add GetPoolByName() for calling rados_pool_lookup() Signed-off-by: Niels de Vos --- rados/conn.go | 16 ++++++++++++++ rados/rados_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/rados/conn.go b/rados/conn.go index 3dfb30b..1b12642 100644 --- a/rados/conn.go +++ b/rados/conn.go @@ -280,6 +280,22 @@ func (c *Conn) DeletePool(name string) error { } } +// GetPoolByName returns the ID of the pool with a given name. +func (c *Conn) GetPoolByName(name string) (int64, error) { + if err := c.ensure_connected(); err != nil { + fmt.Println("NOT CONNECTED WHOOPS") + return 0, err + } + c_name := C.CString(name) + defer C.free(unsafe.Pointer(c_name)) + ret := int64(C.rados_pool_lookup(c.cluster, c_name)) + if ret < 0 { + return 0, RadosError(ret) + } else { + return ret, nil + } +} + // MonCommand sends a command to one of the monitors func (c *Conn) MonCommand(args []byte) (buffer []byte, info string, err error) { return c.monCommand(args, nil) diff --git a/rados/rados_test.go b/rados/rados_test.go index 77bcd49..26c1c9d 100644 --- a/rados/rados_test.go +++ b/rados/rados_test.go @@ -246,6 +246,40 @@ func (suite *RadosTestSuite) TestGetInstanceID() { func (suite *RadosTestSuite) TestMakeDeletePool() { suite.SetupConnection() + // check that new pool name is unique + new_name := uuid.Must(uuid.NewV4()).String() + _, err := suite.conn.GetPoolByName(new_name) + if err == nil { + suite.T().Error("Random pool name exists!") + return + } + + // create pool + err = suite.conn.MakePool(new_name) + assert.NoError(suite.T(), err) + + // verify that the new pool name exists + pool, err := suite.conn.GetPoolByName(new_name) + assert.NoError(suite.T(), err) + + if pool == 0 { + suite.T().Error("Cannot find newly created pool") + } + + // delete the pool + err = suite.conn.DeletePool(new_name) + assert.NoError(suite.T(), err) + + // verify that it is gone + pool, _ = suite.conn.GetPoolByName(new_name) + if pool != 0 { + suite.T().Error("Deleted pool still exists") + } +} + +func (suite *RadosTestSuite) TestGetPoolByName() { + suite.SetupConnection() + // get current list of pool pools, err := suite.conn.ListPools() assert.NoError(suite.T(), err) @@ -259,6 +293,12 @@ func (suite *RadosTestSuite) TestMakeDeletePool() { } } + pool, _ := suite.conn.GetPoolByName(new_name) + if pool != 0 { + suite.T().Error("Pool does not exist, but was found!") + return + } + // create pool err = suite.conn.MakePool(new_name) assert.NoError(suite.T(), err) @@ -278,6 +318,13 @@ func (suite *RadosTestSuite) TestMakeDeletePool() { suite.T().Error("Cannot find newly created pool") } + pool, err = suite.conn.GetPoolByName(new_name) + assert.NoError(suite.T(), err) + if pool == 0 { + suite.T().Error("Pool not found!") + return + } + // delete the pool err = suite.conn.DeletePool(new_name) assert.NoError(suite.T(), err) @@ -296,6 +343,12 @@ func (suite *RadosTestSuite) TestMakeDeletePool() { if found { suite.T().Error("Deleted pool still exists") } + + pool, err = suite.conn.GetPoolByName(new_name) + if pool != 0 || err == nil { + suite.T().Error("Pool should have been deleted, but was found!") + return + } } func (suite *RadosTestSuite) TestPingMonitor() {