1+ #! /bin/bash
2+
3+ # WARNING: this tests rely on log messages of helloworld and
4+ # transparenthelloworld services, changing them may cause this tests to
5+ # fail
6+
7+ source " ${BASH_SOURCE%/* } /helpers.bash"
8+
9+ LOG_SIZE=50
10+
11+ # Expected sequences of log messages for a packet flowing from ns1 to ns2 and
12+ # from ns2 to ns1.
13+ # Sequences are in reversed order.
14+ seq_1to2=(" [hw2] [EGRESS]"
15+ " [hw2] Forwarding"
16+ " [hw2] [INGRESS]"
17+ " [th3] [EGRESS]"
18+ " [hw1] Forwarding"
19+ " [hw1] [INGRESS]"
20+ " [th2] [INGRESS]"
21+ " [th1] [INGRESS]"
22+ )
23+ seq_2to1=(" [th1] [EGRESS]"
24+ " [hw1] [EGRESS]"
25+ " [th2] [EGRESS]"
26+ " [hw1] Forwarding"
27+ " [hw1] [INGRESS]"
28+ " [th3] [INGRESS]"
29+ " [hw2] Forwarding"
30+ " [hw2] [INGRESS]"
31+ )
32+
33+ # Expected sequence of log messages for a packet flowing out of the network
34+ # stack.
35+ # Sequence is in reversed order.
36+ seq_stack_egress=(" [th1] [EGRESS]"
37+ " [hw1] [EGRESS]" )
38+
39+ function cleanup {
40+ set +e
41+
42+ polycubectl hw1 del
43+ polycubectl hw2 del
44+ polycubectl th1 del
45+ polycubectl th2 del
46+ polycubectl th3 del
47+
48+ delete_veth 2
49+
50+ echo " FAIL"
51+ }
52+
53+ # Retrieves the last LOG_SIZE lines of polycubed log, output in string $1
54+ function get_log {
55+ local -n output=$1
56+
57+ # Check if polycubed is running into a container
58+ if [ -z " $container " ]; then
59+ # Retrieve log from tmp file
60+ output=$( tail -n $LOG_SIZE tmp)
61+ else
62+ # Retrieve the log from the stdout of the container
63+ output=$( docker logs --tail $LOG_SIZE $container )
64+ fi
65+ }
66+
67+ # Reverses the log $1 and extracts only inportant parts, output in array $2
68+ function process_log {
69+ local -n output=$2
70+ output=()
71+ IFS_BACKUP=$IFS
72+ while IFS= read -r line; do
73+ IFS=" "
74+ read -ra arr <<< " $line"
75+ log_line=" ${arr[3]} ${arr[5]} "
76+ output=(" $log_line " " ${output[@]} " )
77+ done <<< " $1"
78+ IFS=$IFS_BACKUP
79+ }
80+
81+ # Looks for sequence $1 in array $2, returns 0 if found, 1 otherwise
82+ function find_sequence {
83+ local -n seq=$1
84+ local -n arr=$2
85+ local ret=1
86+ local i=0
87+ for line in " ${arr[@]} " ; do
88+ if [ " $line " = " ${seq[i]} " ]; then
89+ (( i= i+ 1 ))
90+ if [ $i -eq ${# seq[@]} ]; then
91+ ret=0
92+ break
93+ fi
94+
95+ elif [ $i -gt 0 ]; then
96+ break
97+ fi
98+ done
99+
100+ return $ret
101+ }
102+
103+
104+ # Tests whether ingress and egress programs in a chain of cubes are executed
105+ # correcly
106+ # Cubes chain:
107+ # +-----+ +-----+-----+-----+ +-----+
108+ # veth1-| th1 |--------| th2 | hw1 | th2 |--------| hw2 |-------- veth2
109+ # +-----+ +-----+-----+-----+ +-----+
110+ # th = TransparentHelloworld
111+ # hw = HelloWorld
112+ # $1: xdp_skb | tc
113+ function test_programs_chain {
114+ trap cleanup EXIT
115+
116+ create_veth_no_ipv6 2
117+
118+ set -e
119+ set -x
120+
121+ polycubectl helloworld add hw1 loglevel=trace type=$1
122+ polycubectl hw1 set action=forward
123+ polycubectl hw1 ports add to_veth1 peer=veth1
124+ polycubectl hw1 ports add to_hw2
125+
126+ polycubectl helloworld add hw2 loglevel=trace type=$1
127+ polycubectl hw2 set action=forward
128+ polycubectl hw2 ports add to_hw1
129+ polycubectl hw2 ports add to_veth2 peer=veth2
130+
131+ polycubectl connect hw1:to_hw2 hw2:to_hw1
132+
133+ polycubectl transparenthelloworld add th1 loglevel=trace type=$1
134+ polycubectl attach th1 veth1
135+
136+ polycubectl transparenthelloworld add th2 loglevel=trace type=$1
137+ polycubectl attach th2 hw1:to_veth1
138+
139+ polycubectl transparenthelloworld add th3 loglevel=trace type=$1
140+ polycubectl attach th3 hw1:to_hw2
141+
142+ set +x
143+
144+ sudo ip netns exec ns1 ping 10.0.0.2 -c 1 1> /dev/null 2>&1
145+
146+ get_log log
147+
148+ process_log " $log " plog
149+
150+ find_sequence seq_1to2 plog
151+ find_sequence seq_2to1 plog
152+
153+ # Cleanup
154+ set -x
155+ polycubectl hw1 del
156+ polycubectl hw2 del
157+ polycubectl th1 del
158+ polycubectl th2 del
159+ polycubectl th3 del
160+ set +x
161+ delete_veth 2
162+ trap - EXIT
163+
164+ echo " SUCCESS"
165+ }
166+
167+ # Tests whether egress programs are executed correcly with packets flowing out
168+ # of the network stack of the host.
169+ # Cubes chain:
170+ # +-----+ +-----+-----+
171+ # veth1-| th1 |--------| th2 | hw1 |
172+ # +-----+ +-----+-----+
173+ # th = TransparentHelloworld
174+ # hw = HelloWorld
175+ # $1: xdp_skb | tc
176+ function test_stack_programs_chain {
177+ trap cleanup EXIT
178+
179+ create_veth_no_ipv6 1
180+
181+ set -e
182+ set -x
183+
184+ sudo ip addr add 10.0.0.2/24 dev veth1
185+
186+ polycubectl helloworld add hw1 loglevel=trace type=$1
187+ polycubectl hw1 ports add to_veth1 peer=veth1
188+
189+ polycubectl transparenthelloworld add th1 loglevel=trace type=$1
190+ polycubectl attach th1 veth1
191+
192+ polycubectl transparenthelloworld add th2 loglevel=trace type=$1
193+ polycubectl attach th2 hw1:to_veth1
194+
195+ set +x
196+
197+ # This command will fail since the echo reply is dropped by hw1 cube
198+ ping 10.0.0.1 -c 1 1> /dev/null 2>&1 || true
199+
200+ get_log log
201+
202+ process_log " $log " plog
203+
204+ find_sequence seq_stack_egress plog
205+
206+ # Cleanup
207+ set -x
208+ polycubectl hw1 del
209+ polycubectl th1 del
210+ polycubectl th2 del
211+ set +x
212+ delete_veth 1
213+ trap - EXIT
214+
215+ echo " SUCCESS"
216+ }
0 commit comments