fix(rtable): Prevent direct send hairpins
[ric-plt/lib/rmr.git] / test / app_test / run_app_test.ksh
1 #!/usr/bin/env ksh
2 # :vi ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019 Nokia
5 #    Copyright (c) 2018-2019 AT&T Intellectual Property.
6 #
7 #   Licensed under the Apache License, Version 2.0 (the "License");
8 #   you may not use this file except in compliance with the License.
9 #   You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #   Unless required by applicable law or agreed to in writing, software
14 #   distributed under the License is distributed on an "AS IS" BASIS,
15 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #   See the License for the specific language governing permissions and
17 #   limitations under the License.
18 #==================================================================================
19 #
20
21 # ---------------------------------------------------------------------------------
22 #       Mnemonic:       run_app_test.ksh
23 #       Abstract:       This is a simple script to set up and run the basic send/receive
24 #                               processes for some library validation on top of nano/nng.
25 #                               It should be possible to clone the repo, switch to this directory
26 #                               and execute  'ksh run -B'  which will build RMr, make the sender and
27 #                               recevier then  run the basic test.
28 #
29 #                               Example command line:
30 #                                       ksh ./run_app_test.ksh          # default 20 messages at 2 msg/sec
31 #                                       ksh ./run_app_test.ksh -N    # default but with nanomsg lib
32 #                                       ksh ./run_app_test.ksh -d 100 -n 10000 # send 10k messages with 100ms delay between
33 #
34 #       Date:           22 April 2019
35 #       Author:         E. Scott Daniels
36 # ---------------------------------------------------------------------------------
37
38
39 # The sender and receiver are run asynch. Their exit statuses are captured in a
40 # file in order for the 'main' to pick them up easily.
41 #
42 function run_sender {
43         if (( $nano_sender ))
44         then
45                 ./sender_nano $nmsg $delay
46         else
47                 ./sender $nmsg $delay
48         fi
49         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
50 }
51
52 function run_rcvr {
53         if (( $nano_receiver ))
54         then
55                 ./receiver_nano $nmsg
56         else
57                 ./receiver $nmsg
58         fi
59         echo $? >/tmp/PID$$.rrc
60 }
61
62 # snarf the first v4 IP (not the loopback) that belongs to this box/container/guest
63 function snarf_ip {
64         ip addr| sed -e '/inet /b c; d' -e ':c' -e '/127.0.0.1/d; s!/.*!!; s!^.* !!; q'
65 }
66
67 #       Drop a contrived route table in. This table should add a reference to our 
68 #       local IP to an entry to ensure that the route table collector code in RMr
69 #       is removing 'hairpin' loops. If RMr isn't removing the references to our
70 #       hostname and IP address when it builds the endpoint lists, the sender will
71 #       send messages to itself some of the time, causing the receiver to come up 
72 #       short when comparing messages received with expected count and thus failing.
73 #
74 function set_rt {
75         typeset port=4560                       # port the receiver listens on by default
76
77         cat <<endKat >app_test.rt
78                 newrt | start
79                         mse | 0 |  0 | localhost:$port,$my_ip:43086
80                         mse | 1 | 10 | localhost:$port,${my_host//.*/}:43086
81                         mse | 2 | 20 | localhost:$port
82                         rte | 3 | localhost:$port
83                         mse | 3 | 100 | localhost:$port # special test to ensure that this does not affect previous entry
84                         rte | 4 | localhost:$port
85                         rte | 5 | localhost:$port
86                         rte | 6 | localhost:$port
87                         rte | 7 | localhost:$port
88                         rte | 8 | localhost:$port
89                         rte | 9 | localhost:$port
90                         rte | 10 | localhost:$port
91                         rte | 11 | localhost:$port
92                         rte | 12 | localhost:$port
93                         rte | 13 | localhost:$port
94                 newrt | end
95
96 head -3 app_test.rt
97
98 endKat
99
100 }
101
102 # ---------------------------------------------------------
103
104 nmsg=20                                         # total number of messages to be exchanged (-n value changes)
105                                                         # need two sent to each receiver to ensure hairpin entries were removed (will fail if they were not)
106 delay=500000                            # microsec sleep between msg 1,000,000 == 1s
107 nano_sender=0                           # start nano version if set (-N)
108 nano_receiver=0
109 wait=1
110 rebuild=0
111 nopull=""                                       # -b sets so that build does not pull
112 verbose=0
113 use_installed=0
114 my_ip=$(snarf_ip)                       # get an ip to insert into the route table
115 keep=0
116
117
118 while [[ $1 == -* ]]
119 do
120         case $1 in
121                 -B)     rebuild=1;;                                             # build with pull first
122                 -b)     rebuild=1; nopull="nopull";;    # buld without pull
123                 -d)     delay=$2; shift;;
124                 -k) keep=1;;
125                 -i) use_installed=1;;
126                 -N)     nano_sender=1
127                         nano_receiver=1
128                         ;;
129                 -n)     nmsg=$2; shift;;
130                 -v)     verbose=1;;
131
132                 *)      echo "unrecognised option: $1"
133                         echo "usage: $0 [-B] [-d micor-sec-delay] [-i] [-k] [-N] [-n num-msgs]"
134                         echo "  -B forces a rebuild which will use .build"
135                         echo "  -i causes the installd libraries (/usr/local) to be referenced; -B is ignored if supplied"
136                         echo "  -k keeps the route table"
137                         echo ""
138                         echo "total number of messages must > 20 to correctly test hairpin loop removal"
139                         exit 1
140                         ;;
141         esac
142
143         shift
144 done
145
146 if [[ ! -f app_test.rt ]]               # we need the real host name in the local.rt; build one from mask if not there
147 then
148         my_host=$(hostname)
149         set_rt
150         if (( verbose ))
151         then
152                 cat app_test.rt
153         fi
154 fi
155
156 if (( verbose ))
157 then
158         echo "2" >.verbose
159         export RMR_VCTL_FILE=".verbose"
160 fi
161
162
163 if (( use_installed ))                  # point at installed library rather than testing the build
164 then
165         export LD_LIBRARY_PATH=/usr/local/lib
166         export LIBRARY_PATH=$LD_LIBRARY_PATH
167 else
168         if (( rebuild ))
169         then
170                 set -e
171                 ksh ./rebuild.ksh $nopull | read build_path
172                 set +e
173         else
174                 build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
175         
176                 if [[ ! -d $build_path ]]
177                 then
178                         echo "cannot find build in: $build_path"
179                         echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
180                         exit 1
181                 fi
182         fi
183
184         if [[ -d $build_path/lib64 ]]
185         then
186                 export LD_LIBRARY_PATH=$build_path:$build_path/lib64
187         else
188                 export LD_LIBRARY_PATH=$build_path:$build_path/lib
189         fi
190         export LIBRARY_PATH=$LD_LIBRARY_PATH
191 fi
192
193 export RMR_SEED_RT=${RMR_SEED_RT:-./app_test.rt}                # allow easy testing with different rt
194
195 if [[ ! -f ./sender ]]
196 then
197         if ! make >/dev/null 2>&1
198         then
199                 echo "[FAIL] cannot find sender binary, and cannot make it.... humm?"
200                 exit 1
201         fi
202 fi
203
204 run_rcvr &
205 sleep 2                         # if sender starts faster than rcvr we can drop msgs, so pause a bit
206 run_sender &
207
208 wait
209 head -1 /tmp/PID$$.rrc | read rrc
210 head -1 /tmp/PID$$.src | read src
211
212 if (( !! (src + rrc) ))
213 then
214         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
215 else
216         echo "[PASS] sender rc=$src  receiver rc=$rrc"
217 fi
218
219 if (( ! keep )) 
220 then
221         rm app_test.rt
222 fi
223
224 rm /tmp/PID$$.*
225 rm -f .verbose
226
227 exit $(( !! (src + rrc) ))
228