forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegress_router_test.go
241 lines (230 loc) · 6.01 KB
/
egress_router_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
package egress_router_test
import (
"fmt"
"os/exec"
"strings"
"testing"
)
func TestEgressRouter(t *testing.T) {
tests := []struct {
source string
gateway string
dest string
output string
}{
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "10.1.2.3",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4/24",
gateway: "1.1.1.1",
dest: "10.1.2.3",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "10.1.2.3",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "10.1.2.3\n",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "80 tcp 10.4.5.6",
output: `
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "8080 tcp 10.7.8.9 80",
output: `
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "80 tcp 10.4.5.6\n8080 tcp 10.7.8.9 80",
output: `
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "80 tcp 10.4.5.6\n8080 tcp 10.7.8.9 80\n10.1.2.3",
output: `
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: `
# My egress-router rules
# Port 80 forwards to 10.4.5.6
80 tcp 10.4.5.6
# Port 8080 forwards to port 80 on 10.7.8.9
8080 tcp 10.7.8.9 80
# Skip this rule for now
# 8443 tcp 10.7.8.9 443
# Add new rules here
# Fallback; don't add anything after this
10.1.2.3
# No, seriously, don't add anything here
`,
output: `
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
}
for n, test := range tests {
cmd := exec.Command("./egress-router.sh")
cmd.Env = []string{
"EGRESS_ROUTER_MODE=unit-test",
fmt.Sprintf("EGRESS_SOURCE=%s", test.source),
fmt.Sprintf("EGRESS_GATEWAY=%s", test.gateway),
fmt.Sprintf("EGRESS_DESTINATION=%s", test.dest),
}
out, err := cmd.CombinedOutput()
expected := test.output[1:]
if err != nil {
t.Fatalf("test %d expected output %q but got error %v", n+1, expected, err)
}
if string(out) != expected {
t.Fatalf("test %d expected output %q but got %q", n+1, expected, string(out))
}
}
}
func TestEgressRouterBad(t *testing.T) {
tests := []struct {
source string
gateway string
dest string
err string
}{
{
source: "not an IP address",
gateway: "1.1.1.1",
dest: "10.1.2.3",
err: "EGRESS_SOURCE unspecified or invalid",
},
{
source: "not\nan\nIP\naddress",
gateway: "1.1.1.1",
dest: "10.1.2.3",
err: "EGRESS_SOURCE unspecified or invalid",
},
{
source: "1.2.3.4",
gateway: "not an IP address",
dest: "10.1.2.3",
err: "EGRESS_GATEWAY unspecified or invalid",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "",
err: "EGRESS_DESTINATION unspecified",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "not an IP address",
err: "EGRESS_DESTINATION value 'not an IP address' is invalid",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "10.1.2.3\n80 tcp 10.4.5.6",
err: "EGRESS_DESTINATION fallback IP must be the last line",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "10.1.2.3\n10.4.5.6",
err: "EGRESS_DESTINATION fallback IP must be the last line",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "80 tcp 10.4.5.6\n10.1.2.3\n8080 tcp 10.7.8.9 80",
err: "EGRESS_DESTINATION fallback IP must be the last line",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "80 tcp 10.4.5.6\ninvalid\n8080 tcp 10.7.8.9 80",
err: "EGRESS_DESTINATION value 'invalid' is invalid",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "80 sctp 10.4.5.6",
err: "EGRESS_DESTINATION value '80 sctp 10.4.5.6' is invalid",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "800000 tcp 10.4.5.6",
err: "Invalid port: 800000, must be in the range 1 to 65535",
},
{
source: "1.2.3.4",
gateway: "1.1.1.1",
dest: "80 tcp 10.4.5.6\n8080 tcp 10.7.8.9 80\n80 tcp 10.7.8.9 900",
err: "EGRESS_DESTINATION localport 80 is already used, must be unique for each destination",
},
}
for n, test := range tests {
cmd := exec.Command("./egress-router.sh")
cmd.Env = []string{
"EGRESS_ROUTER_MODE=unit-test",
fmt.Sprintf("EGRESS_SOURCE=%s", test.source),
fmt.Sprintf("EGRESS_GATEWAY=%s", test.gateway),
fmt.Sprintf("EGRESS_DESTINATION=%s", test.dest),
}
out, err := cmd.CombinedOutput()
out_lines := strings.Split(string(out), "\n")
got := out_lines[len(out_lines)-2]
if err == nil {
t.Fatalf("test %d expected error %q but got output %q", n+1, test.err, got)
}
if got != test.err {
t.Fatalf("test %d expected output %q but got %q", n+1, test.err, got)
}
}
}