blob: f59471f0a26b1deb0c3c99c58d42e471fc9c788e [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 Hannafordb38dd312014-11-19 13:02:11 +010011const (
12 serverID = "{serverID}"
13 groupID = "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5"
Jamie Hannaford8badf1e2014-11-19 14:39:26 +010014 ruleID = "a4070a0f-5383-454c-872d-58c034bc981b"
Jamie Hannafordb38dd312014-11-19 13:02:11 +010015)
Jamie Hannaford19151792014-11-19 12:46:47 +010016
Jamie Hannaford924c09d2014-11-19 12:05:38 +010017func TestList(t *testing.T) {
18 th.SetupHTTP()
19 defer th.TeardownHTTP()
20
21 mockListGroupsResponse(t)
22
23 count := 0
24
25 err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
26 count++
27 actual, err := ExtractSecurityGroups(page)
28 if err != nil {
29 t.Errorf("Failed to extract users: %v", err)
30 return false, err
31 }
32
33 expected := []SecurityGroup{
34 SecurityGroup{
35 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
36 Description: "default",
37 Name: "default",
38 Rules: []Rule{},
39 TenantID: "openstack",
40 },
41 }
42
43 th.CheckDeepEquals(t, expected, actual)
44
45 return true, nil
46 })
47
48 th.AssertNoErr(t, err)
49 th.AssertEquals(t, 1, count)
50}
Jamie Hannaforda493e642014-11-19 12:40:30 +010051
Jamie Hannaford19151792014-11-19 12:46:47 +010052func TestListByServer(t *testing.T) {
53 th.SetupHTTP()
54 defer th.TeardownHTTP()
55
56 mockListGroupsByServerResponse(t, serverID)
57
58 count := 0
59
60 err := ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) {
61 count++
62 actual, err := ExtractSecurityGroups(page)
63 if err != nil {
64 t.Errorf("Failed to extract users: %v", err)
65 return false, err
66 }
67
68 expected := []SecurityGroup{
69 SecurityGroup{
70 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
71 Description: "default",
72 Name: "default",
73 Rules: []Rule{},
74 TenantID: "openstack",
75 },
76 }
77
78 th.CheckDeepEquals(t, expected, actual)
79
80 return true, nil
81 })
82
83 th.AssertNoErr(t, err)
84 th.AssertEquals(t, 1, count)
85}
86
Jamie Hannaforda493e642014-11-19 12:40:30 +010087func TestCreate(t *testing.T) {
88 th.SetupHTTP()
89 defer th.TeardownHTTP()
90
91 mockCreateGroupResponse(t)
92
93 opts := CreateOpts{
94 Name: "test",
95 Description: "something",
96 }
97
98 group, err := Create(client.ServiceClient(), opts).Extract()
99 th.AssertNoErr(t, err)
100
101 expected := &SecurityGroup{
102 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
103 Name: "test",
104 Description: "something",
105 TenantID: "openstack",
106 Rules: []Rule{},
107 }
108 th.AssertDeepEquals(t, expected, group)
109}
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100110
111func TestGet(t *testing.T) {
112 th.SetupHTTP()
113 defer th.TeardownHTTP()
114
115 mockGetGroupsResponse(t, groupID)
116
117 group, err := Get(client.ServiceClient(), groupID).Extract()
118 th.AssertNoErr(t, err)
119
120 expected := &SecurityGroup{
121 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
122 Description: "default",
123 Name: "default",
124 TenantID: "openstack",
125 Rules: []Rule{
126 Rule{
127 FromPort: 80,
128 ToPort: 85,
129 IPProtocol: "TCP",
130 IPRange: IPRange{CIDR: "0.0.0.0"},
131 Group: Group{TenantID: "openstack", Name: "default"},
132 ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
133 ID: "ebe599e2-6b8c-457c-b1ff-a75e48f10923",
134 },
135 },
136 }
137
138 th.AssertDeepEquals(t, expected, group)
139}
Jamie Hannafordd276e612014-11-19 13:56:28 +0100140
141func TestDelete(t *testing.T) {
142 th.SetupHTTP()
143 defer th.TeardownHTTP()
144
145 mockDeleteGroupResponse(t, groupID)
146
147 err := Delete(client.ServiceClient(), groupID).ExtractErr()
148 th.AssertNoErr(t, err)
149}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100150
151func TestAddRule(t *testing.T) {
152 th.SetupHTTP()
153 defer th.TeardownHTTP()
154
155 mockAddRuleResponse(t)
156
157 opts := AddRuleOpts{
158 ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
159 FromPort: 22,
160 ToPort: 22,
161 IPProtocol: "TCP",
162 CIDR: "0.0.0.0/0",
163 }
164
165 rule, err := AddRule(client.ServiceClient(), opts).Extract()
166 th.AssertNoErr(t, err)
167
168 expected := &Rule{
169 FromPort: 22,
170 ToPort: 22,
171 Group: Group{},
172 IPProtocol: "TCP",
173 ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
174 IPRange: IPRange{CIDR: "0.0.0.0/0"},
175 ID: "f9a97fcf-3a97-47b0-b76f-919136afb7ed",
176 }
177
178 th.AssertDeepEquals(t, expected, rule)
179}