blob: 7fa460117f4ccf8d0b25f883d564f3f0b0df5d81 [file] [log] [blame]
Jamie Hannaford936a5472015-02-10 14:38:28 +01001package instances
2
3import (
4 "testing"
5
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford52dbcee2015-10-06 16:09:56 +02007 "github.com/rackspace/gophercloud/openstack/db/v1/datastores"
Jamie Hannaford8803f832015-02-23 10:44:55 +01008 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +01009 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford2e817322015-02-16 15:29:17 +010010 "github.com/rackspace/gophercloud/pagination"
11 "github.com/rackspace/gophercloud/rackspace/db/v1/backups"
Jamie Hannaford936a5472015-02-10 14:38:28 +010012 th "github.com/rackspace/gophercloud/testhelper"
13 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010014 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford936a5472015-02-10 14:38:28 +010015)
16
Jamie Hannaford72749162015-10-14 12:14:32 +020017func TestInstanceList(t *testing.T) {
18 th.SetupHTTP()
19 defer th.TeardownHTTP()
20
21 fixture.SetupHandler(t, "/instances", "GET", "", listInstancesResp, 200)
22
23 opts := &ListOpts{
24 IncludeHA: false,
25 IncludeReplicas: false,
26 }
27
28 pages := 0
29 err := List(fake.ServiceClient(), opts).EachPage(func(page pagination.Page) (bool, error) {
30 pages++
31
32 actual, err := ExtractInstances(page)
33 if err != nil {
34 return false, err
35 }
36
37 th.CheckDeepEquals(t, []Instance{*expectedInstance}, actual)
38 return true, nil
39 })
40
41 th.AssertNoErr(t, err)
42 th.AssertEquals(t, 1, pages)
43}
44
Jamie Hannaford936a5472015-02-10 14:38:28 +010045func TestGetConfig(t *testing.T) {
46 th.SetupHTTP()
47 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010048 fixture.SetupHandler(t, resURL+"/configuration", "GET", "", getConfigResp, 200)
Jamie Hannaford936a5472015-02-10 14:38:28 +010049
Jamie Hannafordf77fc102015-02-10 14:56:02 +010050 config, err := GetDefaultConfig(fake.ServiceClient(), instanceID).Extract()
Jamie Hannaford936a5472015-02-10 14:38:28 +010051
52 expected := map[string]string{
53 "basedir": "/usr",
54 "connect_timeout": "15",
55 "datadir": "/var/lib/mysql",
56 "default_storage_engine": "innodb",
57 "innodb_buffer_pool_instances": "1",
58 "innodb_buffer_pool_size": "175M",
59 "innodb_checksum_algorithm": "crc32",
60 "innodb_data_file_path": "ibdata1:10M:autoextend",
61 "innodb_file_per_table": "1",
62 "innodb_io_capacity": "200",
63 "innodb_log_file_size": "256M",
64 "innodb_log_files_in_group": "2",
65 "innodb_open_files": "8192",
66 "innodb_thread_concurrency": "0",
67 "join_buffer_size": "1M",
68 "key_buffer_size": "50M",
69 "local-infile": "0",
70 "log-error": "/var/log/mysql/mysqld.log",
71 "max_allowed_packet": "16M",
72 "max_connect_errors": "10000",
73 "max_connections": "40",
74 "max_heap_table_size": "16M",
75 "myisam-recover": "BACKUP",
76 "open_files_limit": "8192",
77 "performance_schema": "off",
78 "pid_file": "/var/run/mysqld/mysqld.pid",
79 "port": "3306",
80 "query_cache_limit": "1M",
81 "query_cache_size": "8M",
82 "query_cache_type": "1",
83 "read_buffer_size": "256K",
84 "read_rnd_buffer_size": "1M",
85 "server_id": "1",
86 "skip-external-locking": "1",
87 "skip_name_resolve": "1",
88 "sort_buffer_size": "256K",
89 "table_open_cache": "4096",
90 "thread_stack": "192K",
91 "tmp_table_size": "16M",
92 "tmpdir": "/var/tmp",
93 "user": "mysql",
94 "wait_timeout": "3600",
95 }
96
97 th.AssertNoErr(t, err)
98 th.AssertDeepEquals(t, expected, config)
99}
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100100
101func TestAssociateWithConfigGroup(t *testing.T) {
102 th.SetupHTTP()
103 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100104 fixture.SetupHandler(t, resURL, "PUT", associateReq, associateResp, 202)
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100105
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100106 res := AssociateWithConfigGroup(fake.ServiceClient(), instanceID, "{configGroupID}")
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100107 th.AssertNoErr(t, res.Err)
108}
Jamie Hannaford2e817322015-02-16 15:29:17 +0100109
110func TestListBackups(t *testing.T) {
111 th.SetupHTTP()
112 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100113 fixture.SetupHandler(t, resURL+"/backups", "GET", "", listBackupsResp, 200)
Jamie Hannaford2e817322015-02-16 15:29:17 +0100114
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100115 pages := 0
Jamie Hannaford2e817322015-02-16 15:29:17 +0100116
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100117 err := ListBackups(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
118 pages++
Jamie Hannaford2e817322015-02-16 15:29:17 +0100119 actual, err := backups.ExtractBackups(page)
120 th.AssertNoErr(t, err)
121
122 expected := []backups.Backup{
123 backups.Backup{
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100124 Created: timeVal,
Jamie Hannaford2e817322015-02-16 15:29:17 +0100125 Description: "Backup from Restored Instance",
126 ID: "87972694-4be2-40f5-83f8-501656e0032a",
127 InstanceID: "29af2cd9-0674-48ab-b87a-b160f00208e6",
128 LocationRef: "http://localhost/path/to/backup",
129 Name: "restored_backup",
130 ParentID: "",
131 Size: 0.141026,
132 Status: "COMPLETED",
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100133 Updated: timeVal,
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100134 Datastore: datastores.DatastorePartial{Version: "5.1", Type: "MySQL", VersionID: "20000000-0000-0000-0000-000000000002"},
Jamie Hannaford2e817322015-02-16 15:29:17 +0100135 },
136 }
137
138 th.AssertDeepEquals(t, expected, actual)
Jamie Hannaford2e817322015-02-16 15:29:17 +0100139 return true, nil
140 })
141
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100142 th.AssertNoErr(t, err)
143 th.AssertEquals(t, 1, pages)
Jamie Hannaford2e817322015-02-16 15:29:17 +0100144}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100145
146func TestCreateReplica(t *testing.T) {
147 th.SetupHTTP()
148 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100149 fixture.SetupHandler(t, _rootURL, "POST", createReplicaReq, createReplicaResp, 200)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100150
151 opts := CreateOpts{
152 Name: "t2s1_ALT_GUEST",
153 FlavorRef: "9",
154 Size: 1,
155 ReplicaOf: "6bdca2fc-418e-40bd-a595-62abda61862d",
156 }
157
158 replica, err := Create(fake.ServiceClient(), opts).Extract()
159 th.AssertNoErr(t, err)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100160 th.AssertDeepEquals(t, expectedReplica, replica)
161}
162
163func TestListReplicas(t *testing.T) {
164 th.SetupHTTP()
165 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100166 fixture.SetupHandler(t, _rootURL, "GET", "", listReplicasResp, 200)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100167
168 pages := 0
Jamie Hannaford72749162015-10-14 12:14:32 +0200169 err := List(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100170 pages++
171
172 actual, err := ExtractInstances(page)
173 if err != nil {
174 return false, err
175 }
176
177 expected := []Instance{
178 Instance{
179 Status: "ACTIVE",
180 Name: "t1s1_ALT_GUEST",
181 Links: []gophercloud.Link{
182 gophercloud.Link{Rel: "self", Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254"},
183 gophercloud.Link{Rel: "bookmark", Href: "https://ord.databases.api.rackspacecloud.com/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254"},
184 },
185 ID: "3c691f06-bf9a-4618-b7ec-2817ce0cf254",
186 IP: []string{"10.0.0.3"},
187 Volume: os.Volume{Size: 1},
Jamie Hannaford8803f832015-02-23 10:44:55 +0100188 Flavor: flavors.Flavor{ID: "9"},
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100189 Datastore: datastores.DatastorePartial{Version: "5.6", Type: "mysql"},
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100190 ReplicaOf: &Instance{
191 ID: "8b499b45-52d6-402d-b398-f9d8f279c69a",
192 },
193 },
194 }
195
196 th.CheckDeepEquals(t, expected, actual)
197
198 return true, nil
199 })
200
201 th.AssertNoErr(t, err)
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100202 th.AssertEquals(t, 1, pages)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100203}
204
205func TestGetReplica(t *testing.T) {
206 th.SetupHTTP()
207 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100208 fixture.SetupHandler(t, resURL, "GET", "", getReplicaResp, 200)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100209
210 replica, err := Get(fake.ServiceClient(), instanceID).Extract()
211 th.AssertNoErr(t, err)
212
213 expectedReplica := &Instance{
214 Status: "ACTIVE",
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100215 Updated: timeVal,
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100216 Name: "t1_ALT_GUEST",
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100217 Created: timeVal,
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100218 IP: []string{
219 "10.0.0.2",
220 },
221 Replicas: []Instance{
222 Instance{ID: "3c691f06-bf9a-4618-b7ec-2817ce0cf254"},
223 },
224 ID: "8b499b45-52d6-402d-b398-f9d8f279c69a",
225 Volume: os.Volume{
226 Used: 0.54,
227 Size: 1,
228 },
Jamie Hannaford8803f832015-02-23 10:44:55 +0100229 Flavor: flavors.Flavor{ID: "9"},
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100230 Datastore: datastores.DatastorePartial{
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100231 Version: "5.6",
232 Type: "mysql",
233 },
234 }
235
236 th.AssertDeepEquals(t, replica, expectedReplica)
237}
238
239func TestDetachReplica(t *testing.T) {
240 th.SetupHTTP()
241 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100242 fixture.SetupHandler(t, resURL, "PATCH", detachReq, "", 202)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100243
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100244 err := DetachReplica(fake.ServiceClient(), instanceID).ExtractErr()
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100245 th.AssertNoErr(t, err)
246}