Address sonar complaints about the listener
[ric-app/mc.git] / sidecars / listener / src / verify_replay.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_replay.sh
24 # Abstract: Simple script to attempt to verify that the replay utility
25 #                       works as expected. This assumes that verify.sh has been run
26 #                       and that at least one RDC file is in /tmp/rdc. This script
27 #                       will start the replay utility and a few pipe listeners to
28 #                       parse the data.
29 #
30 # Date:         19 November 2019
31 # Author:       E. Scott Daniels
32 # ----------------------------------------------------------------------
33
34 # set the various sleep values based on long test or short test
35 function set_wait_values {
36         reader_wait=12
37         main_wait=20
38 }
39
40 function run_replay {
41         echo "starting replayer"
42         file=$( ls ${stage_dir}/MCLT_*|head -1 )
43         chmod 644 $file
44
45         set -x
46         $bin_dir/rdc_replay -f $file -d $fifo_dir >/tmp/replay.log 2>&1
47         lpid=$!
48         set +x
49         echo "replay finished"
50 }
51
52 # run a pipe reader for one message type
53 function run_pr {
54         echo "starting pipe reader $1"
55         $bin_dir/pipe_reader $ext_hdr -m $1 -d $fifo_dir  >/tmp/pr.$1.log 2>&1 &
56         #$bin_dir/pipe_reader -m $1 -d $fifo_dir & # >/tmp/pr.$1.log 2>&1
57         typeset prpid=$!
58
59         sleep $reader_wait
60         echo "stopping pipe reader $1"
61         kill -1 $prpid
62 }
63
64 # ---- run everything ---------------------------------------------------
65
66 ext_hdr=""                                      # run with extended header enabled (-e turns extended off)
67 run_listener=0                          # -a turns on to run all
68 while [[ $1 == -* ]]
69 do
70         case $1 in
71                 -a)     run_listener=1;;
72                 *)      echo "$1 is not a recognised option"
73                         echo "usage: $0 [-a]"
74                         echo "-a will cause the listener verification to run which generates files for this script"
75                         exit 1
76                         ;;
77         esac
78
79         shift
80 done
81
82 if [[ -d /playpen/bin ]]        # designed to run in the container, but this allows unit test by jenkins to drive too
83 then
84         bin_dir=/playpen/${si}bin
85 else
86         bin_dir="."
87 fi
88
89 if (( run_listener ))
90 then
91         echo "running listener to generate test files to replay..."
92         set -e
93         verify.sh                               # assumed to be in the path
94         set +e
95 fi
96
97 set_wait_values
98
99 if (( ! raw_capture ))          # -n set, turn off capture
100 then
101         export MCL_RDC_ENABLE=0
102 fi
103
104 if [[ -d /data/final ]]                 # assume if we find data that final directory goes here
105 then
106         echo "### found /data/final using that as final directory"
107         export MCL_RDC_FINAL=/data/final
108 fi
109
110 stage_dir=${MCL_RDC_STAGE:-/tmp/rdc/stage}
111 if [[ ! -d $stage_dir ]]
112 then
113         echo "abort: cannot find stage directory to replay from: $stage_dir"
114         exit 1
115 fi
116
117 fifo_dir=/tmp/fifos
118 if [[ ! -d $fifo_dir ]]
119 then
120         mkdir -p $fifo_dir                      # redirect fifos so we don't depend on mount
121 fi
122
123
124 for p in 0 1 2 3 4 5 6
125 do
126         run_pr $p &
127 done
128
129 sleep 2
130 run_replay &                             # start after readers are going
131
132 sleep $main_wait                        # long enough for all functions to finish w/o having to risk a wait hanging
133 echo "all functions stopped; looking at logs"
134
135 # ---------- validation -------------------------------------------------
136
137 errors=0
138
139 # logs should be > 0 in size
140 echo "----- logs ---------"
141 ls -al /tmp/*.log
142
143 # pipe reader log files 1-6 should have 'stand up and cheer' messages
144 # pipe reader log for MT 0 will likley be empty as sender sends only
145 # one of those and buffer not likely flushed. So, we only check 1-6
146 #
147 for l in 1 2 3 4 5 6
148 do
149         if [[ ! -s /tmp/pr.$l.log ]]
150         then
151                 echo "[FAIL] log $l was empty"
152                 (( errors++ ))
153         else
154                 if ! grep -q -i "stand up and cheer" /tmp/pr.$l.log
155                 then
156                         echo "[FAIL] pipe reader log did not have any valid messages: /tmp/pr.$l.log"
157                         (( errors++ ))
158                 fi
159         fi
160 done
161
162 if (( ! errors ))
163 then
164         echo "[OK]    All logs seem good"
165 fi
166
167 nfifos=$( ls /tmp/fifos/MT_* | wc -l )
168 if (( nfifos < 7 ))
169 then
170         echo "didn't find enough fifos"
171         ls -al /tmp/fifos/*
172         (( errors++ ))
173 else
174         echo "[OK]    Found expected fifos"
175 fi
176
177 if (( errors ))
178 then
179         echo "[FAIL] $errors errors noticed"
180 else
181         echo "[PASS]"
182 fi
183