Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 4a17028

Browse files
author
Jamie Hannaford
committed
Refactor OS fixtures
1 parent e635b7d commit 4a17028

File tree

8 files changed

+132
-66
lines changed

8 files changed

+132
-66
lines changed

openstack/db/v1/databases/fixtures.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package databases
22

3+
import (
4+
"testing"
5+
6+
"github.com/rackspace/gophercloud/testhelper/fixture"
7+
)
8+
9+
var (
10+
instanceID = "{instanceID}"
11+
resURL = "/instances/" + instanceID + "/databases"
12+
)
13+
314
var createDBsReq = `
415
{
516
"databases": [
@@ -36,3 +47,15 @@ var listDBsResp = `
3647
]
3748
}
3849
`
50+
51+
func HandleCreate(t *testing.T) {
52+
fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202)
53+
}
54+
55+
func HandleList(t *testing.T) {
56+
fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200)
57+
}
58+
59+
func HandleDelete(t *testing.T) {
60+
fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202)
61+
}

openstack/db/v1/databases/requests_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@ import (
66
"github.com/rackspace/gophercloud/pagination"
77
th "github.com/rackspace/gophercloud/testhelper"
88
fake "github.com/rackspace/gophercloud/testhelper/client"
9-
"github.com/rackspace/gophercloud/testhelper/fixture"
10-
)
11-
12-
const instanceID = "{instanceID}"
13-
14-
var (
15-
resURL = "/instances/" + instanceID + "/databases"
169
)
1710

1811
func TestCreate(t *testing.T) {
1912
th.SetupHTTP()
2013
defer th.TeardownHTTP()
21-
22-
fixture.SetupHandler(t, resURL, "POST", createDBsReq, "", 202)
14+
HandleCreate(t)
2315

2416
opts := BatchCreateOpts{
2517
CreateOpts{Name: "testingdb", CharSet: "utf8", Collate: "utf8_general_ci"},
@@ -33,8 +25,7 @@ func TestCreate(t *testing.T) {
3325
func TestList(t *testing.T) {
3426
th.SetupHTTP()
3527
defer th.TeardownHTTP()
36-
37-
fixture.SetupHandler(t, resURL, "GET", "", listDBsResp, 200)
28+
HandleList(t)
3829

3930
expectedDBs := []Database{
4031
Database{Name: "anotherexampledb"},
@@ -68,8 +59,7 @@ func TestList(t *testing.T) {
6859
func TestDelete(t *testing.T) {
6960
th.SetupHTTP()
7061
defer th.TeardownHTTP()
71-
72-
fixture.SetupHandler(t, resURL+"/{dbName}", "DELETE", "", "", 202)
62+
HandleDelete(t)
7363

7464
err := Delete(fake.ServiceClient(), instanceID, "{dbName}").ExtractErr()
7565
th.AssertNoErr(t, err)

openstack/db/v1/flavors/fixtures.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package flavors
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/rackspace/gophercloud/testhelper/fixture"
8+
)
49

510
const flavor = `
611
{
@@ -20,6 +25,12 @@ const flavor = `
2025
}
2126
`
2227

28+
var (
29+
flavorID = "{flavorID}"
30+
_baseURL = "/flavors"
31+
resURL = "/flavors/" + flavorID
32+
)
33+
2334
var (
2435
flavor1 = fmt.Sprintf(flavor, 1, 1, 1, "m1.tiny", 512)
2536
flavor2 = fmt.Sprintf(flavor, 2, 2, 2, "m1.small", 1024)
@@ -29,3 +40,11 @@ var (
2940
listFlavorsResp = fmt.Sprintf(`{"flavors":[%s, %s, %s, %s]}`, flavor1, flavor2, flavor3, flavor4)
3041
getFlavorResp = fmt.Sprintf(`{"flavor": %s}`, flavor1)
3142
)
43+
44+
func HandleList(t *testing.T) {
45+
fixture.SetupHandler(t, _baseURL, "GET", "", listFlavorsResp, 200)
46+
}
47+
48+
func HandleGet(t *testing.T) {
49+
fixture.SetupHandler(t, resURL, "GET", "", getFlavorResp, 200)
50+
}

openstack/db/v1/flavors/requests_test.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ import (
77
"github.com/rackspace/gophercloud/pagination"
88
th "github.com/rackspace/gophercloud/testhelper"
99
fake "github.com/rackspace/gophercloud/testhelper/client"
10-
"github.com/rackspace/gophercloud/testhelper/fixture"
11-
)
12-
13-
var (
14-
flavorID = "{flavorID}"
15-
_baseURL = "/flavors"
16-
resURL = "/flavors/" + flavorID
1710
)
1811

1912
func TestListFlavors(t *testing.T) {
2013
th.SetupHTTP()
2114
defer th.TeardownHTTP()
22-
23-
fixture.SetupHandler(t, _baseURL, "GET", "", listFlavorsResp, 200)
15+
HandleList(t)
2416

2517
pages := 0
2618
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -71,21 +63,17 @@ func TestListFlavors(t *testing.T) {
7163
}
7264

7365
th.AssertDeepEquals(t, expected, actual)
74-
7566
return true, nil
7667
})
7768

7869
th.AssertNoErr(t, err)
79-
if pages != 1 {
80-
t.Errorf("Expected one page, got %d", pages)
81-
}
70+
th.AssertEquals(t, 1, pages)
8271
}
8372

