blob: 184f0f61887d953143641a92395c303a511bf7cd [file] [log] [blame]
Jamie Hannaford05d200d2015-02-20 14:49:05 +01001// +build acceptance db
2
3package v1
4
5import (
Jamie Hannaford99eced52015-03-02 15:24:22 +01006 "os"
Jamie Hannaford11108402015-02-23 10:31:41 +01007 "testing"
8
Jamie Hannaford05d200d2015-02-20 14:49:05 +01009 "github.com/rackspace/gophercloud/acceptance/tools"
10 "github.com/rackspace/gophercloud/openstack/db/v1/instances"
11 "github.com/rackspace/gophercloud/pagination"
12 th "github.com/rackspace/gophercloud/testhelper"
13)
14
Jamie Hannaford99eced52015-03-02 15:24:22 +010015const envDSType = "DATASTORE_TYPE_ID"
16
Jamie Hannaford11108402015-02-23 10:31:41 +010017func TestRunner(t *testing.T) {
Jamie Hannaford05d200d2015-02-20 14:49:05 +010018 c := newContext(t)
19
20 // FLAVOR tests
21 c.listFlavors()
22 c.getFlavor()
23
24 // INSTANCE tests
Jamie Hannaford99eced52015-03-02 15:24:22 +010025 //c.createInstance()
26 c.instanceID = "dbf901f4-fe23-48b7-8c1d-ee60ec85a660"
27
Jamie Hannaford05d200d2015-02-20 14:49:05 +010028 c.listInstances()
29 c.getInstance()
30 c.isRootEnabled()
31 c.enableRootUser()
32 c.isRootEnabled()
33 c.restartInstance()
34 c.resizeInstance()
35 c.resizeVol()
36
37 // DATABASE tests
Jamie Hannaford11108402015-02-23 10:31:41 +010038 c.createDBs()
Jamie Hannaford05d200d2015-02-20 14:49:05 +010039 c.listDBs()
40
41 // USER tests
42 c.createUsers()
43 c.listUsers()
44
45 // TEARDOWN
46 c.deleteUsers()
47 c.deleteDBs()
Jamie Hannaford11108402015-02-23 10:31:41 +010048 c.deleteInstance()
Jamie Hannaford05d200d2015-02-20 14:49:05 +010049}
50
51func (c context) createInstance() {
Jamie Hannaford99eced52015-03-02 15:24:22 +010052 if os.Getenv(envDSType) == "" {
53 c.test.Fatalf("%s must be set as an environment var", envDSType)
54 }
55
56 opts := instances.CreateOpts{
Jamie Hannaford05d200d2015-02-20 14:49:05 +010057 FlavorRef: "1",
58 Size: 1,
59 Name: tools.RandomString("gopher_db", 5),
Jamie Hannaford99eced52015-03-02 15:24:22 +010060 Datastore: &instances.DatastoreOpts{Type: os.Getenv(envDSType)},
Jamie Hannaford05d200d2015-02-20 14:49:05 +010061 }
62
63 instance, err := instances.Create(c.client, opts).Extract()
64 th.AssertNoErr(c.test, err)
65
Jamie Hannaford11108402015-02-23 10:31:41 +010066 c.Logf("Restarting %s. Waiting...", instance.ID)
67 c.WaitUntilActive(instance.ID)
Jamie Hannaford99eced52015-03-02 15:24:22 +010068 c.Logf("Created Instance %s", instance.ID)
Jamie Hannaford05d200d2015-02-20 14:49:05 +010069
70 c.instanceID = instance.ID
71}
72
73func (c context) listInstances() {
74 c.Logf("Listing instances")
75
76 err := instances.List(c.client).EachPage(func(page pagination.Page) (bool, error) {
77 instanceList, err := instances.ExtractInstances(page)
78 c.AssertNoErr(err)
79
Jamie Hannaford11108402015-02-23 10:31:41 +010080 for _, i := range instanceList {
Jamie Hannaford99eced52015-03-02 15:24:22 +010081 c.Logf("Instance: ID [%s] Name [%s] Status [%s] VolSize [%d] Datastore Type [%s]",
82 i.ID, i.Name, i.Status, i.Volume.Size, i.Datastore.Type)
Jamie Hannaford05d200d2015-02-20 14:49:05 +010083 }
84
85 return true, nil
86 })
87
Jamie Hannaford11108402015-02-23 10:31:41 +010088 c.AssertNoErr(err)
Jamie Hannaford05d200d2015-02-20 14:49:05 +010089}
90
91func (c context) getInstance() {
92 instance, err := instances.Get(c.client, c.instanceID).Extract()
93 c.AssertNoErr(err)
Jamie Hannaford99eced52015-03-02 15:24:22 +010094 c.Logf("Getting instance: %s", instance.ID)
Jamie Hannaford05d200d2015-02-20 14:49:05 +010095}
96
97func (c context) deleteInstance() {
98 err := instances.Delete(c.client, c.instanceID).ExtractErr()
99 c.AssertNoErr(err)
100 c.Logf("Deleted instance %s", c.instanceID)
101}
102
103func (c context) enableRootUser() {
Jamie Hannaford11108402015-02-23 10:31:41 +0100104 _, err := instances.EnableRootUser(c.client, c.instanceID).Extract()
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100105 c.AssertNoErr(err)
106 c.Logf("Enabled root user on %s", c.instanceID)
107}
108
109func (c context) isRootEnabled() {
110 enabled, err := instances.IsRootEnabled(c.client, c.instanceID)
111 c.AssertNoErr(err)
Jamie Hannaford99eced52015-03-02 15:24:22 +0100112 c.Logf("Is root enabled? %d", enabled)
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100113}
114
115func (c context) restartInstance() {
116 id := c.instanceID
Jamie Hannaford11108402015-02-23 10:31:41 +0100117 err := instances.RestartService(c.client, id).ExtractErr()
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100118 c.AssertNoErr(err)
119 c.Logf("Restarting %s. Waiting...", id)
120 c.WaitUntilActive(id)
121 c.Logf("Restarted %s", id)
122}
123
124func (c context) resizeInstance() {
125 id := c.instanceID
Jamie Hannaford11108402015-02-23 10:31:41 +0100126 err := instances.ResizeInstance(c.client, id, "2").ExtractErr()
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100127 c.AssertNoErr(err)
128 c.Logf("Resizing %s. Waiting...", id)
129 c.WaitUntilActive(id)
130 c.Logf("Resized %s with flavorRef %s", id, "2")
131}
132
133func (c context) resizeVol() {
134 id := c.instanceID
Jamie Hannaford11108402015-02-23 10:31:41 +0100135 err := instances.ResizeVolume(c.client, id, 2).ExtractErr()
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100136 c.AssertNoErr(err)
137 c.Logf("Resizing volume of %s. Waiting...", id)
138 c.WaitUntilActive(id)
139 c.Logf("Resized the volume of %s to %d GB", id, 2)
140}