blob: 667db598b0efbee3c359c62000e095ad0547ebec [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 Hannaford558572f2014-11-24 14:31:57 +010013 groupID = 1
14 ruleID = 2
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
165func TestDelete(t *testing.T) {
166 th.SetupHTTP()
167 defer th.TeardownHTTP()
168
169 mockDeleteGroupResponse(t, groupID)
170
171 err := Delete(client.ServiceClient(), groupID).ExtractErr()
172 th.AssertNoErr(t, err)
173}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100174
175func TestAddRule(t *testing.T) {
176 th.SetupHTTP()
177 defer th.TeardownHTTP()
178
179 mockAddRuleResponse(t)
180
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100181 opts := CreateRuleOpts{
Jamie Hannaford558572f2014-11-24 14:31:57 +0100182 ParentGroupID: groupID,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100183 FromPort: 22,
184 ToPort: 22,
185 IPProtocol: "TCP",
186 CIDR: "0.0.0.0/0",
187 }
188
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100189 rule, err := CreateRule(client.ServiceClient(), opts).Extract()
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100190 th.AssertNoErr(t, err)
191
192 expected := &Rule{
193 FromPort: 22,
194 ToPort: 22,
195 Group: Group{},
196 IPProtocol: "TCP",
Jamie Hannaford558572f2014-11-24 14:31:57 +0100197 ParentGroupID: groupID,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100198 IPRange: IPRange{CIDR: "0.0.0.0/0"},
Jamie Hannaford558572f2014-11-24 14:31:57 +0100199 ID: ruleID,
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100200 }
201
202 th.AssertDeepEquals(t, expected, rule)
203}
Jamie Hannaford61f81ca2014-11-19 14:44:33 +0100204
205func TestDeleteRule(t *testing.T) {
206 th.SetupHTTP()
207 defer th.TeardownHTTP()
208
209 mockDeleteRuleResponse(t, ruleID)
210
211 err := DeleteRule(client.ServiceClient(), ruleID).ExtractErr()
212 th.AssertNoErr(t, err)
213}
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100214
215func TestAddServer(t *testing.T) {
216 th.SetupHTTP()
217 defer th.TeardownHTTP()
218
219 mockAddServerToGroupResponse(t, serverID)
220
221 err := AddServerToGroup(client.ServiceClient(), serverID, "test").ExtractErr()
222 th.AssertNoErr(t, err)
223}
224
225func TestRemoveServer(t *testing.T) {
226 th.SetupHTTP()
227 defer th.TeardownHTTP()
228
229 mockRemoveServerFromGroupResponse(t, serverID)
230
231 err := RemoveServerFromGroup(client.ServiceClient(), serverID, "test").ExtractErr()
232 th.AssertNoErr(t, err)
233}