github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/sharedfilesystems/v2/sharenetworks/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 func createReq(name, description, network, subnetwork string) string { 13 return fmt.Sprintf(`{ 14 "share_network": { 15 "name": "%s", 16 "description": "%s", 17 "neutron_net_id": "%s", 18 "neutron_subnet_id": "%s" 19 } 20 }`, name, description, network, subnetwork) 21 } 22 23 func createResp(name, description, network, subnetwork string) string { 24 return fmt.Sprintf(` 25 { 26 "share_network": { 27 "name": "%s", 28 "description": "%s", 29 "segmentation_id": null, 30 "created_at": "2015-09-07T14:37:00.583656", 31 "updated_at": null, 32 "id": "77eb3421-4549-4789-ac39-0d5185d68c29", 33 "neutron_net_id": "%s", 34 "neutron_subnet_id": "%s", 35 "ip_version": null, 36 "nova_net_id": null, 37 "cidr": null, 38 "project_id": "e10a683c20da41248cfd5e1ab3d88c62", 39 "network_type": null 40 } 41 }`, name, description, network, subnetwork) 42 } 43 44 func MockCreateResponse(t *testing.T) { 45 th.Mux.HandleFunc("/share-networks", func(w http.ResponseWriter, r *http.Request) { 46 th.TestMethod(t, r, "POST") 47 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 48 th.TestHeader(t, r, "Content-Type", "application/json") 49 th.TestHeader(t, r, "Accept", "application/json") 50 th.TestJSONRequest(t, r, createReq("my_network", 51 "This is my share network", 52 "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 53 "53482b62-2c84-4a53-b6ab-30d9d9800d06")) 54 55 w.Header().Add("Content-Type", "application/json") 56 w.WriteHeader(http.StatusAccepted) 57 58 fmt.Fprint(w, createResp("my_network", 59 "This is my share network", 60 "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 61 "53482b62-2c84-4a53-b6ab-30d9d9800d06")) 62 }) 63 } 64 65 func MockDeleteResponse(t *testing.T) { 66 th.Mux.HandleFunc("/share-networks/fa158a3d-6d9f-4187-9ca5-abbb82646eb2", func(w http.ResponseWriter, r *http.Request) { 67 th.TestMethod(t, r, "DELETE") 68 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 69 w.WriteHeader(http.StatusAccepted) 70 }) 71 } 72 73 func MockListResponse(t *testing.T) { 74 th.Mux.HandleFunc("/share-networks/detail", func(w http.ResponseWriter, r *http.Request) { 75 th.TestMethod(t, r, "GET") 76 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 77 78 w.Header().Add("Content-Type", "application/json") 79 w.WriteHeader(http.StatusOK) 80 81 if err := r.ParseForm(); err != nil { 82 t.Errorf("Failed to parse request form %v", err) 83 } 84 marker := r.Form.Get("offset") 85 86 switch marker { 87 case "": 88 fmt.Fprint(w, `{ 89 "share_networks": [ 90 { 91 "name": "net_my1", 92 "segmentation_id": null, 93 "created_at": "2015-09-04T14:57:13.000000", 94 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 95 "updated_at": null, 96 "id": "32763294-e3d4-456a-998d-60047677c2fb", 97 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 98 "ip_version": null, 99 "nova_net_id": null, 100 "cidr": null, 101 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 102 "network_type": null, 103 "description": "descr" 104 }, 105 { 106 "name": "net_my", 107 "segmentation_id": null, 108 "created_at": "2015-09-04T14:54:25.000000", 109 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 110 "updated_at": null, 111 "id": "713df749-aac0-4a54-af52-10f6c991e80c", 112 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 113 "ip_version": null, 114 "nova_net_id": null, 115 "cidr": null, 116 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 117 "network_type": null, 118 "description": "desecr" 119 }, 120 { 121 "name": null, 122 "segmentation_id": null, 123 "created_at": "2015-09-04T14:51:41.000000", 124 "neutron_subnet_id": null, 125 "updated_at": null, 126 "id": "fa158a3d-6d9f-4187-9ca5-abbb82646eb2", 127 "neutron_net_id": null, 128 "ip_version": null, 129 "nova_net_id": null, 130 "cidr": null, 131 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 132 "network_type": null, 133 "description": null 134 } 135 ] 136 }`) 137 default: 138 fmt.Fprint(w, ` 139 { 140 "share_networks": [] 141 }`) 142 } 143 }) 144 } 145 146 func MockFilteredListResponse(t *testing.T) { 147 th.Mux.HandleFunc("/share-networks/detail", func(w http.ResponseWriter, r *http.Request) { 148 th.TestMethod(t, r, "GET") 149 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 150 151 w.Header().Add("Content-Type", "application/json") 152 w.WriteHeader(http.StatusOK) 153 154 if err := r.ParseForm(); err != nil { 155 t.Errorf("Failed to parse request form %v", err) 156 } 157 marker := r.Form.Get("offset") 158 switch marker { 159 case "": 160 fmt.Fprint(w, ` 161 { 162 "share_networks": [ 163 { 164 "name": "net_my1", 165 "segmentation_id": null, 166 "created_at": "2015-09-04T14:57:13.000000", 167 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 168 "updated_at": null, 169 "id": "32763294-e3d4-456a-998d-60047677c2fb", 170 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 171 "ip_version": null, 172 "nova_net_id": null, 173 "cidr": null, 174 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 175 "network_type": null, 176 "description": "descr" 177 } 178 ] 179 }`) 180 case "1": 181 fmt.Fprint(w, ` 182 { 183 "share_networks": [ 184 { 185 "name": "net_my1", 186 "segmentation_id": null, 187 "created_at": "2015-09-04T14:57:13.000000", 188 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 189 "updated_at": null, 190 "id": "32763294-e3d4-456a-998d-60047677c2fb", 191 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 192 "ip_version": null, 193 "nova_net_id": null, 194 "cidr": null, 195 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 196 "network_type": null, 197 "description": "descr" 198 } 199 ] 200 }`) 201 case "2": 202 fmt.Fprint(w, ` 203 { 204 "share_networks": [ 205 { 206 "name": "net_my1", 207 "segmentation_id": null, 208 "created_at": "2015-09-04T14:57:13.000000", 209 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 210 "updated_at": null, 211 "id": "32763294-e3d4-456a-998d-60047677c2fb", 212 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 213 "ip_version": null, 214 "nova_net_id": null, 215 "cidr": null, 216 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 217 "network_type": null, 218 "description": "descr" 219 } 220 ] 221 }`) 222 default: 223 fmt.Fprint(w, ` 224 { 225 "share_networks": [] 226 }`) 227 } 228 }) 229 } 230 231 func MockGetResponse(t *testing.T) { 232 th.Mux.HandleFunc("/share-networks/7f950b52-6141-4a08-bbb5-bb7ffa3ea5fd", func(w http.ResponseWriter, r *http.Request) { 233 th.TestMethod(t, r, "GET") 234 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 235 236 w.Header().Add("Content-Type", "application/json") 237 w.WriteHeader(http.StatusOK) 238 fmt.Fprint(w, ` 239 { 240 "share_network": { 241 "name": "net_my1", 242 "segmentation_id": null, 243 "created_at": "2015-09-04T14:56:45.000000", 244 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 245 "updated_at": null, 246 "id": "7f950b52-6141-4a08-bbb5-bb7ffa3ea5fd", 247 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 248 "ip_version": null, 249 "nova_net_id": null, 250 "cidr": null, 251 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 252 "network_type": null, 253 "description": "descr" 254 } 255 }`) 256 }) 257 } 258 259 func MockUpdateNeutronResponse(t *testing.T) { 260 th.Mux.HandleFunc("/share-networks/713df749-aac0-4a54-af52-10f6c991e80c", func(w http.ResponseWriter, r *http.Request) { 261 th.TestMethod(t, r, "PUT") 262 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 263 w.WriteHeader(http.StatusOK) 264 fmt.Fprint(w, ` 265 { 266 "share_network": { 267 "name": "net_my2", 268 "segmentation_id": null, 269 "created_at": "2015-09-04T14:54:25.000000", 270 "neutron_subnet_id": "new-neutron-subnet-id", 271 "updated_at": "2015-09-07T08:02:53.512184", 272 "id": "713df749-aac0-4a54-af52-10f6c991e80c", 273 "neutron_net_id": "new-neutron-id", 274 "ip_version": 4, 275 "nova_net_id": null, 276 "cidr": null, 277 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 278 "network_type": null, 279 "description": "new description" 280 } 281 } 282 `) 283 }) 284 } 285 286 func MockUpdateNovaResponse(t *testing.T) { 287 th.Mux.HandleFunc("/share-networks/713df749-aac0-4a54-af52-10f6c991e80c", func(w http.ResponseWriter, r *http.Request) { 288 th.TestMethod(t, r, "PUT") 289 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 290 w.WriteHeader(http.StatusOK) 291 fmt.Fprint(w, ` 292 { 293 "share_network": { 294 "name": "net_my2", 295 "segmentation_id": null, 296 "created_at": "2015-09-04T14:54:25.000000", 297 "neutron_subnet_id": null, 298 "updated_at": "2015-09-07T08:02:53.512184", 299 "id": "713df749-aac0-4a54-af52-10f6c991e80c", 300 "neutron_net_id": null, 301 "ip_version": 4, 302 "nova_net_id": "new-nova-id", 303 "cidr": null, 304 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 305 "network_type": null, 306 "description": "new description" 307 } 308 } 309 `) 310 }) 311 } 312 313 func MockAddSecurityServiceResponse(t *testing.T) { 314 th.Mux.HandleFunc("/share-networks/shareNetworkID/action", func(w http.ResponseWriter, r *http.Request) { 315 th.TestMethod(t, r, "POST") 316 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 317 w.WriteHeader(http.StatusOK) 318 fmt.Fprint(w, ` 319 { 320 "share_network": { 321 "name": "net2", 322 "segmentation_id": null, 323 "created_at": "2015-09-07T12:31:12.000000", 324 "neutron_subnet_id": null, 325 "updated_at": null, 326 "id": "d8ae6799-2567-4a89-aafb-fa4424350d2b", 327 "neutron_net_id": null, 328 "ip_version": 4, 329 "nova_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 330 "cidr": null, 331 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 332 "network_type": null, 333 "description": null 334 } 335 }`) 336 }) 337 } 338 339 func MockRemoveSecurityServiceResponse(t *testing.T) { 340 th.Mux.HandleFunc("/share-networks/shareNetworkID/action", func(w http.ResponseWriter, r *http.Request) { 341 th.TestMethod(t, r, "POST") 342 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 343 w.WriteHeader(http.StatusOK) 344 fmt.Fprint(w, ` 345 { 346 "share_network": { 347 "name": "net2", 348 "segmentation_id": null, 349 "created_at": "2015-09-07T12:31:12.000000", 350 "neutron_subnet_id": null, 351 "updated_at": null, 352 "id": "d8ae6799-2567-4a89-aafb-fa4424350d2b", 353 "neutron_net_id": null, 354 "ip_version": null, 355 "nova_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 356 "cidr": null, 357 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 358 "network_type": null, 359 "description": null 360 } 361 }`) 362 }) 363 }