blob: 1388e2300c28e721573b2b12f59d3078c9813086 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
4 "fmt"
Jamie Hannaford4a170282015-02-18 14:16:57 +01005 "testing"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01006
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +01007 "github.com/rackspace/gophercloud"
Jamie Hannaford52dbcee2015-10-06 16:09:56 +02008 "github.com/rackspace/gophercloud/openstack/db/v1/datastores"
Jamie Hannaford05d200d2015-02-20 14:49:05 +01009 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford4a170282015-02-18 14:16:57 +010010 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010011)
12
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010013const instance = `
14{
15 "created": "2014-02-13T21:47:13",
16 "datastore": {
17 "type": "mysql",
18 "version": "5.6"
19 },
20 "flavor": {
21 "id": "1",
22 "links": [
23 {
24 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
25 "rel": "self"
26 },
27 {
28 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
29 "rel": "bookmark"
30 }
31 ]
32 },
33 "links": [
34 {
35 "href": "https://my-openstack.com/v1.0/1234/instances/1",
36 "rel": "self"
37 }
38 ],
39 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
40 "id": "{instanceID}",
41 "name": "json_rack_instance",
42 "status": "BUILD",
43 "updated": "2014-02-13T21:47:13",
44 "volume": {
45 "size": 2
46 }
47}
48`
49
50var createReq = `
Jamie Hannaford821015f2015-02-10 12:58:36 +010051{
52 "instance": {
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010053 "databases": [
Jamie Hannaford821015f2015-02-10 12:58:36 +010054 {
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010055 "character_set": "utf8",
56 "collate": "utf8_general_ci",
57 "name": "sampledb"
58 },
59 {
60 "name": "nextround"
Jamie Hannaford821015f2015-02-10 12:58:36 +010061 }
62 ],
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010063 "flavorRef": "1",
Jamie Hannaford821015f2015-02-10 12:58:36 +010064 "name": "json_rack_instance",
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010065 "users": [
66 {
67 "databases": [
68 {
69 "name": "sampledb"
70 }
71 ],
72 "name": "demouser",
73 "password": "demopassword"
74 }
75 ],
Jamie Hannaford821015f2015-02-10 12:58:36 +010076 "volume": {
77 "size": 2
78 }
79 }
80}
81`
82
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010083var (
Jamie Hannaford4a170282015-02-18 14:16:57 +010084 instanceID = "{instanceID}"
85 rootURL = "/instances"
86 resURL = rootURL + "/" + instanceID
87 uRootURL = resURL + "/root"
88 aURL = resURL + "/action"
89)
90
91var (
Jamie Hannafordd2b755f2015-10-07 14:01:57 +020092 restartReq = `{"restart": {}}`
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010093 resizeReq = `{"resize": {"flavorRef": "2"}}`
94 resizeVolReq = `{"resize": {"volume": {"size": 4}}}`
95)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010096
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010097var (
98 createResp = fmt.Sprintf(`{"instance": %s}`, instance)
99 listInstancesResp = fmt.Sprintf(`{"instances":[%s]}`, instance)
100 getInstanceResp = createResp
101 enableUserResp = `{"user":{"name":"root","password":"secretsecret"}}`
102 isUserEnabledResp = `{"rootEnabled":true}`
103)
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100104
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100105var expectedInstance = Instance{
106 Created: "2014-02-13T21:47:13",
107 Updated: "2014-02-13T21:47:13",
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100108 Flavor: flavors.Flavor{
Jamie Hannaford11108402015-02-23 10:31:41 +0100109 ID: "1",
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100110 Links: []gophercloud.Link{
111 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
112 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
113 },
114 },
115 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
116 ID: instanceID,
117 Links: []gophercloud.Link{
118 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
119 },
120 Name: "json_rack_instance",
121 Status: "BUILD",
122 Volume: Volume{Size: 2},
Jamie Hannaford52dbcee2015-10-06 16:09:56 +0200123 Datastore: datastores.DatastorePartial{
124 Type: "mysql",
125 Version: "5.6",
126 },
Jamie Hannaford219ca592015-02-10 15:59:05 +0100127}
Jamie Hannaford4a170282015-02-18 14:16:57 +0100128
129func HandleCreate(t *testing.T) {
130 fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
131}
132
133func HandleList(t *testing.T) {
134 fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
135}
136
137func HandleGet(t *testing.T) {
138 fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
139}
140
141func HandleDelete(t *testing.T) {
142 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
143}
144
145func HandleEnableRoot(t *testing.T) {
146 fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
147}
148
149func HandleIsRootEnabled(t *testing.T) {
150 fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
151}
152
153func HandleRestart(t *testing.T) {
154 fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
155}
156
157func HandleResize(t *testing.T) {
158 fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
159}
160
161func HandleResizeVol(t *testing.T) {
162 fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
163}