blob: 726501b44f4f927c7b0938c5fe923afbad888be6 [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 Hannaford05d200d2015-02-20 14:49:05 +01008 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford4a170282015-02-18 14:16:57 +01009 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010010)
11
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010012const instance = `
13{
14 "created": "2014-02-13T21:47:13",
15 "datastore": {
16 "type": "mysql",
17 "version": "5.6"
18 },
19 "flavor": {
20 "id": "1",
21 "links": [
22 {
23 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
24 "rel": "self"
25 },
26 {
27 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
28 "rel": "bookmark"
29 }
30 ]
31 },
32 "links": [
33 {
34 "href": "https://my-openstack.com/v1.0/1234/instances/1",
35 "rel": "self"
36 }
37 ],
38 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
39 "id": "{instanceID}",
40 "name": "json_rack_instance",
41 "status": "BUILD",
42 "updated": "2014-02-13T21:47:13",
43 "volume": {
44 "size": 2
45 }
46}
47`
48
49var createReq = `
Jamie Hannaford821015f2015-02-10 12:58:36 +010050{
51 "instance": {
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010052 "databases": [
Jamie Hannaford821015f2015-02-10 12:58:36 +010053 {
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010054 "character_set": "utf8",
55 "collate": "utf8_general_ci",
56 "name": "sampledb"
57 },
58 {
59 "name": "nextround"
Jamie Hannaford821015f2015-02-10 12:58:36 +010060 }
61 ],
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010062 "flavorRef": "1",
Jamie Hannaford821015f2015-02-10 12:58:36 +010063 "name": "json_rack_instance",
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010064 "users": [
65 {
66 "databases": [
67 {
68 "name": "sampledb"
69 }
70 ],
71 "name": "demouser",
72 "password": "demopassword"
73 }
74 ],
Jamie Hannaford821015f2015-02-10 12:58:36 +010075 "volume": {
76 "size": 2
77 }
78 }
79}
80`
81
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010082var (
Jamie Hannaford4a170282015-02-18 14:16:57 +010083 instanceID = "{instanceID}"
84 rootURL = "/instances"
85 resURL = rootURL + "/" + instanceID
86 uRootURL = resURL + "/root"
87 aURL = resURL + "/action"
88)
89
90var (
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010091 restartReq = `{"restart": true}`
92 resizeReq = `{"resize": {"flavorRef": "2"}}`
93 resizeVolReq = `{"resize": {"volume": {"size": 4}}}`
94)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010095
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010096var (
97 createResp = fmt.Sprintf(`{"instance": %s}`, instance)
98 listInstancesResp = fmt.Sprintf(`{"instances":[%s]}`, instance)
99 getInstanceResp = createResp
100 enableUserResp = `{"user":{"name":"root","password":"secretsecret"}}`
101 isUserEnabledResp = `{"rootEnabled":true}`
102)
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100103
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100104var expectedInstance = Instance{
105 Created: "2014-02-13T21:47:13",
106 Updated: "2014-02-13T21:47:13",
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100107 Flavor: flavors.Flavor{
108 ID: 1,
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100109 Links: []gophercloud.Link{
110 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
111 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
112 },
113 },
114 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
115 ID: instanceID,
116 Links: []gophercloud.Link{
117 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
118 },
119 Name: "json_rack_instance",
120 Status: "BUILD",
121 Volume: Volume{Size: 2},
Jamie Hannaford219ca592015-02-10 15:59:05 +0100122}
Jamie Hannaford4a170282015-02-18 14:16:57 +0100123
124func HandleCreate(t *testing.T) {
125 fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
126}
127
128func HandleList(t *testing.T) {
129 fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
130}
131
132func HandleGet(t *testing.T) {
133 fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
134}
135
136func HandleDelete(t *testing.T) {
137 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
138}
139
140func HandleEnableRoot(t *testing.T) {
141 fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
142}
143
144func HandleIsRootEnabled(t *testing.T) {
145 fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
146}
147
148func HandleRestart(t *testing.T) {
149 fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
150}
151
152func HandleResize(t *testing.T) {
153 fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
154}
155
156func HandleResizeVol(t *testing.T) {
157 fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
158}