2a90412163d0c729d7e3f583cd025068905d7cf4
[ric-app/mc.git] / sidecars / listener / test / run_unit_test.ksh
1 #!/usr/bin/env bash
2
3 #==================================================================================
4 #        Copyright (c) 2018-2019 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_unit_test.ksh
22 #       Abstract:       This drives unit testing setting up working directories
23 #                               and such. This expects that RMR dev package(s) is/are
24 #                               installed. Some CI envirpnments don't provide this, or
25 #                               the ability to pre-install before running this test, so
26 #                               we will force one to be there if we don't find it in /usr.
27 #
28 #                               CI Mode
29 #                               For the Jenkins jobs to capture trigger data allowing sonar
30 #                               analysis, we must force a build in the ../src directory.
31 #                               CI mode is on by default, to force this build, but can be
32 #                               turned off with the -c option for local builds/testing.
33 #
34 #       Date:           10 December 2019
35 #       Author:         E. Scott Daniels
36 # -------------------------------------------------------------------------
37
38 function abort_after {
39         touch $running
40         sleep ${1:-60}
41         if [[ -e $running ]]
42         then
43                 echo "abort: unit test running too long, killing ${2:-nojobgiven}"
44                 kill -9 ${2:-bad-pid}
45         fi
46 }
47
48 function setup_dirs {
49         mkdir -p /tmp/fifos
50         mkdir -p /tmp/mc_listener_test/final
51         mkdir -p /tmp/mc_listener_test/stage
52
53         mv_src=/tmp/mc_listener_test/mv_src             # source that will be renamed rather than copied
54         mv_dest=/tmp/mc_listener_test/mv_dest
55         ps -elf >$mv_src
56
57         copy_src=/tmp/mc_listener_test/copy_src
58         copy_dest=/tmp/mc_listener_test/copy_dest
59         ps -elf >$copy_src
60
61         src_md5=$( cat $copy_src | md5sum )             # use cat so that filename doesn't factor in to output
62         rm -f $copy_dest
63 }
64
65 function purge_dirs {
66         rm -fr /tmp/mc_listener_test
67 }
68
69 # This is a hack! There seems not to be an easy way to have the LF
70 # environment add RMR (or other needed packages) for testing. If we don't
71 # find RMR in the /usr/local part of the filesystem, we'll force it into
72 # /tmp which doesn't require root.  We'll be smart and get the desired
73 # rmr version from the repo root juas as we _expected_ the CI environmnt
74 # woudl do (but seems not to).
75 #
76 function ensure_pkgs {
77         if (( no_rmr_load ))
78         then
79                 return
80         fi
81
82         if (( force_rmr_load )) || [[ -d /usr/local/include/rmr ]]
83         then
84                 echo "[INFO] found RMR installed in /usr/local"
85                 return
86         fi
87
88         rv=$( grep "version:" ../../rmr-version.yaml | awk '{ print $NF; exit( 0 ) }' )
89         rr=$( grep "repo:" ../../rmr-version.yaml | awk '{ print $NF; exit( 0 ) }' )
90         if [[ -z $rv ]]
91         then
92                 rv="4.2.1"                      # some sane version if not found
93         fi
94         if [[ -z $rr ]]
95         then
96                 rr="release"
97         fi
98         echo "[INFO] RMR seems not to be installed in /usr/local; pulling private copy: v=$rv"
99
100         pkg_dir=/tmp/ut_pkg
101         mkdir -p $pkg_dir
102
103         (
104                 set -e
105                 opts="-nv --content-disposition"
106                 url_base="https://packagecloud.io/o-ran-sc/$rr/packages/debian/stretch"
107                 cd /tmp
108                 wget $opts ${url_base}/rmr_${rv}_amd64.deb/download.deb
109                 wget $opts ${url_base}/rmr-dev_${rv}_amd64.deb/download.deb
110
111                 for x in *rmr*deb
112                 do
113                         dpkg -x $x $pkg_dir
114                 done
115         )
116         if (( $? != 0 ))
117         then
118                 echo "[FAIL] unable to install one or more RMR packages"
119                 exit 1
120         fi
121
122         LD_LIBRARY_PATH=$pkg_dir/usr/local/lib:$LD_LIBRARY_PATH
123         LIBRARY_PATH=$pkg_dir/usr/local/lib:$LIBRARY_PATH
124         export C_INCLUDE_PATH="$pkg_dir/usr/local/include:$C_INCLUDE_PATH"
125 }
126
127 # ------------------------------------------------------------------------------------------------
128
129 # these aren't set by default in some of the CI environments
130 #
131 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
132 export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
133
134 # defined in the CI configuration where jenkins jobs are looking for gcov files
135 gcov_dir=/tmp/gcov_rpts
136 if [[ ! -d $cov_dir ]]
137 then
138         echo "[INFO] making $gcov_dir"
139         mkdir $gcov_dir
140 fi
141
142 running=/tmp/PID$$.running
143 force_rmr_load=0
144 ci_mode=1                                                               # -c turns off; see the flower box above
145 verbose=0
146
147 while [[ $1 == -* ]]
148 do
149         case $1 in
150                 -c) ci_mode=0;;
151                 -f)     force_rmr_load=1;;
152                 -N) no_rmr_load=1;;                                     # for local testing
153                 -v)     verbose=1;;
154
155                 *)      echo "unrecognised option: $1"
156                         exit 1
157                         ;;
158         esac
159
160         shift
161 done
162
163 if [[ $1 == "purge" ]]                  # just purge
164 then
165         purge_dirs
166         exit 0
167 fi
168
169 ensure_pkgs                                             # ensure that we have RMR; some CI environments are lacking
170
171 if (( ci_mode ))                                # in CI mode we must force a build in the src so build wrapper captures trigger data
172 then
173         echo "unit_test: building in src for sonar build wrapper capture"
174         (
175                 cd ../src
176                 export TEST_COV_OPTS="-ftest-coverage -fprofile-arcs"           # picked up by make so we get coverage on tools for sonar
177                 make -B
178         )
179 fi
180
181 echo "[INFO] unit_test: building unit test(s)"
182 if ! make -B unit_test                  # ensure that it's fresh
183 then
184         echo "[FAIL] cannot make unit_test"
185         exit 1
186 fi
187 echo "[INFO] unit_test: unit test(s) successfully built"
188
189 setup_dirs
190 echo "[INFO] unit_test: directories set up"
191
192 if [[ $1 == "set"* ]]                   # setup only
193 then
194         echo "[INFO] unit_test: setup only; exiting good"
195         exit
196 fi
197
198 rm -fr *.gcov *.gcda                    # ditch any previously generated coverage info
199
200 echo "[INFO] unit_test: running tests -- may take as long as 120 seconds"
201 ./unit_test >/tmp/PID$$.utlog 2>&1 &
202 pid=$!
203 abort_after 60 $pid &
204 wait $pid
205 if (( $? != 0 ))
206 then
207         echo "<FAIL> run_unit_test: wait popped"
208         rm -f $running
209         cat /tmp/PID$$.utlog
210         rm -f /tmp/PID$$.*
211         purge_dirs
212         exit 1
213 fi
214 rm -f $running
215
216 echo "[PASS] base unit tests all pass"
217 echo "[INFO] file/directory verification begins...."
218
219 # validate files that should have been created/copied
220
221 rc=0
222
223 ls -al /tmp/mc_listener_test/* >/tmp/PID$$.fdlog 2>&1
224
225 if [[ -e $copy_src ]]
226 then
227         echo "[FAIL] copy source test should have been unlinked but was there!"
228         rc=1
229 else
230         dest_md5=$( cat $copy_dest | md5sum )           # use cat so that filename doesn't factor in to output
231         if [[ $dest_md5 != $src_md5 ]]
232         then
233                 echo "[FAIL] md5 of copy test file didn't match soruce"
234                 cat $dest_md5
235                 echo "$dest_md5  $src_md5"
236                 rc=1
237         fi
238 fi
239 purge_dirs
240
241 if (( rc > 0 ))
242 then
243         cat /tmp/PID$$.fdlog
244 fi
245
246
247 ./show_coverage.ksh unit_test.c                                                         # compute coverage and generate .gcov files
248 echo "Coverage with discounting (raw values in parens)"
249 ./discount_chk.ksh $(ls *gcov|egrep -v "^test_|unit_test.c")
250
251
252 if (( rc > 0 ))
253 then
254         echo "[FAIL] overall test fails"
255 else
256         echo "[PASS] overall test passes"
257         rm -f *test*.gcov                                       # ditch test specific coverage files first
258         #cp *.gcov      $gcov_dir/                              # make avilable to jenkins job(s)
259         ./publish_cov.ksh                                       # push to reports dir and fix internal source filenames
260 fi
261
262
263 rm -f /tmp/PID$$.*
264 exit $rc