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" |
| 14 | ) |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 15 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 16 | func TestList(t *testing.T) { |
| 17 | th.SetupHTTP() |
| 18 | defer th.TeardownHTTP() |
| 19 | |
| 20 | mockListGroupsResponse(t) |
| 21 | |
| 22 | count := 0 |
| 23 | |
| 24 | err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 25 | count++ |
| 26 | actual, err := ExtractSecurityGroups(page) |
| 27 | if err != nil { |
| 28 | t.Errorf("Failed to extract users: %v", err) |
| 29 | return false, err |
| 30 | } |
| 31 | |
| 32 | expected := []SecurityGroup{ |
| 33 | SecurityGroup{ |
| 34 | ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 35 | Description: "default", |
| 36 | Name: "default", |
| 37 | Rules: []Rule{}, |
| 38 | TenantID: "openstack", |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | th.CheckDeepEquals(t, expected, actual) |
| 43 | |
| 44 | return true, nil |
| 45 | }) |
| 46 | |
| 47 | th.AssertNoErr(t, err) |
| 48 | th.AssertEquals(t, 1, count) |
| 49 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 50 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 51 | func TestListByServer(t *testing.T) { |
| 52 | th.SetupHTTP() |
| 53 | defer th.TeardownHTTP() |
| 54 | |
| 55 | mockListGroupsByServerResponse(t, serverID) |
| 56 | |
| 57 | count := 0 |
| 58 | |
| 59 | err := ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) { |
| 60 | count++ |
| 61 | actual, err := ExtractSecurityGroups(page) |
| 62 | if err != nil { |
| 63 | t.Errorf("Failed to extract users: %v", err) |
| 64 | return false, err |
| 65 | } |
| 66 | |
| 67 | expected := []SecurityGroup{ |
| 68 | SecurityGroup{ |
| 69 | ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 70 | Description: "default", |
| 71 | Name: "default", |
| 72 | Rules: []Rule{}, |
| 73 | TenantID: "openstack", |
| 74 | }, |
| 75 | } |
| 76 | |
| 77 | th.CheckDeepEquals(t, expected, actual) |
| 78 | |
| 79 | return true, nil |
| 80 | }) |
| 81 | |
| 82 | th.AssertNoErr(t, err) |
| 83 | th.AssertEquals(t, 1, count) |
| 84 | } |
| 85 | |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 86 | func TestCreate(t *testing.T) { |
| 87 | th.SetupHTTP() |
| 88 | defer th.TeardownHTTP() |
| 89 | |
| 90 | mockCreateGroupResponse(t) |
| 91 | |
| 92 | opts := CreateOpts{ |
| 93 | Name: "test", |
| 94 | Description: "something", |
| 95 | } |
| 96 | |
| 97 | group, err := Create(client.ServiceClient(), opts).Extract() |
| 98 | th.AssertNoErr(t, err) |
| 99 | |
| 100 | expected := &SecurityGroup{ |
| 101 | ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 102 | Name: "test", |
| 103 | Description: "something", |
| 104 | TenantID: "openstack", |
| 105 | Rules: []Rule{}, |
| 106 | } |
| 107 | th.AssertDeepEquals(t, expected, group) |
| 108 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame^] | 109 | |
| 110 | func TestGet(t *testing.T) { |
| 111 | th.SetupHTTP() |
| 112 | defer th.TeardownHTTP() |
| 113 | |
| 114 | mockGetGroupsResponse(t, groupID) |
| 115 | |
| 116 | group, err := Get(client.ServiceClient(), groupID).Extract() |
| 117 | th.AssertNoErr(t, err) |
| 118 | |
| 119 | expected := &SecurityGroup{ |
| 120 | ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 121 | Description: "default", |
| 122 | Name: "default", |
| 123 | TenantID: "openstack", |
| 124 | Rules: []Rule{ |
| 125 | Rule{ |
| 126 | FromPort: 80, |
| 127 | ToPort: 85, |
| 128 | IPProtocol: "TCP", |
| 129 | IPRange: IPRange{CIDR: "0.0.0.0"}, |
| 130 | Group: Group{TenantID: "openstack", Name: "default"}, |
| 131 | ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 132 | ID: "ebe599e2-6b8c-457c-b1ff-a75e48f10923", |
| 133 | }, |
| 134 | }, |
| 135 | } |
| 136 | |
| 137 | th.AssertDeepEquals(t, expected, group) |
| 138 | } |