Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 1 | package secgroups |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud/pagination" |
| 7 | th "github.com/gophercloud/gophercloud/testhelper" |
| 8 | "github.com/gophercloud/gophercloud/testhelper/client" |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 9 | ) |
| 10 | |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 11 | const ( |
| 12 | serverID = "{serverID}" |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 13 | groupID = "{groupID}" |
| 14 | ruleID = "{ruleID}" |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 15 | ) |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 16 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 17 | func 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 Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 35 | ID: groupID, |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 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 Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 51 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 52 | func 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 Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 70 | ID: groupID, |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 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 Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 87 | func 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 Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 102 | ID: groupID, |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 103 | Name: "test", |
| 104 | Description: "something", |
| 105 | TenantID: "openstack", |
| 106 | Rules: []Rule{}, |
| 107 | } |
| 108 | th.AssertDeepEquals(t, expected, group) |
| 109 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 110 | |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 111 | func TestUpdate(t *testing.T) { |
| 112 | th.SetupHTTP() |
| 113 | defer th.TeardownHTTP() |
| 114 | |
| 115 | mockUpdateGroupResponse(t, groupID) |
| 116 | |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 117 | opts := UpdateOpts{ |
| 118 | Name: "new_name", |
| 119 | Description: "new_desc", |
| 120 | } |
| 121 | |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 122 | group, err := Update(client.ServiceClient(), groupID, opts).Extract() |
| 123 | th.AssertNoErr(t, err) |
| 124 | |
| 125 | expected := &SecurityGroup{ |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 126 | ID: groupID, |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 127 | Name: "new_name", |
| 128 | Description: "something", |
| 129 | TenantID: "openstack", |
| 130 | Rules: []Rule{}, |
| 131 | } |
| 132 | th.AssertDeepEquals(t, expected, group) |
| 133 | } |
| 134 | |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 135 | func 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 Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 145 | ID: groupID, |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 146 | 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 Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 156 | ParentGroupID: groupID, |
| 157 | ID: ruleID, |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 158 | }, |
| 159 | }, |
| 160 | } |
| 161 | |
| 162 | th.AssertDeepEquals(t, expected, group) |
| 163 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 164 | |
Jamie Hannaford | cb0c19a | 2014-11-25 11:57:35 +0100 | [diff] [blame] | 165 | func 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 Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 180 | func 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 Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 189 | |
| 190 | func TestAddRule(t *testing.T) { |
| 191 | th.SetupHTTP() |
| 192 | defer th.TeardownHTTP() |
| 193 | |
| 194 | mockAddRuleResponse(t) |
| 195 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 196 | opts := CreateRuleOpts{ |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 197 | ParentGroupID: groupID, |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 198 | FromPort: 22, |
| 199 | ToPort: 22, |
| 200 | IPProtocol: "TCP", |
| 201 | CIDR: "0.0.0.0/0", |
| 202 | } |
| 203 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 204 | rule, err := CreateRule(client.ServiceClient(), opts).Extract() |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 205 | th.AssertNoErr(t, err) |
| 206 | |
| 207 | expected := &Rule{ |
| 208 | FromPort: 22, |
| 209 | ToPort: 22, |
| 210 | Group: Group{}, |
| 211 | IPProtocol: "TCP", |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 212 | ParentGroupID: groupID, |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 213 | IPRange: IPRange{CIDR: "0.0.0.0/0"}, |
Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 214 | ID: ruleID, |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | th.AssertDeepEquals(t, expected, rule) |
| 218 | } |
Jamie Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame] | 219 | |
Joe Topjian | 4f9dce2 | 2016-02-28 00:03:37 +0000 | [diff] [blame] | 220 | func TestAddRuleICMPZero(t *testing.T) { |
| 221 | th.SetupHTTP() |
| 222 | defer th.TeardownHTTP() |
| 223 | |
| 224 | mockAddRuleResponseICMPZero(t) |
| 225 | |
| 226 | opts := CreateRuleOpts{ |
| 227 | ParentGroupID: groupID, |
jrperritt | bc54861 | 2016-04-13 17:03:59 -0500 | [diff] [blame^] | 228 | FromPort: 80, |
| 229 | ToPort: 80, |
Joe Topjian | 4f9dce2 | 2016-02-28 00:03:37 +0000 | [diff] [blame] | 230 | IPProtocol: "ICMP", |
| 231 | CIDR: "0.0.0.0/0", |
| 232 | } |
| 233 | |
| 234 | rule, err := CreateRule(client.ServiceClient(), opts).Extract() |
| 235 | th.AssertNoErr(t, err) |
| 236 | |
| 237 | expected := &Rule{ |
jrperritt | bc54861 | 2016-04-13 17:03:59 -0500 | [diff] [blame^] | 238 | FromPort: 80, |
| 239 | ToPort: 80, |
Joe Topjian | 4f9dce2 | 2016-02-28 00:03:37 +0000 | [diff] [blame] | 240 | Group: Group{}, |
| 241 | IPProtocol: "ICMP", |
| 242 | ParentGroupID: groupID, |
| 243 | IPRange: IPRange{CIDR: "0.0.0.0/0"}, |
| 244 | ID: ruleID, |
| 245 | } |
| 246 | |
| 247 | th.AssertDeepEquals(t, expected, rule) |
| 248 | } |
| 249 | |
Jamie Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame] | 250 | func TestDeleteRule(t *testing.T) { |
| 251 | th.SetupHTTP() |
| 252 | defer th.TeardownHTTP() |
| 253 | |
| 254 | mockDeleteRuleResponse(t, ruleID) |
| 255 | |
| 256 | err := DeleteRule(client.ServiceClient(), ruleID).ExtractErr() |
| 257 | th.AssertNoErr(t, err) |
| 258 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 259 | |
| 260 | func TestAddServer(t *testing.T) { |
| 261 | th.SetupHTTP() |
| 262 | defer th.TeardownHTTP() |
| 263 | |
| 264 | mockAddServerToGroupResponse(t, serverID) |
| 265 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 266 | err := AddServer(client.ServiceClient(), serverID, "test").ExtractErr() |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 267 | th.AssertNoErr(t, err) |
| 268 | } |
| 269 | |
| 270 | func TestRemoveServer(t *testing.T) { |
| 271 | th.SetupHTTP() |
| 272 | defer th.TeardownHTTP() |
| 273 | |
| 274 | mockRemoveServerFromGroupResponse(t, serverID) |
| 275 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 276 | err := RemoveServer(client.ServiceClient(), serverID, "test").ExtractErr() |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 277 | th.AssertNoErr(t, err) |
| 278 | } |