github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/backups/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "testing" 8 "time" 9 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/blockstorage/v3/backups" 11 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 12 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 13 ) 14 15 const ListResponse = ` 16 { 17 "backups": [ 18 { 19 "id": "289da7f8-6440-407c-9fb4-7db01ec49164", 20 "name": "backup-001" 21 }, 22 { 23 "id": "96c3bda7-c82a-4f50-be73-ca7621794835", 24 "name": "backup-002" 25 } 26 ], 27 "backups_links": [ 28 { 29 "href": "%s/backups?marker=1", 30 "rel": "next" 31 } 32 ] 33 } 34 ` 35 36 const ListDetailResponse = ` 37 { 38 "backups": [ 39 { 40 "id": "289da7f8-6440-407c-9fb4-7db01ec49164", 41 "name": "backup-001", 42 "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c", 43 "description": "Daily Backup", 44 "status": "available", 45 "size": 30, 46 "created_at": "2017-05-30T03:35:03.000000" 47 }, 48 { 49 "id": "96c3bda7-c82a-4f50-be73-ca7621794835", 50 "name": "backup-002", 51 "volume_id": "76b8950a-8594-4e5b-8dce-0dfa9c696358", 52 "description": "Weekly Backup", 53 "status": "available", 54 "size": 25, 55 "created_at": "2017-05-30T03:35:03.000000" 56 } 57 ], 58 "backups_links": [ 59 { 60 "href": "%s/backups/detail?marker=1", 61 "rel": "next" 62 } 63 ] 64 } 65 ` 66 67 const GetResponse = ` 68 { 69 "backup": { 70 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 71 "name": "backup-001", 72 "description": "Daily backup", 73 "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c", 74 "status": "available", 75 "size": 30, 76 "created_at": "2017-05-30T03:35:03.000000" 77 } 78 } 79 ` 80 const CreateRequest = ` 81 { 82 "backup": { 83 "volume_id": "1234", 84 "name": "backup-001" 85 } 86 } 87 ` 88 89 const CreateResponse = ` 90 { 91 "backup": { 92 "volume_id": "1234", 93 "name": "backup-001", 94 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 95 "description": "Daily backup", 96 "volume_id": "1234", 97 "status": "available", 98 "size": 30, 99 "created_at": "2017-05-30T03:35:03.000000" 100 } 101 } 102 ` 103 104 const RestoreRequest = ` 105 { 106 "restore": { 107 "name": "vol-001", 108 "volume_id": "1234" 109 } 110 } 111 ` 112 113 const RestoreResponse = ` 114 { 115 "restore": { 116 "backup_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 117 "volume_id": "1234", 118 "volume_name": "vol-001" 119 } 120 } 121 ` 122 123 const ExportResponse = ` 124 { 125 "backup-record": { 126 "backup_service": "cinder.backup.drivers.swift.SwiftBackupDriver", 127 "backup_url": "eyJpZCI6ImQzMjAxOWQzLWJjNmUtNDMxOS05YzFkLTY3MjJmYzEzNmEyMiIsInZvbHVtZV9pZCI6ImNmOWJjNmZhLWM1YmMtNDFmNi1iYzRlLTZlNzZjMGJlYTk1OSIsInNuYXBzaG90X2lkIjpudWxsLCJzdGF0dXMiOiJhdmFpbGFibGUiLCJzaXplIjoxLCJvYmplY3RfY291bnQiOjIsImNvbnRhaW5lciI6Im15LXRlc3QtYmFja3VwIiwic2VydmljZV9tZXRhZGF0YSI6InZvbHVtZV9jZjliYzZmYS1jNWJjLTQxZjYtYmM0ZS02ZTc2YzBiZWE5NTkvMjAyMDAzMTExOTI4NTUvYXpfcmVnaW9uYl9iYWNrdXBfYjg3YmIxZTUtMGQ0ZS00NDVlLWE1NDgtNWFlNzQyNTYyYmFjIiwic2VydmljZSI6ImNpbmRlci5iYWNrdXAuZHJpdmVycy5zd2lmdC5Td2lmdEJhY2t1cERyaXZlciIsImhvc3QiOiJjaW5kZXItYmFja3VwLWhvc3QxIiwidXNlcl9pZCI6IjkzNTE0ZTA0LWEwMjYtNGY2MC04MTc2LTM5NWM4NTk1MDFkZCIsInRlbXBfc25hcHNob3RfaWQiOm51bGwsInRlbXBfdm9sdW1lX2lkIjpudWxsLCJyZXN0b3JlX3ZvbHVtZV9pZCI6bnVsbCwibnVtX2RlcGVuZGVudF9iYWNrdXBzIjpudWxsLCJlbmNyeXB0aW9uX2tleV9pZCI6bnVsbCwicGFyZW50X2lkIjpudWxsLCJkZWxldGVkIjpmYWxzZSwiZGlzcGxheV9uYW1lIjpudWxsLCJkaXNwbGF5X2Rlc2NyaXB0aW9uIjpudWxsLCJkcml2ZXJfaW5mbyI6bnVsbCwiZmFpbF9yZWFzb24iOm51bGwsInByb2plY3RfaWQiOiIxNGYxYzFmNWQxMmI0NzU1Yjk0ZWRlZjc4ZmY4YjMyNSIsIm1ldGFkYXRhIjp7fSwiYXZhaWxhYmlsaXR5X3pvbmUiOiJyZWdpb24xYiIsImNyZWF0ZWRfYXQiOiIyMDIwLTAzLTExVDE5OjI1OjI0WiIsInVwZGF0ZWRfYXQiOiIyMDIwLTAzLTExVDE5OjI5OjA4WiIsImRlbGV0ZWRfYXQiOm51bGwsImRhdGFfdGltZXN0YW1wIjoiMjAyMC0wMy0xMVQxOToyNToyNFoifQ==" 128 } 129 } 130 ` 131 132 const ImportRequest = ExportResponse 133 134 const ImportResponse = ` 135 { 136 "backup": { 137 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 138 "links": [ 139 { 140 "href": "https://volume/v2/14f1c1f5d12b4755b94edef78ff8b325/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22", 141 "rel": "self" 142 }, 143 { 144 "href": "https://volume/14f1c1f5d12b4755b94edef78ff8b325/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22", 145 "rel": "bookmark" 146 } 147 ], 148 "name": null 149 } 150 } 151 ` 152 153 const ResetRequest = ` 154 { 155 "os-reset_status": { 156 "status": "error" 157 } 158 } 159 ` 160 161 const ForceDeleteRequest = ` 162 { 163 "os-force_delete": {} 164 } 165 ` 166 167 var ( 168 status = "available" 169 availabilityZone = "region1b" 170 host = "cinder-backup-host1" 171 serviceMetadata = "volume_cf9bc6fa-c5bc-41f6-bc4e-6e76c0bea959/20200311192855/az_regionb_backup_b87bb1e5-0d4e-445e-a548-5ae742562bac" 172 size = 1 173 objectCount = 2 174 container = "my-test-backup" 175 service = "cinder.backup.drivers.swift.SwiftBackupDriver" 176 backupImport = backups.ImportBackup{ 177 ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", 178 Status: &status, 179 AvailabilityZone: &availabilityZone, 180 VolumeID: "cf9bc6fa-c5bc-41f6-bc4e-6e76c0bea959", 181 UpdatedAt: time.Date(2020, 3, 11, 19, 29, 8, 0, time.UTC), 182 Host: &host, 183 UserID: "93514e04-a026-4f60-8176-395c859501dd", 184 ServiceMetadata: &serviceMetadata, 185 Size: &size, 186 ObjectCount: &objectCount, 187 Container: &container, 188 Service: &service, 189 CreatedAt: time.Date(2020, 3, 11, 19, 25, 24, 0, time.UTC), 190 DataTimestamp: time.Date(2020, 3, 11, 19, 25, 24, 0, time.UTC), 191 ProjectID: "14f1c1f5d12b4755b94edef78ff8b325", 192 Metadata: make(map[string]string), 193 } 194 backupURL, _ = json.Marshal(backupImport) 195 ) 196 197 func MockListResponse(t *testing.T) { 198 th.Mux.HandleFunc("/backups", func(w http.ResponseWriter, r *http.Request) { 199 th.TestMethod(t, r, "GET") 200 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 201 202 w.Header().Add("Content-Type", "application/json") 203 w.WriteHeader(http.StatusOK) 204 205 if err := r.ParseForm(); err != nil { 206 t.Errorf("Failed to parse request form %v", err) 207 } 208 marker := r.Form.Get("marker") 209 switch marker { 210 case "": 211 fmt.Fprintf(w, ListResponse, th.Server.URL) 212 case "1": 213 fmt.Fprint(w, `{"backups": []}`) 214 default: 215 t.Fatalf("Unexpected marker: [%s]", marker) 216 } 217 }) 218 } 219 220 func MockListDetailResponse(t *testing.T) { 221 th.Mux.HandleFunc("/backups/detail", func(w http.ResponseWriter, r *http.Request) { 222 th.TestMethod(t, r, "GET") 223 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 224 225 w.Header().Add("Content-Type", "application/json") 226 w.WriteHeader(http.StatusOK) 227 228 if err := r.ParseForm(); err != nil { 229 t.Errorf("Failed to parse request form %v", err) 230 } 231 marker := r.Form.Get("marker") 232 switch marker { 233 case "": 234 fmt.Fprintf(w, ListDetailResponse, th.Server.URL) 235 case "1": 236 fmt.Fprint(w, `{"backups": []}`) 237 default: 238 t.Fatalf("Unexpected marker: [%s]", marker) 239 } 240 }) 241 } 242 243 func MockGetResponse(t *testing.T) { 244 th.Mux.HandleFunc("/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 245 th.TestMethod(t, r, "GET") 246 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 247 248 w.Header().Add("Content-Type", "application/json") 249 w.WriteHeader(http.StatusOK) 250 fmt.Fprint(w, GetResponse) 251 }) 252 } 253 254 func MockCreateResponse(t *testing.T) { 255 th.Mux.HandleFunc("/backups", func(w http.ResponseWriter, r *http.Request) { 256 th.TestMethod(t, r, "POST") 257 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 258 th.TestHeader(t, r, "Content-Type", "application/json") 259 th.TestHeader(t, r, "Accept", "application/json") 260 th.TestJSONRequest(t, r, CreateRequest) 261 262 w.Header().Add("Content-Type", "application/json") 263 w.WriteHeader(http.StatusAccepted) 264 265 fmt.Fprint(w, CreateResponse) 266 }) 267 } 268 269 func MockRestoreResponse(t *testing.T) { 270 th.Mux.HandleFunc("/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22/restore", func(w http.ResponseWriter, r *http.Request) { 271 th.TestMethod(t, r, "POST") 272 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 273 th.TestHeader(t, r, "Content-Type", "application/json") 274 th.TestHeader(t, r, "Accept", "application/json") 275 th.TestJSONRequest(t, r, RestoreRequest) 276 277 w.Header().Add("Content-Type", "application/json") 278 w.WriteHeader(http.StatusAccepted) 279 280 fmt.Fprint(w, RestoreResponse) 281 }) 282 } 283 284 func MockDeleteResponse(t *testing.T) { 285 th.Mux.HandleFunc("/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 286 th.TestMethod(t, r, "DELETE") 287 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 288 w.WriteHeader(http.StatusNoContent) 289 }) 290 } 291 292 func MockExportResponse(t *testing.T) { 293 th.Mux.HandleFunc("/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22/export_record", func(w http.ResponseWriter, r *http.Request) { 294 th.TestMethod(t, r, "GET") 295 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 296 th.TestHeader(t, r, "Accept", "application/json") 297 298 w.Header().Add("Content-Type", "application/json") 299 w.WriteHeader(http.StatusOK) 300 301 fmt.Fprint(w, ExportResponse) 302 }) 303 } 304 305 func MockImportResponse(t *testing.T) { 306 th.Mux.HandleFunc("/backups/import_record", func(w http.ResponseWriter, r *http.Request) { 307 th.TestMethod(t, r, "POST") 308 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 309 th.TestHeader(t, r, "Content-Type", "application/json") 310 th.TestHeader(t, r, "Accept", "application/json") 311 th.TestJSONRequest(t, r, ImportRequest) 312 313 w.Header().Add("Content-Type", "application/json") 314 w.WriteHeader(http.StatusCreated) 315 316 fmt.Fprint(w, ImportResponse) 317 }) 318 } 319 320 // MockResetStatusResponse provides mock response for reset backup status API call 321 func MockResetStatusResponse(t *testing.T) { 322 th.Mux.HandleFunc("/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) { 323 th.TestMethod(t, r, "POST") 324 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 325 th.TestHeader(t, r, "Content-Type", "application/json") 326 th.TestJSONRequest(t, r, ResetRequest) 327 328 w.WriteHeader(http.StatusAccepted) 329 }) 330 } 331 332 // MockForceDeleteResponse provides mock response for force delete backup API call 333 func MockForceDeleteResponse(t *testing.T) { 334 th.Mux.HandleFunc("/backups/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) { 335 th.TestMethod(t, r, "POST") 336 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 337 th.TestHeader(t, r, "Content-Type", "application/json") 338 th.TestJSONRequest(t, r, ForceDeleteRequest) 339 340 w.WriteHeader(http.StatusAccepted) 341 }) 342 }