forked from IQTLabs/dovesnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_test.sh
343 lines (318 loc) · 9.21 KB
/
lib_test.sh
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
#!/bin/bash
reset_ovsid ()
{
OVSID="$(docker ps -q --filter name=ovs)"
}
reset_bridgename ()
{
BRIDGE=""
while [ "$BRIDGE" == "" ] ; do
echo refreshing bridge name
BRIDGE=$(wget -q -O- http://0.0.0.0:9401/networks|jq -r '.[] | select(."NetworkName" == "testnet").BridgeName')
sleep 1
done
}
restart_container ()
{
containerid=$1
echo restarting $containerid
CID=$(docker ps -q --filter name=$containerid)
docker logs $CID
docker restart $CID
docker logs $CID
}
restart_dovesnap ()
{
restart_container dovesnap-plugin
}
restart_ovs ()
{
restart_container ovs
}
restart_wait_dovesnap ()
{
echo waiting for FAUCET config to have testnet mirror port
TESTNETCOUNT=0
while [ "$TESTNETCOUNT" != "1" ] ; do
TESTNETCOUNT=$(sudo grep -c 99: $FAUCET_CONFIG)
sleep 1
done
restart_dovesnap
}
init_dirs()
{
export TMPDIR=$(mktemp -d)
export FAUCET_CONFIG=$TMPDIR/etc/faucet/faucet.yaml
export GAUGE_CONFIG=$TMPDIR/etc/faucet/gauge.yaml
if [ ! -d "$TMPDIR" ] ; then
exit 1
fi
mkdir -p $TMPDIR/etc/faucet
MIRROR_PCAP=$TMPDIR/mirror.cap
sed -i -E 's/version = "([0-9\.]+)"/version = "\1.dev"/g' main.go || exit 1
cd release && ./update_docker_compose.py && cd .. || exit 1
}
clean_dirs()
{
wget -q -O- localhost:9401/networks || exit 1
py=$(which python3)
echo using: $py
sudo "${py}" ./src/dovesnap/graph_dovesnap.py -o /tmp/dovesnapviz || exit 1
./src/dovesnap/cleanup_dovesnap
rm -rf $TMPDIR
VETHS="$(ip link | grep -E ':( ovs-veth|ovp)')"
if [ "$VETHS" != "" ] ; then
echo veths leaked: $VETHS
exit 1
fi
DIEC=$(docker system events --since=15m --until=0m --filter="container=dovesnap-plugin-1" --filter="event=die" | grep -v exitCode=0)
if [ "$DIEC" != "" ] ; then
echo dovesnap exited unexpectedly: $DIEC
exit 1
fi
}
conf_faucet()
{
echo configuring faucet
sudo rm -f $FAUCET_CONFIG
cat >$FAUCET_CONFIG <<EOFC || exit 1
meters:
lossymeter:
meter_id: 1
entry:
flags: "KBPS"
bands: [{type: "DROP", rate: 100}]
acls:
ratelimitit:
- rule:
actions:
meter: lossymeter
allow: 1
allowall:
- rule:
actions:
allow: 1
denyall:
- rule:
actions:
allow: 0
dps:
testnet:
dp_id: 0x1
hardware: Open vSwitch
interfaces:
0xfffffffe:
native_vlan: 100
opstatus_reconf: false
EOFC
}
conf_gauge()
{
echo configuring gauge
cat >$GAUGE_CONFIG <<EOGC || exit 1
faucet_configs:
- '/etc/faucet/faucet.yaml'
watchers:
port_status_poller:
type: 'port_state'
all_dps: True
db: 'prometheus'
port_stats_poller:
type: 'port_stats'
all_dps: True
interval: 30
db: 'prometheus'
dbs:
prometheus:
type: 'prometheus'
prometheus_addr: '0.0.0.0'
prometheus_port: 9303
EOGC
}
conf_keys ()
{
echo creating keys
mkdir -p /opt/faucetconfrpc || exit 1
FAUCET_PREFIX=$TMPDIR docker compose -f docker-compose.yml -f docker-compose-standalone.yml up faucet_certstrap || exit 1
ls -al /opt/faucetconfrpc/faucetconfrpc.key || exit 1
}
wait_faucet ()
{
for p in 6653 6654 9302 59999 ; do
PORTCOUNT=""
while [ "$PORTCOUNT" = "0" ] ; do
echo waiting for $p
PORTCOUNT=$(ss -tHl sport = :$p|grep -c $p)
sleep 1
done
done
echo waiting for frpc
OUTPUT=""
while [ "$OUTPUT" == "" ] ; do
OUTPUT=$(docker ps -q --filter health=healthy --filter name=faucetconfrpc)
sleep 1
done
}
wait_acl ()
{
echo waiting for ACL to be applied
DOVESNAPID="$(docker ps -q --filter name=dovesnap-plugin)"
ACLCOUNT=0
while [ "$ACLCOUNT" != "2" ] ; do
docker logs $DOVESNAPID
sudo cat $FAUCET_CONFIG
ACLCOUNT=$(sudo grep -c ratelimitit $FAUCET_CONFIG)
sleep 1
done
reset_ovsid
reset_bridgename
OUTPUT=""
while [ "$OUTPUT" != "meter" ] ; do
OUTPUT=$(docker exec -t $OVSID ovs-ofctl dump-flows -OOpenFlow13 $BRIDGE table=0|grep -o meter|cat)
echo -n waiting for meter flow in table 0:
docker exec -t $OVSID ovs-ofctl dump-flows -OOpenFlow13 $BRIDGE table=0
sleep 1
done
}
wait_testcon ()
{
DOVESNAPID="$(docker ps -q --filter name=dovesnap-plugin)"
OUTPUT=""
while [ "$OUTPUT" == "" ] ; do
OUTPUT=$(sudo grep "description: /testcon" $FAUCET_CONFIG)
echo waiting for /testcon in $FAUCET_CONFIG
sleep 1
docker logs $DOVESNAPID
done
}
wait_verify_internet ()
{
gwip=$(docker exec -t testcon ip route |grep default|grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
echo testcon gateway: $gwip
dockerip=$(ip address show docker0 |grep -Eo "inet [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"|sed "s/inet //g")
echo docker0 gateway: $dockerip
docker exec -t testcon ping -c3 $gwip
docker exec -t testcon ping -c3 $dockerip
docker network inspect testnet
sudo iptables -t nat -L
ip=$(dig +short azure.archive.ubuntu.com | grep '^[.0-9]*$')
testurl=http://$ip/ubuntu
docker exec -t testcon wget -O/dev/null $testurl || exit 1
}
wait_stack_state ()
{
state=$1
count=$2
FIP=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' dovesnap-faucet-1)
STACKUPCOUNT=0
echo waiting for $count stack ports to be state $state
while [ "$STACKUPCOUNT" != "$count" ] ; do
STACKUPCOUNT=$(wget -q -O- $FIP:9302|grep -Ec "^port_stack_state.+$state.0$")
sleep 1
done
sleep 5
}
wait_push_vlan ()
{
table=$1
inport=$2
reset_ovsid
reset_bridgename
OUTPUT=""
while [ "$OUTPUT" != "push_vlan:" ] ; do
OUTPUT=$(docker exec -t $OVSID ovs-ofctl dump-flows -OOpenFlow13 $BRIDGE in_port=$inport,table=$table|grep -o push_vlan:|cat)
docker exec -t $OVSID ovs-ofctl dump-flows -OOpenFlow13 $BRIDGE table=$table
echo waiting for push vlan rule for port $inport in table $table
sleep 1
done
}
wait_mirror ()
{
table=$1
if [ "$table" == "" ] ; then
table=0
fi
echo waiting for mirror to be applied to config
DOVESNAPID="$(docker ps -q --filter name=dovesnap-plugin)"
MIRRORCOUNT=0
while [ "$MIRRORCOUNT" != "1" ] ; do
docker logs $DOVESNAPID
sudo cat $FAUCET_CONFIG
MIRRORCOUNT=$(sudo grep -c mirror: $FAUCET_CONFIG)
sleep 1
done
reset_bridgename
OUTPUT=""
while [ "$OUTPUT" != "output:" ] ; do
OUTPUT=$(docker exec -t $OVSID ovs-ofctl dump-flows -OOpenFlow13 $BRIDGE table=$table|grep -o output:|cat)
echo waiting for mirror flow in table $table
sleep 1
done
}
init_ovs ()
{
docker compose -f docker-compose.yml up -d ovs || exit 1
reset_ovsid
while ! docker exec -t $OVSID ovs-vsctl show ; do
echo waiting for OVS
sleep 1
done
docker exec -t $OVSID /bin/sh -c 'for i in `ovs-vsctl list-br` ; do ovs-vsctl del-br $i ; done' || exit 1
}
wait_for_container_ip ()
{
i=0
IP=$1
OUT=""
while [ "$OUT" == "" ] && [ "$i" != 30 ] ; do
echo waiting for container IP: $i
OUT=$(docker exec -t testcon ifconfig|grep "inet addr:$IP"|cat)
((i=i+1))
sleep 1
done
if [ "$OUT" == "" ] ; then
echo No IP
DOVESNAPID="$(docker ps -q --filter name=dovesnap-plugin)"
docker logs $DOVESNAPID
exit 1
fi
echo $OUT
}
wait_for_status_container_ip ()
{
i=0
IP=$1
OUT=""
STATUS=""
while [ "$OUT" == "" ] && [ "$i" != 30 ] ; do
echo waiting for status container IP: $i
STATUS=$(wget -q -O- localhost:9401/networks|jq -c)
OUT=$(echo $STATUS|grep HostIP|grep $IP|cat)
((i=i+1))
sleep 1
done
if [ "$OUT" == "" ] ; then
echo No IP
echo $STATUS
exit 1
fi
echo $OUT
}
wait_for_pcap_match ()
{
i=0
OUT=""
sudo chmod go+rx $TMPDIR
while [ "$OUT" == "" ] && [ "$i" != 30 ] ; do
echo waiting for pcap match $PCAPMATCH: $i
sudo chown root $MIRROR_PCAP
OUT=$(sudo tcpdump -n -r $MIRROR_PCAP -v | grep $PCAPMATCH)
((i=i+1))
sleep 1
done
if [ "$OUT" == "" ] ; then
echo $PCAPMATCH not found in pcap
exit 1
fi
echo $PCAPMATCH
}