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