8473
func TestGetFlavor(t *testing.T) {
8574
th.SetupHTTP()
8675
defer th.TeardownHTTP()
87-
88-
fixture.SetupHandler(t, resURL, "GET", "", getFlavorResp, 200)
76+
HandleGet(t)
8977

9078
actual, err := Get(fake.ServiceClient(), flavorID).Extract()
9179
th.AssertNoErr(t, err)

openstack/db/v1/instances/fixtures.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package instances
22

33
import (
44
"fmt"
5+
"testing"
56

67
"github.com/rackspace/gophercloud"
8+
"github.com/rackspace/gophercloud/testhelper/fixture"
79
)
810

911
const instance = `
@@ -76,6 +78,14 @@ var createReq = `
7678
}
7779
`
7880

81+
var (
82+
instanceID = "{instanceID}"
83+
rootURL = "/instances"
84+
resURL = rootURL + "/" + instanceID
85+
uRootURL = resURL + "/root"
86+
aURL = resURL + "/action"
87+
)
88+
7989
var (
8090
restartReq = `{"restart": true}`
8191
resizeReq = `{"resize": {"flavorRef": "2"}}`
@@ -109,3 +119,39 @@ var expectedInstance = Instance{
109119
Status: "BUILD",
110120
Volume: Volume{Size: 2},
111121
}
122+
123+
func HandleCreate(t *testing.T) {
124+
fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
125+
}
126+
127+
func HandleList(t *testing.T) {
128+
fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
129+
}
130+
131+
func HandleGet(t *testing.T) {
132+
fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
133+
}
134+
135+
func HandleDelete(t *testing.T) {
136+
fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
137+
}
138+
139+
func HandleEnableRoot(t *testing.T) {
140+
fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
141+
}
142+
143+
func HandleIsRootEnabled(t *testing.T) {
144+
fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
145+
}
146+
147+
func HandleRestart(t *testing.T) {
148+
fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
149+
}
150+
151+
func HandleResize(t *testing.T) {
152+
fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
153+
}
154+
155+
func HandleResizeVol(t *testing.T) {
156+
fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
157+
}

openstack/db/v1/instances/requests_test.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,12 @@ import (
88
"github.com/rackspace/gophercloud/pagination"
99
th "github.com/rackspace/gophercloud/testhelper"
1010
fake "github.com/rackspace/gophercloud/testhelper/client"
11-
"github.com/rackspace/gophercloud/testhelper/fixture"
12-
)
13-
14-
var (
15-
instanceID = "{instanceID}"
16-
rootURL = "/instances"
17-
resURL = rootURL + "/" + instanceID
18-
uRootURL = resURL + "/root"
19-
aURL = resURL + "/action"
2011
)
2112

2213
func TestCreate(t *testing.T) {
2314
th.SetupHTTP()
2415
defer th.TeardownHTTP()
25-
fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
16+
HandleCreate(t)
2617

2718
opts := CreateOpts{
2819
Name: "json_rack_instance",
@@ -52,7 +43,7 @@ func TestCreate(t *testing.T) {
5243
func TestInstanceList(t *testing.T) {
5344
th.SetupHTTP()
5445
defer th.TeardownHTTP()
55-
fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
46+
HandleList(t)
5647

5748
pages := 0
5849
err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
@@ -64,7 +55,6 @@ func TestInstanceList(t *testing.T) {
6455
}
6556

6657
th.CheckDeepEquals(t, []Instance{expectedInstance}, actual)
67-
6858
return true, nil
6959
})
7060

@@ -75,7 +65,7 @@ func TestInstanceList(t *testing.T) {
7565
func TestGetInstance(t *testing.T) {
7666
th.SetupHTTP()
7767
defer th.TeardownHTTP()
78-
fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
68+
HandleGet(t)
7969

8070
instance, err := Get(fake.ServiceClient(), instanceID).Extract()
8171

@@ -86,7 +76,7 @@ func TestGetInstance(t *testing.T) {
8676
func TestDeleteInstance(t *testing.T) {
8777
th.SetupHTTP()
8878
defer th.TeardownHTTP()
89-
fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
79+
HandleDelete(t)
9080

9181
res := Delete(fake.ServiceClient(), instanceID)
9282
th.AssertNoErr(t, res.Err)
@@ -95,7 +85,7 @@ func TestDeleteInstance(t *testing.T) {
9585
func TestEnableRootUser(t *testing.T) {
9686
th.SetupHTTP()
9787
defer th.TeardownHTTP()
98-
fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
88+
HandleEnableRoot(t)
9989

10090
expected := &users.User{Name: "root", Password: "secretsecret"}
10191
user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract()
@@ -107,7 +97,7 @@ func TestEnableRootUser(t *testing.T) {
10797
func TestIsRootEnabled(t *testing.T) {
10898
th.SetupHTTP()
10999
defer th.TeardownHTTP()
110-
fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
100+
HandleIsRootEnabled(t)
111101

112102
isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID)
113103

@@ -118,7 +108,7 @@ func TestIsRootEnabled(t *testing.T) {
118108
func TestRestartService(t *testing.T) {
119109
th.SetupHTTP()
120110
defer th.TeardownHTTP()
121-
fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
111+
HandleRestart(t)
122112

123113
res := RestartService(fake.ServiceClient(), instanceID)
124114
th.AssertNoErr(t, res.Err)
@@ -127,7 +117,7 @@ func TestRestartService(t *testing.T) {
127117
func TestResizeInstance(t *testing.T) {
128118
th.SetupHTTP()
129119
defer th.TeardownHTTP()
130-
fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
120+
HandleResize(t)
131121

132122
res := ResizeInstance(fake.ServiceClient(), instanceID, "2")
133123
th.AssertNoErr(t, res.Err)
@@ -136,7 +126,7 @@ func TestResizeInstance(t *testing.T) {
136126
func TestResizeVolume(t *testing.T) {
137127
th.SetupHTTP()
138128
defer th.TeardownHTTP()
139-
fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
129+
HandleResizeVol(t)
140130

141131
res := ResizeVolume(fake.ServiceClient(), instanceID, 4)
142132
th.AssertNoErr(t, res.Err)

openstack/db/v1/users/fixtures.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package users
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/rackspace/gophercloud/testhelper/fixture"
8+
)
49

510
const user1 = `
611
{"databases": [{"name": "databaseA"}],"name": "dbuser3"%s}
@@ -11,11 +16,22 @@ const user2 = `
1116
`
1217

