Refactor fixtures
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)
}