Skip to content

Commit 94fc098

Browse files
authored
Networking V2: add QoS policies acceptance tests (#1603)
* Networking V2: add QoS policy Update call Implement QoS policy update call. * Networking V2: add QoS policy Delete call Implement QoS policy delete method. * Networking V2: add QoS policies acceptance tests Add TestPoliciesCRUD acceptance test and CreateQoSPolicy, DeleteQoSPolicy helpers. * Networking V2: use 200 code for Update policy call Use StatusOK for Update QoS policy call.
1 parent b8b6f3e commit 94fc098

9 files changed

Lines changed: 236 additions & 41 deletions

File tree

acceptance/openstack/networking/v2/extensions/qos/policies/policies.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,16 @@ func CreateQoSPolicy(t *testing.T, client *gophercloud.ServiceClient) (*policies
3434

3535
return policy, nil
3636
}
37+
38+
// DeleteQoSPolicy will delete a QoS policy with a specified ID.
39+
// A fatal error will occur if the delete was not successful.
40+
func DeleteQoSPolicy(t *testing.T, client *gophercloud.ServiceClient, policyID string) {
41+
t.Logf("Attempting to delete the QoS policy: %s", policyID)
42+
43+
err := policies.Delete(client, policyID).ExtractErr()
44+
if err != nil {
45+
t.Fatalf("Unable to delete QoS policy %s: %v", policyID, err)
46+
}
47+
48+
t.Logf("Deleted QoS policy: %s", policyID)
49+
}

acceptance/openstack/networking/v2/extensions/qos/policies/policies_test.go

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,53 @@ package policies
44

55
import (
66
"testing"
7+
8+
"github.com/gophercloud/gophercloud/acceptance/clients"
9+
"github.com/gophercloud/gophercloud/acceptance/tools"
10+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/policies"
11+
th "github.com/gophercloud/gophercloud/testhelper"
712
)
813

914
func TestPoliciesCRUD(t *testing.T) {
10-
//client, err := clients.NewNetworkV2Client()
11-
//th.AssertNoErr(t, err)
12-
13-
// Create a QoS policy
14-
//policy, err := CreatePolicy(t, client)
15-
//th.AssertNoErr(t, err)
16-
//defer DeletePolicy(t, client, policy.ID)
17-
//
18-
//tools.PrintResource(t, policy)
19-
//
20-
//newName := tools.RandomString("TESTACC-", 8)
21-
//newDescription := ""
22-
//updateOpts := &policies.UpdateOpts{
23-
// Name: newName,
24-
// Description: &newDescription,
25-
//}
26-
//
27-
//_, err = policies.Update(client, policy.ID, updateOpts).Extract()
28-
//th.AssertNoErr(t, err)
29-
30-
//newPolicy, err := policies.Get(client, policy.ID).Extract()
31-
//th.AssertNoErr(t, err)
32-
//
33-
//tools.PrintResource(t, newPolicy)
34-
//th.AssertEquals(t, newPolicy.Name, newName)
35-
//th.AssertEquals(t, newPolicy.Description, newDescription)
36-
//
37-
//allPages, err := policies.List(client, nil).AllPages()
38-
//th.AssertNoErr(t, err)
39-
//
40-
//allPolicies, err := policies.ExtractPolicies(allPages)
41-
//th.AssertNoErr(t, err)
42-
//
43-
//var found bool
44-
//for _, policy := range allPolicies {
45-
// if policy.ID == newPolicy.ID {
46-
// found = true
47-
// }
48-
//}
49-
//
50-
//th.AssertEquals(t, found, true)
15+
client, err := clients.NewNetworkV2Client()
16+
th.AssertNoErr(t, err)
17+
18+
// Create a QoS policy.
19+
policy, err := CreateQoSPolicy(t, client)
20+
th.AssertNoErr(t, err)
21+
defer DeleteQoSPolicy(t, client, policy.ID)
22+
23+
tools.PrintResource(t, policy)
24+
25+
newName := tools.RandomString("TESTACC-", 8)
26+
newDescription := ""
27+
updateOpts := &policies.UpdateOpts{
28+
Name: newName,
29+
Description: &newDescription,
30+
}
31+
32+
_, err = policies.Update(client, policy.ID, updateOpts).Extract()
33+
th.AssertNoErr(t, err)
34+
35+
newPolicy, err := policies.Get(client, policy.ID).Extract()
36+
th.AssertNoErr(t, err)
37+
38+
tools.PrintResource(t, newPolicy)
39+
th.AssertEquals(t, newPolicy.Name, newName)
40+
th.AssertEquals(t, newPolicy.Description, newDescription)
41+
42+
allPages, err := policies.List(client, nil).AllPages()
43+
th.AssertNoErr(t, err)
44+
45+
allPolicies, err := policies.ExtractPolicies(allPages)
46+
th.AssertNoErr(t, err)
47+
48+
var found bool
49+
for _, policy := range allPolicies {
50+
if policy.ID == newPolicy.ID {
51+
found = true
52+
}
53+
}
54+
55+
th.AssertEquals(t, found, true)
5156
}

openstack/networking/v2/extensions/qos/policies/doc.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,33 @@ Example to Create a QoS policy
225225
}
226226
227227
fmt.Printf("%+v\n", policy)
228+
229+
Example to Update a QoS policy
230+
231+
shared := true
232+
isDefault := false
233+
opts := policies.UpdateOpts{
234+
Name: "new-name",
235+
Shared: &shared,
236+
IsDefault: &isDefault,
237+
}
238+
239+
policyID := "30a57f4a-336b-4382-8275-d708babd2241"
240+
241+
policy, err := policies.Update(networkClient, policyID, opts).Extract()
242+
if err != nil {
243+
panic(err)
244+
}
245+
246+
fmt.Printf("%+v\n", policy)
247+
248+
Example to Delete a QoS policy
249+
250+
policyID := "30a57f4a-336b-4382-8275-d708babd2241"
251+
252+
err := policies.Delete(networkClient, policyID).ExtractErr()
253+
if err != nil {
254+
panic(err)
255+
}
228256
*/
229257
package policies

