Refactor to use new handler setup
diff --git a/openstack/db/v1/databases/fixtures.go b/openstack/db/v1/databases/fixtures.go
index d03466f..4d653ca 100644
--- a/openstack/db/v1/databases/fixtures.go
+++ b/openstack/db/v1/databases/fixtures.go
@@ -1,47 +1,21 @@
package databases
-import (
- "fmt"
- "net/http"
- "testing"
-
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
-)
-
-func HandleCreateDBSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/databases", 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 createDBsReq = `
{
- "databases": [
- {
- "character_set": "utf8",
- "collate": "utf8_general_ci",
- "name": "testingdb"
- },
- {
- "name": "sampledb"
- }
- ]
+ "databases": [
+ {
+ "character_set": "utf8",
+ "collate": "utf8_general_ci",
+ "name": "testingdb"
+ },
+ {
+ "name": "sampledb"
+ }
+ ]
}
-`)
+`
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleListDBsSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/databases", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- fmt.Fprintf(w, `
+var listDBsResp = `
{
"databases": [
{
@@ -61,16 +35,4 @@
}
]
}
-`)
- })
-}
-
-func HandleDeleteDBSuccessfully(t *testing.T, instanceID, dbName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/databases/"+dbName, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
+`
diff --git a/openstack/db/v1/databases/requests_test.go b/openstack/db/v1/databases/requests_test.go
index 6f5edb6..11bf9c8 100644
--- a/openstack/db/v1/databases/requests_test.go
+++ b/openstack/db/v1/databases/requests_test.go
@@ -6,15 +6,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 instanceID = "{instanceID}"
+var (
+ resURL = "/instances/" + instanceID + "/databases"
+)
+
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleCreateDBSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202)
opts := BatchCreateOpts{
CreateOpts{Name: "testingdb", CharSet: "utf8", Collate: "utf8_general_ci"},
@@ -29,7 +34,7 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleListDBsSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200)
expectedDBs := []Database{
Database{Name: "anotherexampledb"},
@@ -64,7 +69,7 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleDeleteDBSuccessfully(t, instanceID, "{dbName}")
+ fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202)
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 653b5c8..42e2dca 100644
--- a/openstack/db/v1/flavors/fixtures.go
+++ b/openstack/db/v1/flavors/fixtures.go
@@ -1,113 +1,31 @@
package flavors
-import (
- "fmt"
- "net/http"
- "testing"
+import "fmt"
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
+const flavor = `
+{
+ "id": %d,
+ "links": [
+ {
+ "href": "https://openstack.example.com/v1.0/1234/flavors/%d",
+ "rel": "self"
+ },
+ {
+ "href": "https://openstack.example.com/flavors/%d",
+ "rel": "bookmark"
+ }
+ ],
+ "name": "%s",
+ "ram": %d
+}
+`
+
+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)
+ flavor4 = fmt.Sprintf(flavor, 4, 4, 4, "m1.large", 4096)
+
+ listFlavorsResp = fmt.Sprintf(`{"flavors":[%s, %s, %s, %s]}`, flavor1, flavor2, flavor3, flavor4)
+ getFlavorResp = fmt.Sprintf(`{"flavor": %s}`, flavor1)
)
-
-func HandleListFlavorsSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/flavors", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- fmt.Fprintf(w, `
-{
- "flavors": [
- {
- "id": 1,
- "links": [
- {
- "href": "https://openstack.example.com/v1.0/1234/flavors/1",
- "rel": "self"
- },
- {
- "href": "https://openstack.example.com/flavors/1",
- "rel": "bookmark"
- }
- ],
- "name": "m1.tiny",
- "ram": 512
- },
- {
- "id": 2,
- "links": [
- {
- "href": "https://openstack.example.com/v1.0/1234/flavors/2",
- "rel": "self"
- },
- {
- "href": "https://openstack.example.com/flavors/2",
- "rel": "bookmark"
- }
- ],
- "name": "m1.small",
- "ram": 1024
- },
- {
- "id": 3,
- "links": [
- {
- "href": "https://openstack.example.com/v1.0/1234/flavors/3",
- "rel": "self"
- },
- {
- "href": "https://openstack.example.com/flavors/3",
- "rel": "bookmark"
- }
- ],
- "name": "m1.medium",
- "ram": 2048
- },
- {
- "id": 4,
- "links": [
- {
- "href": "https://openstack.example.com/v1.0/1234/flavors/4",
- "rel": "self"
- },
- {
- "href": "https://openstack.example.com/flavors/4",
- "rel": "bookmark"
- }
- ],
- "name": "m1.large",
- "ram": 4096
- }
- ]
-}
-`)
- })
-}
-
-func HandleGetFlavorSuccessfully(t *testing.T, flavorID string) {
- th.Mux.HandleFunc("/flavors/"+flavorID, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- fmt.Fprintf(w, `
-{
- "flavor": {
- "id": 1,
- "links": [
- {
- "href": "https://openstack.example.com/v1.0/1234/flavors/1",
- "rel": "self"
- }
- ],
- "name": "m1.tiny",
- "ram": 512
- }
-}
-`)
- })
-}
diff --git a/openstack/db/v1/flavors/requests_test.go b/openstack/db/v1/flavors/requests_test.go
index 9ed9e59..62e5fec 100644
--- a/openstack/db/v1/flavors/requests_test.go
+++ b/openstack/db/v1/flavors/requests_test.go
@@ -7,13 +7,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"
+)
+
+var (
+ flavorID = "{flavorID}"
+ _baseURL = "/flavors"
+ resURL = "/flavors/" + flavorID
)
func TestListFlavors(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleListFlavorsSuccessfully(t)
+ fixture.SetupHandler(t, _baseURL, "GET", "", listFlavorsResp, 200)
pages := 0
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -78,9 +85,9 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleGetFlavorSuccessfully(t, "12345")
+ fixture.SetupHandler(t, resURL, "GET", "", getFlavorResp, 200)
- actual, err := Get(fake.ServiceClient(), "12345").Extract()
+ actual, err := Get(fake.ServiceClient(), flavorID).Extract()
th.AssertNoErr(t, err)
expected := &Flavor{
diff --git a/openstack/db/v1/instances/fixtures.go b/openstack/db/v1/instances/fixtures.go
index 0dda158..dedbfbb 100644
--- a/openstack/db/v1/instances/fixtures.go
+++ b/openstack/db/v1/instances/fixtures.go
@@ -2,45 +2,73 @@
import (
"fmt"
- "net/http"
- "testing"
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
+ "github.com/rackspace/gophercloud"
)
-const singleInstanceJson = `
+const instance = `
+{
+ "created": "2014-02-13T21:47:13",
+ "datastore": {
+ "type": "mysql",
+ "version": "5.6"
+ },
+ "flavor": {
+ "id": "1",
+ "links": [
+ {
+ "href": "https://my-openstack.com/v1.0/1234/flavors/1",
+ "rel": "self"
+ },
+ {
+ "href": "https://my-openstack.com/v1.0/1234/flavors/1",
+ "rel": "bookmark"
+ }
+ ]
+ },
+ "links": [
+ {
+ "href": "https://my-openstack.com/v1.0/1234/instances/1",
+ "rel": "self"
+ }
+ ],
+ "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
+ "id": "{instanceID}",
+ "name": "json_rack_instance",
+ "status": "BUILD",
+ "updated": "2014-02-13T21:47:13",
+ "volume": {
+ "size": 2
+ }
+}
+`
+
+var createReq = `
{
"instance": {
- "created": "2014-02-13T21:47:13",
- "datastore": {
- "type": "mysql",
- "version": "5.6"
- },
- "flavor": {
- "id": "1",
- "links": [
- {
- "href": "https://my-openstack.com/v1.0/1234/flavors/1",
- "rel": "self"
- },
- {
- "href": "https://my-openstack.com/v1.0/1234/flavors/1",
- "rel": "bookmark"
- }
- ]
- },
- "links": [
+ "databases": [
{
- "href": "https://my-openstack.com/v1.0/1234/instances/1",
- "rel": "self"
+ "character_set": "utf8",
+ "collate": "utf8_general_ci",
+ "name": "sampledb"
+ },
+ {
+ "name": "nextround"
}
],
- "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
- "id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
+ "flavorRef": "1",
"name": "json_rack_instance",
- "status": "BUILD",
- "updated": "2014-02-13T21:47:13",
+ "users": [
+ {
+ "databases": [
+ {
+ "name": "sampledb"
+ }
+ ],
+ "name": "demouser",
+ "password": "demopassword"
+ }
+ ],
"volume": {
"size": 2
}
@@ -48,154 +76,36 @@
}
`
-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)
+var (
+ restartReq = `{"restart": true}`
+ resizeReq = `{"resize": {"flavorRef": "2"}}`
+ resizeVolReq = `{"resize": {"volume": {"size": 4}}}`
+)
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
+var (
+ createResp = fmt.Sprintf(`{"instance": %s}`, instance)
+ listInstancesResp = fmt.Sprintf(`{"instances":[%s]}`, instance)
+ getInstanceResp = createResp
+ enableUserResp = `{"user":{"name":"root","password":"secretsecret"}}`
+ isUserEnabledResp = `{"rootEnabled":true}`
+)
- th.TestJSONRequest(t, r, `
-{
- "instance": {
- "databases": [
- {
- "character_set": "utf8",
- "collate": "utf8_general_ci",
- "name": "sampledb"
- },
- {
- "name": "nextround"
- }
- ],
- "flavorRef": "1",
- "name": "json_rack_instance",
- "users": [
- {
- "databases": [
- {
- "name": "sampledb"
- }
- ],
- "name": "demouser",
- "password": "demopassword"
- }
- ],
- "volume": {
- "size": 2
- }
- }
-}
-`)
-
- fmt.Fprintf(w, singleInstanceJson)
- })
-}
-
-func HandleListInstanceSuccessfully(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, `
-{
- "instances": [
- {
- "name": "xml_rack_instance",
- "status": "ACTIVE",
- "volume": {
- "size": 2
- },
- "flavor": {
- "id": "1",
- "links": [
- {
- "href": "https://openstack.example.com/v1.0/1234/flavors/1",
- "rel": "self"
- },
- {
- "href": "https://openstack.example.com/flavors/1",
- "rel": "bookmark"
- }
- ]
- },
- "id": "8fb081af-f237-44f5-80cc-b46be1840ca9",
- "links": [
- {
- "href": "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9",
- "rel": "self"
- }
- ]
- }
- ]
-}
-`)
- })
-}
-
-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 HandleDeleteInstanceSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleEnableRootUserSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id+"/root", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `{"user":{"name":"root","password":"secretsecret"}}`)
- })
-}
-
-func HandleIsRootEnabledSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id+"/root", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `{"rootEnabled":true}`)
- })
-}
-
-func HandleRestartSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id+"/action", 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, `{"restart": true}`)
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleResizeInstanceSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id+"/action", 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, `{"resize": {"flavorRef": "2"}}`)
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleResizeVolSuccessfully(t *testing.T, id string) {
- th.Mux.HandleFunc("/instances/"+id+"/action", 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, `{"resize": {"volume": {"size": 4}}}`)
- w.WriteHeader(http.StatusAccepted)
- })
+var expectedInstance = Instance{
+ Created: "2014-02-13T21:47:13",
+ Updated: "2014-02-13T21:47:13",
+ Flavor: Flavor{
+ ID: "1",
+ Links: []gophercloud.Link{
+ gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
+ gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
+ },
+ },
+ Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
+ ID: instanceID,
+ Links: []gophercloud.Link{
+ gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
+ },
+ Name: "json_rack_instance",
+ Status: "BUILD",
+ Volume: Volume{Size: 2},
}
diff --git a/openstack/db/v1/instances/requests_test.go b/openstack/db/v1/instances/requests_test.go
index bdda1f6..d44220d 100644
--- a/openstack/db/v1/instances/requests_test.go
+++ b/openstack/db/v1/instances/requests_test.go
@@ -3,41 +3,26 @@
import (
"testing"
- "github.com/rackspace/gophercloud"
db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
"github.com/rackspace/gophercloud/openstack/db/v1/users"
"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 = "d4603f69-ec7e-4e9b-803f-600b9205576f"
-
-var expectedInstance = &Instance{
- Created: "2014-02-13T21:47:13",
- Updated: "2014-02-13T21:47:13",
- Flavor: Flavor{
- ID: "1",
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
- gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
- },
- },
- Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
- ID: instanceID,
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
- },
- Name: "json_rack_instance",
- Status: "BUILD",
- Volume: Volume{Size: 2},
-}
+var (
+ instanceID = "{instanceID}"
+ rootURL = "/instances"
+ resURL = rootURL + "/" + instanceID
+ uRootURL = resURL + "/root"
+ aURL = resURL + "/action"
+)
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",
@@ -61,31 +46,13 @@
instance, err := Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
- th.AssertDeepEquals(t, expectedInstance, instance)
+ th.AssertDeepEquals(t, &expectedInstance, instance)
}
func TestInstanceList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleListInstanceSuccessfully(t)
-
- expectedInstance := Instance{
- Flavor: Flavor{
- ID: "1",
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
- gophercloud.Link{Href: "https://openstack.example.com/flavors/1", Rel: "bookmark"},
- },
- },
- ID: "8fb081af-f237-44f5-80cc-b46be1840ca9",
- Links: []gophercloud.Link{
- gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9", Rel: "self"},
- },
- Name: "xml_rack_instance",
- Status: "ACTIVE",
- Volume: Volume{Size: 2},
- }
+ fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
pages := 0
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -96,38 +63,30 @@
return false, err
}
- if len(actual) != 1 {
- t.Fatalf("Expected 1 DB instance, got %d", len(actual))
- }
- th.CheckDeepEquals(t, expectedInstance, actual[0])
+ th.CheckDeepEquals(t, []Instance{expectedInstance}, 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 TestGetInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleGetInstanceSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
instance, err := Get(fake.ServiceClient(), instanceID).Extract()
th.AssertNoErr(t, err)
- th.AssertDeepEquals(t, instance, expectedInstance)
+ th.AssertDeepEquals(t, &expectedInstance, instance)
}
func TestDeleteInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleDeleteInstanceSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
res := Delete(fake.ServiceClient(), instanceID)
th.AssertNoErr(t, res.Err)
@@ -136,12 +95,11 @@
func TestEnableRootUser(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleEnableRootUserSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
expected := &users.User{Name: "root", Password: "secretsecret"}
-
user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
+
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, expected, user)
}
@@ -149,8 +107,7 @@
func TestIsRootEnabled(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleIsRootEnabledSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID)
@@ -161,32 +118,26 @@
func TestRestartService(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleRestartSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
res := RestartService(fake.ServiceClient(), instanceID)
-
th.AssertNoErr(t, res.Err)
}
func TestResizeInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleResizeInstanceSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
-
th.AssertNoErr(t, res.Err)
}
func TestResizeVolume(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleResizeVolSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
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 6bada41..25b75f1 100644
--- a/openstack/db/v1/users/fixtures.go
+++ b/openstack/db/v1/users/fixtures.go
@@ -1,91 +1,21 @@
package users
-import (
- "fmt"
- "net/http"
- "testing"
+import "fmt"
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
+const user1 = `
+{"databases": [{"name": "databaseA"}],"name": "dbuser3"%s}
+`
+
+const user2 = `
+{"databases": [{"name": "databaseB"},{"name": "databaseC"}],"name": "dbuser4"%s}
+`
+
+var (
+ pUser1 = fmt.Sprintf(user1, `,"password":"secretsecret"`)
+ pUser2 = fmt.Sprintf(user2, `,"password":"secretsecret"`)
)
-func HandleCreateUserSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users", 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, `
-{
- "users": [
- {
- "databases": [
- {
- "name": "databaseA"
- }
- ],
- "name": "dbuser3",
- "password": "secretsecret"
- },
- {
- "databases": [
- {
- "name": "databaseB"
- },
- {
- "name": "databaseC"
- }
- ],
- "name": "dbuser4",
- "password": "secretsecret"
- }
- ]
-}
-`)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusAccepted)
- })
-}
-
-func HandleListUsersSuccessfully(t *testing.T, instanceID string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- fmt.Fprintf(w, `
-{
- "users": [
- {
- "databases": [
- {
- "name": "databaseA"
- }
- ],
- "name": "dbuser3"
- },
- {
- "databases": [
- {
- "name": "databaseB"
- },
- {
- "name": "databaseC"
- }
- ],
- "name": "dbuser4"
- }
- ]
-}
-`)
- })
-}
-
-func HandleDeleteUserSuccessfully(t *testing.T, instanceID, dbName string) {
- th.Mux.HandleFunc("/instances/"+instanceID+"/users/"+dbName, func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.WriteHeader(http.StatusAccepted)
- })
-}
+var (
+ createReq = fmt.Sprintf(`{"users":[%s, %s]}`, pUser1, pUser2)
+ listResp = fmt.Sprintf(`{"users":[%s, %s]}`, fmt.Sprintf(user1, ""), fmt.Sprintf(user2, ""))
+)
diff --git a/openstack/db/v1/users/requests_test.go b/openstack/db/v1/users/requests_test.go
index 00c4e58..47eccea 100644
--- a/openstack/db/v1/users/requests_test.go
+++ b/openstack/db/v1/users/requests_test.go
@@ -7,15 +7,18 @@
"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 (
+ instanceID = "{instanceID}"
+ _rootURL = "/instances/" + instanceID + "/users"
+)
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleCreateUserSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, _rootURL, "POST", createReq, "", 202)
opts := BatchCreateOpts{
CreateOpts{
@@ -42,8 +45,7 @@
func TestUserList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleListUsersSuccessfully(t, instanceID)
+ fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
expectedUsers := []User{
User{
@@ -71,22 +73,17 @@
}
th.CheckDeepEquals(t, expectedUsers, 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 TestDeleteInstance(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
-
- HandleDeleteUserSuccessfully(t, instanceID, "{dbName}")
+ fixture.SetupHandler(t, _rootURL+"/{dbName}", "DELETE", "", "", 202)
res := Delete(fake.ServiceClient(), instanceID, "{dbName}")
th.AssertNoErr(t, res.Err)