blob: 0805dd81ee9cf5f9d1a7bd651e630add164b0a65 [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"
7 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford2e817322015-02-16 15:29:17 +01008 "github.com/rackspace/gophercloud/pagination"
9 "github.com/rackspace/gophercloud/rackspace/db/v1/backups"
Jamie Hannaforda50d1352015-02-18 11:38:38 +010010 "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
Jamie Hannaford936a5472015-02-10 14:38:28 +010011 th "github.com/rackspace/gophercloud/testhelper"
12 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010013 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannaford936a5472015-02-10 14:38:28 +010014)
15
16func TestGetConfig(t *testing.T) {
17 th.SetupHTTP()
18 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010019 fixture.SetupHandler(t, resURL+"/configuration", "GET", "", getConfigResp, 200)
Jamie Hannaford936a5472015-02-10 14:38:28 +010020
Jamie Hannafordf77fc102015-02-10 14:56:02 +010021 config, err := GetDefaultConfig(fake.ServiceClient(), instanceID).Extract()
Jamie Hannaford936a5472015-02-10 14:38:28 +010022
23 expected := map[string]string{
24 "basedir": "/usr",
25 "connect_timeout": "15",
26 "datadir": "/var/lib/mysql",
27 "default_storage_engine": "innodb",
28 "innodb_buffer_pool_instances": "1",
29 "innodb_buffer_pool_size": "175M",
30 "innodb_checksum_algorithm": "crc32",
31 "innodb_data_file_path": "ibdata1:10M:autoextend",
32 "innodb_file_per_table": "1",
33 "innodb_io_capacity": "200",
34 "innodb_log_file_size": "256M",
35 "innodb_log_files_in_group": "2",
36 "innodb_open_files": "8192",
37 "innodb_thread_concurrency": "0",
38 "join_buffer_size": "1M",
39 "key_buffer_size": "50M",
40 "local-infile": "0",
41 "log-error": "/var/log/mysql/mysqld.log",
42 "max_allowed_packet": "16M",
43 "max_connect_errors": "10000",
44 "max_connections": "40",
45 "max_heap_table_size": "16M",
46 "myisam-recover": "BACKUP",
47 "open_files_limit": "8192",
48 "performance_schema": "off",
49 "pid_file": "/var/run/mysqld/mysqld.pid",
50 "port": "3306",
51 "query_cache_limit": "1M",
52 "query_cache_size": "8M",
53 "query_cache_type": "1",
54 "read_buffer_size": "256K",
55 "read_rnd_buffer_size": "1M",
56 "server_id": "1",
57 "skip-external-locking": "1",
58 "skip_name_resolve": "1",
59 "sort_buffer_size": "256K",
60 "table_open_cache": "4096",
61 "thread_stack": "192K",
62 "tmp_table_size": "16M",
63 "tmpdir": "/var/tmp",
64 "user": "mysql",
65 "wait_timeout": "3600",
66 }
67
68 th.AssertNoErr(t, err)
69 th.AssertDeepEquals(t, expected, config)
70}
Jamie Hannafordf77fc102015-02-10 14:56:02 +010071
72func TestAssociateWithConfigGroup(t *testing.T) {
73 th.SetupHTTP()
74 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010075 fixture.SetupHandler(t, resURL, "PUT", associateReq, associateResp, 202)
Jamie Hannafordf77fc102015-02-10 14:56:02 +010076
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010077 res := AssociateWithConfigGroup(fake.ServiceClient(), instanceID, "{configGroupID}")
Jamie Hannafordf77fc102015-02-10 14:56:02 +010078 th.AssertNoErr(t, res.Err)
79}
Jamie Hannaford2e817322015-02-16 15:29:17 +010080
81func TestListBackups(t *testing.T) {
82 th.SetupHTTP()
83 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010084 fixture.SetupHandler(t, resURL+"/backups", "GET", "", listBackupsResp, 200)
Jamie Hannaford2e817322015-02-16 15:29:17 +010085
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010086 pages := 0
Jamie Hannaford2e817322015-02-16 15:29:17 +010087
Jamie Hannaforde635b7d2015-02-18 14:11:46 +010088 err := ListBackups(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
89 pages++
Jamie Hannaford2e817322015-02-16 15:29:17 +010090 actual, err := backups.ExtractBackups(page)
91 th.AssertNoErr(t, err)
92
93 expected := []backups.Backup{
94 backups.Backup{
95 Created: "2014-06-18T21:23:35",
96 Description: "Backup from Restored Instance",
97 ID: "87972694-4be2-40f5-83f8-501656e0032a",
98 InstanceID: "29af2cd9-0674-48ab-b87a-b160f00208e6",
99 LocationRef: "http://localhost/path/to/backup",
100 Name: "restored_backup",
101 ParentID: "",
102 Size: 0.141026,
103 Status: "COMPLETED",
104 Updated: "2014-06-18T21:24:39",
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100105 Datastore: datastores.DatastorePartial{Version: "5.1", Type: "MySQL", VersionID: "20000000-0000-0000-0000-000000000002"},
Jamie Hannaford2e817322015-02-16 15:29:17 +0100106 },
107 }
108
109 th.AssertDeepEquals(t, expected, actual)
Jamie Hannaford2e817322015-02-16 15:29:17 +0100110 return true, nil
111 })
112
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100113 th.AssertNoErr(t, err)
114 th.AssertEquals(t, 1, pages)
Jamie Hannaford2e817322015-02-16 15:29:17 +0100115}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100116
117func TestCreateReplica(t *testing.T) {
118 th.SetupHTTP()
119 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100120 fixture.SetupHandler(t, _rootURL, "POST", createReplicaReq, createReplicaResp, 200)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100121
122 opts := CreateOpts{
123 Name: "t2s1_ALT_GUEST",
124 FlavorRef: "9",
125 Size: 1,
126 ReplicaOf: "6bdca2fc-418e-40bd-a595-62abda61862d",
127 }
128
129 replica, err := Create(fake.ServiceClient(), opts).Extract()
130 th.AssertNoErr(t, err)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100131 th.AssertDeepEquals(t, expectedReplica, replica)
132}
133
134func TestListReplicas(t *testing.T) {
135 th.SetupHTTP()
136 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100137 fixture.SetupHandler(t, _rootURL, "GET", "", listReplicasResp, 200)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100138
139 pages := 0
140 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
141 pages++
142
143 actual, err := ExtractInstances(page)
144 if err != nil {
145 return false, err
146 }
147
148 expected := []Instance{
149 Instance{
150 Status: "ACTIVE",
151 Name: "t1s1_ALT_GUEST",
152 Links: []gophercloud.Link{
153 gophercloud.Link{Rel: "self", Href: "https://ord.databases.api.rackspacecloud.com/v1.0/1234/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254"},
154 gophercloud.Link{Rel: "bookmark", Href: "https://ord.databases.api.rackspacecloud.com/instances/3c691f06-bf9a-4618-b7ec-2817ce0cf254"},
155 },
156 ID: "3c691f06-bf9a-4618-b7ec-2817ce0cf254",
157 IP: []string{"10.0.0.3"},
158 Volume: os.Volume{Size: 1},
159 Flavor: os.Flavor{ID: "9"},
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100160 Datastore: datastores.DatastorePartial{Version: "5.6", Type: "mysql"},
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100161 ReplicaOf: &Instance{
162 ID: "8b499b45-52d6-402d-b398-f9d8f279c69a",
163 },
164 },
165 }
166
167 th.CheckDeepEquals(t, expected, actual)
168
169 return true, nil
170 })
171
172 th.AssertNoErr(t, err)
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100173 th.AssertEquals(t, 1, pages)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100174}
175
176func TestGetReplica(t *testing.T) {
177 th.SetupHTTP()
178 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100179 fixture.SetupHandler(t, resURL, "GET", "", getReplicaResp, 200)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100180
181 replica, err := Get(fake.ServiceClient(), instanceID).Extract()
182 th.AssertNoErr(t, err)
183
184 expectedReplica := &Instance{
185 Status: "ACTIVE",
186 Updated: "2014-09-26T19:15:57",
187 Name: "t1_ALT_GUEST",
188 Created: "2014-09-26T19:15:50",
189 IP: []string{
190 "10.0.0.2",
191 },
192 Replicas: []Instance{
193 Instance{ID: "3c691f06-bf9a-4618-b7ec-2817ce0cf254"},
194 },
195 ID: "8b499b45-52d6-402d-b398-f9d8f279c69a",
196 Volume: os.Volume{
197 Used: 0.54,
198 Size: 1,
199 },
200 Flavor: os.Flavor{ID: "9"},
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100201 Datastore: datastores.DatastorePartial{
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100202 Version: "5.6",
203 Type: "mysql",
204 },
205 }
206
207 th.AssertDeepEquals(t, replica, expectedReplica)
208}
209
210func TestDetachReplica(t *testing.T) {
211 th.SetupHTTP()
212 defer th.TeardownHTTP()
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100213 fixture.SetupHandler(t, resURL, "PATCH", detachReq, "", 202)
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100214
Jamie Hannaforde635b7d2015-02-18 14:11:46 +0100215 err := DetachReplica(fake.ServiceClient(), instanceID).ExtractErr()
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100216 th.AssertNoErr(t, err)
217}