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