github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/peerings/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 9 "github.com/huaweicloud/golangsdk/openstack/networking/v2/peerings" 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 ) 12 13 func TestListVpcPeerings(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 th.Mux.HandleFunc("/v2.0/vpc/peerings", func(w http.ResponseWriter, r *http.Request) { 18 th.TestMethod(t, r, "GET") 19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 20 21 w.Header().Add("Content-Type", "application/json") 22 w.WriteHeader(http.StatusOK) 23 24 fmt.Fprintf(w, ` 25 { 26 "peerings": [ 27 { 28 "status": "PENDING_ACCEPTANCE", 29 "accept_vpc_info": { 30 "vpc_id": "c6efbdb7-dca4-4178-b3ec-692f125c1e25", 31 "tenant_id": "17fbda95add24720a4038ba4b1c705ed" 32 }, 33 "request_vpc_info": { 34 "vpc_id": "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", 35 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 36 }, 37 "name": "test_peering", 38 "id": "22a3e5b1-1150-408e-99f7-5e25a391cead" 39 }, 40 { 41 "status": "ACTIVE", 42 "accept_vpc_info": { 43 "vpc_id": "93e94d8e-31a6-4c22-bdf7-8b23c7b67329", 44 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 45 }, 46 "request_vpc_info": { 47 "vpc_id": "b0d686e5-312c-4279-b69c-eedbc779ae69", 48 "tenant_id": "bf74229f30c0421fae270386a43315ee" 49 }, 50 "name": "peering-7750-sunway", 51 "id": "283aabd7-dab4-409d-96ff-6c878b9a0219" 52 }, 53 { 54 "status": "ACTIVE", 55 "accept_vpc_info": { 56 "vpc_id": "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", 57 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 58 }, 59 "request_vpc_info": { 60 "vpc_id": "4117d38e-4c8f-4624-a505-bd96b97d024c", 61 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 62 }, 63 "name": "test", 64 "id": "71d64714-bd4e-44c4-917a-d8d1239e5292" 65 } 66 ] 67 } 68 `) 69 }) 70 71 //count := 0 72 actual, err := peerings.List(fake.ServiceClient(), peerings.ListOpts{}) 73 74 if err != nil { 75 t.Errorf("Failed to extract vpc_peering_connections: %v", err) 76 } 77 78 expected := []peerings.Peering{ 79 { 80 ID: "22a3e5b1-1150-408e-99f7-5e25a391cead", 81 Name: "test_peering", 82 Status: "PENDING_ACCEPTANCE", 83 RequestVpcInfo: peerings.VpcInfo{VpcId: "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, 84 AcceptVpcInfo: peerings.VpcInfo{VpcId: "c6efbdb7-dca4-4178-b3ec-692f125c1e25", TenantId: "17fbda95add24720a4038ba4b1c705ed"}, 85 }, 86 { 87 ID: "283aabd7-dab4-409d-96ff-6c878b9a0219", 88 Name: "peering-7750-sunway", 89 Status: "ACTIVE", 90 RequestVpcInfo: peerings.VpcInfo{VpcId: "b0d686e5-312c-4279-b69c-eedbc779ae69", TenantId: "bf74229f30c0421fae270386a43315ee"}, 91 AcceptVpcInfo: peerings.VpcInfo{VpcId: "93e94d8e-31a6-4c22-bdf7-8b23c7b67329", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, 92 }, 93 { 94 ID: "71d64714-bd4e-44c4-917a-d8d1239e5292", 95 Name: "test", 96 Status: "ACTIVE", 97 RequestVpcInfo: peerings.VpcInfo{VpcId: "4117d38e-4c8f-4624-a505-bd96b97d024c", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, 98 AcceptVpcInfo: peerings.VpcInfo{VpcId: "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, 99 }, 100 } 101 102 th.AssertDeepEquals(t, expected, actual) 103 } 104 105 func TestCreateVpcPeeringConnection(t *testing.T) { 106 th.SetupHTTP() 107 defer th.TeardownHTTP() 108 109 th.Mux.HandleFunc("/v2.0/vpc/peerings", 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.TestJSONRequest(t, r, ` 115 { 116 "peering": { 117 "name": "test", 118 119 "request_vpc_info": { 120 "vpc_id": "4117d38e-4c8f-4624-a505-bd96b97d024c" 121 }, 122 "accept_vpc_info": { 123 "vpc_id": "c6efbdb7-dca4-4178-b3ec-692f125c1e25", 124 "tenant_id": "17fbda95add24720a4038ba4b1c705ed" 125 } 126 } 127 } `) 128 129 w.Header().Add("Content-Type", "application/json") 130 w.WriteHeader(http.StatusCreated) 131 132 fmt.Fprintf(w, ` 133 { 134 "peering": { 135 "status": "PENDING_ACCEPTANCE", 136 "accept_vpc_info": { 137 "vpc_id": "c6efbdb7-dca4-4178-b3ec-692f125c1e25", 138 "tenant_id": "17fbda95add24720a4038ba4b1c705ed" 139 }, 140 "request_vpc_info": { 141 "vpc_id": "4117d38e-4c8f-4624-a505-bd96b97d024c", 142 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 143 }, 144 "name": "test", 145 "id": "4e6ca99d-8344-4eb2-b2c9-b77368db3704" 146 } 147 } `) 148 }) 149 150 options := peerings.CreateOpts{ 151 Name: "test", 152 RequestVpcInfo: peerings.VpcInfo{VpcId: "4117d38e-4c8f-4624-a505-bd96b97d024c"}, 153 AcceptVpcInfo: peerings.VpcInfo{VpcId: "c6efbdb7-dca4-4178-b3ec-692f125c1e25", TenantId: "17fbda95add24720a4038ba4b1c705ed"}, 154 } 155 n, err := peerings.Create(fake.ServiceClient(), options).Extract() 156 th.AssertNoErr(t, err) 157 th.AssertEquals(t, "test", n.Name) 158 th.AssertEquals(t, "4e6ca99d-8344-4eb2-b2c9-b77368db3704", n.ID) 159 160 th.AssertEquals(t, peerings.VpcInfo{VpcId: "4117d38e-4c8f-4624-a505-bd96b97d024c", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, n.RequestVpcInfo) 161 th.AssertEquals(t, peerings.VpcInfo{VpcId: "c6efbdb7-dca4-4178-b3ec-692f125c1e25", TenantId: "17fbda95add24720a4038ba4b1c705ed"}, n.AcceptVpcInfo) 162 th.AssertEquals(t, "PENDING_ACCEPTANCE", n.Status) 163 } 164 165 func TestUpdateVpcPeeringConnection(t *testing.T) { 166 th.SetupHTTP() 167 defer th.TeardownHTTP() 168 169 th.Mux.HandleFunc("/v2.0/vpc/peerings/4e6ca99d-8344-4eb2-b2c9-b77368db3704", func(w http.ResponseWriter, r *http.Request) { 170 th.TestMethod(t, r, "PUT") 171 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 172 th.TestHeader(t, r, "Content-Type", "application/json") 173 th.TestHeader(t, r, "Accept", "application/json") 174 th.TestJSONRequest(t, r, ` 175 { 176 "peering": { 177 "name": "test2" 178 } 179 }`) 180 181 w.Header().Add("Content-Type", "application/json") 182 w.WriteHeader(http.StatusOK) 183 184 fmt.Fprintf(w, ` 185 { 186 "peering": { 187 "status": "PENDING_ACCEPTANCE", 188 "accept_vpc_info": { 189 "vpc_id": "c6efbdb7-dca4-4178-b3ec-692f125c1e25", 190 "tenant_id": "17fbda95add24720a4038ba4b1c705ed" 191 }, 192 "request_vpc_info": { 193 "vpc_id": "4117d38e-4c8f-4624-a505-bd96b97d024c", 194 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 195 }, 196 "name": "test2", 197 "id": "4e6ca99d-8344-4eb2-b2c9-b77368db3704" 198 } 199 } 200 `) 201 }) 202 203 options := peerings.UpdateOpts{Name: "test2"} 204 205 n, err := peerings.Update(fake.ServiceClient(), "4e6ca99d-8344-4eb2-b2c9-b77368db3704", options).Extract() 206 th.AssertNoErr(t, err) 207 th.AssertEquals(t, "test2", n.Name) 208 th.AssertEquals(t, "4e6ca99d-8344-4eb2-b2c9-b77368db3704", n.ID) 209 th.AssertEquals(t, peerings.VpcInfo{VpcId: "4117d38e-4c8f-4624-a505-bd96b97d024c", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, n.RequestVpcInfo) 210 th.AssertEquals(t, peerings.VpcInfo{VpcId: "c6efbdb7-dca4-4178-b3ec-692f125c1e25", TenantId: "17fbda95add24720a4038ba4b1c705ed"}, n.AcceptVpcInfo) 211 th.AssertEquals(t, "PENDING_ACCEPTANCE", n.Status) 212 } 213 214 func TestDeleteVpcPeeringConnection(t *testing.T) { 215 th.SetupHTTP() 216 defer th.TeardownHTTP() 217 218 th.Mux.HandleFunc("/v2.0/vpc/peerings/4e6ca99d-8344-4eb2-b2c9-b77368db3704", func(w http.ResponseWriter, r *http.Request) { 219 th.TestMethod(t, r, "DELETE") 220 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 221 w.WriteHeader(http.StatusNoContent) 222 }) 223 224 res := peerings.Delete(fake.ServiceClient(), "4e6ca99d-8344-4eb2-b2c9-b77368db3704") 225 th.AssertNoErr(t, res.Err) 226 } 227 228 func TestAcceptVpcPeering(t *testing.T) { 229 230 th.SetupHTTP() 231 defer th.TeardownHTTP() 232 233 th.Mux.HandleFunc("/v2.0/vpc/peerings/22a3e5b1-1150-408e-99f7-5e25a391cead/accept", func(w http.ResponseWriter, r *http.Request) { 234 th.TestMethod(t, r, "PUT") 235 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 236 th.TestHeader(t, r, "Accept", "application/json") 237 238 w.Header().Add("Content-Type", "application/json") 239 w.WriteHeader(http.StatusOK) 240 241 fmt.Fprintf(w, ` { 242 "status": "ACTIVE", 243 "name": "test_peering", 244 "tenant_id": "17fbda95add24720a4038ba4b1c705ed", 245 "request_vpc_info": { 246 "vpc_id": "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", 247 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 248 }, 249 "accept_vpc_info": { 250 "vpc_id": "c6efbdb7-dca4-4178-b3ec-692f125c1e25", 251 "tenant_id": "17fbda95add24720a4038ba4b1c705ed" 252 }, 253 "id": "22a3e5b1-1150-408e-99f7-5e25a391cead" 254 } 255 `) 256 }) 257 258 n, err := peerings.Accept(fake.ServiceClient(), "22a3e5b1-1150-408e-99f7-5e25a391cead").ExtractResult() 259 th.AssertNoErr(t, err) 260 th.AssertEquals(t, "22a3e5b1-1150-408e-99f7-5e25a391cead", n.ID) 261 th.AssertEquals(t, "test_peering", n.Name) 262 th.AssertEquals(t, "ACTIVE", n.Status) 263 th.AssertDeepEquals(t, peerings.VpcInfo{VpcId: "c6efbdb7-dca4-4178-b3ec-692f125c1e25", TenantId: "17fbda95add24720a4038ba4b1c705ed"}, n.AcceptVpcInfo) 264 th.AssertDeepEquals(t, peerings.VpcInfo{VpcId: "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, n.RequestVpcInfo) 265 266 } 267 268 func TestRejectVpcPeering(t *testing.T) { 269 270 th.SetupHTTP() 271 defer th.TeardownHTTP() 272 273 th.Mux.HandleFunc("/v2.0/vpc/peerings/22a3e5b1-1150-408e-99f7-5e25a391cead/reject", func(w http.ResponseWriter, r *http.Request) { 274 th.TestMethod(t, r, "PUT") 275 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 276 th.TestHeader(t, r, "Accept", "application/json") 277 278 w.Header().Add("Content-Type", "application/json") 279 w.WriteHeader(http.StatusOK) 280 281 fmt.Fprintf(w, ` { 282 "status": "ACTIVE", 283 "name": "test_peering", 284 "tenant_id": "17fbda95add24720a4038ba4b1c705ed", 285 "request_vpc_info": { 286 "vpc_id": "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", 287 "tenant_id": "87a56a48977e42068f70ad3280c50f0e" 288 }, 289 "accept_vpc_info": { 290 "vpc_id": "c6efbdb7-dca4-4178-b3ec-692f125c1e25", 291 "tenant_id": "17fbda95add24720a4038ba4b1c705ed" 292 }, 293 "id": "22a3e5b1-1150-408e-99f7-5e25a391cead" 294 } 295 `) 296 }) 297 298 n, err := peerings.Reject(fake.ServiceClient(), "22a3e5b1-1150-408e-99f7-5e25a391cead").ExtractResult() 299 th.AssertNoErr(t, err) 300 th.AssertEquals(t, "22a3e5b1-1150-408e-99f7-5e25a391cead", n.ID) 301 th.AssertEquals(t, "test_peering", n.Name) 302 th.AssertEquals(t, "ACTIVE", n.Status) 303 th.AssertDeepEquals(t, peerings.VpcInfo{VpcId: "c6efbdb7-dca4-4178-b3ec-692f125c1e25", TenantId: "17fbda95add24720a4038ba4b1c705ed"}, n.AcceptVpcInfo) 304 th.AssertDeepEquals(t, peerings.VpcInfo{VpcId: "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", TenantId: "87a56a48977e42068f70ad3280c50f0e"}, n.RequestVpcInfo) 305 306 }