-
Notifications
You must be signed in to change notification settings - Fork 59
/
logical_router_policy_test.go
234 lines (210 loc) · 5.63 KB
/
logical_router_policy_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
package goovn
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
LR6 = "lr6"
PRIORITY = 10
LR_POLICY_MATCH_2 = "ip4.src == 3.3.3.0/24"
ACTION_DROP = "drop"
ACTION_ALLOW = "allow"
ACTION_REROUTE = "reroute"
)
var (
LR_POLICY_MATCH_1 = "ip4.src == 1.1.1.0/24"
)
func TestLogicalRouterPolicy(t *testing.T) {
ovndbapi := getOVNClient(DBNB)
cmd, err := ovndbapi.LRAdd(LR6, nil)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
t.Logf("Add router %s Done", LR6)
// verify router create
lrs, err := ovndbapi.LRGet(LR6)
if err != nil {
t.Fatal(err)
}
if len(lrs) != 1 {
t.Fatalf("lr not created %v", lrs)
}
cmd, err = ovndbapi.LRPolicyAdd(LR6, PRIORITY, LR_POLICY_MATCH_1, ACTION_DROP, nil, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Adding policy to LR6 failed with err %v", err)
}
// verify policy addition to LR6
lrpolicy, err := ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
if len(lrpolicy) == 0 {
t.Fatalf("policy %s not created in %s", LR_POLICY_MATCH_1, LR6)
}
assert.Equal(t, true, lrpolicy[0].Match == LR_POLICY_MATCH_1, "Added policy to LR6")
// add another policy
cmd, err = ovndbapi.LRPolicyAdd(LR6, PRIORITY, LR_POLICY_MATCH_2, ACTION_ALLOW, nil, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Adding policy to LR6 failed with err %v", err)
}
// verify policy addition to LR6
lrpolicy, err = ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
if len(lrpolicy) < 2 {
t.Fatalf("policy %s not created in %s", LR_POLICY_MATCH_2, LR6)
}
found := false
var secondPolicy *LogicalRouterPolicy
for _, p := range lrpolicy {
if p.Match == LR_POLICY_MATCH_2 {
found = true
secondPolicy = p
}
}
assert.Equal(t, true, found, "Added second policy to LR6")
// delete policy
cmd, err = ovndbapi.LRPolicyDelByUUID(LR6, secondPolicy.UUID)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Deleting policy from LR6 via %s failed with err %v", LR_POLICY_MATCH_2, err)
}
t.Logf("Deleted policy %s from LRouter %s", LR_POLICY_MATCH_2, LR6)
// verify policy via delete from LR6
lrpolicy, err = ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
found = false
for _, p := range lrpolicy {
if p.Match == LR_POLICY_MATCH_2 {
found = true
}
}
assert.Equal(t, false, found, "Deleted second policy from LR6")
// Delete the policy first policy from LR6
cmd, err = ovndbapi.LRPolicyDel(LR6, PRIORITY, &LR_POLICY_MATCH_1)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Deleting policy from LR6 failed with err %v", err)
}
t.Logf("Deleting policy %s from LRouter %s Done", LR_POLICY_MATCH_1, LR6)
// verify policy delete from LR6
lrpolicy, err = ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
if len(lrpolicy) != 0 {
t.Fatalf("policy %s not deleted in %s", LR_POLICY_MATCH_1, LR6)
}
assert.Equal(t, true, len(lrpolicy) == 0, "Deleted policy from LR6")
// Add re-route policy
nexthop := "4.4.4.4"
cmd, err = ovndbapi.LRPolicyAdd(LR6, PRIORITY, LR_POLICY_MATCH_1, ACTION_REROUTE, &nexthop, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Adding policy to LR6 failed with err %v", err)
}
lrpolicy, err = ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
if len(lrpolicy) < 1 {
t.Fatalf("policy %s with nexthop %s not created in %s", LR_POLICY_MATCH_1, nexthop, LR6)
}
assert.Equal(t, nexthop, *lrpolicy[0].Nexthop)
assert.Equal(t, LR_POLICY_MATCH_1, lrpolicy[0].Match)
// Delete the policy by priority
cmd, err = ovndbapi.LRPolicyDel(LR6, PRIORITY, nil)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Deleting policy from LR6 failed with err %v", err)
}
t.Logf("Deleting policy %s from LRouter %s Done", LR_POLICY_MATCH_1, LR6)
lrpolicy, err = ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, true, len(lrpolicy) == 0, "Deleted policy from LR6")
// once more
cmd, err = ovndbapi.LRPolicyAdd(LR6, PRIORITY, LR_POLICY_MATCH_1, ACTION_DROP, nil, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Adding policy to LR6 failed with err %v", err)
}
// verify policy addition to LR6
lrpolicy, err = ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
if len(lrpolicy) == 0 {
t.Fatalf("policy %s not created in %s", LR_POLICY_MATCH_1, LR6)
}
assert.Equal(t, true, lrpolicy[0].Match == LR_POLICY_MATCH_1, "Added policy to LR6")
// delete all policies on the LR
cmd, err = ovndbapi.LRPolicyDelAll(LR6)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatalf("Deleting policy from LR6 failed with err %v", err)
}
t.Logf("Deleting policy %s from LRouter %s Done", LR_POLICY_MATCH_1, LR6)
lrpolicy, err = ovndbapi.LRPolicyList(LR6)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, true, len(lrpolicy) == 0, "Deleted policy from LR6")
// Delete the router
cmd, err = ovndbapi.LRDel(LR6)
if err != nil {
t.Fatal(err)
}
err = ovndbapi.Execute(cmd)
if err != nil {
t.Fatal(err)
}
t.Logf("Delete router %s Done", LR6)
// verify router delete
lrs, err = ovndbapi.LRGet(LR6)
if err != nil {
t.Fatal(err)
}
if len(lrs) != 0 {
t.Fatalf("lr not deleted %v", lrs)
}
// verify policy list for non-existing routers
_, err = ovndbapi.LRPolicyList(FAKENOROUTER)
if err != nil {
assert.EqualError(t, ErrorNotFound, err.Error())
}
}