Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 1 | package defsecrules |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 12 | const ruleID = "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5" |
| 13 | |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 14 | func TestList(t *testing.T) { |
| 15 | th.SetupHTTP() |
| 16 | defer th.TeardownHTTP() |
| 17 | |
| 18 | mockListRulesResponse(t) |
| 19 | |
| 20 | count := 0 |
| 21 | |
| 22 | err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 23 | count++ |
| 24 | actual, err := ExtractDefaultRules(page) |
| 25 | th.AssertNoErr(t, err) |
| 26 | |
| 27 | expected := []DefaultRule{ |
| 28 | DefaultRule{ |
| 29 | FromPort: 80, |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 30 | ID: ruleID, |
Jamie Hannaford | 17d2f87 | 2014-11-24 12:20:33 +0100 | [diff] [blame] | 31 | IPProtocol: "TCP", |
| 32 | IPRange: secgroups.IPRange{CIDR: "10.10.10.0/24"}, |
| 33 | ToPort: 80, |
| 34 | }, |
| 35 | } |
| 36 | |
| 37 | th.CheckDeepEquals(t, expected, actual) |
| 38 | |
| 39 | return true, nil |
| 40 | }) |
| 41 | |
| 42 | th.AssertNoErr(t, err) |
| 43 | th.AssertEquals(t, 1, count) |
| 44 | } |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 45 | |
| 46 | func TestCreate(t *testing.T) { |
| 47 | th.SetupHTTP() |
| 48 | defer th.TeardownHTTP() |
| 49 | |
| 50 | mockCreateRuleResponse(t) |
| 51 | |
| 52 | opts := CreateOpts{ |
| 53 | IPProtocol: "TCP", |
| 54 | FromPort: 80, |
| 55 | ToPort: 80, |
| 56 | CIDR: "10.10.12.0/24", |
| 57 | } |
| 58 | |
| 59 | group, err := Create(client.ServiceClient(), opts).Extract() |
| 60 | th.AssertNoErr(t, err) |
| 61 | |
| 62 | expected := &DefaultRule{ |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 63 | ID: ruleID, |
Jamie Hannaford | 43fa4a2 | 2014-11-24 12:49:17 +0100 | [diff] [blame] | 64 | FromPort: 80, |
| 65 | ToPort: 80, |
| 66 | IPProtocol: "TCP", |
| 67 | IPRange: secgroups.IPRange{CIDR: "10.10.12.0/24"}, |
| 68 | } |
| 69 | th.AssertDeepEquals(t, expected, group) |
| 70 | } |
Jamie Hannaford | 8031b73 | 2014-11-24 12:55:41 +0100 | [diff] [blame] | 71 | |
| 72 | func TestGet(t *testing.T) { |
| 73 | th.SetupHTTP() |
| 74 | defer th.TeardownHTTP() |
| 75 | |
| 76 | mockGetRuleResponse(t, ruleID) |
| 77 | |
| 78 | group, err := Get(client.ServiceClient(), ruleID).Extract() |
| 79 | th.AssertNoErr(t, err) |
| 80 | |
| 81 | expected := &DefaultRule{ |
| 82 | ID: ruleID, |
| 83 | FromPort: 80, |
| 84 | ToPort: 80, |
| 85 | IPProtocol: "TCP", |
| 86 | IPRange: secgroups.IPRange{CIDR: "10.10.12.0/24"}, |
| 87 | } |
| 88 | |
| 89 | th.AssertDeepEquals(t, expected, group) |
| 90 | } |
Jamie Hannaford | 20e9291 | 2014-11-24 13:01:45 +0100 | [diff] [blame^] | 91 | |
| 92 | func TestDelete(t *testing.T) { |
| 93 | th.SetupHTTP() |
| 94 | defer th.TeardownHTTP() |
| 95 | |
| 96 | mockDeleteRuleResponse(t, ruleID) |
| 97 | |
| 98 | err := Delete(client.ServiceClient(), ruleID).ExtractErr() |
| 99 | th.AssertNoErr(t, err) |
| 100 | } |