Refactor OS fixtures
diff --git a/openstack/db/v1/databases/fixtures.go b/openstack/db/v1/databases/fixtures.go
index 4d653ca..3e67721 100644
--- a/openstack/db/v1/databases/fixtures.go
+++ b/openstack/db/v1/databases/fixtures.go
@@ -1,5 +1,16 @@
package databases
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud/testhelper/fixture"
+)
+
+var (
+ instanceID = "{instanceID}"
+ resURL = "/instances/" + instanceID + "/databases"
+)
+
var createDBsReq = `
{
"databases": [
@@ -36,3 +47,15 @@
]
}
`
+
+func HandleCreate(t *testing.T) {
+ fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202)
+}
+
+func HandleList(t *testing.T) {
+ fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200)
+}
+
+func HandleDelete(t *testing.T) {
+ fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202)
+}
diff --git a/openstack/db/v1/databases/requests_test.go b/openstack/db/v1/databases/requests_test.go
index 11bf9c8..8a1b297 100644
--- a/openstack/db/v1/databases/requests_test.go
+++ b/openstack/db/v1/databases/requests_test.go
@@ -6,20 +6,12 @@
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
- "github.com/rackspace/gophercloud/testhelper/fixture"
-)
-
-const instanceID = "{instanceID}"
-
-var (
- resURL = "/instances/" + instanceID + "/databases"
)
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202)
+ HandleCreate(t)
opts := BatchCreateOpts{
CreateOpts{Name: "testingdb", CharSet: "utf8", Collate: "utf8_general_ci"},
@@ -33,8 +25,7 @@
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200)
+ HandleList(t)
expectedDBs := []Database{
Database{Name: "anotherexampledb"},
@@ -68,8 +59,7 @@
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202)
+ HandleDelete(t)
err := Delete(fake.ServiceClient(), instanceID, "{dbName}").ExtractErr()
th.AssertNoErr(t, err)
diff --git a/openstack/db/v1/flavors/fixtures.go b/openstack/db/v1/flavors/fixtures.go
index 42e2dca..f0016bc 100644
--- a/openstack/db/v1/flavors/fixtures.go
+++ b/openstack/db/v1/flavors/fixtures.go
@@ -1,6 +1,11 @@
package flavors
-import "fmt"
+import (
+ "fmt"
+ "testing"
+
+ "github.com/rackspace/gophercloud/testhelper/fixture"
+)
const flavor = `
{
@@ -21,6 +26,12 @@
`
var (
+ flavorID = "{flavorID}"
+ _baseURL = "/flavors"
+ resURL = "/flavors/" + flavorID
+)
+
+var (
flavor1 = fmt.Sprintf(flavor, 1, 1, 1, "m1.tiny", 512)
flavor2 = fmt.Sprintf(flavor, 2, 2, 2, "m1.small", 1024)
flavor3 = fmt.Sprintf(flavor, 3, 3, 3, "m1.medium", 2048)
@@ -29,3 +40,11 @@
listFlavorsResp = fmt.Sprintf(`{"flavors":[%s, %s, %s, %s]}`, flavor1, flavor2, flavor3, flavor4)
getFlavorResp = fmt.Sprintf(`{"flavor": %s}`, flavor1)
)
+
+func HandleList(t *testing.T) {
+ fixture.SetupHandler(t, _baseURL, "GET", "", listFlavorsResp, 200)
+}
+
+func HandleGet(t *testing.T) {
+ fixture.SetupHandler(t, resURL, "GET", "", getFlavorResp, 200)
+}
diff --git a/openstack/db/v1/flavors/requests_test.go b/openstack/db/v1/flavors/requests_test.go
index 62e5fec..0afe9bb 100644
--- a/openstack/db/v1/flavors/requests_test.go
+++ b/openstack/db/v1/flavors/requests_test.go
@@ -7,20 +7,12 @@
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
- "github.com/rackspace/gophercloud/testhelper/fixture"
-)
-
-var (
- flavorID = "{flavorID}"
- _baseURL = "/flavors"
- resURL = "/flavors/" + flavorID
)
func TestListFlavors(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- fixture.SetupHandler(t, _baseURL, "GET", "", listFlavorsResp, 200)
+ HandleList(t)
pages := 0
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -71,21 +63,17 @@
}
th.AssertDeepEquals(t, expected, actual)
-
return true, nil
})
th.AssertNoErr(t, err)
- if pages != 1 {
- t.Errorf("Expected one page, got %d", pages)
- }
+ th.AssertEquals(t, 1, pages)
}
func TestGetFlavor(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- fixture.SetupHandler(t, resURL, "GET", "", getFlavorResp, 200)
+ HandleGet(t)
actual, err := Get(fake.ServiceClient(), flavorID).Extract()
th.AssertNoErr(t, err)
diff --git a/openstack/db/v1/instances/fixtures.go b/openstack/db/v1/instances/fixtures.go
index dedbfbb..8b6ea06 100644
--- a/openstack/db/v1/instances/fixtures.go
+++ b/openstack/db/v1/instances/fixtures.go
@@ -2,8 +2,10 @@
import (
"fmt"
+ "testing"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/testhelper/fixture"
)
const instance = `
@@ -77,6 +79,14 @@
`
var (
+ instanceID = "{instanceID}"
+ rootURL = "/instances"
+ resURL = rootURL + "/" + instanceID
+ uRootURL = resURL + "/root"
+ aURL = resURL + "/action"
+)
+
+var (
restartReq = `{"restart": true}`
resizeReq = `{"resize": {"flavorRef": "2"}}`
resizeVolReq = `{"resize": {"volume": {"size": 4}}}`
@@ -109,3 +119,39 @@
Status: "BUILD",
Volume: Volume{Size: 2},
}
+
+func HandleCreate(t *testing.T) {
+ fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
+}
+
+func HandleList(t *testing.T) {
+ fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
+}
+
+func HandleGet(t *testing.T) {
+ fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
+}
+
+func HandleDelete(t *testing.T) {
+ fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
+}
+
+func HandleEnableRoot(t *testing.T) {
+ fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
+}
+
+func HandleIsRootEnabled(t *testing.T) {
+ fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
+}
+
+func HandleRestart(t *testing.T) {
+ fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
+}
+
+func HandleResize(t *testing.T) {
+ fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
+}
+
+func HandleResizeVol(t *testing.T) {
+ fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
+}
diff --git a/openstack/db/v1/instances/requests_test.go b/openstack/db/v1/instances/requests_test.go
index d44220d..9e28df4 100644
--- a/openstack/db/v1/instances/requests_test.go
+++ b/openstack/db/v1/instances/requests_test.go
@@ -8,21 +8,12 @@
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
- "github.com/rackspace/gophercloud/testhelper/fixture"
-)
-
-var (
- instanceID = "{instanceID}"
- rootURL = "/instances"
- resURL = rootURL + "/" + instanceID
- uRootURL = resURL + "/root"
- aURL = resURL + "/action"
)
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
+ HandleCreate(t)
opts := CreateOpts{
Name: "json_rack_instance",
@@ -52,7 +43,7 @@
func TestInstanceList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
+ HandleList(t)
pages := 0
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -64,7 +55,6 @@
}
th.CheckDeepEquals(t, []Instance{expectedInstance}, actual)
-
return true, nil
})
@@ -75,7 +65,7 @@
func TestGetInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
+ HandleGet(t)
instance, err := Get(fake.ServiceClient(), instanceID).Extract()
@@ -86,7 +76,7 @@
func TestDeleteInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
+ HandleDelete(t)
res := Delete(fake.ServiceClient(), instanceID)
th.AssertNoErr(t, res.Err)
@@ -95,7 +85,7 @@
func TestEnableRootUser(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
+ HandleEnableRoot(t)
expected := &users.User{Name: "root", Password: "secretsecret"}
user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
@@ -107,7 +97,7 @@
func TestIsRootEnabled(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
+ HandleIsRootEnabled(t)
isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID)
@@ -118,7 +108,7 @@
func TestRestartService(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
+ HandleRestart(t)
res := RestartService(fake.ServiceClient(), instanceID)
th.AssertNoErr(t, res.Err)
@@ -127,7 +117,7 @@
func TestResizeInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
+ HandleResize(t)
res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
th.AssertNoErr(t, res.Err)
@@ -136,7 +126,7 @@
func TestResizeVolume(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
+ HandleResizeVol(t)
res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
th.AssertNoErr(t, res.Err)
diff --git a/openstack/db/v1/users/fixtures.go b/openstack/db/v1/users/fixtures.go
index 25b75f1..516b335 100644
--- a/openstack/db/v1/users/fixtures.go
+++ b/openstack/db/v1/users/fixtures.go
@@ -1,6 +1,11 @@
package users
-import "fmt"
+import (
+ "fmt"
+ "testing"
+
+ "github.com/rackspace/gophercloud/testhelper/fixture"
+)
const user1 = `
{"databases": [{"name": "databaseA"}],"name": "dbuser3"%s}
@@ -11,11 +16,22 @@
`
var (
- pUser1 = fmt.Sprintf(user1, `,"password":"secretsecret"`)
- pUser2 = fmt.Sprintf(user2, `,"password":"secretsecret"`)
+ instanceID = "{instanceID}"
+ _rootURL = "/instances/" + instanceID + "/users"
+ pUser1 = fmt.Sprintf(user1, `,"password":"secretsecret"`)
+ pUser2 = fmt.Sprintf(user2, `,"password":"secretsecret"`)
+ createReq = fmt.Sprintf(`{"users":[%s, %s]}`, pUser1, pUser2)
+ listResp = fmt.Sprintf(`{"users":[%s, %s]}`, fmt.Sprintf(user1, ""), fmt.Sprintf(user2, ""))
)
-var (
- createReq = fmt.Sprintf(`{"users":[%s, %s]}`, pUser1, pUser2)
- listResp = fmt.Sprintf(`{"users":[%s, %s]}`, fmt.Sprintf(user1, ""), fmt.Sprintf(user2, ""))
-)
+func HandleCreate(t *testing.T) {
+ fixture.SetupHandler(t, _rootURL, "POST", createReq, "", 202)
+}
+
+func HandleList(t *testing.T) {
+ fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
+}
+
+func HandleDelete(t *testing.T) {
+ fixture.SetupHandler(t, _rootURL+"/{userName}", "DELETE", "", "", 202)
+}
diff --git a/openstack/db/v1/users/requests_test.go b/openstack/db/v1/users/requests_test.go
index 47eccea..5711f63 100644
--- a/openstack/db/v1/users/requests_test.go
+++ b/openstack/db/v1/users/requests_test.go
@@ -7,18 +7,12 @@
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
- "github.com/rackspace/gophercloud/testhelper/fixture"
-)
-
-var (
- instanceID = "{instanceID}"
- _rootURL = "/instances/" + instanceID + "/users"
)
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, _rootURL, "POST", createReq, "", 202)
+ HandleCreate(t)
opts := BatchCreateOpts{
CreateOpts{
@@ -45,7 +39,7 @@
func TestUserList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
+ HandleList(t)
expectedUsers := []User{
User{
@@ -80,11 +74,11 @@
th.AssertEquals(t, 1, pages)
}
-func TestDeleteInstance(t *testing.T) {
+func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixture.SetupHandler(t, _rootURL+"/{dbName}", "DELETE", "", "", 202)
+ HandleDelete(t)
- res := Delete(fake.ServiceClient(), instanceID, "{dbName}")
+ res := Delete(fake.ServiceClient(), instanceID, "{userName}")
th.AssertNoErr(t, res.Err)
}