Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 6b88f18

Browse files
committed
Merge pull request #2 from jtopjian/icmp-zero-unit-tests
Unit tests for Security Group Rules with ICMP values of 0
2 parents 9920d78 + 4f9dce2 commit 6b88f18

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

openstack/compute/v2/extensions/defsecrules/fixtures.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,41 @@ func mockCreateRuleResponse(t *testing.T) {
7272
})
7373
}
7474

75+
func mockCreateRuleResponseICMPZero(t *testing.T) {
76+
th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
77+
th.TestMethod(t, r, "POST")
78+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
79+
80+
th.TestJSONRequest(t, r, `
81+
{
82+
"security_group_default_rule": {
83+
"ip_protocol": "ICMP",
84+
"from_port": 0,
85+
"to_port": 0,
86+
"cidr": "10.10.12.0/24"
87+
}
88+
}
89+
`)
90+
91+
w.Header().Add("Content-Type", "application/json")
92+
w.WriteHeader(http.StatusOK)
93+
94+
fmt.Fprintf(w, `
95+
{
96+
"security_group_default_rule": {
97+
"from_port": 0,
98+
"id": "{ruleID}",
99+
"ip_protocol": "ICMP",
100+
"ip_range": {
101+
"cidr": "10.10.12.0/24"
102+
},
103+
"to_port": 0
104+
}
105+
}
106+
`)
107+
})
108+
}
109+
75110
func mockGetRuleResponse(t *testing.T, ruleID string) {
76111
url := rootPath + "/" + ruleID
77112
th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {

openstack/compute/v2/extensions/defsecrules/requests_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ func TestCreate(t *testing.T) {
6969
th.AssertDeepEquals(t, expected, group)
7070
}
7171

72+
func 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+
7298
func TestGet(t *testing.T) {
7399
th.SetupHTTP()
74100
defer th.TeardownHTTP()

openstack/compute/v2/extensions/secgroups/fixtures.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,42 @@ func mockAddRuleResponse(t *testing.T) {
216216
})
217217
}
218218

219+
func mockAddRuleResponseICMPZero(t *testing.T) {
220+
th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) {
221+
th.TestMethod(t, r, "POST")
222+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
223+
224+
th.TestJSONRequest(t, r, `
225+
{
226+
"security_group_rule": {
227+
"from_port": 0,
228+
"ip_protocol": "ICMP",
229+
"to_port": 0,
230+
"parent_group_id": "{groupID}",
231+
"cidr": "0.0.0.0/0"
232+
}
233+
} `)
234+
235+
w.Header().Add("Content-Type", "application/json")
236+
w.WriteHeader(http.StatusOK)
237+
238+
fmt.Fprintf(w, `
239+
{
240+
"security_group_rule": {
241+
"from_port": 0,
242+
"group": {},
243+
"ip_protocol": "ICMP",
244+
"to_port": 0,
245+
"parent_group_id": "{groupID}",
246+
"ip_range": {
247+
"cidr": "0.0.0.0/0"
248+
},
249+
"id": "{ruleID}"
250+
}
251+
}`)
252+
})
253+
}
254+
219255
func mockDeleteRuleResponse(t *testing.T, ruleID string) {
220256
url := fmt.Sprintf("/os-security-group-rules/%s", ruleID)
221257
th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {

openstack/compute/v2/extensions/secgroups/requests_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,36 @@ func TestAddRule(t *testing.T) {
217217
th.AssertDeepEquals(t, expected, rule)
218218
}
219219

220+
func TestAddRuleICMPZero(t *testing.T) {
221+
th.SetupHTTP()
222+
defer th.TeardownHTTP()
223+
224+
mockAddRuleResponseICMPZero(t)
225+
226+
opts := CreateRuleOpts{
227+
ParentGroupID: groupID,
228+
FromPort: 0,
229+
ToPort: 0,
230+
IPProtocol: "ICMP",
231+
CIDR: "0.0.0.0/0",
232+
}
233+
234+
rule, err := CreateRule(client.ServiceClient(), opts).Extract()
235+
th.AssertNoErr(t, err)
236+
237+
expected := &Rule{
238+
FromPort: 0,
239+
ToPort: 0,
240+
Group: Group{},
241+
IPProtocol: "ICMP",
242+
ParentGroupID: groupID,
243+
IPRange: IPRange{CIDR: "0.0.0.0/0"},
244+
ID: ruleID,
245+
}
246+
247+
th.AssertDeepEquals(t, expected, rule)
248+
}
249+
220250
func TestDeleteRule(t *testing.T) {
221251
th.SetupHTTP()
222252
defer th.TeardownHTTP()

0 commit comments

Comments
 (0)