blob: 1ab66370ca6408f1d57ad82db3080f45766ac6d3 [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
Joe Topjian4f9dce22016-02-28 00:03:37 +000072func TestCreateICMPZero(t *testing.T) {
73 th.SetupHTTP()
74 defer th.TeardownHTTP()
75
76 mockCreateRuleResponseICMPZero(t)
77
78 opts := CreateOpts{
79 IPProtocol: "ICMP",
80 FromPort: 0,
81 ToPort: 0,
82 CIDR: "10.10.12.0/24",
83 }
84
85 group, err := Create(client.ServiceClient(), opts).Extract()
86 th.AssertNoErr(t, err)
87
88 expected := &DefaultRule{
89 ID: ruleID,
90 FromPort: 0,
91 ToPort: 0,
92 IPProtocol: "ICMP",
93 IPRange: secgroups.IPRange{CIDR: "10.10.12.0/24"},
94 }
95 th.AssertDeepEquals(t, expected, group)
96}
97
Jamie Hannaford8031b732014-11-24 12:55:41 +010098func TestGet(t *testing.T) {
99 th.SetupHTTP()
100 defer th.TeardownHTTP()
101
102 mockGetRuleResponse(t, ruleID)
103
104 group, err := Get(client.ServiceClient(), ruleID).Extract()
105 th.AssertNoErr(t, err)
106
107 expected := &DefaultRule{
108 ID: ruleID,
109 FromPort: 80,
110 ToPort: 80,
111 IPProtocol: "TCP",
112 IPRange: secgroups.IPRange{CIDR: "10.10.12.0/24"},
113 }
114
115 th.AssertDeepEquals(t, expected, group)
116}
Jamie Hannaford20e92912014-11-24 13:01:45 +0100117
118func TestDelete(t *testing.T) {
119 th.SetupHTTP()
120 defer th.TeardownHTTP()
121
122 mockDeleteRuleResponse(t, ruleID)
123
124 err := Delete(client.ServiceClient(), ruleID).ExtractErr()
125 th.AssertNoErr(t, err)
126}