Address sonar complaints about the listener
[ric-app/mc.git] / sidecars / listener / src / verify.sh
1 #!/usr/bin/env bash
2 # vim: ts=4 sw=4 noet:
3 #----------------------------------------------------------------------------------
4 #
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 # ----------------------------------------------------------------------
23 # Mnemonic:     verify.sh
24 # Abstract: Simple script to attempt to verify that the mc_listener is
25 #                       capable of running. This will start a listener and a sender
26 #                       and then will cat a few lines from one of the FIFOs.
27 #                       This script is designed to run using the geneated runtime
28 #                       image; in other words, it expects to find the binaries
29 #                       in /playpen/bin if that directory exists. If it does not it
30 #                       assumes the current working directory is where the binaries
31 #                       exist.
32 #
33 # Date:         26 August 2019
34 # Author:       E. Scott Daniels
35 # ----------------------------------------------------------------------
36
37 # set the various sleep values based on long test or short test
38 function set_wait_values {
39         if (( long_test ))
40         then
41                 export MCL_RDC_FREQ=13          # file cycle after 13s
42                 sender_wait=100
43                 listener_wait=105
44                 reader_wait=102
45                 main_wait=120
46         else
47                 sender_wait=10
48                 listener_wait=15
49                 reader_wait=12
50                 main_wait=20
51         fi
52 }
53
54 # run sender at a 2 msg/sec rate (500000 musec delay)
55 # sender sends msg types 0-6 and the route table in /tmp
56 # will direct them to the listener. We also need to switch
57 # the RT listen port so there is no collision with the listen
58 # preocess.
59 #
60 function run_sender {
61         echo "starting sender"
62         RMR_SEED_RT=/tmp/local.rt RMR_RTG_SVC=9989 $bin_dir/sender 43086 10000 >/tmp/sender.log 2>&1 &
63         spid=$!
64         sleep $sender_wait
65
66         echo "stopping sender $spid"
67         kill -15 $spid
68 }
69
70 function run_listener {
71         echo "starting listener"
72         $bin_dir/mc_listener $ext_hdr -r 1 -d $fifo_dir >/tmp/listen.log 2>&1 &
73         lpid=$!
74
75         sleep $listener_wait
76         echo "stopping listener $lpid"
77         kill -15 $lpid
78 }
79
80 # run a pipe reader for one message type
81 function run_pr {
82         echo "starting pipe reader $1"
83         $bin_dir/pipe_reader $ext_hdr -m $1 -d $fifo_dir  >/tmp/pr.$1.log 2>&1 &
84         #$bin_dir/pipe_reader -m $1 -d $fifo_dir & # >/tmp/pr.$1.log 2>&1 
85         typeset prpid=$!
86         
87         sleep $reader_wait
88         echo "stopping pipe reader $ppid"
89         kill -15 $prpid
90 }
91
92 # generate a dummy route table that the sender needs
93 function gen_rt {
94         cat <<endKat >/tmp/local.rt
95         newrt|start
96         mse | 0 | -1 | localhost:4560   
97         mse | 1 | -1 | localhost:4560   
98         mse | 2 | -1 | localhost:4560   
99         mse | 3 | -1 | localhost:4560   
100         mse | 4 | -1 | localhost:4560   
101         mse | 5 | -1 | localhost:4560   
102         mse | 6 | -1 | localhost:4560   
103         newrt|end
104 endKat
105 }
106
107 # ---- run everything ---------------------------------------------------
108
109 si=""                                           # if -s given then we add this to sender/listener to run SI95 versions
110 ext_hdr=""                                      # run with extended header enabled (-e turns extended off)
111 long_test=0
112 raw_capture=1
113 while [[ $1 == -* ]]
114 do
115         case $1 in 
116                 -l)     long_test=1;;
117                 -n)     raw_capture=0;;
118                 -s)     si="si_";;
119                 *)      echo "$1 is not a recognised option"
120                         exit 1
121                         ;;
122         esac
123
124         shift
125 done
126
127
128 if [[ -d /playpen/bin ]]        # designed to run in the container, but this allows unit test by jenkins to drive too
129 then
130         bin_dir=/playpen/${si}bin
131 else
132         bin_dir="."
133 fi
134
135 set_wait_values
136
137 if (( ! raw_capture ))          # -n set, turn off capture
138 then
139         export MCL_RDC_ENABLE=0
140 fi
141
142 if [[ -d /data/final ]]                 # assume if we find data that final directory goes here
143 then
144         echo "### found /data/final using that as final directory"
145         export MCL_RDC_FINAL=/data/final
146 fi
147
148 if [[ -d /data/stage ]]
149 then
150         echo "### found /data/staging using that as stage directory"
151         export MCL_RDC_STAGE=/data/stage
152 fi
153 final_dir=${MCL_RDC_FINAL:-/tmp/rdc/final}
154 stage_dir=${MCL_RDC_STAGE:-/tmp/rdc/stage}
155
156 fifo_dir=/tmp/fifos
157 mkdir -p $fifo_dir                      # redirect fifos so we don't depend on mount
158
159 gen_rt                                          # generate a dummy route table
160 run_listener &
161 sleep 4
162
163 for p in 0 1 2 3 4 5 6
164 do
165         run_pr $p &
166 done
167 sleep 1
168 run_sender &
169
170 sleep $main_wait                        # long enough for all functions to finish w/o having to risk a wait hanging
171 echo "all functions stopped; looking at logs"
172
173 # ---------- validation -------------------------------------------------
174
175 errors=0
176
177 # logs should be > 0 in size
178 echo "----- logs ---------"
179 ls -al /tmp/*.log
180
181 # pipe reader log files 1-6 should have 'stand up and cheer' messages
182 # pipe reader log for MT 0 will likley be empty as sender sends only
183 # one of those and buffer not likely flushed. So, we only check 1-6
184 #
185 for l in 1 2 3 4 5 6
186 do
187         if [[ ! -s /tmp/pr.$l.log ]]
188         then
189                 echo "[FAIL] log $l was empty"
190                 (( errors++ ))
191         else
192                 if ! grep -q -i "stand up and cheer" /tmp/pr.$l.log
193                 then
194                         echo "[FAIL] pipe reader log did not have any valid messages: /tmp/pr.$l.log"
195                         (( errors++ ))
196                 fi
197         fi
198 done
199
200 if (( ! errors )) 
201 then
202         echo "[OK]    All logs seem good"
203 fi
204
205 nfifos=$( ls /tmp/fifos/MT_* | wc -l )
206 if (( nfifos < 7 ))
207 then
208         echo "didn't find enough fifos"
209         ls -al /tmp/fifos/*
210         (( errors++ ))
211 else
212         echo "[OK]    Found expected fifos"
213 fi
214
215 if (( raw_capture ))            # not an error if not capturing
216 then
217         if [[ -d $stage_dir ]]
218         then
219                 echo "[OK]    Found staging direcory ($stage_dir)"
220                 ls -al $stage_dir
221         else
222                 (( errors++ ))
223                 echo "[FAIL]  No staging directory found ($stage_dir)"
224         fi
225
226
227         if [[ -d $final_dir ]]
228         then
229                 echo "[OK]    Found final direcory ($final_dir)"
230                 ls -al $final_dir
231         
232                 if (( long_test ))              # look for files in final dir to ensure roll
233                 then
234                         found=$( ls $final_dir/MC* | wc -l )
235                         if (( found > 0 ))
236                         then
237                                 echo "[OK]   Found $found files in final directory ($final_dir)"
238                         else
239                                 echo "[FAIL] Did not find any files in the final directory ($final_dir)"
240                                 (( errors++ ))
241                         fi
242                 fi
243         else
244                 (( errors++ ))
245                 echo "[FAIL]  No final directory found"
246         fi
247 fi
248
249 if (( errors ))
250 then
251         echo "[FAIL] $errors errors noticed"
252 else
253         echo "[PASS]"
254 fi
255