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 | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame^] | 11 | const serverID = "{serverID}" |
| 12 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 13 | func TestList(t *testing.T) { |
| 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
| 16 | |
| 17 | mockListGroupsResponse(t) |
| 18 | |
| 19 | count := 0 |
| 20 | |
| 21 | err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 22 | count++ |
| 23 | actual, err := ExtractSecurityGroups(page) |
| 24 | if err != nil { |
| 25 | t.Errorf("Failed to extract users: %v", err) |
| 26 | return false, err |
| 27 | } |
| 28 | |
| 29 | expected := []SecurityGroup{ |
| 30 | SecurityGroup{ |
| 31 | ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 32 | Description: "default", |
| 33 | Name: "default", |
| 34 | Rules: []Rule{}, |
| 35 | TenantID: "openstack", |
| 36 | }, |
| 37 | } |
| 38 | |
| 39 | th.CheckDeepEquals(t, expected, actual) |
| 40 | |
| 41 | return true, nil |
| 42 | }) |
| 43 | |
| 44 | th.AssertNoErr(t, err) |
| 45 | th.AssertEquals(t, 1, count) |
| 46 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 47 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame^] | 48 | func TestListByServer(t *testing.T) { |
| 49 | th.SetupHTTP() |
| 50 | defer th.TeardownHTTP() |
| 51 | |
| 52 | mockListGroupsByServerResponse(t, serverID) |
| 53 | |
| 54 | count := 0 |
| 55 | |
| 56 | err := ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) { |
| 57 | count++ |
| 58 | actual, err := ExtractSecurityGroups(page) |
| 59 | if err != nil { |
| 60 | t.Errorf("Failed to extract users: %v", err) |
| 61 | return false, err |
| 62 | } |
| 63 | |
| 64 | expected := []SecurityGroup{ |
| 65 | SecurityGroup{ |
| 66 | ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 67 | Description: "default", |
| 68 | Name: "default", |
| 69 | Rules: []Rule{}, |
| 70 | TenantID: "openstack", |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | th.CheckDeepEquals(t, expected, actual) |
| 75 | |
| 76 | return true, nil |
| 77 | }) |
| 78 | |
| 79 | th.AssertNoErr(t, err) |
| 80 | th.AssertEquals(t, 1, count) |
| 81 | } |
| 82 | |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 83 | func TestCreate(t *testing.T) { |
| 84 | th.SetupHTTP() |
| 85 | defer th.TeardownHTTP() |
| 86 | |
| 87 | mockCreateGroupResponse(t) |
| 88 | |
| 89 | opts := CreateOpts{ |
| 90 | Name: "test", |
| 91 | Description: "something", |
| 92 | } |
| 93 | |
| 94 | group, err := Create(client.ServiceClient(), opts).Extract() |
| 95 | th.AssertNoErr(t, err) |
| 96 | |
| 97 | expected := &SecurityGroup{ |
| 98 | ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5", |
| 99 | Name: "test", |
| 100 | Description: "something", |
| 101 | TenantID: "openstack", |
| 102 | Rules: []Rule{}, |
| 103 | } |
| 104 | th.AssertDeepEquals(t, expected, group) |
| 105 | } |