Move listener src into subdir for easier testing
[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 #       Date:           10 December 2019
29 #       Author:         E. Scott Daniels
30 # -------------------------------------------------------------------------
31
32 function abort_after {
33         touch $running
34         sleep ${1:-60}
35         if [[ -e $running ]]
36         then
37                 echo "abort: unit test running too long, killing ${2:-nojobgiven}"
38                 kill -9 ${2:-bad-pid}
39         fi
40 }
41
42 function setup_dirs {
43         mkdir -p /tmp/fifos
44         mkdir -p /tmp/mc_listener_test/final
45         mkdir -p /tmp/mc_listener_test/stage
46
47         mv_src=/tmp/mc_listener_test/mv_src             # source that will be renamed rather than copied
48         mv_dest=/tmp/mc_listener_test/mv_dest
49         ps -elf >$mv_src
50
51         copy_src=/tmp/mc_listener_test/copy_src
52         copy_dest=/tmp/mc_listener_test/copy_dest
53         ps -elf >$copy_src
54
55         src_md5=$( cat $copy_src | md5sum )             # use cat so that filename doesn't factor in to output
56         rm -f $copy_dest
57 }
58
59 function purge_dirs {
60         rm -fr /tmp/mc_listener_test
61 }
62
63 # This is a hack! There seems not to be an easy way to have the LF
64 # environment add RMR (or other needed packages) for testing. If we don't
65 # find RMR in the /usr/local part of the filesystem, we'll force it into
66 # /tmp which doesn't require root.  We'll be smart and get the desired
67 # rmr version from the repo root juas as we _expected_ the CI environmnt
68 # woudl do (but seems not to).
69 #
70 function ensure_pkgs {
71         if (( no_rmr_load ))
72         then
73                 return
74         fi
75
76         if (( force_rmr_load )) || [[ -d /usr/local/include/rmr ]]
77         then
78                 echo "[INFO] found RMR installed in /usr/local"
79                 return
80         fi
81
82         rv=$( grep "version:" ../../rmr-version.yaml | awk '{ print $NF; exit( 0 ) }' )
83         rr=$( grep "repo:" ../../rmr-version.yaml | awk '{ print $NF; exit( 0 ) }' )
84         if [[ -z $rv ]]
85         then
86                 rv="4.2.1"                      # some sane version if not found
87         fi
88         if [[ -z $rr ]]
89         then
90                 rr="release"
91         fi
92         echo "[INFO] RMR seems not to be installed in /usr/local; pulling private copy: v=$rv"
93
94         pkg_dir=/tmp/ut_pkg
95         mkdir -p $pkg_dir
96
97         (
98                 set -e
99                 opts="-nv --content-disposition"
100                 url_base="https://packagecloud.io/o-ran-sc/$rr/packages/debian/stretch"
101                 cd /tmp
102                 wget $opts ${url_base}/rmr_${rv}_amd64.deb/download.deb
103                 wget $opts ${url_base}/rmr-dev_${rv}_amd64.deb/download.deb
104
105                 for x in *rmr*deb
106                 do
107                         dpkg -x $x $pkg_dir
108                 done
109         )
110         if (( $? != 0 ))
111         then
112                 echo "[FAIL] unable to install one or more RMR packages"
113                 exit 1
114         fi
115
116         LD_LIBRARY_PATH=$pkg_dir/usr/local/lib:$LD_LIBRARY_PATH
117         LIBRARY_PATH=$pkg_dir/usr/local/lib:$LIBRARY_PATH
118         export C_INCLUDE_PATH="$pkg_dir/usr/local/include:$C_INCLUDE_PATH"
119 }
120
121 # ------------------------------------------------------------------------------------------------
122
123 # these aren't set by default in some of the CI environments
124 #
125 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
126 export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
127
128 running=/tmp/PID$$.running
129 force_rmr_load=0
130
131 while [[ $1 == -* ]]
132 do
133         case $1 in
134                 -f)     force_rmr_load=1;;
135                 -N) no_rmr_load=1;;                                     # for local testing
136
137                 *)      echo "unrecognised option: $1"
138                         exit 1
139                         ;;
140         esac
141
142         shift
143 done
144
145 if [[ $1 == "purge" ]]                  # just purge
146 then
147         purge_dirs
148         exit 0
149 fi
150
151 ensure_pkgs                                             # ensure that we have RMR; some CI environments are lacking
152
153 if ! make -B unit_test                  # ensure that it's fresh
154 then
155         echo "[FAIL] cannot make unit_test"
156         exit 1
157 fi
158 echo "unit test(s) successfully built"
159
160 setup_dirs
161
162 if [[ $1 == "set"* ]]                   # setup only
163 then
164         exit
165 fi
166
167 ./unit_test >/tmp/PID$$.utlog 2>&1 &
168 pid=$!
169 abort_after 60 $pid &
170 wait $pid
171 #if ! unit_test >/tmp/PID$$.utlog 2>&1
172 if (( $? != 0 ))
173 then
174         echo ">>>> wait popped"
175         rm -f $running
176         cat /tmp/PID$$.utlog
177         rm -f /tmp/PID$$.*
178         purge_dirs
179         exit 1
180 fi
181 rm -f $running
182
183 echo "[PASS] base unit tests all pass"
184 echo "[INFO] file/directory verification begins...."
185
186 # validate files that should have been created/copied
187
188 rc=0
189
190 ls -al /tmp/mc_listener_test/* >/tmp/PID$$.fdlog 2>&1
191
192 if [[ -e $copy_src ]]
193 then
194         echo "[FAIL] copy source test should have been unlinked but was there!"
195         rc=1
196 else
197         dest_md5=$( cat $copy_dest | md5sum )           # use cat so that filename doesn't factor in to output
198         if [[ $dest_md5 != $src_md5 ]]
199         then
200                 echo "[FAIL] md5 of copy test file didn't match soruce"
201                 cat $dest_md5
202                 echo "$dest_md5  $src_md5"
203                 rc=1
204         fi
205 fi
206 purge_dirs
207
208 if (( rc > 0 ))
209 then
210         cat /tmp/PID$$.fdlog
211 fi
212
213
214 ./show_coverage.ksh unit_test.c                                                         # compute coverage and generate .gcov files
215 echo "Coverage with discounting (raw values in parens)"
216 ./discount_chk.ksh $(ls *gcov|egrep -v "^test_|unit_test.c")
217
218
219 if (( rc > 0 ))
220 then
221         echo "[FAIL] overall test fails"
222 else
223         echo "[PASS] overall test passes"
224         rm -f *test*.gcov
225 fi
226
227 rm -f /tmp/PID$$.*
228 exit $rc