github.com/gophercloud/gophercloud@v1.11.0/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/gophercloud/gophercloud/testhelper" 9 fake "github.com/gophercloud/gophercloud/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.Fprintf(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 r.ParseForm() 82 marker := r.Form.Get("offset") 83 84 switch marker { 85 case "": 86 fmt.Fprintf(w, `{ 87 "share_networks": [ 88 { 89 "name": "net_my1", 90 "segmentation_id": null, 91 "created_at": "2015-09-04T14:57:13.000000", 92 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 93 "updated_at": null, 94 "id": "32763294-e3d4-456a-998d-60047677c2fb", 95 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 96 "ip_version": null, 97 "nova_net_id": null, 98 "cidr": null, 99 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 100 "network_type": null, 101 "description": "descr" 102 }, 103 { 104 "name": "net_my", 105 "segmentation_id": null, 106 "created_at": "2015-09-04T14:54:25.000000", 107 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 108 "updated_at": null, 109 "id": "713df749-aac0-4a54-af52-10f6c991e80c", 110 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 111 "ip_version": null, 112 "nova_net_id": null, 113 "cidr": null, 114 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 115 "network_type": null, 116 "description": "desecr" 117 }, 118 { 119 "name": null, 120 "segmentation_id": null, 121 "created_at": "2015-09-04T14:51:41.000000", 122 "neutron_subnet_id": null, 123 "updated_at": null, 124 "id": "fa158a3d-6d9f-4187-9ca5-abbb82646eb2", 125 "neutron_net_id": null, 126 "ip_version": null, 127 "nova_net_id": null, 128 "cidr": null, 129 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 130 "network_type": null, 131 "description": null 132 } 133 ] 134 }`) 135 default: 136 fmt.Fprintf(w, ` 137 { 138 "share_networks": [] 139 }`) 140 } 141 }) 142 } 143 144 func MockFilteredListResponse(t *testing.T) { 145 th.Mux.HandleFunc("/share-networks/detail", func(w http.ResponseWriter, r *http.Request) { 146 th.TestMethod(t, r, "GET") 147 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 148 149 w.Header().Add("Content-Type", "application/json") 150 w.WriteHeader(http.StatusOK) 151 152 r.ParseForm() 153 marker := r.Form.Get("offset") 154 switch marker { 155 case "": 156 fmt.Fprintf(w, ` 157 { 158 "share_networks": [ 159 { 160 "name": "net_my1", 161 "segmentation_id": null, 162 "created_at": "2015-09-04T14:57:13.000000", 163 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 164 "updated_at": null, 165 "id": "32763294-e3d4-456a-998d-60047677c2fb", 166 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 167 "ip_version": null, 168 "nova_net_id": null, 169 "cidr": null, 170 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 171 "network_type": null, 172 "description": "descr" 173 } 174 ] 175 }`) 176 case "1": 177 fmt.Fprintf(w, ` 178 { 179 "share_networks": [ 180 { 181 "name": "net_my1", 182 "segmentation_id": null, 183 "created_at": "2015-09-04T14:57:13.000000", 184 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 185 "updated_at": null, 186 "id": "32763294-e3d4-456a-998d-60047677c2fb", 187 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 188 "ip_version": null, 189 "nova_net_id": null, 190 "cidr": null, 191 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 192 "network_type": null, 193 "description": "descr" 194 } 195 ] 196 }`) 197 case "2": 198 fmt.Fprintf(w, ` 199 { 200 "share_networks": [ 201 { 202 "name": "net_my1", 203 "segmentation_id": null, 204 "created_at": "2015-09-04T14:57:13.000000", 205 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 206 "updated_at": null, 207 "id": "32763294-e3d4-456a-998d-60047677c2fb", 208 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 209 "ip_version": null, 210 "nova_net_id": null, 211 "cidr": null, 212 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 213 "network_type": null, 214 "description": "descr" 215 } 216 ] 217 }`) 218 default: 219 fmt.Fprintf(w, ` 220 { 221 "share_networks": [] 222 }`) 223 } 224 }) 225 } 226 227 func MockGetResponse(t *testing.T) { 228 th.Mux.HandleFunc("/share-networks/7f950b52-6141-4a08-bbb5-bb7ffa3ea5fd", func(w http.ResponseWriter, r *http.Request) { 229 th.TestMethod(t, r, "GET") 230 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 231 232 w.Header().Add("Content-Type", "application/json") 233 w.WriteHeader(http.StatusOK) 234 fmt.Fprintf(w, ` 235 { 236 "share_network": { 237 "name": "net_my1", 238 "segmentation_id": null, 239 "created_at": "2015-09-04T14:56:45.000000", 240 "neutron_subnet_id": "53482b62-2c84-4a53-b6ab-30d9d9800d06", 241 "updated_at": null, 242 "id": "7f950b52-6141-4a08-bbb5-bb7ffa3ea5fd", 243 "neutron_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 244 "ip_version": null, 245 "nova_net_id": null, 246 "cidr": null, 247 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 248 "network_type": null, 249 "description": "descr" 250 } 251 }`) 252 }) 253 } 254 255 func MockUpdateNeutronResponse(t *testing.T) { 256 th.Mux.HandleFunc("/share-networks/713df749-aac0-4a54-af52-10f6c991e80c", func(w http.ResponseWriter, r *http.Request) { 257 th.TestMethod(t, r, "PUT") 258 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 259 w.WriteHeader(http.StatusOK) 260 fmt.Fprintf(w, ` 261 { 262 "share_network": { 263 "name": "net_my2", 264 "segmentation_id": null, 265 "created_at": "2015-09-04T14:54:25.000000", 266 "neutron_subnet_id": "new-neutron-subnet-id", 267 "updated_at": "2015-09-07T08:02:53.512184", 268 "id": "713df749-aac0-4a54-af52-10f6c991e80c", 269 "neutron_net_id": "new-neutron-id", 270 "ip_version": 4, 271 "nova_net_id": null, 272 "cidr": null, 273 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 274 "network_type": null, 275 "description": "new description" 276 } 277 } 278 `) 279 }) 280 } 281 282 func MockUpdateNovaResponse(t *testing.T) { 283 th.Mux.HandleFunc("/share-networks/713df749-aac0-4a54-af52-10f6c991e80c", func(w http.ResponseWriter, r *http.Request) { 284 th.TestMethod(t, r, "PUT") 285 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 286 w.WriteHeader(http.StatusOK) 287 fmt.Fprintf(w, ` 288 { 289 "share_network": { 290 "name": "net_my2", 291 "segmentation_id": null, 292 "created_at": "2015-09-04T14:54:25.000000", 293 "neutron_subnet_id": null, 294 "updated_at": "2015-09-07T08:02:53.512184", 295 "id": "713df749-aac0-4a54-af52-10f6c991e80c", 296 "neutron_net_id": null, 297 "ip_version": 4, 298 "nova_net_id": "new-nova-id", 299 "cidr": null, 300 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 301 "network_type": null, 302 "description": "new description" 303 } 304 } 305 `) 306 }) 307 } 308 309 func MockAddSecurityServiceResponse(t *testing.T) { 310 th.Mux.HandleFunc("/share-networks/shareNetworkID/action", func(w http.ResponseWriter, r *http.Request) { 311 th.TestMethod(t, r, "POST") 312 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 313 w.WriteHeader(http.StatusOK) 314 fmt.Fprintf(w, ` 315 { 316 "share_network": { 317 "name": "net2", 318 "segmentation_id": null, 319 "created_at": "2015-09-07T12:31:12.000000", 320 "neutron_subnet_id": null, 321 "updated_at": null, 322 "id": "d8ae6799-2567-4a89-aafb-fa4424350d2b", 323 "neutron_net_id": null, 324 "ip_version": 4, 325 "nova_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 326 "cidr": null, 327 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 328 "network_type": null, 329 "description": null 330 } 331 }`) 332 }) 333 } 334 335 func MockRemoveSecurityServiceResponse(t *testing.T) { 336 th.Mux.HandleFunc("/share-networks/shareNetworkID/action", func(w http.ResponseWriter, r *http.Request) { 337 th.TestMethod(t, r, "POST") 338 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 339 w.WriteHeader(http.StatusOK) 340 fmt.Fprintf(w, ` 341 { 342 "share_network": { 343 "name": "net2", 344 "segmentation_id": null, 345 "created_at": "2015-09-07T12:31:12.000000", 346 "neutron_subnet_id": null, 347 "updated_at": null, 348 "id": "d8ae6799-2567-4a89-aafb-fa4424350d2b", 349 "neutron_net_id": null, 350 "ip_version": null, 351 "nova_net_id": "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 352 "cidr": null, 353 "project_id": "16e1ab15c35a457e9c2b2aa189f544e1", 354 "network_type": null, 355 "description": null 356 } 357 }`) 358 }) 359 }