github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/sharedfilesystems/v2/replicas/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 9 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 10 ) 11 12 const ( 13 shareEndpoint = "/share-replicas" 14 replicaID = "3b9c33e8-b136-45c6-84a6-019c8db1d550" 15 ) 16 17 var createRequest = `{ 18 "share_replica": { 19 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 20 "availability_zone": "zone-1" 21 } 22 } 23 ` 24 25 var createResponse = `{ 26 "share_replica": { 27 "id": "3b9c33e8-b136-45c6-84a6-019c8db1d550", 28 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 29 "availability_zone": "zone-1", 30 "created_at": "2023-05-26T12:32:56.391337", 31 "status": "creating", 32 "share_network_id": "ca0163c8-3941-4420-8b01-41517e19e366", 33 "share_server_id": null, 34 "replica_state": null, 35 "updated_at": null 36 } 37 } 38 ` 39 40 // MockCreateResponse creates a mock response 41 func MockCreateResponse(t *testing.T) { 42 th.Mux.HandleFunc(shareEndpoint, func(w http.ResponseWriter, r *http.Request) { 43 th.TestMethod(t, r, "POST") 44 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 45 th.TestHeader(t, r, "Content-Type", "application/json") 46 th.TestHeader(t, r, "Accept", "application/json") 47 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 48 th.TestJSONRequest(t, r, createRequest) 49 w.Header().Add("Content-Type", "application/json") 50 w.WriteHeader(http.StatusAccepted) 51 fmt.Fprint(w, createResponse) 52 }) 53 } 54 55 // MockDeleteResponse creates a mock delete response 56 func MockDeleteResponse(t *testing.T) { 57 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID, func(w http.ResponseWriter, r *http.Request) { 58 th.TestMethod(t, r, "DELETE") 59 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 60 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 61 w.WriteHeader(http.StatusAccepted) 62 }) 63 } 64 65 var promoteRequest = `{ 66 "promote": { 67 "quiesce_wait_time": 30 68 } 69 } 70 ` 71 72 func MockPromoteResponse(t *testing.T) { 73 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID+"/action", func(w http.ResponseWriter, r *http.Request) { 74 th.TestMethod(t, r, "POST") 75 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 76 th.TestHeader(t, r, "Content-Type", "application/json") 77 th.TestHeader(t, r, "Accept", "application/json") 78 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 79 th.TestJSONRequest(t, r, promoteRequest) 80 w.WriteHeader(http.StatusAccepted) 81 }) 82 } 83 84 var resyncRequest = `{ 85 "resync": null 86 } 87 ` 88 89 func MockResyncResponse(t *testing.T) { 90 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID+"/action", func(w http.ResponseWriter, r *http.Request) { 91 th.TestMethod(t, r, "POST") 92 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 93 th.TestHeader(t, r, "Content-Type", "application/json") 94 th.TestHeader(t, r, "Accept", "application/json") 95 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 96 th.TestJSONRequest(t, r, resyncRequest) 97 w.WriteHeader(http.StatusAccepted) 98 }) 99 } 100 101 var resetStatusRequest = `{ 102 "reset_status": { 103 "status": "available" 104 } 105 } 106 ` 107 108 func MockResetStatusResponse(t *testing.T) { 109 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID+"/action", func(w http.ResponseWriter, r *http.Request) { 110 th.TestMethod(t, r, "POST") 111 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 112 th.TestHeader(t, r, "Content-Type", "application/json") 113 th.TestHeader(t, r, "Accept", "application/json") 114 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 115 th.TestJSONRequest(t, r, resetStatusRequest) 116 w.WriteHeader(http.StatusAccepted) 117 }) 118 } 119 120 var resetStateRequest = `{ 121 "reset_replica_state": { 122 "replica_state": "active" 123 } 124 } 125 ` 126 127 func MockResetStateResponse(t *testing.T) { 128 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID+"/action", func(w http.ResponseWriter, r *http.Request) { 129 th.TestMethod(t, r, "POST") 130 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 131 th.TestHeader(t, r, "Content-Type", "application/json") 132 th.TestHeader(t, r, "Accept", "application/json") 133 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 134 th.TestJSONRequest(t, r, resetStateRequest) 135 w.WriteHeader(http.StatusAccepted) 136 }) 137 } 138 139 var deleteRequest = `{ 140 "force_delete": null 141 } 142 ` 143 144 func MockForceDeleteResponse(t *testing.T) { 145 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID+"/action", func(w http.ResponseWriter, r *http.Request) { 146 th.TestMethod(t, r, "POST") 147 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 148 th.TestHeader(t, r, "Content-Type", "application/json") 149 th.TestHeader(t, r, "Accept", "application/json") 150 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 151 th.TestJSONRequest(t, r, deleteRequest) 152 w.WriteHeader(http.StatusAccepted) 153 }) 154 } 155 156 var getResponse = `{ 157 "share_replica": { 158 "id": "3b9c33e8-b136-45c6-84a6-019c8db1d550", 159 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 160 "availability_zone": "zone-1", 161 "created_at": "2023-05-26T12:32:56.391337", 162 "status": "available", 163 "share_network_id": "ca0163c8-3941-4420-8b01-41517e19e366", 164 "share_server_id": "5ccc1b0c-334a-4e46-81e6-b52e03223060", 165 "replica_state": "active", 166 "updated_at": "2023-05-26T12:33:28.265716" 167 } 168 } 169 ` 170 171 // MockGetResponse creates a mock get response 172 func MockGetResponse(t *testing.T) { 173 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID, func(w http.ResponseWriter, r *http.Request) { 174 th.TestMethod(t, r, "GET") 175 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 176 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 177 w.WriteHeader(http.StatusOK) 178 fmt.Fprint(w, getResponse) 179 }) 180 } 181 182 var listResponse = `{ 183 "share_replicas": [ 184 { 185 "id": "3b9c33e8-b136-45c6-84a6-019c8db1d550", 186 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 187 "status": "available", 188 "replica_state": "active" 189 }, 190 { 191 "id": "4b70c2e2-eec7-4699-880d-4da9051ca162", 192 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 193 "status": "available", 194 "replica_state": "out_of_sync" 195 }, 196 { 197 "id": "920bb037-bdd7-48a1-98f0-1aa1787ca3eb", 198 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 199 "status": "available", 200 "replica_state": "in_sync" 201 } 202 ] 203 } 204 ` 205 206 var listEmptyResponse = `{"share_replicas": []}` 207 208 // MockListResponse creates a mock detailed-list response 209 func MockListResponse(t *testing.T) { 210 th.Mux.HandleFunc(shareEndpoint, func(w http.ResponseWriter, r *http.Request) { 211 th.TestMethod(t, r, "GET") 212 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 213 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 214 215 w.Header().Add("Content-Type", "application/json") 216 w.WriteHeader(http.StatusOK) 217 218 if err := r.ParseForm(); err != nil { 219 t.Errorf("Failed to parse request form %v", err) 220 } 221 marker := r.Form.Get("offset") 222 shareID := r.Form.Get("share_id") 223 if shareID != "65a34695-f9e5-4eea-b48d-a0b261d82943" { 224 th.AssertNoErr(t, fmt.Errorf("unexpected share_id")) 225 } 226 227 switch marker { 228 case "": 229 fmt.Fprint(w, listResponse) 230 default: 231 fmt.Fprint(w, listEmptyResponse) 232 } 233 }) 234 } 235 236 var listDetailResponse = `{ 237 "share_replicas": [ 238 { 239 "id": "3b9c33e8-b136-45c6-84a6-019c8db1d550", 240 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 241 "availability_zone": "zone-1", 242 "created_at": "2023-05-26T12:32:56.391337", 243 "status": "available", 244 "share_network_id": "ca0163c8-3941-4420-8b01-41517e19e366", 245 "share_server_id": "5ccc1b0c-334a-4e46-81e6-b52e03223060", 246 "replica_state": "active", 247 "updated_at": "2023-05-26T12:33:28.265716" 248 }, 249 { 250 "id": "4b70c2e2-eec7-4699-880d-4da9051ca162", 251 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 252 "availability_zone": "zone-2", 253 "created_at": "2023-05-26T11:59:38.313089", 254 "status": "available", 255 "share_network_id": "ca0163c8-3941-4420-8b01-41517e19e366", 256 "share_server_id": "81aa586e-3a03-4f92-98bd-807d87a61c1a", 257 "replica_state": "out_of_sync", 258 "updated_at": "2023-05-26T12:00:04.321081" 259 }, 260 { 261 "id": "920bb037-bdd7-48a1-98f0-1aa1787ca3eb", 262 "share_id": "65a34695-f9e5-4eea-b48d-a0b261d82943", 263 "availability_zone": "zone-1", 264 "created_at": "2023-05-26T12:32:45.751834", 265 "status": "available", 266 "share_network_id": "ca0163c8-3941-4420-8b01-41517e19e366", 267 "share_server_id": "b87ea601-7d4c-47f3-8956-6876b7a6b6db", 268 "replica_state": "in_sync", 269 "updated_at": "2023-05-26T12:36:04.110328" 270 } 271 ] 272 } 273 ` 274 275 var listDetailEmptyResponse = `{"share_replicas": []}` 276 277 // MockListDetailResponse creates a mock detailed-list response 278 func MockListDetailResponse(t *testing.T) { 279 th.Mux.HandleFunc(shareEndpoint+"/detail", func(w http.ResponseWriter, r *http.Request) { 280 th.TestMethod(t, r, "GET") 281 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 282 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.11") 283 284 w.Header().Add("Content-Type", "application/json") 285 w.WriteHeader(http.StatusOK) 286 287 if err := r.ParseForm(); err != nil { 288 t.Errorf("Failed to parse request form %v", err) 289 } 290 marker := r.Form.Get("offset") 291 shareID := r.Form.Get("share_id") 292 if shareID != "65a34695-f9e5-4eea-b48d-a0b261d82943" { 293 th.AssertNoErr(t, fmt.Errorf("unexpected share_id")) 294 } 295 296 switch marker { 297 case "": 298 fmt.Fprint(w, listDetailResponse) 299 default: 300 fmt.Fprint(w, listDetailEmptyResponse) 301 } 302 }) 303 } 304 305 var listExportLocationsResponse = `{ 306 "export_locations": [ 307 { 308 "id": "3fc02d3c-da47-42a2-88b8-2d48f8c276bd", 309 "path": "192.168.1.123:/var/lib/manila/mnt/share-3b9c33e8-b136-45c6-84a6-019c8db1d550", 310 "preferred": true, 311 "replica_state": "active", 312 "availability_zone": "zone-1" 313 }, 314 { 315 "id": "ae73e762-e8b9-4aad-aad3-23afb7cd6825", 316 "path": "192.168.1.124:/var/lib/manila/mnt/share-3b9c33e8-b136-45c6-84a6-019c8db1d550", 317 "preferred": false, 318 "replica_state": "active", 319 "availability_zone": "zone-1" 320 } 321 ] 322 } 323 ` 324 325 // MockListExportLocationsResponse creates a mock get export locations response 326 func MockListExportLocationsResponse(t *testing.T) { 327 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID+"/export-locations", func(w http.ResponseWriter, r *http.Request) { 328 th.TestMethod(t, r, "GET") 329 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 330 w.Header().Add("Content-Type", "application/json") 331 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.47") 332 w.WriteHeader(http.StatusOK) 333 fmt.Fprint(w, listExportLocationsResponse) 334 }) 335 } 336 337 var getExportLocationResponse = `{ 338 "export_location": { 339 "id": "ae73e762-e8b9-4aad-aad3-23afb7cd6825", 340 "path": "192.168.1.124:/var/lib/manila/mnt/share-3b9c33e8-b136-45c6-84a6-019c8db1d550", 341 "preferred": false, 342 "created_at": "2023-05-26T12:44:33.987960", 343 "updated_at": "2023-05-26T12:44:33.958363", 344 "replica_state": "active", 345 "availability_zone": "zone-1" 346 } 347 } 348 ` 349 350 // MockGetExportLocationResponse creates a mock get export location response 351 func MockGetExportLocationResponse(t *testing.T) { 352 th.Mux.HandleFunc(shareEndpoint+"/"+replicaID+"/export-locations/ae73e762-e8b9-4aad-aad3-23afb7cd6825", func(w http.ResponseWriter, r *http.Request) { 353 th.TestMethod(t, r, "GET") 354 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 355 w.Header().Add("Content-Type", "application/json") 356 th.TestHeader(t, r, "X-OpenStack-Manila-API-Version", "2.47") 357 w.WriteHeader(http.StatusOK) 358 fmt.Fprint(w, getExportLocationResponse) 359 }) 360 }