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