github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/vpnaas/ikepolicies/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/ikepolicies" 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/ikepolicies", 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 "ikepolicy":{ 27 "name": "policy", 28 "description": "IKE policy", 29 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 30 "ike_version": "v2" 31 } 32 } 33 `) 34 35 w.Header().Add("Content-Type", "application/json") 36 w.WriteHeader(http.StatusCreated) 37 38 fmt.Fprint(w, ` 39 { 40 "ikepolicy":{ 41 "name": "policy", 42 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 43 "project_id": "9145d91459d248b1b02fdaca97c6a75d", 44 "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 45 "description": "IKE policy", 46 "auth_algorithm": "sha1", 47 "encryption_algorithm": "aes-128", 48 "pfs": "Group5", 49 "lifetime": { 50 "value": 3600, 51 "units": "seconds" 52 }, 53 "phase1_negotiation_mode": "main", 54 "ike_version": "v2" 55 } 56 } 57 `) 58 }) 59 60 options := ikepolicies.CreateOpts{ 61 TenantID: "9145d91459d248b1b02fdaca97c6a75d", 62 Name: "policy", 63 Description: "IKE policy", 64 IKEVersion: ikepolicies.IKEVersionv2, 65 } 66 67 actual, err := ikepolicies.Create(context.TODO(), fake.ServiceClient(), options).Extract() 68 th.AssertNoErr(t, err) 69 expectedLifetime := ikepolicies.Lifetime{ 70 Units: "seconds", 71 Value: 3600, 72 } 73 expected := ikepolicies.Policy{ 74 AuthAlgorithm: "sha1", 75 IKEVersion: "v2", 76 TenantID: "9145d91459d248b1b02fdaca97c6a75d", 77 Phase1NegotiationMode: "main", 78 PFS: "Group5", 79 EncryptionAlgorithm: "aes-128", 80 Description: "IKE policy", 81 Name: "policy", 82 ID: "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 83 Lifetime: expectedLifetime, 84 ProjectID: "9145d91459d248b1b02fdaca97c6a75d", 85 } 86 th.AssertDeepEquals(t, expected, *actual) 87 } 88 89 func TestGet(t *testing.T) { 90 th.SetupHTTP() 91 defer th.TeardownHTTP() 92 93 th.Mux.HandleFunc("/v2.0/vpn/ikepolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 94 th.TestMethod(t, r, "GET") 95 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 96 97 w.Header().Add("Content-Type", "application/json") 98 w.WriteHeader(http.StatusOK) 99 100 fmt.Fprint(w, ` 101 { 102 "ikepolicy":{ 103 "name": "policy", 104 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 105 "project_id": "9145d91459d248b1b02fdaca97c6a75d", 106 "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 107 "description": "IKE policy", 108 "auth_algorithm": "sha1", 109 "encryption_algorithm": "aes-128", 110 "pfs": "Group5", 111 "lifetime": { 112 "value": 3600, 113 "units": "seconds" 114 }, 115 "phase1_negotiation_mode": "main", 116 "ike_version": "v2" 117 } 118 } 119 `) 120 }) 121 122 actual, err := ikepolicies.Get(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract() 123 th.AssertNoErr(t, err) 124 expectedLifetime := ikepolicies.Lifetime{ 125 Units: "seconds", 126 Value: 3600, 127 } 128 expected := ikepolicies.Policy{ 129 AuthAlgorithm: "sha1", 130 IKEVersion: "v2", 131 TenantID: "9145d91459d248b1b02fdaca97c6a75d", 132 ProjectID: "9145d91459d248b1b02fdaca97c6a75d", 133 Phase1NegotiationMode: "main", 134 PFS: "Group5", 135 EncryptionAlgorithm: "aes-128", 136 Description: "IKE policy", 137 Name: "policy", 138 ID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 139 Lifetime: expectedLifetime, 140 } 141 th.AssertDeepEquals(t, expected, *actual) 142 } 143 144 func TestDelete(t *testing.T) { 145 th.SetupHTTP() 146 defer th.TeardownHTTP() 147 148 th.Mux.HandleFunc("/v2.0/vpn/ikepolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 149 th.TestMethod(t, r, "DELETE") 150 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 151 w.WriteHeader(http.StatusNoContent) 152 }) 153 154 res := ikepolicies.Delete(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828") 155 th.AssertNoErr(t, res.Err) 156 } 157 158 func TestList(t *testing.T) { 159 th.SetupHTTP() 160 defer th.TeardownHTTP() 161 162 th.Mux.HandleFunc("/v2.0/vpn/ikepolicies", func(w http.ResponseWriter, r *http.Request) { 163 th.TestMethod(t, r, "GET") 164 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 165 w.Header().Add("Content-Type", "application/json") 166 w.WriteHeader(http.StatusOK) 167 168 fmt.Fprint(w, ` 169 { 170 "ikepolicies": [ 171 { 172 "name": "policy", 173 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 174 "project_id": "9145d91459d248b1b02fdaca97c6a75d", 175 "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 176 "description": "IKE policy", 177 "auth_algorithm": "sha1", 178 "encryption_algorithm": "aes-128", 179 "pfs": "Group5", 180 "lifetime": { 181 "value": 3600, 182 "units": "seconds" 183 }, 184 "phase1_negotiation_mode": "main", 185 "ike_version": "v2" 186 } 187 ] 188 } 189 `) 190 }) 191 192 count := 0 193 194 err := ikepolicies.List(fake.ServiceClient(), ikepolicies.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 195 count++ 196 actual, err := ikepolicies.ExtractPolicies(page) 197 if err != nil { 198 t.Errorf("Failed to extract members: %v", err) 199 return false, err 200 } 201 expectedLifetime := ikepolicies.Lifetime{ 202 Units: "seconds", 203 Value: 3600, 204 } 205 expected := []ikepolicies.Policy{ 206 { 207 AuthAlgorithm: "sha1", 208 IKEVersion: "v2", 209 TenantID: "9145d91459d248b1b02fdaca97c6a75d", 210 ProjectID: "9145d91459d248b1b02fdaca97c6a75d", 211 Phase1NegotiationMode: "main", 212 PFS: "Group5", 213 EncryptionAlgorithm: "aes-128", 214 Description: "IKE policy", 215 Name: "policy", 216 ID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 217 Lifetime: expectedLifetime, 218 }, 219 } 220 221 th.CheckDeepEquals(t, expected, actual) 222 223 return true, nil 224 }) 225 th.AssertNoErr(t, err) 226 227 if count != 1 { 228 t.Errorf("Expected 1 page, got %d", count) 229 } 230 } 231 232 func TestUpdate(t *testing.T) { 233 th.SetupHTTP() 234 defer th.TeardownHTTP() 235 236 th.Mux.HandleFunc("/v2.0/vpn/ikepolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 237 th.TestMethod(t, r, "PUT") 238 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 239 th.TestHeader(t, r, "Content-Type", "application/json") 240 th.TestHeader(t, r, "Accept", "application/json") 241 th.TestJSONRequest(t, r, ` 242 { 243 "ikepolicy":{ 244 "name": "updatedname", 245 "description": "updated policy", 246 "lifetime": { 247 "value": 7000 248 } 249 } 250 } 251 `) 252 253 w.Header().Add("Content-Type", "application/json") 254 w.WriteHeader(http.StatusOK) 255 256 fmt.Fprint(w, ` 257 { 258 "ikepolicy": { 259 "name": "updatedname", 260 "transform_protocol": "esp", 261 "auth_algorithm": "sha1", 262 "encapsulation_mode": "tunnel", 263 "encryption_algorithm": "aes-128", 264 "pfs": "group5", 265 "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b", 266 "project_id": "b4eedccc6fb74fa8a7ad6b08382b852b", 267 "lifetime": { 268 "units": "seconds", 269 "value": 7000 270 }, 271 "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 272 "description": "updated policy" 273 } 274 } 275 `) 276 }) 277 278 updatedName := "updatedname" 279 updatedDescription := "updated policy" 280 options := ikepolicies.UpdateOpts{ 281 Name: &updatedName, 282 Description: &updatedDescription, 283 Lifetime: &ikepolicies.LifetimeUpdateOpts{ 284 Value: 7000, 285 }, 286 } 287 288 actual, err := ikepolicies.Update(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract() 289 th.AssertNoErr(t, err) 290 expectedLifetime := ikepolicies.Lifetime{ 291 Units: "seconds", 292 Value: 7000, 293 } 294 expected := ikepolicies.Policy{ 295 TenantID: "b4eedccc6fb74fa8a7ad6b08382b852b", 296 ProjectID: "b4eedccc6fb74fa8a7ad6b08382b852b", 297 Name: "updatedname", 298 AuthAlgorithm: "sha1", 299 EncryptionAlgorithm: "aes-128", 300 PFS: "group5", 301 Description: "updated policy", 302 Lifetime: expectedLifetime, 303 ID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 304 } 305 th.AssertDeepEquals(t, expected, *actual) 306 }