github.com/leeclow-ops/gophercloud@v1.2.1/openstack/db/v1/instances/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/leeclow-ops/gophercloud" 9 "github.com/leeclow-ops/gophercloud/openstack/db/v1/datastores" 10 "github.com/leeclow-ops/gophercloud/openstack/db/v1/instances" 11 "github.com/leeclow-ops/gophercloud/testhelper/fixture" 12 ) 13 14 var ( 15 timestamp = "2015-11-12T14:22:42" 16 timeVal, _ = time.Parse(gophercloud.RFC3339NoZ, timestamp) 17 ) 18 19 var instance = ` 20 { 21 "created": "` + timestamp + `", 22 "datastore": { 23 "type": "mysql", 24 "version": "5.6" 25 }, 26 "flavor": { 27 "id": "1", 28 "links": [ 29 { 30 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 31 "rel": "self" 32 }, 33 { 34 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 35 "rel": "bookmark" 36 } 37 ] 38 }, 39 "links": [ 40 { 41 "href": "https://openstack.example.com/v1.0/1234/instances/1", 42 "rel": "self" 43 } 44 ], 45 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 46 "id": "{instanceID}", 47 "name": "json_rack_instance", 48 "status": "BUILD", 49 "updated": "` + timestamp + `", 50 "volume": { 51 "size": 2 52 } 53 } 54 ` 55 56 var instanceGet = ` 57 { 58 "created": "` + timestamp + `", 59 "datastore": { 60 "type": "mysql", 61 "version": "5.6" 62 }, 63 "flavor": { 64 "id": "1", 65 "links": [ 66 { 67 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 68 "rel": "self" 69 }, 70 { 71 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 72 "rel": "bookmark" 73 } 74 ] 75 }, 76 "links": [ 77 { 78 "href": "https://openstack.example.com/v1.0/1234/instances/1", 79 "rel": "self" 80 } 81 ], 82 "id": "{instanceID}", 83 "name": "test", 84 "status": "ACTIVE", 85 "operating_status": "HEALTHY", 86 "updated": "` + timestamp + `", 87 "volume": { 88 "size": 1, 89 "used": 0.12 90 }, 91 "addresses": [ 92 { 93 "address": "10.1.0.62", 94 "type": "private" 95 }, 96 { 97 "address": "172.24.5.114", 98 "type": "public" 99 } 100 ] 101 } 102 ` 103 104 var createReq = ` 105 { 106 "instance": { 107 "databases": [ 108 { 109 "character_set": "utf8", 110 "collate": "utf8_general_ci", 111 "name": "sampledb" 112 }, 113 { 114 "name": "nextround" 115 } 116 ], 117 "flavorRef": "1", 118 "name": "json_rack_instance", 119 "users": [ 120 { 121 "databases": [ 122 { 123 "name": "sampledb" 124 } 125 ], 126 "name": "demouser", 127 "password": "demopassword" 128 } 129 ], 130 "volume": { 131 "size": 2, 132 "type": "ssd" 133 } 134 } 135 } 136 ` 137 138 var instanceWithFault = ` 139 { 140 "created": "` + timestamp + `", 141 "datastore": { 142 "type": "mysql", 143 "version": "5.6" 144 }, 145 "flavor": { 146 "id": "1", 147 "links": [ 148 { 149 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 150 "rel": "self" 151 }, 152 { 153 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 154 "rel": "bookmark" 155 } 156 ] 157 }, 158 "links": [ 159 { 160 "href": "https://openstack.example.com/v1.0/1234/instances/1", 161 "rel": "self" 162 } 163 ], 164 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 165 "id": "{instanceID}", 166 "name": "json_rack_instance", 167 "status": "BUILD", 168 "updated": "` + timestamp + `", 169 "volume": { 170 "size": 2 171 }, 172 "fault": { 173 "message": "some error message", 174 "created": "` + timestamp + `", 175 "details": "some details about the error" 176 } 177 } 178 ` 179 180 var ( 181 instanceID = "{instanceID}" 182 configGroupID = "00000000-0000-0000-0000-000000000000" 183 rootURL = "/instances" 184 resURL = rootURL + "/" + instanceID 185 uRootURL = resURL + "/root" 186 aURL = resURL + "/action" 187 ) 188 189 var ( 190 restartReq = `{"restart": {}}` 191 resizeReq = `{"resize": {"flavorRef": "2"}}` 192 resizeVolReq = `{"resize": {"volume": {"size": 4}}}` 193 attachConfigurationGroupReq = `{"instance": {"configuration": "00000000-0000-0000-0000-000000000000"}}` 194 detachConfigurationGroupReq = `{"instance": {}}` 195 ) 196 197 var ( 198 createResp = fmt.Sprintf(`{"instance": %s}`, instance) 199 createWithFaultResp = fmt.Sprintf(`{"instance": %s}`, instanceWithFault) 200 listInstancesResp = fmt.Sprintf(`{"instances":[%s]}`, instance) 201 getInstanceResp = fmt.Sprintf(`{"instance": %s}`, instanceGet) 202 enableUserResp = `{"user":{"name":"root","password":"secretsecret"}}` 203 isUserEnabledResp = `{"rootEnabled":true}` 204 ) 205 206 var expectedInstance = instances.Instance{ 207 Created: timeVal, 208 Updated: timeVal, 209 Flavor: instances.Flavor{ 210 ID: "1", 211 Links: []gophercloud.Link{ 212 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, 213 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"}, 214 }, 215 }, 216 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 217 ID: instanceID, 218 Links: []gophercloud.Link{ 219 {Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"}, 220 }, 221 Name: "json_rack_instance", 222 Status: "BUILD", 223 Volume: instances.Volume{Size: 2}, 224 Datastore: datastores.DatastorePartial{ 225 Type: "mysql", 226 Version: "5.6", 227 }, 228 } 229 230 var expectedGetInstance = instances.Instance{ 231 Created: timeVal, 232 Updated: timeVal, 233 Flavor: instances.Flavor{ 234 ID: "1", 235 Links: []gophercloud.Link{ 236 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, 237 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"}, 238 }, 239 }, 240 ID: instanceID, 241 Links: []gophercloud.Link{ 242 {Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"}, 243 }, 244 Name: "test", 245 Status: "ACTIVE", 246 Volume: instances.Volume{Size: 1, Used: 0.12}, 247 Datastore: datastores.DatastorePartial{ 248 Type: "mysql", 249 Version: "5.6", 250 }, 251 Addresses: []instances.Address{ 252 {Type: "private", Address: "10.1.0.62"}, 253 {Type: "public", Address: "172.24.5.114"}, 254 }, 255 } 256 257 var expectedInstanceWithFault = instances.Instance{ 258 Created: timeVal, 259 Updated: timeVal, 260 Flavor: instances.Flavor{ 261 ID: "1", 262 Links: []gophercloud.Link{ 263 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, 264 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"}, 265 }, 266 }, 267 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 268 ID: instanceID, 269 Links: []gophercloud.Link{ 270 {Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"}, 271 }, 272 Name: "json_rack_instance", 273 Status: "BUILD", 274 Volume: instances.Volume{Size: 2}, 275 Datastore: datastores.DatastorePartial{ 276 Type: "mysql", 277 Version: "5.6", 278 }, 279 Fault: &instances.Fault{ 280 Created: timeVal, 281 Message: "some error message", 282 Details: "some details about the error", 283 }, 284 } 285 286 func HandleCreate(t *testing.T) { 287 fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200) 288 } 289 290 func HandleCreateWithFault(t *testing.T) { 291 fixture.SetupHandler(t, rootURL, "POST", createReq, createWithFaultResp, 200) 292 } 293 294 func HandleList(t *testing.T) { 295 fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200) 296 } 297 298 func HandleGet(t *testing.T) { 299 fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200) 300 } 301 302 func HandleDelete(t *testing.T) { 303 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202) 304 } 305 306 func HandleEnableRoot(t *testing.T) { 307 fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200) 308 } 309 310 func HandleIsRootEnabled(t *testing.T) { 311 fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200) 312 } 313 314 func HandleRestart(t *testing.T) { 315 fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202) 316 } 317 318 func HandleResize(t *testing.T) { 319 fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202) 320 } 321 322 func HandleResizeVol(t *testing.T) { 323 fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202) 324 } 325 326 func HandleAttachConfigurationGroup(t *testing.T) { 327 fixture.SetupHandler(t, resURL, "PUT", attachConfigurationGroupReq, "", 202) 328 } 329 330 func HandleDetachConfigurationGroup(t *testing.T) { 331 fixture.SetupHandler(t, resURL, "PUT", detachConfigurationGroupReq, "", 202) 332 }