blob: 311fc4d885b3cb5386017f4b592d9ba2e7e4ffec [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
8 "github.com/rackspace/gophercloud/testhelper/client"
9)
10
Jamie Hannaford19151792014-11-19 12:46:47 +010011const serverID = "{serverID}"
12
Jamie Hannaford924c09d2014-11-19 12:05:38 +010013func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 mockListGroupsResponse(t)
18
19 count := 0
20
21 err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
22 count++
23 actual, err := ExtractSecurityGroups(page)
24 if err != nil {
25 t.Errorf("Failed to extract users: %v", err)
26 return false, err
27 }
28
29 expected := []SecurityGroup{
30 SecurityGroup{
31 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
32 Description: "default",
33 Name: "default",
34 Rules: []Rule{},
35 TenantID: "openstack",
36 },
37 }
38
39 th.CheckDeepEquals(t, expected, actual)
40
41 return true, nil
42 })
43
44 th.AssertNoErr(t, err)
45 th.AssertEquals(t, 1, count)
46}
Jamie Hannaforda493e642014-11-19 12:40:30 +010047
Jamie Hannaford19151792014-11-19 12:46:47 +010048func TestListByServer(t *testing.T) {
49 th.SetupHTTP()
50 defer th.TeardownHTTP()
51
52 mockListGroupsByServerResponse(t, serverID)
53
54 count := 0
55
56 err := ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) {
57 count++
58 actual, err := ExtractSecurityGroups(page)
59 if err != nil {
60 t.Errorf("Failed to extract users: %v", err)
61 return false, err
62 }
63
64 expected := []SecurityGroup{
65 SecurityGroup{
66 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
67 Description: "default",
68 Name: "default",
69 Rules: []Rule{},
70 TenantID: "openstack",
71 },
72 }
73
74 th.CheckDeepEquals(t, expected, actual)
75
76 return true, nil
77 })
78
79 th.AssertNoErr(t, err)
80 th.AssertEquals(t, 1, count)
81}
82
Jamie Hannaforda493e642014-11-19 12:40:30 +010083func TestCreate(t *testing.T) {
84 th.SetupHTTP()
85 defer th.TeardownHTTP()
86
87 mockCreateGroupResponse(t)
88
89 opts := CreateOpts{
90 Name: "test",
91 Description: "something",
92 }
93
94 group, err := Create(client.ServiceClient(), opts).Extract()
95 th.AssertNoErr(t, err)
96
97 expected := &SecurityGroup{
98 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
99 Name: "test",
100 Description: "something",
101 TenantID: "openstack",
102 Rules: []Rule{},
103 }
104 th.AssertDeepEquals(t, expected, group)
105}