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