Correct bug in listener not detecting eagain/eintr correctly
[ric-app/mc.git] / sidecars / listener / test / run_app_tests.ksh
1 #!/usr/bin/env bash
2
3 #==================================================================================
4 #        Copyright (c) 2018-2020 AT&T Intellectual Property.
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 #==================================================================================
18
19
20 #
21 #       Mnemonic:       run_app_test.ksh
22 #       Abstract:       This script drives various applications (both mc_listener  related
23 #                               and testers) to generate coverage stats for sonar.  There is little
24 #                               we can validate with these tests other than the programme doesn't
25 #                               crash.  The verify scripts are run at code checkin and will flag
26 #                               any real problems, so this is just to keep sonar's knickers unbunched.
27 #
28 #                               Assumptions:
29 #                                       - execution directory is listener/test
30 #                                       - source directory is ../src
31 #
32 #       Date:           2 September 2020
33 #       Author:         E. Scott Daniels
34 # -------------------------------------------------------------------------
35
36
37 # This is a hack! There seems not to be an easy way to have the LF
38 # environment adds RMR (or other needed packages) for testing. If we don't
39 # find RMR in the /usr/local part of the filesystem, we'll force it into
40 # /tmp which doesn't require root.  We'll be smart and get the desired
41 # rmr version from the repo root just as we _expected_ the CI environmnt
42 # would do (but seems not to).
43 #
44 function ensure_pkgs {
45         if (( no_rmr_load ))
46         then
47                 return
48         fi
49
50         if (( force_rmr_load )) || [[ -d /usr/local/include/rmr ]]
51         then
52                 echo "[INFO] found RMR installed in /usr/local"
53                 return
54         fi
55
56         rv=$( grep "version:" ../../rmr-version.yaml | awk '{ print $NF; exit( 0 ) }' )
57         rr=$( grep "repo:" ../../rmr-version.yaml | awk '{ print $NF; exit( 0 ) }' )
58         if [[ -z $rv ]]
59         then
60                 rv="4.2.1"                      # some sane version if not found
61         fi
62         if [[ -z $rr ]]
63         then
64                 rr="release"
65         fi
66         echo "[INFO] RMR seems not to be installed in /usr/local; pulling private copy: v=$rv"
67
68         pkg_dir=/tmp/ut_pkg
69         mkdir -p $pkg_dir
70
71         (
72                 set -e
73                 opts="-nv --content-disposition"
74                 url_base="https://packagecloud.io/o-ran-sc/$rr/packages/debian/stretch"
75                 cd /tmp
76                 wget $opts ${url_base}/rmr_${rv}_amd64.deb/download.deb
77                 wget $opts ${url_base}/rmr-dev_${rv}_amd64.deb/download.deb
78
79                 for x in *rmr*deb
80                 do
81                         dpkg -x $x $pkg_dir
82                 done
83         )
84         if (( $? != 0 ))
85         then
86                 echo "[FAIL] unable to install one or more RMR packages"
87                 exit 1
88         fi
89
90         LD_LIBRARY_PATH=$pkg_dir/usr/local/lib:$LD_LIBRARY_PATH
91         LIBRARY_PATH=$pkg_dir/usr/local/lib:$LIBRARY_PATH
92         export C_INCLUDE_PATH="$pkg_dir/usr/local/include:$C_INCLUDE_PATH"
93 }
94
95 # ------------------------------------------------------------------------------------------------
96
97 # these aren't set by default in some of the CI environments
98 #
99 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
100 export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
101
102 force_rmr_load=0
103 no_rmr_load=0
104 test_dir=$PWD
105
106 # defined in the CI configuration where jenkins jobs are looking for gcov files
107 gcov_dir=/tmp/gcov_rpts
108 if [[ ! -d $cov_dir ]]
109 then
110         echo "<INFO> making $gcov_dir"
111         mkdir $gcov_dir
112 fi
113
114
115 verbose=0
116 while [[ $1 == -* ]]
117 do
118         case $1 in
119                 -f)     force_rmr_load=1;;
120                 -N) no_rmr_load=1;;                                     # for local testing
121                 -v)     verbose=1; vflag="-v";;
122
123                 *)      echo "unrecognised option: $1"
124                         exit 1
125                         ;;
126         esac
127
128         shift
129 done
130
131 ensure_pkgs                                                                     # some CI enviroments may not have RMR; get it
132
133 cd ../src
134
135 # build the binaries with coverage options set
136 export TEST_COV_OPTS="-ftest-coverage -fprofile-arcs"           # picked up by make so we get coverage on tools for sonar
137 make clean                      # ensure coverage files removed
138 make -B                         # force build under the eyes of sonar build wrapper
139 if (( $? != 0 ))
140 then
141         echo "[FAIL] build failed"
142         exit
143 fi
144
145 rm -fr *.gcov *.gcda                    # ditch any previously generated coverage info
146
147 # drive with full complement to test good branches, then with bad (missing value) to drive exceptions
148 ./mc_listener -p 4567 -q -r 10 -e -d foo -x  >/dev/null 2>&1            # -x (invalid) prevents execution loop
149 for x in d p r \? h                                             # drive with missing values for d, p, r and singletons -h and -?
150 do
151         ./mc_listener -$x >/dev/null 2>&1
152 done
153 gcov  mc_listener.c                                     # debugging because jenkins gcov doesn't seem to be accumulating data
154
155 ./pipe_reader -d foo -e -f -m 0 -s  -x >/dev/null 2>&1          # drive for all "good" conditions
156 for x in d m \? h
157 do
158         ./pipe_reader  -$x >/dev/null 2>&1              # drive each exception (missing value) or 'help'
159 done
160
161 ./rdc_replay -d foo -f bar -t 0  -x >/dev/null 2>&1                     # drive for all "good" conditions
162 for x in d f t  \? h
163 do
164         ./rdc_replay  -$x >/dev/null 2>&1               # drive each exception (missing value) or 'help'
165 done
166
167 ./verify.sh $vflag                                      # verify MUST be first (replay relies on its output)
168 ./verify_replay.sh
169
170 # generate and copy coverage files to parent which is where the CI jobs are looking for them
171 # we do NOT gen stats for the library functions; the unit test script(s) do that
172 #
173 for x in mc_listener sender rdc_replay pipe_reader
174 do
175         gcov  $x.c
176         #cp $x.c.gcov $gcov_dir/
177 done
178 $test_dir/publish_cov.ksh                       # publish coverage files and fixup source names
179
180 echo "[INFO] ----- published coverage information ----------------------------------"
181 ls -al $gcov_dir
182 grep -i "0:Source:" $gcov_dir/*.gcov
183
184 echo "------------------------------------------------------------------------------"
185 echo "run_app_tests finished"
186
187 exit