blob: d30d889e450ead673d568ebaa628f5863490f3a7 [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 Hannaforde65ad952015-11-16 14:05:11 +01006 "time"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01007
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/openstack/db/v1/datastores"
10 "github.com/gophercloud/gophercloud/openstack/db/v1/flavors"
11 "github.com/gophercloud/gophercloud/testhelper/fixture"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010012)
13
Jamie Hannaforde65ad952015-11-16 14:05:11 +010014var (
15 timestamp = "2015-11-12T14:22:42Z"
16 timeVal, _ = time.Parse(time.RFC3339, timestamp)
17)
18
19var instance = `
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010020{
Jamie Hannaforde65ad952015-11-16 14:05:11 +010021 "created": "` + timestamp + `",
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010022 "datastore": {
23 "type": "mysql",
24 "version": "5.6"
25 },
26 "flavor": {
27 "id": "1",
28 "links": [
29 {
30 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
31 "rel": "self"
32 },
33 {
34 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
35 "rel": "bookmark"
36 }
37 ]
38 },
39 "links": [
40 {
41 "href": "https://my-openstack.com/v1.0/1234/instances/1",
42 "rel": "self"
43 }
44 ],
45 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
46 "id": "{instanceID}",
47 "name": "json_rack_instance",
48 "status": "BUILD",
Jamie Hannaforde65ad952015-11-16 14:05:11 +010049 "updated": "` + timestamp + `",
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010050 "volume": {
51 "size": 2
52 }
53}
54`
55
56var createReq = `
Jamie Hannaford821015f2015-02-10 12:58:36 +010057{
58 "instance": {
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010059 "databases": [
Jamie Hannaford821015f2015-02-10 12:58:36 +010060 {
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010061 "character_set": "utf8",
62 "collate": "utf8_general_ci",
63 "name": "sampledb"
64 },
65 {
66 "name": "nextround"
Jamie Hannaford821015f2015-02-10 12:58:36 +010067 }
68 ],
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010069 "flavorRef": "1",
Jamie Hannaford821015f2015-02-10 12:58:36 +010070 "name": "json_rack_instance",
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010071 "users": [
72 {
73 "databases": [
74 {
75 "name": "sampledb"
76 }
77 ],
78 "name": "demouser",
79 "password": "demopassword"
80 }
81 ],
Jamie Hannaford821015f2015-02-10 12:58:36 +010082 "volume": {
83 "size": 2
84 }
85 }
86}
87`
88
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010089var (
Jamie Hannaford4a170282015-02-18 14:16:57 +010090 instanceID = "{instanceID}"
91 rootURL = "/instances"
92 resURL = rootURL + "/" + instanceID
93 uRootURL = resURL + "/root"
94 aURL = resURL + "/action"
95)
96
97var (
Jamie Hannafordd2b755f2015-10-07 14:01:57 +020098 restartReq = `{"restart": {}}`
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010099 resizeReq = `{"resize": {"flavorRef": "2"}}`
100 resizeVolReq = `{"resize": {"volume": {"size": 4}}}`
101)
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100102
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100103var (
104 createResp = fmt.Sprintf(`{"instance": %s}`, instance)
105 listInstancesResp = fmt.Sprintf(`{"instances":[%s]}`, instance)
106 getInstanceResp = createResp
107 enableUserResp = `{"user":{"name":"root","password":"secretsecret"}}`
108 isUserEnabledResp = `{"rootEnabled":true}`
109)
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100110
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100111var expectedInstance = Instance{
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100112 Created: timeVal,
113 Updated: timeVal,
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100114 Flavor: flavors.Flavor{
Jamie Hannaford11108402015-02-23 10:31:41 +0100115 ID: "1",
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +0100116 Links: []gophercloud.Link{
117 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"},
118 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"},
119 },
120 },
121 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
122 ID: instanceID,
123 Links: []gophercloud.Link{
124 gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"},
125 },
126 Name: "json_rack_instance",
127 Status: "BUILD",
128 Volume: Volume{Size: 2},
Jamie Hannaford52dbcee2015-10-06 16:09:56 +0200129 Datastore: datastores.DatastorePartial{
130 Type: "mysql",
131 Version: "5.6",
132 },
Jamie Hannaford219ca592015-02-10 15:59:05 +0100133}
Jamie Hannaford4a170282015-02-18 14:16:57 +0100134
135func HandleCreate(t *testing.T) {
136 fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
137}
138
139func HandleList(t *testing.T) {
140 fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
141}
142
143func HandleGet(t *testing.T) {
144 fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
145}
146
147func HandleDelete(t *testing.T) {
148 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
149}
150
151func HandleEnableRoot(t *testing.T) {
152 fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
153}
154
155func HandleIsRootEnabled(t *testing.T) {
156 fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
157}
158
159func HandleRestart(t *testing.T) {
160 fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
161}
162
163func HandleResize(t *testing.T) {
164 fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
165}
166
167func HandleResizeVol(t *testing.T) {
168 fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
169}