blob: 4e21d5deaa142831aaa70cf8e07578e4a1587719 [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}"
Jamie Hannaford2f226172014-11-25 11:52:25 +010013 groupID = "{groupID}"
14 ruleID = "{ruleID}"
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{
Jamie Hannaford558572f2014-11-24 14:31:57 +010035 ID: groupID,
Jamie Hannaford924c09d2014-11-19 12:05:38 +010036 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{
Jamie Hannaford558572f2014-11-24 14:31:57 +010070 ID: groupID,
Jamie Hannaford19151792014-11-19 12:46:47 +010071 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{
Jamie Hannaford558572f2014-11-24 14:31:57 +0100102 ID: groupID,
Jamie Hannaforda493e642014-11-19 12:40:30 +0100103 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
Jamie Hannaford30c74662014-11-19 15:37:34 +0100111func TestUpdate(t *testing.T) {
112 th.SetupHTTP()
113 defer th.TeardownHTTP()
114
115 mockUpdateGroupResponse(t, groupID)
116
Jamie Hannaford0e750962014-11-24 16:04:38 +0100117 opts := UpdateOpts{
118 Name: "new_name",
119 Description: "new_desc",
120 }
121
Jamie Hannaford30c74662014-11-19 15:37:34 +0100122 group, err := Update(client.ServiceClient(), groupID, opts).Extract()
123 th.AssertNoErr(t, err)
124
125 expected := &SecurityGroup{
Jamie Hannaford558572f2014-11-24 14:31:57 +0100126 ID: groupID,
Jamie Hannaford30c74662014-11-19 15:37:34 +0100127 Name: "new_name",
128 Description: "something",
129 TenantID: "openstack",
130 Rules: []Rule{},
131 }
132 th.AssertDeepEquals(t, expected, group)
133}
134
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100135func TestGet(t *testing.T) {
136 th.SetupHTTP()
137 defer th.TeardownHTTP()
138
139 mockGetGroupsResponse(t, groupID)
140
141 group, err := Get(client.ServiceClient(), groupID).Extract()
142 th.AssertNoErr(t, err)
143
144 expected := &SecurityGroup{
Jamie Hannaford558572f2014-11-24 14:31:57 +0100145 ID: groupID,
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100146 Description: "default",
147 Name: "default",
148 TenantID: "openstack",
149 Rules: []Rule{
150 Rule{
151 FromPort: 80,
152 ToPort: 85,
153 IPProtocol: "TCP",
154 IPRange: IPRange{CIDR: "0.0.0.0"},
155 Group: Group{TenantID: "openstack", Name: "default"},
Jamie Hannaford558572f2014-11-24 14:31:57 +0100156 ParentGroupID: groupID,
157 ID: ruleID,
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100158 },
159 },
160 }
161
162 th.AssertDeepEquals(t, expected, group)
163}
Jamie Hannafordd276e612014-11-19 13:56:28 +0100164
Jamie Hannafordcb0c19a2014-11-25 11:57:35 +0100165func TestGetNumericID(t *testing.T) {
166 th.SetupHTTP()
167 defer th.TeardownHTTP()
168
169 numericGroupID := 12345
170
171 mockGetNumericIDGroupResponse(t, numericGroupID)
172
173 group, err := Get(client.ServiceClient(), "12345").Extract()
174 th.AssertNoErr(t, err)
175
176 expected := &SecurityGroup{ID: "12345"}
177 th.AssertDeepEquals(t, expected, group)
178}
179
Jamie Hannafordd276e612014-11-19 13:56:28 +0100180func TestDelete(t *testing.T) {
181 th.SetupHTTP()
182 defer th.TeardownHTTP()
183
184 mockDeleteGroupResponse(t, groupID)
185
186 err := Delete(client.ServiceClient(), groupID).ExtractErr()
187 th.AssertNoErr(t, err)
188}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100189
190func TestAddRule(t *testing.T) {
191 th.SetupHTTP()
192 defer th.TeardownHTTP()
193
194 mockAddRuleResponse(t)
195
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100196 opts := CreateRuleOpts{
Jamie Hannaford558572f2014-11-24 14:31:57 +0100197 ParentGroupID: groupID,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100198 FromPort: 22,
199 ToPort: 22,
200 IPProtocol: "TCP",
201 CIDR: "0.0.0.0/0",
202 }
203
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100204 rule, err := CreateRule(client.ServiceClient(), opts).Extract()
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100205 th.AssertNoErr(t, err)
206
207 expected := &Rule{
208 FromPort: 22,
209 ToPort: 22,
210 Group: Group{},
211 IPProtocol: "TCP",
Jamie Hannaford558572f2014-11-24 14:31:57 +0100212 ParentGroupID: groupID,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100213 IPRange: IPRange{CIDR: "0.0.0.0/0"},
Jamie Hannaford558572f2014-11-24 14:31:57 +0100214 ID: ruleID,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100215 }
216
217 th.AssertDeepEquals(t, expected, rule)
218}
Jamie Hannaford61f81ca2014-11-19 14:44:33 +0100219
220func TestDeleteRule(t *testing.T) {
221 th.SetupHTTP()
222 defer th.TeardownHTTP()
223
224 mockDeleteRuleResponse(t, ruleID)
225
226 err := DeleteRule(client.ServiceClient(), ruleID).ExtractErr()
227 th.AssertNoErr(t, err)
228}
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100229
230func TestAddServer(t *testing.T) {
231 th.SetupHTTP()
232 defer th.TeardownHTTP()
233
234 mockAddServerToGroupResponse(t, serverID)
235
236 err := AddServerToGroup(client.ServiceClient(), serverID, "test").ExtractErr()
237 th.AssertNoErr(t, err)
238}
239
240func TestRemoveServer(t *testing.T) {
241 th.SetupHTTP()
242 defer th.TeardownHTTP()
243
244 mockRemoveServerFromGroupResponse(t, serverID)
245
246 err := RemoveServerFromGroup(client.ServiceClient(), serverID, "test").ExtractErr()
247 th.AssertNoErr(t, err)
248}