Tweak unit test script to ensure LD paths are set
[ric-app/mc.git] / sidecars / listener / 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.
24 #
25 #       Date:           10 December 2019
26 #       Author:         E. Scott Daniels
27 # -------------------------------------------------------------------------
28
29 function abort_after {
30         touch /tmp/running
31         sleep ${1:-60}
32         if [[ -e /tmp/running ]]
33         then
34                 echo "abort: unit test running too long, killing $2"
35                 kill -9 ${2:-bad-pid}
36         fi
37 }
38
39 function setup_dirs {
40         mkdir -p /tmp/fifos
41         mkdir -p /tmp/mc_listener_test/final
42         mkdir -p /tmp/mc_listener_test/stage
43
44         mv_src=/tmp/mc_listener_test/mv_src             # source that will be renamed rather than copied
45         mv_dest=/tmp/mc_listener_test/mv_dest
46         ps -elf >$mv_src
47
48         copy_src=/tmp/mc_listener_test/copy_src
49         copy_dest=/tmp/mc_listener_test/copy_dest
50         ps -elf >$copy_src
51
52         src_md5=$( cat $copy_src | md5sum )             # use cat so that filename doesn't factor in to output
53         rm -f $copy_dest
54 }
55
56 function purge_dirs {
57         rm -fr /tmp/mc_listener_test
58 }
59
60 # ------------------------------------------------------------------------------------------------
61 # these aren't set by default in some of the CI environments
62 #
63 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
64 export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
65
66 if ! make -B unit_test                  # ensure that it's fresh
67 then
68         echo "[FAIL] cannot make unit_test"
69         exit 1
70 fi
71
72 if [[ $1 == "purge" ]]
73 then
74         purge_dirs
75         exit 0
76 fi
77
78 setup_dirs
79
80 if [[ $1 == "set"* ]]
81 then
82         exit
83 fi
84
85 abort_after 60
86 if ! unit_test >/tmp/PID$$.utlog 2>&1
87 then
88         rm -f /tmp/running
89         cat /tmp/PID$$.utlog
90         rm -f /tmp/PID$$.*
91         purge_dirs
92         exit 1
93 fi
94 rm -f /tmp/running
95
96 echo "[PASS] base unit tests all pass"
97 echo "[INFO] file/directory verification begins...."
98
99 # validate files that should have been created/copied
100
101 rc=0
102
103 ls -al /tmp/mc_listener_test/* >/tmp/PID$$.fdlog 2>&1
104
105 if [[ -e $copy_src ]]
106 then
107         echo "[FAIL] copy source test should have been unlinked but was there!"
108         rc=1
109 else
110         dest_md5=$( cat $copy_dest | md5sum )           # use cat so that filename doesn't factor in to output
111         if [[ $dest_md5 != $src_md5 ]]
112         then
113                 echo "[FAIL] md5 of copy test file didn't match soruce"
114 cat $dest_md5
115 echo "$dest_md5  $src_md5"
116                 rc=1
117         fi
118 fi
119 purge_dirs
120
121 if (( rc > 0 ))
122 then
123         cat /tmp/PID$$.fdlog
124 fi
125
126
127 show_coverage.ksh unit_test.c                                                           # compute coverage and generate .gcov files
128 echo "Coverage with discounting (raw values in parens)"
129 discount_chk.ksh $(ls *gcov|egrep -v "^test_|unit_test.c")
130
131
132 if (( rc > 0 ))
133 then
134         echo "[FAIL] overall test fails"
135 else
136         echo "[PASS] overall test passes"
137         rm -f *test*.gcov
138 fi
139
140 rm -f /tmp/PID$$.*
141 exit $rc