Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 1 | package secgroups |
| 2 | |
| 3 | import ( |
| 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 Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 11 | const ( |
| 12 | serverID = "{serverID}" |
| 13 | groupID = "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5" |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 14 | ruleID = "a4070a0f-5383-454c-872d-58c034bc981b" |
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{ |
| 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 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{ |
| 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 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{ |
| 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 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 | |
| 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 Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 131 | func 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 Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 160 | |
| 161 | func 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 Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 170 | |
| 171 | func TestAddRule(t *testing.T) { |
| 172 | th.SetupHTTP() |
| 173 | defer th.TeardownHTTP() |
| 174 | |
| 175 | mockAddRuleResponse(t) |
| 176 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame^] | 177 | opts := CreateRuleOpts{ |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 178 | 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 Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame^] | 185 | rule, err := CreateRule(client.ServiceClient(), opts).Extract() |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 186 | 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 Hannaford | 61f81ca | 2014-11-19 14:44:33 +0100 | [diff] [blame] | 200 | |
| 201 | func 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 Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 210 | |
| 211 | func 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 | |
| 221 | func 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 | } |