From 15dfd6fc0911d81e515a51ce8f2d4054dc3c8574 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Fri, 20 Dec 2019 15:01:57 -0500 Subject: [PATCH] rados: reduce boilerplate in TestGetPoolByName using testify Testfiy's assert and require libs have helpful functions for checking values. Use them to reduce a bunch of boilerplate code in the TestGetPoolByName function. Signed-off-by: John Mulligan --- rados/rados_test.go | 51 ++++++++++----------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-) diff --git a/rados/rados_test.go b/rados/rados_test.go index aa0f6fe..88f9fc6 100644 --- a/rados/rados_test.go +++ b/rados/rados_test.go @@ -300,18 +300,11 @@ func (suite *RadosTestSuite) TestGetPoolByName() { // check that new pool name is unique new_name := uuid.Must(uuid.NewV4()).String() - for _, poolname := range pools { - if new_name == poolname { - suite.T().Error("Random pool name exists!") - return - } - } + require.NotContains( + suite.T(), pools, new_name, "Random pool name exists!") pool, _ := suite.conn.GetPoolByName(new_name) - if pool != 0 { - suite.T().Error("Pool does not exist, but was found!") - return - } + assert.Equal(suite.T(), int64(0), pool, "Pool does not exist, but was found!") // create pool err = suite.conn.MakePool(new_name) @@ -320,24 +313,12 @@ func (suite *RadosTestSuite) TestGetPoolByName() { // verify that the new pool name exists pools, err = suite.conn.ListPools() assert.NoError(suite.T(), err) - - found := false - for _, poolname := range pools { - if new_name == poolname { - found = true - } - } - - if !found { - suite.T().Error("Cannot find newly created pool") - } + assert.Contains( + suite.T(), pools, new_name, "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 - } + assert.NotEqual(suite.T(), int64(0), pool, "Pool not found!") // delete the pool err = suite.conn.DeletePool(new_name) @@ -346,23 +327,13 @@ func (suite *RadosTestSuite) TestGetPoolByName() { // verify that it is gone pools, err = suite.conn.ListPools() assert.NoError(suite.T(), err) - - found = false - for _, poolname := range pools { - if new_name == poolname { - found = true - } - } - - if found { - suite.T().Error("Deleted pool still exists") - } + assert.NotContains( + suite.T(), pools, new_name, "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 - } + assert.Error(suite.T(), err) + assert.Equal( + suite.T(), int64(0), pool, "Pool should have been deleted, but was found!") } func (suite *RadosTestSuite) TestGetPoolByID() {