blob: 29d1119f255e8860b8c2c7417158eaed89be2cbe [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
7 "github.com/rackspace/gophercloud/rackspace/db/v1/instances"
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10 "github.com/rackspace/gophercloud/testhelper/fixture"
11)
12
13var (
14 configID = "{configID}"
15 _baseURL = "/configurations"
16 resURL = _baseURL + "/" + configID
Jamie Hannaford23867bb2015-02-17 15:56:48 +010017
18 dsID = "{datastoreID}"
19 versionID = "{versionID}"
20 paramID = "{paramID}"
21 dsParamListURL = "/datastores/" + dsID + "/versions/" + versionID + "/parameters"
22 dsParamGetURL = "/datastores/" + dsID + "/versions/" + versionID + "/parameters/" + paramID
23 globalParamListURL = "/datastores/versions/" + versionID + "/parameters"
24 globalParamGetURL = "/datastores/versions/" + versionID + "/parameters/" + paramID
Jamie Hannaforded7f4532015-02-17 14:56:30 +010025)
26
27func TestList(t *testing.T) {
28 th.SetupHTTP()
29 defer th.TeardownHTTP()
30
31 fixture.SetupHandler(t, _baseURL, "GET", "", listConfigsJSON, 200)
32
33 count := 0
34 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
35 count++
36 actual, err := ExtractConfigs(page)
37 th.AssertNoErr(t, err)
38
39 expected := []Config{exampleConfig}
40 th.AssertDeepEquals(t, expected, actual)
41
42 return true, nil
43 })
44
45 th.AssertEquals(t, 1, count)
46 th.AssertNoErr(t, err)
47}
48
49func TestGet(t *testing.T) {
50 th.SetupHTTP()
51 defer th.TeardownHTTP()
52
53 fixture.SetupHandler(t, resURL, "GET", "", getConfigJSON, 200)
54
55 config, err := Get(fake.ServiceClient(), configID).Extract()
56 th.AssertNoErr(t, err)
57 th.AssertDeepEquals(t, &exampleConfig, config)
58}
59
60func TestCreate(t *testing.T) {
61 th.SetupHTTP()
62 defer th.TeardownHTTP()
63
64 fixture.SetupHandler(t, _baseURL, "POST", createReq, createConfigJSON, 201)
65
66 opts := CreateOpts{
67 Datastore: &DatastoreOpts{
68 Type: "a00000a0-00a0-0a00-00a0-000a000000aa",
69 Version: "b00000b0-00b0-0b00-00b0-000b000000bb",
70 },
71 Description: "example description",
72 Name: "example-configuration-name",
73 Values: map[string]interface{}{
74 "collation_server": "latin1_swedish_ci",
75 "connect_timeout": 120,
76 },
77 }
78
79 config, err := Create(fake.ServiceClient(), opts).Extract()
80 th.AssertNoErr(t, err)
81 th.AssertDeepEquals(t, &exampleConfigWithValues, config)
82}
83
84func TestUpdate(t *testing.T) {
85 th.SetupHTTP()
86 defer th.TeardownHTTP()
87
88 fixture.SetupHandler(t, resURL, "PATCH", updateReq, "", 200)
89
90 opts := UpdateOpts{
91 Values: map[string]interface{}{
92 "connect_timeout": 300,
93 },
94 }
95
96 err := Update(fake.ServiceClient(), configID, opts).ExtractErr()
97 th.AssertNoErr(t, err)
98}
99
100func TestReplace(t *testing.T) {
101 th.SetupHTTP()
102 defer th.TeardownHTTP()
103
104 fixture.SetupHandler(t, resURL, "PUT", updateReq, "", 202)
105
106 opts := UpdateOpts{
107 Values: map[string]interface{}{
108 "connect_timeout": 300,
109 },
110 }
111
112 err := Replace(fake.ServiceClient(), configID, opts).ExtractErr()
113 th.AssertNoErr(t, err)
114}
115
116func TestDelete(t *testing.T) {
117 th.SetupHTTP()
118 defer th.TeardownHTTP()
119
120 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
121
122 err := Delete(fake.ServiceClient(), configID).ExtractErr()
123 th.AssertNoErr(t, err)
124}
125
126func TestListInstances(t *testing.T) {
127 th.SetupHTTP()
128 defer th.TeardownHTTP()
129
130 fixture.SetupHandler(t, resURL+"/instances", "GET", "", listInstancesJSON, 200)
131
132 expectedInstance := instances.Instance{
133 ID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
134 Name: "json_rack_instance",
135 }
136
137 pages := 0
138 err := ListInstances(fake.ServiceClient(), configID).EachPage(func(page pagination.Page) (bool, error) {
139 pages++
140
141 actual, err := instances.ExtractInstances(page)
142 if err != nil {
143 return false, err
144 }
145
146 th.AssertDeepEquals(t, actual, []instances.Instance{expectedInstance})
147
148 return true, nil
149 })
150
151 th.AssertNoErr(t, err)
152 th.AssertEquals(t, 1, pages)
153}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100154
155func TestListDSParams(t *testing.T) {
156 th.SetupHTTP()
157 defer th.TeardownHTTP()
158
159 fixture.SetupHandler(t, dsParamListURL, "GET", "", listParamsJSON, 200)
160
161 pages := 0
162 err := ListDatastoreParams(fake.ServiceClient(), dsID, versionID).EachPage(func(page pagination.Page) (bool, error) {
163 pages++
164
165 actual, err := ExtractParams(page)
166 if err != nil {
167 return false, err
168 }
169
170 expected := []Param{
171 Param{Max: 1, Min: 0, Name: "innodb_file_per_table", RestartRequired: true, Type: "integer"},
172 Param{Max: 4294967296, Min: 0, Name: "key_buffer_size", RestartRequired: false, Type: "integer"},
173 Param{Max: 65535, Min: 2, Name: "connect_timeout", RestartRequired: false, Type: "integer"},
174 Param{Max: 4294967296, Min: 0, Name: "join_buffer_size", RestartRequired: false, Type: "integer"},
175 }
176
177 th.AssertDeepEquals(t, actual, expected)
178
179 return true, nil
180 })
181
182 th.AssertNoErr(t, err)
183 th.AssertEquals(t, 1, pages)
184}
185
186func TestGetDSParam(t *testing.T) {
187 th.SetupHTTP()
188 defer th.TeardownHTTP()
189
190 fixture.SetupHandler(t, dsParamGetURL, "GET", "", getParamJSON, 200)
191
192 param, err := GetDatastoreParam(fake.ServiceClient(), dsID, versionID, paramID).Extract()
193 th.AssertNoErr(t, err)
194
195 expected := &Param{
196 Max: 1, Min: 0, Name: "innodb_file_per_table", RestartRequired: true, Type: "integer",
197 }
198
199 th.AssertDeepEquals(t, expected, param)
200}
201
202func TestListGlobalParams(t *testing.T) {
203 th.SetupHTTP()
204 defer th.TeardownHTTP()
205
206 fixture.SetupHandler(t, globalParamListURL, "GET", "", listParamsJSON, 200)
207
208 pages := 0
209 err := ListGlobalParams(fake.ServiceClient(), versionID).EachPage(func(page pagination.Page) (bool, error) {
210 pages++
211
212 actual, err := ExtractParams(page)
213 if err != nil {
214 return false, err
215 }
216
217 expected := []Param{
218 Param{Max: 1, Min: 0, Name: "innodb_file_per_table", RestartRequired: true, Type: "integer"},
219 Param{Max: 4294967296, Min: 0, Name: "key_buffer_size", RestartRequired: false, Type: "integer"},
220 Param{Max: 65535, Min: 2, Name: "connect_timeout", RestartRequired: false, Type: "integer"},
221 Param{Max: 4294967296, Min: 0, Name: "join_buffer_size", RestartRequired: false, Type: "integer"},
222 }
223
224 th.AssertDeepEquals(t, actual, expected)
225
226 return true, nil
227 })
228
229 th.AssertNoErr(t, err)
230 th.AssertEquals(t, 1, pages)
231}
232
233func TestGetGlobalParam(t *testing.T) {
234 th.SetupHTTP()
235 defer th.TeardownHTTP()
236
237 fixture.SetupHandler(t, globalParamGetURL, "GET", "", getParamJSON, 200)
238
239 param, err := GetGlobalParam(fake.ServiceClient(), versionID, paramID).Extract()
240 th.AssertNoErr(t, err)
241
242 expected := &Param{
243 Max: 1, Min: 0, Name: "innodb_file_per_table", RestartRequired: true, Type: "integer",
244 }
245
246 th.AssertDeepEquals(t, expected, param)
247}