blob: f2f6eb479fb235ae0c2c926955c66e534a67ce3e [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
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
117 opts := UpdateOpts{Name: "new_name"}
118 group, err := Update(client.ServiceClient(), groupID, opts).Extract()
119 th.AssertNoErr(t, err)
120
121 expected := &SecurityGroup{
122 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
123 Name: "new_name",
124 Description: "something",
125 TenantID: "openstack",
126 Rules: []Rule{},
127 }
128 th.AssertDeepEquals(t, expected, group)
129}
130
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100131func TestGet(t *testing.T) {
132 th.SetupHTTP()
133 defer th.TeardownHTTP()
134
135 mockGetGroupsResponse(t, groupID)
136
137 group, err := Get(client.ServiceClient(), groupID).Extract()
138 th.AssertNoErr(t, err)
139
140 expected := &SecurityGroup{
141 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
142 Description: "default",
143 Name: "default",
144 TenantID: "openstack",
145 Rules: []Rule{
146 Rule{
147 FromPort: 80,
148 ToPort: 85,
149 IPProtocol: "TCP",
150 IPRange: IPRange{CIDR: "0.0.0.0"},
151 Group: Group{TenantID: "openstack", Name: "default"},
152 ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
153 ID: "ebe599e2-6b8c-457c-b1ff-a75e48f10923",
154 },
155 },
156 }
157
158 th.AssertDeepEquals(t, expected, group)
159}
Jamie Hannafordd276e612014-11-19 13:56:28 +0100160
161func TestDelete(t *testing.T) {
162 th.SetupHTTP()
163 defer th.TeardownHTTP()
164
165 mockDeleteGroupResponse(t, groupID)
166
167 err := Delete(client.ServiceClient(), groupID).ExtractErr()
168 th.AssertNoErr(t, err)
169}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100170
171func TestAddRule(t *testing.T) {
172 th.SetupHTTP()
173 defer th.TeardownHTTP()
174
175 mockAddRuleResponse(t)
176
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100177 opts := CreateRuleOpts{
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100178 ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
179 FromPort: 22,
180 ToPort: 22,
181 IPProtocol: "TCP",
182 CIDR: "0.0.0.0/0",
183 }
184
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100185 rule, err := CreateRule(client.ServiceClient(), opts).Extract()
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100186 th.AssertNoErr(t, err)
187
188 expected := &Rule{
189 FromPort: 22,
190 ToPort: 22,
191 Group: Group{},
192 IPProtocol: "TCP",
193 ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
194 IPRange: IPRange{CIDR: "0.0.0.0/0"},
195 ID: "f9a97fcf-3a97-47b0-b76f-919136afb7ed",
196 }
197
198 th.AssertDeepEquals(t, expected, rule)
199}
Jamie Hannaford61f81ca2014-11-19 14:44:33 +0100200
201func TestDeleteRule(t *testing.T) {
202 th.SetupHTTP()
203 defer th.TeardownHTTP()
204
205 mockDeleteRuleResponse(t, ruleID)
206
207 err := DeleteRule(client.ServiceClient(), ruleID).ExtractErr()
208 th.AssertNoErr(t, err)
209}
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100210
211func TestAddServer(t *testing.T) {
212 th.SetupHTTP()
213 defer th.TeardownHTTP()
214
215 mockAddServerToGroupResponse(t, serverID)
216
217 err := AddServerToGroup(client.ServiceClient(), serverID, "test").ExtractErr()
218 th.AssertNoErr(t, err)
219}
220
221func TestRemoveServer(t *testing.T) {
222 th.SetupHTTP()
223 defer th.TeardownHTTP()
224
225 mockRemoveServerFromGroupResponse(t, serverID)
226
227 err := RemoveServerFromGroup(client.ServiceClient(), serverID, "test").ExtractErr()
228 th.AssertNoErr(t, err)
229}