1318
var (
14-
pUser1 = fmt.Sprintf(user1, `,"password":"secretsecret"`)
15-
pUser2 = fmt.Sprintf(user2, `,"password":"secretsecret"`)
19+
instanceID = "{instanceID}"
20+
_rootURL = "/instances/" + instanceID + "/users"
21+
pUser1 = fmt.Sprintf(user1, `,"password":"secretsecret"`)
22+
pUser2 = fmt.Sprintf(user2, `,"password":"secretsecret"`)
23+
createReq = fmt.Sprintf(`{"users":[%s, %s]}`, pUser1, pUser2)
24+
listResp = fmt.Sprintf(`{"users":[%s, %s]}`, fmt.Sprintf(user1, ""), fmt.Sprintf(user2, ""))
1625
)
1726

18-
var (
19-
createReq = fmt.Sprintf(`{"users":[%s, %s]}`, pUser1, pUser2)
20-
listResp = fmt.Sprintf(`{"users":[%s, %s]}`, fmt.Sprintf(user1, ""), fmt.Sprintf(user2, ""))
21-
)
27+
func HandleCreate(t *testing.T) {
28+
fixture.SetupHandler(t, _rootURL, "POST", createReq, "", 202)
29+
}
30+
31+
func HandleList(t *testing.T) {
32+
fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
33+
}
34+
35+
func HandleDelete(t *testing.T) {
36+
fixture.SetupHandler(t, _rootURL+"/{userName}", "DELETE", "", "", 202)
37+
}

0 commit comments

Comments
 (0)