-
Notifications
You must be signed in to change notification settings - Fork 5
/
firewallpolicies_test.go
361 lines (309 loc) · 9.28 KB
/
firewallpolicies_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package oneandone
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
var (
set_fp sync.Once
test_fp_name string
test_fp_desc string
test_fp *FirewallPolicy
)
// Helper functions
func create_firewall_policy() *FirewallPolicy {
rand.Seed(time.Now().UnixNano())
rint := rand.Intn(999)
test_fp_name = fmt.Sprintf("FirewallPol_%d", rint)
test_fp_desc = fmt.Sprintf("FirewallPol_%d description", rint)
req := FirewallPolicyRequest{
Name: test_fp_name,
Description: test_fp_desc,
Rules: []FirewallPolicyRule{
{
Protocol: "UDP",
Port:"161",
Action: "allow",
Description:"test rules",
},
},
}
fmt.Printf("Creating new firewall policy '%s'...\n", test_fp_name)
fp_id, fp, err := api.CreateFirewallPolicy(&req)
if err != nil {
fmt.Printf("Unable to create firewall policy '%s'. Error: %s", test_fp_name, err.Error())
return nil
}
if fp_id == "" || fp.Id == "" {
fmt.Printf("Unable to create firewall policy '%s'.", test_fp_name)
return nil
}
api.WaitForState(fp, "ACTIVE", 10, 30)
return fp
}
func set_firewall_policy() {
test_fp = create_firewall_policy()
}
// /firewall_policies tests
func TestCreateFirewallPolicy(t *testing.T) {
set_fp.Do(set_firewall_policy)
if test_fp == nil {
t.Errorf("CreateFirewallPolicy failed.")
}
if test_fp.Name != test_fp_name {
t.Errorf("Wrong name of the firewall policy.")
}
if test_fp.Description != test_fp_desc {
t.Errorf("Wrong description of the firewall policy.")
}
}
func TestGetFirewallPolicy(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Getting firewall policy '%s'...\n", test_fp.Name)
fp, err := api.GetFirewallPolicy(test_fp.Id)
if err != nil {
t.Errorf("GetFirewallPolicy failed. Error: " + err.Error())
}
if fp.Id != test_fp.Id {
t.Errorf("Wrong ID of the firewall policy.")
}
if fp.Name != test_fp.Name {
t.Errorf("Wrong name of the firewall policy.")
}
if fp.Description != test_fp.Description {
t.Errorf("Wrong description of the firewall policy.")
}
}
func TestListFirewallPolicies(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Println("Listing all firewall policies...")
res, err := api.ListFirewallPolicies()
if err != nil {
t.Errorf("ListFirewallPolicies failed. Error: " + err.Error())
}
if len(res) == 0 {
t.Errorf("No firewall policy found.")
}
res, err = api.ListFirewallPolicies(1, 4, "name", "", "id,name")
if err != nil {
t.Errorf("ListFirewallPolicies with parameter options failed. Error: " + err.Error())
}
if len(res) == 0 {
t.Errorf("No firewall policy found.")
}
// Here we consider two default policies as well, Linux and Windows.
if len(res) < 3 || len(res) > 4 {
t.Errorf("Wrong number of objects per page.")
}
if res[0].Id == "" {
t.Errorf("Filtering parameters failed.")
}
if res[0].Name == "" {
t.Errorf("Filtering parameters failed.")
}
if res[0].State != "" {
t.Errorf("Filtering parameters failed.")
}
if res[0].Name > res[1].Name {
t.Errorf("Sorting parameters failed.")
}
// Test for error response
res, err = api.ListFirewallPolicies("name", 0)
if res != nil || err == nil {
t.Errorf("ListFirewallPolicies failed to handle incorrect argument type.")
}
res, err = api.ListFirewallPolicies(0, 0, "", test_fp.Name, "")
if err != nil {
t.Errorf("ListFirewallPolicies with parameter options failed. Error: " + err.Error())
}
if len(res) != 1 {
t.Errorf("Search parameter failed.")
}
if res[0].Name != test_fp.Name {
t.Errorf("Search parameter failed.")
}
}
func TestAddFirewallPolicyServerIps(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Assigning server IPs to firewall policy '%s'...\n", test_fp.Name)
sync_server.Do(func() { deploy_test_server(false) })
test_server, _ = api.GetServer(test_server.Id)
ips := []string{test_server.Ips[0].Id}
fp, err := api.AddFirewallPolicyServerIps(test_fp.Id, ips)
if err != nil {
t.Errorf("AddFirewallPolicyServerIps failed. Error: " + err.Error())
} else {
api.WaitForState(fp, "ACTIVE", 10, 60)
fp, err = api.GetFirewallPolicy(fp.Id)
if err != nil {
t.Errorf("FirewallPolicyAddServerIps failed. Error: " + err.Error())
}
if len(fp.ServerIps) != 1 {
t.Errorf("Found no server IP attached to the firewall policy.")
}
if fp.ServerIps[0].Id != test_server.Ips[0].Id {
t.Errorf("Wrong server IP attached to the firewall policy.")
}
test_fp = fp
}
}
func TestGetFirewallPolicyServerIp(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Getting server IPs of firewall policy '%s'...\n", test_fp.Name)
fp_ser_ip, err := api.GetFirewallPolicyServerIp(test_fp.Id, test_fp.ServerIps[0].Id)
if err != nil {
t.Errorf("GetFirewallPolicyServerIp failed. Error: " + err.Error())
}
if fp_ser_ip.Id != test_fp.ServerIps[0].Id {
t.Errorf("Wrong ID of the server IP attached to firewall policy '%s'.", test_fp.Name)
}
if fp_ser_ip.Ip != test_fp.ServerIps[0].Ip {
t.Errorf("Wrong server IP address attached to firewall policy '%s'.", test_fp.Name)
}
}
func TestListFirewallPolicyServerIps(t *testing.T) {
set_fp.Do(set_firewall_policy)
sync_server.Do(func() { deploy_test_server(false) })
fmt.Printf("Listing server IPs of firewall policy '%s'...\n", test_fp.Name)
fp_ips, err := api.ListFirewallPolicyServerIps(test_fp.Id)
if err != nil {
t.Errorf("ListFirewallPolicyServerIps failed. Error: " + err.Error())
}
if len(fp_ips) != 1 {
t.Errorf("Wrong number of server IPs added to firewall policy '%s'.", test_fp.Name)
}
if fp_ips[0].Id != test_server.Ips[0].Id {
t.Errorf("Wrong server IP added to firewall policy '%s'.", test_fp.Name)
}
}
func TestGetFirewallPolicyRule(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Getting the rule of firewall policy '%s'...\n", test_fp.Name)
fp_rule, err := api.GetFirewallPolicyRule(test_fp.Id, test_fp.Rules[0].Id)
if err != nil {
t.Errorf("GetFirewallPolicyRule failed. Error: " + err.Error())
}
if fp_rule.Id != test_fp.Rules[0].Id {
t.Errorf("Wrong rule ID.")
}
if *fp_rule.PortFrom != *test_fp.Rules[0].PortFrom {
t.Errorf("Wrong rule port_from field.")
}
if *fp_rule.PortTo != *test_fp.Rules[0].PortTo {
t.Errorf("Wrong rule port_to field.")
}
if fp_rule.Protocol != test_fp.Rules[0].Protocol {
t.Errorf("Wrong rule protocol.")
}
if fp_rule.SourceIp != test_fp.Rules[0].SourceIp {
t.Errorf("Wrong rule source IP.")
}
}
func TestUpdateFirewallPolicy(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Updating firewall policy '%s'...\n", test_fp.Name)
new_name := test_fp.Name + "_updated"
new_desc := test_fp.Description + "_updated"
fp, err := api.UpdateFirewallPolicy(test_fp.Id, new_name, new_desc)
if err != nil {
t.Errorf("UpdateFirewallPolicy failed. Error: " + err.Error())
}
if fp.Id != test_fp.Id {
t.Errorf("Wrong firewall policy ID.")
}
if fp.Name != new_name {
t.Errorf("Wrong firewall policy name.")
}
if fp.Description != new_desc {
t.Errorf("Wrong firewall policy description.")
}
test_fp = fp
}
func TestAddFirewallPolicyRules(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Adding rules to firewall policy '%s'...\n", test_fp.Name)
rules := []FirewallPolicyRule{
{
Protocol: "TCP",
Port:"4567",
Action:"allow",
SourceIp: "0.0.0.0",
},
{
Protocol: "TCP/UDP",
Port:"143",
Action:"allow",
},
{
Protocol: "GRE", // PortFrom & PortTo are optional for GRE, ICMP and IPSEC protocols.
},
{
Protocol: "ICMP",
PortFrom: nil,
PortTo: nil,
},
{
Protocol: "IPSEC",
},
}
fp, err := api.AddFirewallPolicyRules(test_fp.Id, rules)
if err != nil {
t.Errorf("AddFirewallPolicyRules failed. Error: " + err.Error())
} else {
api.WaitForState(fp, "ACTIVE", 10, 60)
}
fp, _ = api.GetFirewallPolicy(fp.Id)
if len(fp.Rules) != 6 {
t.Errorf("Unable to add rules to firewall policy '%s'.\n", test_fp.Name)
}
}
func TestListFirewallPolicyRules(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Listing firewall policy '%s' rules...\n", test_fp.Name)
fp_rules, err := api.ListFirewallPolicyRules(test_fp.Id)
if err != nil {
t.Errorf("ListFirewallPolicyRules failed. Error: " + err.Error())
}
if len(fp_rules) != 6 {
t.Errorf("Wrong number of rules found at firewall policy '%s'.", test_fp.Name)
}
}
func TestDeleteFirewallPolicyRule(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Deleting rule '%s' from firewall policy '%s'...\n", test_fp.Rules[0].Id, test_fp.Name)
fp, err := api.DeleteFirewallPolicyRule(test_fp.Id, test_fp.Rules[0].Id)
if err != nil {
t.Errorf("DeleteFirewallPolicyRule failed. Error: " + err.Error())
}
api.WaitForState(fp, "ACTIVE", 10, 60)
fp, err = api.GetFirewallPolicy(fp.Id)
if err != nil {
t.Errorf("Deleting rule from the firewall policy failed.")
}
if len(fp.Rules) != 5 {
t.Errorf("Rule not deleted from the firewall policy.")
}
for _, rule := range fp.Rules {
if rule.Id == test_fp.Rules[0].Id {
t.Errorf("Rule not deleted from the firewall policy.")
}
}
}
func TestDeleteFirewallPolicy(t *testing.T) {
set_fp.Do(set_firewall_policy)
fmt.Printf("Deleting firewall policy '%s'...\n", test_fp.Name)
fp, err := api.DeleteFirewallPolicy(test_fp.Id)
if err != nil {
t.Errorf("DeleteFirewallPolicy failed. Error: " + err.Error())
} else {
api.WaitUntilDeleted(fp)
}
fp, _ = api.GetFirewallPolicy(fp.Id)
if fp != nil {
t.Errorf("Unable to delete the firewall policy.")
} else {
test_fp = nil
}
}