Refactor fixtures
diff --git a/rackspace/db/v1/backups/fixtures.go b/rackspace/db/v1/backups/fixtures.go
index 3402e3a..c6244e2 100644
--- a/rackspace/db/v1/backups/fixtures.go
+++ b/rackspace/db/v1/backups/fixtures.go
@@ -1,12 +1,6 @@
package backups
-import (
- "testing"
-
- "github.com/rackspace/gophercloud/testhelper/fixture"
-)
-
-var singleBackup = `
+var getResp = `
{
"backup": {
"created": "2014-02-13T21:47:16",
@@ -28,8 +22,7 @@
}
`
-func HandleCreateSuccessfully(t *testing.T) {
- requestJSON := `
+var createReq = `
{
"backup": {
"description": "My Backup",
@@ -39,11 +32,9 @@
}
`
- fixture.SetupHandler(t, "/backups", "POST", requestJSON, singleBackup, 202)
-}
+var createResp = getResp
-func HandleListSuccessfully(t *testing.T) {
- responseJSON := `
+var listResp = `
{
"backups": [
{
@@ -66,14 +57,3 @@
]
}
`
-
- fixture.SetupHandler(t, "/backups", "GET", "", responseJSON, 200)
-}
-
-func HandleGetSuccessfully(t *testing.T, backupID string) {
- fixture.SetupHandler(t, "/backups/"+backupID, "GET", "", singleBackup, 200)
-}
-
-func HandleDeleteSuccessfully(t *testing.T, backupID string) {
- fixture.SetupHandler(t, "/backups/"+backupID, "DELETE", "", "", 202)
-}
diff --git a/rackspace/db/v1/backups/requests_test.go b/rackspace/db/v1/backups/requests_test.go
index e5d8f7c..a56bc35 100644
--- a/rackspace/db/v1/backups/requests_test.go
+++ b/rackspace/db/v1/backups/requests_test.go
@@ -7,15 +7,19 @@
"github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
+ "github.com/rackspace/gophercloud/testhelper/fixture"
)
-const backupID = "61f12fef-edb1-4561-8122-e7c00ef26a82"
+var (
+ backupID = "{backupID}"
+ _rootURL = "/backups"
+ resURL = _rootURL + "/" + backupID
+)
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleCreateSuccessfully(t)
+ fixture.SetupHandler(t, _rootURL, "POST", createReq, createResp, 202)
opts := CreateOpts{
Name: "snapshot",
@@ -50,13 +54,12 @@
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
- HandleListSuccessfully(t)
+ pages := 0
- count := 0
-
- List(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
- count++
+ err := List(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
+ pages++
actual, err := ExtractBackups(page)
th.AssertNoErr(t, err)
@@ -85,16 +88,14 @@
return true, nil
})
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, 1, pages)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleGetSuccessfully(t, backupID)
+ fixture.SetupHandler(t, resURL, "GET", "", getResp, 200)
instance, err := Get(fake.ServiceClient(), backupID).Extract()
th.AssertNoErr(t, err)
@@ -123,8 +124,7 @@
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleDeleteSuccessfully(t, backupID)
+ fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
err := Delete(fake.ServiceClient(), backupID).ExtractErr()
th.AssertNoErr(t, err)
diff --git a/rackspace/db/v1/configurations/requests_test.go b/rackspace/db/v1/configurations/requests_test.go
index 29d1119..f026be4 100644
--- a/rackspace/db/v1/configurations/requests_test.go
+++ b/rackspace/db/v1/configurations/requests_test.go
@@ -27,7 +27,6 @@
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, _baseURL, "GET", "", listConfigsJSON, 200)
count := 0
@@ -49,7 +48,6 @@
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, resURL, "GET", "", getConfigJSON, 200)
config, err := Get(fake.ServiceClient(), configID).Extract()
@@ -60,7 +58,6 @@
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, _baseURL, "POST", createReq, createConfigJSON, 201)
opts := CreateOpts{
@@ -84,7 +81,6 @@
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, resURL, "PATCH", updateReq, "", 200)
opts := UpdateOpts{
@@ -100,7 +96,6 @@
func TestReplace(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, resURL, "PUT", updateReq, "", 202)
opts := UpdateOpts{
@@ -116,7 +111,6 @@
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
err := Delete(fake.ServiceClient(), configID).ExtractErr()
@@ -126,7 +120,6 @@
func TestListInstances(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, resURL+"/instances", "GET", "", listInstancesJSON, 200)
expectedInstance := instances.Instance{
@@ -155,7 +148,6 @@
func TestListDSParams(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, dsParamListURL, "GET", "", listParamsJSON, 200)
pages := 0
@@ -186,7 +178,6 @@
func TestGetDSParam(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, dsParamGetURL, "GET", "", getParamJSON, 200)
param, err := GetDatastoreParam(fake.ServiceClient(), dsID, versionID, paramID).Extract()
@@ -202,7 +193,6 @@
func TestListGlobalParams(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, globalParamListURL, "GET", "", listParamsJSON, 200)
pages := 0
@@ -233,7 +223,6 @@
func TestGetGlobalParam(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, globalParamGetURL, "GET", "", getParamJSON, 200)
param, err := GetGlobalParam(fake.ServiceClient(), versionID, paramID).Extract()
diff --git a/rackspace/db/v1/databases/delegate_test.go b/rackspace/db/v1/databases/delegate_test.go
index f557a15..b9e50a5 100644
--- a/rackspace/db/v1/databases/delegate_test.go
+++ b/rackspace/db/v1/databases/delegate_test.go
@@ -9,13 +9,18 @@
fake "github.com/rackspace/gophercloud/testhelper/client"
)
-const instanceID = "{instanceID}"
+var (
+ instanceID = "{instanceID}"
+ rootURL = "/instances"
+ resURL = rootURL + "/" + instanceID
+ uRootURL = resURL + "/root"
+ aURL = resURL + "/action"
+)
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleCreateDBSuccessfully(t, instanceID)
+ os.HandleCreate(t)
opts := os.BatchCreateOpts{
os.CreateOpts{Name: "testingdb", CharSet: "utf8", Collate: "utf8_general_ci"},
@@ -29,8 +34,7 @@
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleListDBsSuccessfully(t, instanceID)
+ os.HandleList(t)
expectedDBs := []os.Database{
os.Database{Name: "anotherexampledb"},
@@ -50,22 +54,17 @@
}
th.CheckDeepEquals(t, expectedDBs, actual)
-
return true, nil
})
th.AssertNoErr(t, err)
-
- if pages != 1 {
- t.Errorf("Expected 1 page, saw %d", pages)
- }
+ th.AssertEquals(t, 1, pages)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleDeleteDBSuccessfully(t, instanceID, "{dbName}")
+ os.HandleDelete(t)
err := os.Delete(fake.ServiceClient(), instanceID, "{dbName}").ExtractErr()
th.AssertNoErr(t, err)
diff --git a/rackspace/db/v1/datastores/requests_test.go b/rackspace/db/v1/datastores/requests_test.go
index 36e6b32..5246310 100644
--- a/rackspace/db/v1/datastores/requests_test.go
+++ b/rackspace/db/v1/datastores/requests_test.go
@@ -12,7 +12,6 @@
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, "/datastores", "GET", "", listDSResp, 200)
pages := 0
@@ -37,7 +36,6 @@
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, "/datastores/{dsID}", "GET", "", getDSResp, 200)
ds, err := Get(fake.ServiceClient(), "{dsID}").Extract()
@@ -48,7 +46,6 @@
func TestListVersions(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, "/datastores/{dsID}/versions", "GET", "", listVersionsResp, 200)
pages := 0
@@ -73,7 +70,6 @@
func TestGetVersion(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
fixture.SetupHandler(t, "/datastores/{dsID}/versions/{versionID}", "GET", "", getVersionResp, 200)
ds, err := GetVersion(fake.ServiceClient(), "{dsID}", "{versionID}").Extract()
diff --git a/rackspace/db/v1/flavors/delegate_test.go b/rackspace/db/v1/flavors/delegate_test.go
index 5c67d55..4f51776 100644
--- a/rackspace/db/v1/flavors/delegate_test.go
+++ b/rackspace/db/v1/flavors/delegate_test.go
@@ -13,8 +13,7 @@
func TestListFlavors(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleListFlavorsSuccessfully(t)
+ os.HandleList(t)
pages := 0
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -78,10 +77,9 @@
func TestGetFlavor(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ os.HandleGet(t)
- os.HandleGetFlavorSuccessfully(t, "12345")
-
- actual, err := Get(fake.ServiceClient(), "12345").Extract()
+ actual, err := Get(fake.ServiceClient(), "{flavorID}").Extract()
th.AssertNoErr(t, err)
expected := &os.Flavor{
diff --git a/rackspace/db/v1/instances/delegate_test.go b/rackspace/db/v1/instances/delegate_test.go
index d43b1e1..4a13286 100644
--- a/rackspace/db/v1/instances/delegate_test.go
+++ b/rackspace/db/v1/instances/delegate_test.go
@@ -10,9 +10,14 @@
"github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
+ "github.com/rackspace/gophercloud/testhelper/fixture"
)
-var instanceID = "d4603f69-ec7e-4e9b-803f-600b9205576f"
+var (
+ instanceID = "{instanceID}"
+ _rootURL = "/instances"
+ resURL = "/instances/" + instanceID
+)
var expectedInstance = &Instance{
Created: "2014-02-13T21:47:13",
@@ -38,8 +43,7 @@
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleCreateInstanceSuccessfully(t)
+ fixture.SetupHandler(t, _rootURL, "POST", createReq, createResp, 200)
opts := CreateOpts{
Name: "json_rack_instance",
@@ -70,8 +74,7 @@
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleGetInstanceSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, resURL, "GET", "", getResp, 200)
instance, err := Get(fake.ServiceClient(), instanceID).Extract()
@@ -82,8 +85,7 @@
func TestDeleteInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleDeleteInstanceSuccessfully(t, instanceID)
+ os.HandleDelete(t)
res := Delete(fake.ServiceClient(), instanceID)
th.AssertNoErr(t, res.Err)
@@ -92,8 +94,7 @@
func TestEnableRootUser(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleEnableRootUserSuccessfully(t, instanceID)
+ os.HandleEnableRoot(t)
expected := &osUsers.User{Name: "root", Password: "secretsecret"}
@@ -105,32 +106,26 @@
func TestRestartService(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleRestartSuccessfully(t, instanceID)
+ os.HandleRestart(t)
res := RestartService(fake.ServiceClient(), instanceID)
-
th.AssertNoErr(t, res.Err)
}
func TestResizeInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleResizeInstanceSuccessfully(t, instanceID)
+ os.HandleResize(t)
res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
-
th.AssertNoErr(t, res.Err)
}
func TestResizeVolume(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleResizeVolSuccessfully(t, instanceID)
+ os.HandleResizeVol(t)
res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
-
th.AssertNoErr(t, res.Err)
}
diff --git a/rackspace/db/v1/instances/fixtures.go b/rackspace/db/v1/instances/fixtures.go
index e144099..92ad7ec 100644
--- a/rackspace/db/v1/instances/fixtures.go
+++ b/rackspace/db/v1/instances/fixtures.go
@@ -1,15 +1,12 @@
package instances
import (
- "fmt"
- "net/http"
- "testing"
-
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
+ "github.com/rackspace/gophercloud"
+ os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
+ "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
)
-const singleInstanceJson = `
+const instance = `
{
"instance": {
"created": "2014-02-13T21:47:13",
@@ -37,7 +34,7 @@
}
],
"hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.rackspaceclouddb.com",
- "id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
+ "id": "{instanceID}",
"name": "json_rack_instance",
"status": "BUILD",
"updated": "2014-02-13T21:47:13",
@@ -48,15 +45,7 @@
}
`
-func HandleCreateInstanceSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- th.TestJSONRequest(t, r, `
+var createReq = `
{
"instance": {
"databases": [
@@ -85,22 +74,14 @@
"volume": {
"size": 2
},
- "restorePoint": {
- "backupRef": "1234567890"
- }
+ "restorePoint": {
+ "backupRef": "1234567890"
+ }
}
}
-`)
+`
- fmt.Fprintf(w, singleInstanceJson)
- })
-}
-
-func HandleCreateReplicaSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestJSONRequest(t, r, `
+var createReplicaReq = `
{
"instance": {
"volume": {
@@ -111,12 +92,9 @@
"replica_of": "6bdca2fc-418e-40bd-a595-62abda61862d"
}
}
-`)
+`
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- fmt.Fprintf(w, `
+var createReplicaResp = `
{
"instance": {
"status": "BUILD",
@@ -134,85 +112,62 @@
],
"created": "2014-10-14T18:42:15",
"id": "8367c312-7c40-4a66-aab1-5767478914fc",
- "volume": {"size": 1},
- "flavor": {"id": "9"},
+ "volume": {
+ "size": 1
+ },
+ "flavor": {
+ "id": "9"
+ },
"datastore": {
"version": "5.6",
"type": "mysql"
},
- "replica_of": {"id": "6bdca2fc-418e-40bd-a595-62abda61862d"}
+ "replica_of": {
+ "id": "6bdca2fc-418e-40bd-a595-62abda61862d"
+ }
}
}
-`)
- })
-}
+`
-func HandleListReplicasSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, `
+var listReplicasResp = `
{
- "instances": [
- {
- "status": "ACTIVE",
- "name": "t1s1_ALT_GUEST",
- "links": [
- {
- "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254",
- "rel": "self"
- },
- {
- "href": "https://ord.databases.api.rackspacecloud.com/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254",
- "rel": "bookmark"
- }
- ],
- "ip": [
- "10.0.0.3"
- ],
- "id": "3c691f06-bf9a-4618-b7ec-2817ce0cf254",
- "volume": {
- "size": 1
- },
- "flavor": {
- "id": "9"
- },
- "datastore": {
- "version": "5.6",
- "type": "mysql"
- },
- "replica_of": {
- "id": "8b499b45-52d6-402d-b398-f9d8f279c69a"
- }
- }
- ]
+ "instances": [
+ {
+ "status": "ACTIVE",
+ "name": "t1s1_ALT_GUEST",
+ "links": [
+ {
+ "href": "https://ord.databases.api.rackspacecloud.com/v1.0/1234/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254",
+ "rel": "self"
+ },
+ {
+ "href": "https://ord.databases.api.rackspacecloud.com/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254",
+ "rel": "bookmark"
+ }
+ ],
+ "ip": [
+ "10.0.0.3"
+ ],
+ "id": "3c691f06-bf9a-4618-b7ec-2817ce0cf254",
+ "volume": {
+ "size": 1
+ },
+ "flavor": {
+ "id": "9"
+ },
+ "datastore": {
+ "version": "5.6",
+ "type": "mysql"
+ },
+ "replica_of": {
+ "id": "8b499b45-52d6-402d-b398-f9d8f279c69a"
+ }
+ }
+ ]
}
-`)
- })
-}
+`
-func HandleGetInstanceSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, singleInstanceJson)
- })
-}
-
-func HandleGetReplicaSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, `
+var getReplicaResp = `
{
"instance": {
"status": "ACTIVE",
@@ -223,32 +178,36 @@
"10.0.0.2"
],
"replicas": [
- {"id": "3c691f06-bf9a-4618-b7ec-2817ce0cf254"}
+ {
+ "id": "3c691f06-bf9a-4618-b7ec-2817ce0cf254"
+ }
],
"id": "8b499b45-52d6-402d-b398-f9d8f279c69a",
"volume": {
"used": 0.54,
"size": 1
},
- "flavor": {"id": "9"},
+ "flavor": {
+ "id": "9"
+ },
"datastore": {
"version": "5.6",
"type": "mysql"
}
}
}
-`)
- })
+`
+
+var detachReq = `
+{
+ "instance": {
+ "replica_of": "",
+ "slave_of": ""
+ }
}
+`
-func HandleGetConfigSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id+"/configuration", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, `
+var getConfigResp = `
{
"instance": {
"configuration": {
@@ -297,31 +256,11 @@
}
}
}
-`)
- })
-}
+`
-func HandleAssociateGroupSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestJSONRequest(t, r, `{"instance": {"configuration": "{configGroupID}"}}`)
+var associateReq = `{"instance": {"configuration": "{configGroupID}"}}`
- w.WriteHeader(http.StatusAccepted)
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, singleInstanceJson)
- })
-}
-
-func HandleListBackupsSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id+"/backups", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, `
+var listBackupsResp = `
{
"backups": [
{
@@ -343,25 +282,28 @@
}
]
}
-`)
- })
-}
+`
-func HandleDetachReplicaSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PATCH")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+var (
+ createResp = instance
+ getResp = instance
+ associateResp = instance
+)
- th.TestJSONRequest(t, r, `
-{
- "instance": {
- "replica_of": "",
- "slave_of": ""
- }
-}
-`)
-
- w.Header().Add("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
+var expectedReplica = &Instance{
+ Status: "BUILD",
+ Updated: "2014-10-14T18:42:15",
+ Name: "t2s1_ALT_GUEST",
+ Links: []gophercloud.Link{
+ gophercloud.Link{Rel: "self", Href: "https://ord.databases.api.rackspacecloud.com/v1.0/5919009/instances/8367c312-7c40-4a66-aab1-5767478914fc"},
+ gophercloud.Link{Rel: "bookmark", Href: "https://ord.databases.api.rackspacecloud.com/instances/8367c312-7c40-4a66-aab1-5767478914fc"},
+ },
+ Created: "2014-10-14T18:42:15",
+ ID: "8367c312-7c40-4a66-aab1-5767478914fc",
+ Volume: os.Volume{Size: 1},
+ Flavor: os.Flavor{ID: "9"},
+ Datastore: datastores.DatastorePartial{Version: "5.6", Type: "mysql"},
+ ReplicaOf: &Instance{
+ ID: "6bdca2fc-418e-40bd-a595-62abda61862d",
+ },
}
diff --git a/rackspace/db/v1/instances/requests_test.go b/rackspace/db/v1/instances/requests_test.go
index 35afe01..0805dd8 100644
--- a/rackspace/db/v1/instances/requests_test.go
+++ b/rackspace/db/v1/instances/requests_test.go
@@ -10,31 +10,13 @@
"github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
+ "github.com/rackspace/gophercloud/testhelper/fixture"
)
-var expectedReplica = &Instance{
- Status: "BUILD",
- Updated: "2014-10-14T18:42:15",
- Name: "t2s1_ALT_GUEST",
- Links: []gophercloud.Link{
- gophercloud.Link{Rel: "self", Href: "https://ord.databases.api.rackspacecloud.com/v1.0/5919009/instances/8367c312-7c40-4a66-aab1-5767478914fc"},
- gophercloud.Link{Rel: "bookmark", Href: "https://ord.databases.api.rackspacecloud.com/instances/8367c312-7c40-4a66-aab1-5767478914fc"},
- },
- Created: "2014-10-14T18:42:15",
- ID: "8367c312-7c40-4a66-aab1-5767478914fc",
- Volume: os.Volume{Size: 1},
- Flavor: os.Flavor{ID: "9"},
- Datastore: datastores.DatastorePartial{Version: "5.6", Type: "mysql"},
- ReplicaOf: &Instance{
- ID: "6bdca2fc-418e-40bd-a595-62abda61862d",
- },
-}
-
func TestGetConfig(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleGetConfigSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, resURL+"/configuration", "GET", "", getConfigResp, 200)
config, err := GetDefaultConfig(fake.ServiceClient(), instanceID).Extract()
@@ -90,23 +72,21 @@
func TestAssociateWithConfigGroup(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ fixture.SetupHandler(t, resURL, "PUT", associateReq, associateResp, 202)
- HandleAssociateGroupSuccessfully(t, instanceID)
-
- configGroupID := "{configGroupID}"
- res := AssociateWithConfigGroup(fake.ServiceClient(), instanceID, configGroupID)
+ res := AssociateWithConfigGroup(fake.ServiceClient(), instanceID, "{configGroupID}")
th.AssertNoErr(t, res.Err)
}
func TestListBackups(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ fixture.SetupHandler(t, resURL+"/backups", "GET", "", listBackupsResp, 200)
- HandleListBackupsSuccessfully(t, instanceID)
- count := 0
+ pages := 0
- ListBackups(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
- count++
+ err := ListBackups(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
+ pages++
actual, err := backups.ExtractBackups(page)
th.AssertNoErr(t, err)
@@ -127,20 +107,17 @@
}
th.AssertDeepEquals(t, expected, actual)
-
return true, nil
})
- if count != 1 {
- t.Errorf("Expected 1 page, got %d", count)
- }
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, 1, pages)
}
func TestCreateReplica(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleCreateReplicaSuccessfully(t)
+ fixture.SetupHandler(t, _rootURL, "POST", createReplicaReq, createReplicaResp, 200)
opts := CreateOpts{
Name: "t2s1_ALT_GUEST",
@@ -151,15 +128,13 @@
replica, err := Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
-
th.AssertDeepEquals(t, expectedReplica, replica)
}
func TestListReplicas(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleListReplicasSuccessfully(t)
+ fixture.SetupHandler(t, _rootURL, "GET", "", listReplicasResp, 200)
pages := 0
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -195,17 +170,13 @@
})
th.AssertNoErr(t, err)
-
- if pages != 1 {
- t.Errorf("Expected 1 page, saw %d", pages)
- }
+ th.AssertEquals(t, 1, pages)
}
func TestGetReplica(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleGetReplicaSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, resURL, "GET", "", getReplicaResp, 200)
replica, err := Get(fake.ServiceClient(), instanceID).Extract()
th.AssertNoErr(t, err)
@@ -239,9 +210,8 @@
func TestDetachReplica(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ fixture.SetupHandler(t, resURL, "PATCH", detachReq, "", 202)
- HandleDetachReplicaSuccessfully(t, "{replicaID}")
-
- err := DetachReplica(fake.ServiceClient(), "{replicaID}").ExtractErr()
+ err := DetachReplica(fake.ServiceClient(), instanceID).ExtractErr()
th.AssertNoErr(t, err)
}
diff --git a/rackspace/db/v1/users/delegate_test.go b/rackspace/db/v1/users/delegate_test.go
index a5da864..c818fe1 100644
--- a/rackspace/db/v1/users/delegate_test.go
+++ b/rackspace/db/v1/users/delegate_test.go
@@ -15,8 +15,7 @@
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleCreateUserSuccessfully(t, instanceID)
+ os.HandleCreate(t)
opts := os.BatchCreateOpts{
os.CreateOpts{
@@ -43,8 +42,7 @@
func TestUserList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleListUsersSuccessfully(t, instanceID)
+ os.HandleList(t)
expectedUsers := []os.User{
os.User{
@@ -77,17 +75,13 @@
})
th.AssertNoErr(t, err)
-
- if pages != 1 {
- t.Errorf("Expected 1 page, saw %d", pages)
- }
+ th.AssertEquals(t, 1, pages)
}
-func TestDeleteInstance(t *testing.T) {
+func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- os.HandleDeleteUserSuccessfully(t, instanceID, "{userName}")
+ os.HandleDelete(t)
res := Delete(fake.ServiceClient(), instanceID, "{userName}")
th.AssertNoErr(t, res.Err)
diff --git a/rackspace/db/v1/users/fixtures.go b/rackspace/db/v1/users/fixtures.go
index 9fdae69..5388123 100644
--- a/rackspace/db/v1/users/fixtures.go
+++ b/rackspace/db/v1/users/fixtures.go
@@ -1,39 +1,8 @@
package users
-import (
- "fmt"
- "net/http"
- "testing"
-
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
-)
-
const singleDB = `{"databases": [{"name": "databaseE"}]}`
-func setupHandler(t *testing.T, url, method, requestBody, responseBody string, status int) {
- th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, method)
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- if requestBody != "" {
- th.TestJSONRequest(t, r, requestBody)
- }
-
- w.WriteHeader(status)
-
- if responseBody != "" {
- w.Header().Add("Content-Type", "application/json")
- fmt.Fprintf(w, responseBody)
- }
- })
-}
-
-func HandleChangePasswordSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestJSONRequest(t, r, `
+var changePwdReq = `
{
"users": [
{
@@ -46,74 +15,35 @@
}
]
}
-`)
+`
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleUpdateSuccessfully(t *testing.T, instanceID, userName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestJSONRequest(t, r, `
+var updateReq = `
{
- "user": {
- "name": "new_username",
- "password": "new_password"
- }
+ "user": {
+ "name": "new_username",
+ "password": "new_password"
+ }
}
-`)
+`
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleGetSuccessfully(t *testing.T, instanceID, userName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, `
+var getResp = `
{
- "user": {
- "name": "exampleuser",
- "host": "foo",
- "databases": [
- {
- "name": "databaseA"
- },
- {
- "name": "databaseB"
- }
- ]
- }
+ "user": {
+ "name": "exampleuser",
+ "host": "foo",
+ "databases": [
+ {
+ "name": "databaseA"
+ },
+ {
+ "name": "databaseB"
+ }
+ ]
+ }
}
-`)
- })
-}
+`
-func HandleListUserAccessSuccessfully(t *testing.T, instanceID, userName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+userName+"/databases", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
-
- fmt.Fprintf(w, singleDB)
- })
-}
-
-func HandleGrantUserAccessSuccessfully(t *testing.T, instanceID, userName string) {
- url := "/instances/" + instanceID + "/users/" + userName + "/databases"
- setupHandler(t, url, "PUT", singleDB, "", http.StatusAccepted)
-}
-
-func HandleRevokeUserAccessSuccessfully(t *testing.T, instanceID, userName, dbName string) {
- url := "/instances/" + instanceID + "/users/" + userName + "/databases/" + dbName
- setupHandler(t, url, "DELETE", "", "", http.StatusAccepted)
-}
+var (
+ listUserAccessResp = singleDB
+ grantUserAccessReq = singleDB
+)
diff --git a/rackspace/db/v1/users/requests_test.go b/rackspace/db/v1/users/requests_test.go
index 17de21d..f18492d 100644
--- a/rackspace/db/v1/users/requests_test.go
+++ b/rackspace/db/v1/users/requests_test.go
@@ -8,15 +8,20 @@
"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 userName = "{userName}"
+var (
+ userName = "{userName}"
+ _rootURL = "/instances/" + instanceID + "/users"
+ _userURL = _rootURL + "/" + userName
+ _dbURL = _userURL + "/databases"
+)
func TestChangeUserPassword(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleChangePasswordSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, _rootURL, "PUT", changePwdReq, "", 202)
opts := os.BatchCreateOpts{
os.CreateOpts{Name: "dbuser1", Password: "newpassword"},
@@ -24,15 +29,13 @@
}
err := ChangePassword(fake.ServiceClient(), instanceID, opts).ExtractErr()
-
th.AssertNoErr(t, err)
}
func TestUpdateUser(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleUpdateSuccessfully(t, instanceID, userName)
+ fixture.SetupHandler(t, _userURL, "PUT", updateReq, "", 202)
opts := os.CreateOpts{
Name: "new_username",
@@ -40,15 +43,13 @@
}
err := Update(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
-
th.AssertNoErr(t, err)
}
func TestGetUser(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleGetSuccessfully(t, instanceID, userName)
+ fixture.SetupHandler(t, _userURL, "GET", "", getResp, 200)
user, err := Get(fake.ServiceClient(), instanceID, userName).Extract()
@@ -69,8 +70,7 @@
func TestUserAccessList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleListUserAccessSuccessfully(t, instanceID, userName)
+ fixture.SetupHandler(t, _userURL+"/databases", "GET", "", listUserAccessResp, 200)
expectedDBs := []db.Database{
db.Database{Name: "databaseE"},
@@ -91,22 +91,15 @@
})
th.AssertNoErr(t, err)
-
- if pages != 1 {
- t.Errorf("Expected 1 page, saw %d", pages)
- }
+ th.AssertEquals(t, 1, pages)
}
func TestGrantAccess(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
+ fixture.SetupHandler(t, _dbURL, "PUT", grantUserAccessReq, "", 202)
- HandleGrantUserAccessSuccessfully(t, instanceID, userName)
-
- opts := db.BatchCreateOpts{
- db.CreateOpts{Name: "databaseE"},
- }
-
+ opts := db.BatchCreateOpts{db.CreateOpts{Name: "databaseE"}}
err := GrantAccess(fake.ServiceClient(), instanceID, userName, opts).ExtractErr()
th.AssertNoErr(t, err)
}
@@ -114,8 +107,7 @@
func TestRevokeAccess(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleRevokeUserAccessSuccessfully(t, instanceID, userName, "{dbName}")
+ fixture.SetupHandler(t, _dbURL+"/{dbName}", "DELETE", "", "", 202)
err := RevokeAccess(fake.ServiceClient(), instanceID, userName, "{dbName}").ExtractErr()
th.AssertNoErr(t, err)