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

Commit 05d200d

Browse files
author
Jamie Hannaford
committed
First crack at acceptance tests
1 parent b0d267b commit 05d200d

File tree

20 files changed

+985
-5
lines changed

20 files changed

+985
-5
lines changed

acceptance/openstack/db/v1/common.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// +build acceptance db
2+
3+
package v1
4+
5+
import (
6+
"os"
7+
"testing"
8+
9+
"github.com/rackspace/gophercloud"
10+
"github.com/rackspace/gophercloud/openstack"
11+
th "github.com/rackspace/gophercloud/testhelper"
12+
)
13+
14+
func newClient(t *testing.T) *gophercloud.ServiceClient {
15+
ao, err := openstack.AuthOptionsFromEnv()
16+
th.AssertNoErr(t, err)
17+
18+
client, err := openstack.AuthenticatedClient(ao)
19+
th.AssertNoErr(t, err)
20+
21+
c, err := openstack.NewDBV1(client, gophercloud.EndpointOpts{
22+
Region: os.Getenv("OS_REGION_NAME"),
23+
})
24+
th.AssertNoErr(t, err)
25+
26+
return c
27+
}
28+
29+
type context struct {
30+
test *testing.T
31+
client *gophercloud.ServiceClient
32+
instanceID string
33+
DBIDs []string
34+
}
35+
36+
func newContext(t *testing.T) context {
37+
return context{
38+
test: t,
39+
client: newClient(t),
40+
}
41+
}
42+
43+
func (c context) Logf(msg string, args ...interface{}) {
44+
c.test.Logf(msg, args)
45+
}
46+
47+
func (c context) AssertNoErr(err error) {
48+
th.AssertNoErr(c.test, err)
49+
}
50+
51+
func (c context) WaitUntilActive(id string) {
52+
err := gophercloud.WaitFor(60, func() (bool, error) {
53+
inst, err := instances.Get(c.client, id).Extract()
54+
if err != nil {
55+
return false, err
56+
}
57+
if inst.Status == "ACTIVE" {
58+
return true, nil
59+
}
60+
return false, nil
61+
})
62+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// +build acceptance db
2+
3+
package v1
4+
5+
import (
6+
"github.com/rackspace/gophercloud/acceptance/tools"
7+
db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
8+
"github.com/rackspace/gophercloud/pagination"
9+
)
10+
11+
func (c context) createDBs() {
12+
dbs := []string{
13+
tools.RandomString("db_"),
14+
tools.RandomString("db_"),
15+
tools.RandomString("db_"),
16+
}
17+
18+
opts := db.BatchCreateOpts{
19+
db.CreateOpts{Name: dbs[0]},
20+
db.CreateOpts{Name: dbs[1]},
21+
db.CreateOpts{Name: dbs[2]},
22+
}
23+
24+
res := db.Create(c.client, c.instanceID, opts)
25+
c.Logf("Created three databases on instance %s: %s, %s, %s", c.instanceID, dbs[0], dbs[1], dbs[2])
26+
c.DBIDs = dbs
27+
}
28+
29+
func (c context) listDBs() {
30+
c.Logf("Listing databases on instance %s", c.instanceID)
31+
32+
err := dbs.List(c.client, c.instanceID).EachPage(func(page pagination.Page) (bool, error) {
33+
dbList, err := db.ExtractDBs(page)
34+
c.AssertNoErr(err)
35+
36+
for _, db := range dbList {
37+
c.Logf("DB: %#v", db)
38+
}
39+
40+
return true, nil
41+
})
42+
43+
c.CheckNoErr(err)
44+
}
45+
46+
func (c context) deleteDBs() {
47+
for _, id := range c.DBIDs {
48+
err := db.Delete(c.client, c.instanceID, id).ExtractErr()
49+
c.CheckNoErr(err)
50+
t.Logf("Deleted DB %s", id)
51+
}
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// +build acceptance db
2+
3+
package v1
4+
5+
import (
6+
"github.com/rackspace/gophercloud/openstack/db/v1/flavors"
7+
"github.com/rackspace/gophercloud/pagination"
8+
)
9+
10+
func (c context) listFlavors() {
11+
c.Logf("Listing flavors")
12+
13+
err := flavors.List(c.client, c.instanceID).EachPage(func(page pagination.Page) (bool, error) {
14+
flavorList, err := db.ExtractFlavors(page)
15+
c.AssertNoErr(err)
16+
17+
for _, f := range flavorList {
18+
c.Logf("Flavor: %#v", f)
19+
}
20+
21+
return true, nil
22+
})
23+
24+
c.CheckNoErr(err)
25+
}
26+
27+
func (c context) getFlavor() {
28+
flavor, err := flavors.Get(c.client, "1").Extract()
29+
c.Logf("Getting flavor %s", flavor.ID)
30+
c.CheckNoErr(err)
31+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// +build acceptance db
2+
3+
package v1
4+
5+
import (
6+
"github.com/rackspace/gophercloud/acceptance/tools"
7+
"github.com/rackspace/gophercloud/openstack/db/v1/instances"
8+
"github.com/rackspace/gophercloud/pagination"
9+
th "github.com/rackspace/gophercloud/testhelper"
10+
)
11+
12+
func TestRunner(t *testingT) {
13+
c := newContext(t)
14+
15+
// FLAVOR tests
16+
c.listFlavors()
17+
c.getFlavor()
18+
19+
// INSTANCE tests
20+
c.createInstance()
21+
c.listInstances()
22+
c.getInstance()
23+
c.isRootEnabled()
24+
c.enableRootUser()
25+
c.isRootEnabled()
26+
c.restartInstance()
27+
c.resizeInstance()
28+
c.resizeVol()
29+
30+
// DATABASE tests
31+
c.createDB()
32+
c.listDBs()
33+
34+
// USER tests
35+
c.createUsers()
36+
c.listUsers()
37+
38+
// TEARDOWN
39+
c.deleteUsers()
40+
c.deleteDBs()
41+
c.deleteInstance(id)
42+
}
43+
44+
func (c context) createInstance() {
45+
opts := instances.CreateOpts{
46+
FlavorRef: "1",
47+
Size: 1,
48+
Name: tools.RandomString("gopher_db", 5),
49+
}
50+
51+
instance, err := instances.Create(c.client, opts).Extract()
52+
th.AssertNoErr(c.test, err)
53+
54+
c.Logf("Restarting %s. Waiting...", id)
55+
c.WaitUntilActive(id)
56+
c.Logf("Created DB %#v", instance)
57+
58+
c.instanceID = instance.ID
59+
}
60+
61+
func (c context) listInstances() {
62+
c.Logf("Listing instances")
63+
64+
err := instances.List(c.client).EachPage(func(page pagination.Page) (bool, error) {
65+
instanceList, err := instances.ExtractInstances(page)
66+
c.AssertNoErr(err)
67+
68+
for _, n := range networkList {
69+
c.Logf("Instance: %#v", instance)
70+
}
71+
72+
return true, nil
73+
})
74+
75+
c.CheckNoErr(err)
76+
}
77+
78+
func (c context) getInstance() {
79+
instance, err := instances.Get(c.client, c.instanceID).Extract()
80+
c.AssertNoErr(err)
81+
c.Logf("Getting instance: %#v", instance)
82+
}
83+
84+
func (c context) deleteInstance() {
85+
err := instances.Delete(c.client, c.instanceID).ExtractErr()
86+
c.AssertNoErr(err)
87+
c.Logf("Deleted instance %s", c.instanceID)
88+
}
89+
90+
func (c context) enableRootUser() {
91+
err := instances.EnableRootUser(c.client, c.instanceID).ExtractErr()
92+
c.AssertNoErr(err)
93+
c.Logf("Enabled root user on %s", c.instanceID)
94+
}
95+
96+
func (c context) isRootEnabled() {
97+
enabled, err := instances.IsRootEnabled(c.client, c.instanceID)
98+
c.AssertNoErr(err)
99+
c.Logf("Is root enabled? %s", enabled)
100+
}
101+
102+
func (c context) restartInstance() {
103+
id := c.instanceID
104+
err := instances.Restart(c.client, id).ExtractErr()
105+
c.AssertNoErr(err)
106+
c.Logf("Restarting %s. Waiting...", id)
107+
c.WaitUntilActive(id)
108+
c.Logf("Restarted %s", id)
109+
}
110+
111+
func (c context) resizeInstance() {
112+
id := c.instanceID
113+
err := instances.Resize(c.client, id, "2").ExtractErr()
114+
c.AssertNoErr(err)
115+
c.Logf("Resizing %s. Waiting...", id)
116+
c.WaitUntilActive(id)
117+
c.Logf("Resized %s with flavorRef %s", id, "2")
118+
}
119+
120+
func (c context) resizeVol() {
121+
id := c.instanceID
122+
err := instances.ResizeVol(c.client, id, 2).ExtractErr()
123+
c.AssertNoErr(err)
124+
c.Logf("Resizing volume of %s. Waiting...", id)
125+
c.WaitUntilActive(id)
126+
c.Logf("Resized the volume of %s to %d GB", id, 2)
127+
}

acceptance/openstack/db/v1/pkg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package v1
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// +build acceptance db
2+
3+
package v1
4+
5+
import (
6+
"github.com/rackspace/gophercloud/acceptance/tools"
7+
db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
8+
u "github.com/rackspace/gophercloud/openstack/db/v1/users"
9+
"github.com/rackspace/gophercloud/pagination"
10+
)
11+
12+
func (c context) createUsers() {
13+
users := []string{
14+
tools.RandomString("user_"),
15+
tools.RandomString("user_"),
16+
tools.RandomString("user_"),
17+
}
18+
19+
db1 := db.CreateOpt{Name: c.DBIDs[0]}
20+
db2 := db.CreateOpt{Name: c.DBIDs[1]}
21+
db3 := db.CreateOpt{Name: c.DBIDs[2]}
22+
23+
opts := u.BatchCreateOpts{
24+
u.CreateOpts{
25+
Name: users[0],
26+
Password: tools.RandomString(),
27+
databases: db.BatchCreateOpts{db1, db2, db3},
28+
},
29+
u.CreateOpts{
30+
Name: users[1],
31+
Password: tools.RandomString(),
32+
databases: db.BatchCreateOpts{db1, db2},
33+
},
34+
u.CreateOpts{
35+
Name: users[2],
36+
Password: tools.RandomString(),
37+
databases: db.BatchCreateOpts{db3},
38+
},
39+
}
40+
41+
err := u.Create(c.client, c.instanceID, opts).ExtractErr()
42+
c.Logf("Created three users on instance %s: %s, %s, %s", c.instanceID, users[0], users[1], users[2])
43+
c.users = users
44+
}
45+
46+
func (c context) listUsers() {
47+
c.Logf("Listing databases on instance %s", c.instanceID)
48+
49+
err := dbs.List(c.client, c.instanceID).EachPage(func(page pagination.Page) (bool, error) {
50+
dbList, err := db.ExtractDBs(page)
51+
c.AssertNoErr(err)
52+
53+
for _, db := range dbList {
54+
c.Logf("DB: %#v", db)
55+
}
56+
57+
return true, nil
58+
})
59+
60+
c.CheckNoErr(err)
61+
}
62+
63+
func (c context) deleteUsers() {
64+
for _, id := range c.DBIDs {
65+
err := db.Delete(c.client, c.instanceID, id).ExtractErr()
66+
c.CheckNoErr(err)
67+
t.Logf("Deleted DB %s", id)
68+
}
69+
}

0 commit comments

Comments
 (0)