openstack/networking/v2/extensions/qos/policies/requests.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,49 @@ func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r Create
219219
})
220220
return
221221
}
222+
223+
// UpdateOptsBuilder allows extensions to add additional parameters to the
224+
// Update request.
225+
type UpdateOptsBuilder interface {
226+
ToPolicyUpdateMap() (map[string]interface{}, error)
227+
}
228+
229+
// UpdateOpts represents options used to update a QoS policy.
230+
type UpdateOpts struct {
231+
// Name is the human-readable name of the QoS policy.
232+
Name string `json:"name,omitempty"`
233+
234+
// Shared indicates whether this QoS policy is shared across all projects.
235+
Shared *bool `json:"shared,omitempty"`
236+
237+
// Description is the human-readable description for the QoS policy.
238+
Description *string `json:"description,omitempty"`
239+
240+
// IsDefault indicates if this QoS policy is default policy or not.
241+
IsDefault *bool `json:"is_default,omitempty"`
242+
}
243+
244+
// ToPolicyUpdateMap builds a request body from UpdateOpts.
245+
func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
246+
return gophercloud.BuildRequestBody(opts, "policy")
247+
}
248+
249+
// Update accepts a UpdateOpts struct and updates an existing policy using the
250+
// values provided.
251+
func Update(c *gophercloud.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) {
252+
b, err := opts.ToPolicyUpdateMap()
253+
if err != nil {
254+
r.Err = err
255+
return
256+
}
257+
_, r.Err = c.Put(updateURL(c, policyID), b, &r.Body, &gophercloud.RequestOpts{
258+
OkCodes: []int{200},
259+
})
260+
return
261+
}
262+
263+
// Delete accepts a unique ID and deletes the QoS policy associated with it.
264+
func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
265+
_, r.Err = c.Delete(deleteURL(c, id), nil)
266+
return
267+
}

openstack/networking/v2/extensions/qos/policies/results.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ type CreateResult struct {
2929
commonResult
3030
}
3131

