3da098a4bab6edee080d0f81c29113d045f32dd6
[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 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 -d 100 -n 10000 # send 10k messages with 100ms delay between
32 #
33 #       Date:           22 April 2019
34 #       Author:         E. Scott Daniels
35 # ---------------------------------------------------------------------------------
36
37
38 # The sender and receiver are run asynch. Their exit statuses are captured in a
39 # file in order for the 'main' to pick them up easily.
40 #
41 function run_sender {
42         ./sender $nmsg $delay
43         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
44 }
45
46 function run_rcvr {
47         if (( mt_receiver ))
48         then
49                 echo "<TEST> testing with mt-receiver" >&2
50                 ./mt_receiver $nmsg
51         else
52                 ./receiver $nmsg
53         fi
54         echo $? >/tmp/PID$$.rrc
55 }
56
57 # snarf the first v4 IP (not the loopback) that belongs to this box/container/guest
58 function snarf_ip {
59         ip addr| sed -e '/inet /b c; d' -e ':c' -e '/127.0.0.1/d; s!/.*!!; s!^.* !!; q'
60 }
61
62 #       Drop a contrived route table in. This table should add a reference to our 
63 #       local IP to an entry to ensure that the route table collector code in RMr
64 #       is removing 'hairpin' loops. If RMr isn't removing the references to our
65 #       hostname and IP address when it builds the endpoint lists, the sender will
66 #       send messages to itself some of the time, causing the receiver to come up 
67 #       short when comparing messages received with expected count and thus failing.
68 #
69 function set_rt {
70         typeset port=4560                       # port the receiver listens on by default
71
72         cat <<endKat >app_test.rt
73                 newrt | start
74                         mse | 0 |  0 | localhost:$port,$my_ip:43086
75                         mse | 1 | 10 | localhost:$port,${my_host//.*/}:43086
76                         mse | 2 | 20 | localhost:$port
77                         rte | 3 | localhost:$port
78                         mse | 3 | 100 | localhost:$port # special test to ensure that this does not affect previous entry
79                         rte | 4 | localhost:$port
80                         rte | 5 | localhost:$port
81                         rte | 6 | localhost:$port
82                         rte | 7 | localhost:$port
83                         rte | 8 | localhost:$port
84                         rte | 9 | localhost:$port
85                         rte | 10 | localhost:$port
86                         rte | 11 | localhost:$port
87                         rte | 12 | localhost:$port
88                         rte | 13 | localhost:$port
89                 newrt | end
90
91 head -3 app_test.rt
92
93 endKat
94
95 }
96
97 # ---------------------------------------------------------
98
99 nmsg=20                                         # total number of messages to be exchanged (-n value changes)
100                                                         # need two sent to each receiver to ensure hairpin entries were removed (will fail if they were not)
101 delay=500000                            # microsec sleep between msg 1,000,000 == 1s
102 wait=1
103 rebuild=0
104 nopull=""                                       # -b sets so that build does not pull
105 verbose=0
106 use_installed=0
107 my_ip=$(snarf_ip)                       # get an ip to insert into the route table
108 keep=0
109 mt_receiver=0                           # -m sets in order to test with multi-threaded receive stuff
110 force_make=0
111
112
113 while [[ $1 == -* ]]
114 do
115         case $1 in
116                 -B)     rebuild=1;;                                             # build with pull first
117                 -b)     rebuild=1; nopull="nopull";;    # buld without pull
118                 -d)     delay=$2; shift;;
119                 -k) keep=1;;
120                 -i) use_installed=1;;
121                 -M)     force_make=1;;
122                 -m)     mt_receiver=1;;
123                 -n)     nmsg=$2; shift;;
124                 -v)     verbose=1;;
125
126                 *)      echo "unrecognised option: $1"
127                         echo "usage: $0 [-B] [-d micor-sec-delay] [-i] [-k] [-M] [-m] [-n num-msgs]"
128                         echo "  -B forces an RMR rebuild which will use .build"
129                         echo "  -i causes the installd libraries (/usr/local) to be referenced; -B is ignored if supplied"
130                         echo "  -k keeps the route table"
131                         echo "  -M force make on test applications"
132                         echo "  -m test with mt-receive mode"
133                         echo ""
134                         echo "total number of messages must > 20 to correctly test hairpin loop removal"
135                         exit 1
136                         ;;
137         esac
138
139         shift
140 done
141
142 if [[ ! -f app_test.rt ]]               # we need the real host name in the local.rt; build one from mask if not there
143 then
144         my_host=$(hostname)
145         set_rt
146         if (( verbose ))
147         then
148                 cat app_test.rt
149         fi
150 fi
151
152 if (( verbose ))
153 then
154         echo "2" >.verbose
155         export RMR_VCTL_FILE=".verbose"
156 fi
157
158
159 if (( use_installed ))                  # point at installed library rather than testing the build
160 then
161         export LD_LIBRARY_PATH=/usr/local/lib
162         export LIBRARY_PATH=$LD_LIBRARY_PATH
163 else
164         if (( rebuild ))
165         then
166                 set -e
167                 ksh ./rebuild.ksh $nopull | read build_path
168                 set +e
169         else
170                 build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
171         
172                 if [[ ! -d $build_path ]]
173                 then
174                         echo "[FAIL] cannot find build in: $build_path"
175                         echo "[FAIL] either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
176                         exit 1
177                 fi
178         fi
179
180         if [[ -d $build_path/lib64 ]]
181         then
182                 export LD_LIBRARY_PATH=$build_path:$build_path/lib64
183         else
184                 export LD_LIBRARY_PATH=$build_path:$build_path/lib
185         fi
186         export LIBRARY_PATH=$LD_LIBRARY_PATH
187 fi
188
189 export RMR_SEED_RT=${RMR_SEED_RT:-./app_test.rt}                # allow easy testing with different rt
190
191 if (( force_make )) || [[ ! -f ./sender || ! -f ./receiver ]]
192 then
193         if ! make -B >/dev/null 2>&1
194         then
195                 echo "[FAIL] cannot find sender binary, and cannot make it.... humm?"
196                 exit 1
197         fi
198 fi
199
200 run_rcvr &
201 sleep 2                         # if sender starts faster than rcvr we can drop msgs, so pause a bit
202 run_sender &
203
204 wait
205 head -1 /tmp/PID$$.rrc | read rrc
206 head -1 /tmp/PID$$.src | read src
207
208 if (( !! (src + rrc) ))
209 then
210         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
211 else
212         echo "[PASS] sender rc=$src  receiver rc=$rrc"
213 fi
214
215 if (( ! keep )) 
216 then
217         rm app_test.rt
218 fi
219
220 rm /tmp/PID$$.*
221 rm -f .verbose
222
223 exit $(( !! (src + rrc) ))
224