github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/vpnaas/endpointgroups/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "testing" 8 9 fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common" 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/vpnaas/endpointgroups" 11 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 12 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 13 ) 14 15 func TestCreate(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups", func(w http.ResponseWriter, r *http.Request) { 20 th.TestMethod(t, r, "POST") 21 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 22 th.TestHeader(t, r, "Content-Type", "application/json") 23 th.TestHeader(t, r, "Accept", "application/json") 24 th.TestJSONRequest(t, r, ` 25 { 26 "endpoint_group": { 27 "endpoints": [ 28 "10.2.0.0/24", 29 "10.3.0.0/24" 30 ], 31 "type": "cidr", 32 "name": "peers" 33 } 34 } `) 35 36 w.Header().Add("Content-Type", "application/json") 37 w.WriteHeader(http.StatusCreated) 38 39 fmt.Fprint(w, ` 40 { 41 "endpoint_group": { 42 "description": "", 43 "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d", 44 "project_id": "4ad57e7ce0b24fca8f12b9834d91079d", 45 "endpoints": [ 46 "10.2.0.0/24", 47 "10.3.0.0/24" 48 ], 49 "type": "cidr", 50 "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 51 "name": "peers" 52 } 53 } 54 `) 55 }) 56 57 options := endpointgroups.CreateOpts{ 58 Name: "peers", 59 Type: endpointgroups.TypeCIDR, 60 Endpoints: []string{ 61 "10.2.0.0/24", 62 "10.3.0.0/24", 63 }, 64 } 65 actual, err := endpointgroups.Create(context.TODO(), fake.ServiceClient(), options).Extract() 66 th.AssertNoErr(t, err) 67 expected := endpointgroups.EndpointGroup{ 68 Name: "peers", 69 TenantID: "4ad57e7ce0b24fca8f12b9834d91079d", 70 ProjectID: "4ad57e7ce0b24fca8f12b9834d91079d", 71 ID: "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 72 Description: "", 73 Endpoints: []string{ 74 "10.2.0.0/24", 75 "10.3.0.0/24", 76 }, 77 Type: "cidr", 78 } 79 th.AssertDeepEquals(t, expected, *actual) 80 } 81 82 func TestGet(t *testing.T) { 83 th.SetupHTTP() 84 defer th.TeardownHTTP() 85 86 th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 87 th.TestMethod(t, r, "GET") 88 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 89 90 w.Header().Add("Content-Type", "application/json") 91 w.WriteHeader(http.StatusOK) 92 93 fmt.Fprint(w, ` 94 { 95 "endpoint_group": { 96 "description": "", 97 "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d", 98 "project_id": "4ad57e7ce0b24fca8f12b9834d91079d", 99 "endpoints": [ 100 "10.2.0.0/24", 101 "10.3.0.0/24" 102 ], 103 "type": "cidr", 104 "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 105 "name": "peers" 106 } 107 } 108 `) 109 }) 110 111 actual, err := endpointgroups.Get(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract() 112 th.AssertNoErr(t, err) 113 expected := endpointgroups.EndpointGroup{ 114 Name: "peers", 115 TenantID: "4ad57e7ce0b24fca8f12b9834d91079d", 116 ProjectID: "4ad57e7ce0b24fca8f12b9834d91079d", 117 ID: "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 118 Description: "", 119 Endpoints: []string{ 120 "10.2.0.0/24", 121 "10.3.0.0/24", 122 }, 123 Type: "cidr", 124 } 125 th.AssertDeepEquals(t, expected, *actual) 126 } 127 128 func TestList(t *testing.T) { 129 th.SetupHTTP() 130 defer th.TeardownHTTP() 131 132 th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups", func(w http.ResponseWriter, r *http.Request) { 133 th.TestMethod(t, r, "GET") 134 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 135 w.Header().Add("Content-Type", "application/json") 136 w.WriteHeader(http.StatusOK) 137 138 fmt.Fprint(w, ` 139 { 140 "endpoint_groups": [ 141 { 142 "description": "", 143 "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d", 144 "project_id": "4ad57e7ce0b24fca8f12b9834d91079d", 145 "endpoints": [ 146 "10.2.0.0/24", 147 "10.3.0.0/24" 148 ], 149 "type": "cidr", 150 "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 151 "name": "peers" 152 } 153 ] 154 } 155 `) 156 }) 157 158 count := 0 159 160 err := endpointgroups.List(fake.ServiceClient(), endpointgroups.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 161 count++ 162 actual, err := endpointgroups.ExtractEndpointGroups(page) 163 if err != nil { 164 t.Errorf("Failed to extract members: %v", err) 165 return false, err 166 } 167 expected := []endpointgroups.EndpointGroup{ 168 { 169 Name: "peers", 170 TenantID: "4ad57e7ce0b24fca8f12b9834d91079d", 171 ProjectID: "4ad57e7ce0b24fca8f12b9834d91079d", 172 ID: "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 173 Description: "", 174 Endpoints: []string{ 175 "10.2.0.0/24", 176 "10.3.0.0/24", 177 }, 178 Type: "cidr", 179 }, 180 } 181 th.CheckDeepEquals(t, expected, actual) 182 183 return true, nil 184 }) 185 th.AssertNoErr(t, err) 186 187 if count != 1 { 188 t.Errorf("Expected 1 page, got %d", count) 189 } 190 } 191 192 func TestDelete(t *testing.T) { 193 th.SetupHTTP() 194 defer th.TeardownHTTP() 195 196 th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 197 th.TestMethod(t, r, "DELETE") 198 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 199 w.WriteHeader(http.StatusNoContent) 200 }) 201 202 res := endpointgroups.Delete(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828") 203 th.AssertNoErr(t, res.Err) 204 } 205 206 func TestUpdate(t *testing.T) { 207 th.SetupHTTP() 208 defer th.TeardownHTTP() 209 210 th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 211 th.TestMethod(t, r, "PUT") 212 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 213 th.TestHeader(t, r, "Content-Type", "application/json") 214 th.TestHeader(t, r, "Accept", "application/json") 215 th.TestJSONRequest(t, r, ` 216 { 217 "endpoint_group": { 218 "description": "updated description", 219 "name": "updatedname" 220 } 221 } 222 `) 223 224 w.Header().Add("Content-Type", "application/json") 225 w.WriteHeader(http.StatusOK) 226 227 fmt.Fprint(w, ` 228 { 229 "endpoint_group": { 230 "description": "updated description", 231 "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d", 232 "project_id": "4ad57e7ce0b24fca8f12b9834d91079d", 233 "endpoints": [ 234 "10.2.0.0/24", 235 "10.3.0.0/24" 236 ], 237 "type": "cidr", 238 "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 239 "name": "updatedname" 240 } 241 } 242 `) 243 }) 244 245 updatedName := "updatedname" 246 updatedDescription := "updated description" 247 options := endpointgroups.UpdateOpts{ 248 Name: &updatedName, 249 Description: &updatedDescription, 250 } 251 252 actual, err := endpointgroups.Update(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract() 253 th.AssertNoErr(t, err) 254 expected := endpointgroups.EndpointGroup{ 255 Name: "updatedname", 256 TenantID: "4ad57e7ce0b24fca8f12b9834d91079d", 257 ProjectID: "4ad57e7ce0b24fca8f12b9834d91079d", 258 ID: "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a", 259 Description: "updated description", 260 Endpoints: []string{ 261 "10.2.0.0/24", 262 "10.3.0.0/24", 263 }, 264 Type: "cidr", 265 } 266 th.AssertDeepEquals(t, expected, *actual) 267 }