32+
// UpdateResult represents the result of a Create operation. Call its Extract
33+
// method to interpret it as a QoS policy.
34+
type UpdateResult struct {
35+
commonResult
36+
}
37+
38+
// DeleteResult represents the result of a delete operation. Call its
39+
// ExtractErr method to determine if the request succeeded or failed.
40+
type DeleteResult struct {
41+
gophercloud.ErrResult
42+
}
43+
3244
// Extract is a function that accepts a result and extracts a QoS policy resource.
3345
func (r commonResult) Extract() (*Policy, error) {
3446
var s struct {

openstack/networking/v2/extensions/qos/policies/testing/fixtures.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,32 @@ const CreatePolicyResponse = `
273273
}
274274
}
275275
`
276+
277+
const UpdatePolicyRequest = `
278+
{
279+
"policy": {
280+
"name": "new-name",
281+
"shared": true,
282+
"description": ""
283+
}
284+
}
285+
`
286+
287+
const UpdatePolicyResponse = `
288+
{
289+
"policy": {
290+
"name": "new-name",
291+
"tags": [],
292+
"rules": [],
293+
"tenant_id": "a77cbe0998374aed9a6798ad6c61677e",
294+
"created_at": "2019-05-19T11:17:50Z",
295+
"updated_at": "2019-06-01T13:17:57Z",
296+
"is_default": false,
297+
"revision_number": 1,
298+
"shared": true,
299+
"project_id": "a77cbe0998374aed9a6798ad6c61677e",
300+
"id": "d6ae28ce-fcb5-4180-aa62-d260a27e09ae",
301+
"description": ""
302+
}
303+
}
304+
`

openstack/networking/v2/extensions/qos/policies/testing/requests_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,56 @@ func TestCreatePolicy(t *testing.T) {
409409
th.AssertEquals(t, 0, p.RevisionNumber)
410410
th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID)
411411
}
412+
413+
func TestUpdatePolicy(t *testing.T) {
414+
th.SetupHTTP()
415+
defer th.TeardownHTTP()
416+
417+
th.Mux.HandleFunc("/v2.0/qos/policies/d6ae28ce-fcb5-4180-aa62-d260a27e09ae", func(w http.ResponseWriter, r *http.Request) {
418+
th.TestMethod(t, r, "PUT")
419+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
420+
th.TestHeader(t, r, "Content-Type", "application/json")
421+
th.TestHeader(t, r, "Accept", "application/json")
422+
th.TestJSONRequest(t, r, UpdatePolicyRequest)
423+
424+
w.Header().Add("Content-Type", "application/json")
425+
w.WriteHeader(http.StatusOK)
426+
427+
fmt.Fprintf(w, UpdatePolicyResponse)
428+
})
429+
430+
shared := true
431+
description := ""
432+
opts := policies.UpdateOpts{
433+
Name: "new-name",
434+
Shared: &shared,
435+
Description: &description,
436+
}
437+
p, err := policies.Update(fake.ServiceClient(), "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", opts).Extract()
438+
th.AssertNoErr(t, err)
439+
440+
th.AssertEquals(t, "new-name", p.Name)
441+
th.AssertEquals(t, true, p.Shared)
442+
th.AssertEquals(t, false, p.IsDefault)
443+
th.AssertEquals(t, "", p.Description)
444+
th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID)
445+
th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID)
446+
th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt)
447+
th.AssertEquals(t, time.Date(2019, 6, 1, 13, 17, 57, 0, time.UTC), p.UpdatedAt)
448+
th.AssertEquals(t, 1, p.RevisionNumber)
449+
th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID)
450+
}
451+
452+
func TestDeletePolicy(t *testing.T) {
453+
th.SetupHTTP()
454+
defer th.TeardownHTTP()
455+
456+
th.Mux.HandleFunc("/v2.0/qos/policies/d6ae28ce-fcb5-4180-aa62-d260a27e09ae", func(w http.ResponseWriter, r *http.Request) {
457+
th.TestMethod(t, r, "DELETE")
458+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
459+
w.WriteHeader(http.StatusNoContent)
460+
})
461+
462+
res := policies.Delete(fake.ServiceClient(), "d6ae28ce-fcb5-4180-aa62-d260a27e09ae")
463+
th.AssertNoErr(t, res.Err)
464+
}

openstack/networking/v2/extensions/qos/policies/urls.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ func getURL(c *gophercloud.ServiceClient, id string) string {
2323
func createURL(c *gophercloud.ServiceClient) string {
2424
return rootURL(c)
2525
}
26+
27+
func updateURL(c *gophercloud.ServiceClient, id string) string {
28+
return resourceURL(c, id)
29+
}
30+
31+
func deleteURL(c *gophercloud.ServiceClient, id string) string {
32+
return resourceURL(c, id)
33+
}

script/acceptancetest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ acceptance/openstack/networking/v2/extensions/layer3
6363
acceptance/openstack/networking/v2/extensions/mtu
6464
acceptance/openstack/networking/v2/extensions/networkipavailabilities
6565
acceptance/openstack/networking/v2/extensions/portsbinding
66+
acceptance/openstack/networking/v2/extensions/qos/policies
6667
acceptance/openstack/networking/v2/extensions/qos/ruletypes
6768
acceptance/openstack/networking/v2/extensions/rbacpolicies
6869
acceptance/openstack/networking/v2/extensions/subnetpools

0 commit comments

Comments
 (0)