blob: d4ebe87c564171b006e85b22949b143eec4801d0 [file] [log] [blame]
Jamie Hannaford17d2f872014-11-24 12:20:33 +01001package defsecrules
2
3import (
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 Hannaford2f226172014-11-25 11:52:25 +010012const ruleID = "{ruleID}"
Jamie Hannaford8031b732014-11-24 12:55:41 +010013
Jamie Hannaford17d2f872014-11-24 12:20:33 +010014func 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 Hannaford8031b732014-11-24 12:55:41 +010030 ID: ruleID,
Jamie Hannaford17d2f872014-11-24 12:20:33 +010031 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 Hannaford43fa4a22014-11-24 12:49:17 +010045
46func 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 Hannaford8031b732014-11-24 12:55:41 +010063 ID: ruleID,
Jamie Hannaford43fa4a22014-11-24 12:49:17 +010064 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 Hannaford8031b732014-11-24 12:55:41 +010071
72func 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 Hannaford20e92912014-11-24 13:01:45 +010091
92func 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}