Tweak unit tests to save coverage in common dir
[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 add 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 juas as we _expected_ the CI environmnt
42 # woudl 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
105 # defined in the CI configuration where jenkins jobs are looking for gcov files
106 gcov_dir=/tmp/gcov_rpts
107 if [[ ! -d $cov_dir ]]
108 then
109         echo "<INFO> making $gcov_dir"
110         mkdir $gcov_dir
111 fi
112
113
114 while [[ $1 == -* ]]
115 do
116         case $1 in
117                 -f)     force_rmr_load=1;;
118                 -N) no_rmr_load=1;;                                     # for local testing
119
120                 *)      echo "unrecognised option: $1"
121                         exit 1
122                         ;;
123         esac
124
125         shift
126 done
127
128 ensure_pkgs                                                                     # some CI enviroments may not have RMR; get it
129
130 script_dir=${PWD%/*}/src
131 cd ../src
132
133 # build the binaries with coverage options set
134 export TEST_COV_OPTS="-ftest-coverage -fprofile-arcs"           # picked up by make so we get coverage on tools for sonar
135 make clean                      # ensure coverage files removed
136 make -B                         # ensure coverage data is nuked
137
138 # drive with full complement to test good branches, then with bad (missing value) to drive exceptions
139 mc_listener -p 4567 -q -r 10 -e -d foo -x  >/dev/null 2>&1              # -x (invalid) prevents execution loop
140 for x in d p r \? h                                             # drive with missing values for d, p, r and singletons -h and -?
141 do
142         gcov mc_listener.c                                      # debugging because jenkins gcov doesn't seem to be accumulating data
143         mc_listener -$x >/dev/null 2>&1
144 done
145 gcov mc_listener.c                                      # debugging because jenkins gcov doesn't seem to be accumulating data
146
147 pipe_reader -d foo -e -f -m 0 -s  -x >/dev/null 2>&1            # drive for all "good" conditions
148 for x in d m \? h
149 do
150         pipe_reader  -$x >/dev/null 2>&1                # drive each exception (missing value) or 'help'
151 done
152
153 rdc_replay -d foo -f bar -t 0  -x >/dev/null 2>&1                       # drive for all "good" conditions
154 for x in d f t  \? h
155 do
156         rdc_replay  -$x >/dev/null 2>&1         # drive each exception (missing value) or 'help'
157 done
158
159 $script_dir/verify.sh                                   # verify MUST be first (replay relies on its output)
160 $script_dir/verify_replay.sh
161
162 # generate and copy coverage files to parent which is where the CI jobs are looking for them
163 # we do NOT gen stats for the library functions; the unit test script(s) do that
164 #
165 for x in mc_listener sender rdc_replay pipe_reader
166 do
167         gcov $x.c
168         cp $x.c.gcov $gcov_dir/
169 done
170
171 exit