3 # Copyright (C) 2020 Konsulko Group
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # gcovr wrapper for generating coverage reports against AGL bindings
19 # on target. With a given binding name or coverage widget file, the
20 # coverage version will be installed, and the pyagl tests for the
21 # binding run before generating a report with gcovr. The afm-test
22 # test widget or a user-supplied command may be run instead of the
23 # pyagl tests, see usage below, or run with "--help".
29 $(basename $0) [options] <binding name | widget file>
33 Print this help and exit
36 Do not remove temporary files/directories
39 Additional gcovr options, multiple options should be quoted
42 gcov/gcovr temporary working directory, defaults to /tmp/gcov
43 The directory will be removed after running without --keep if it
44 is empty, use caution if specifying existing system directories!
47 Run pyagl tests for binding, enabled by default
50 Run afm-test test widget tests for binding.
51 If specified, disables pyagl tests; note that the last argument in
52 the command-line will take precedence.
55 Test command to use in place of pyagl or afm-test, should be quote
56 If specified, disables pyagl and afm-test tests.
61 # Helper to validate widget install dir
63 if [ ! \( -d $1 -o -f $1/config.xml \) ]; then
64 echo "ERROR: No widget install at $1"
66 elif [ ! -d $1/src ]; then
67 echo "ERROR: No source in $1/src"
72 # Helper to run gcovr inside mount namespace environment
74 wgt_install_dir=/var/local/lib/afm/applications/$1
75 check_wgt_install ${wgt_install_dir}
77 if [ ! -d $workdir/$1 ]; then
78 echo "ERROR: No coverage data in $workdir/$1"
82 # Get original source path
83 gcno=$(cd $workdir/$1 && find -name '*.gcno' | head -n 1 | cut -d/ -f2-)
84 if [ -z "$gcno" ]; then
85 echo "ERROR: no gcno file found in $workdir/$1"
88 srcfile=$(strings $workdir/$1/${gcno} | grep "$(basename ${gcno%.gcno})$" | uniq)
89 srcdir=$(echo $srcfile | sed "s|/${gcno%%/*}/.*$||")
91 # Set up mounts for chroot to run gcovr in
92 # NOTE: We do not unmount these later, as we assume we are in a
93 # private mount namespace and they will go away on exit from
95 echo "Setting up mounts"
97 echo $tmpdir > $workdir/.runner_tmpdir
98 mkdir -p $tmpdir/{lower,upper,work,merged}
99 # NOTE: Could potentially use rbind here, but explicitly mounting
100 # just what we need seems safer
101 mount --bind / $tmpdir/lower
102 mount -t overlay -o lowerdir=$tmpdir/lower,upperdir=$tmpdir/upper,workdir=$tmpdir/work overlay $tmpdir/merged
103 mount --bind /proc $tmpdir/merged/proc
104 mount --bind /sys $tmpdir/merged/sys
105 mount --bind /dev $tmpdir/merged/dev
106 mount --bind /tmp $tmpdir/merged/tmp
107 # Bind in the data files
108 # NOTE: $workdir is bound instead of specifically just $workdir/$1,
109 # so that e.g. html output to another directory in /tmp will
110 # work as expected. A determined user may be able to shoot
111 # themselves in the foot, but for now the trade off seems
113 mkdir -p $tmpdir/merged/$workdir
114 mount --bind $workdir $tmpdir/merged/$workdir
115 # Bind the source files to their expected location
116 mkdir -p $tmpdir/merged/$srcdir
117 mount --bind ${wgt_install_dir}/src $tmpdir/merged/$srcdir
119 echo "Entering chroot"
121 exec chroot $tmpdir/merged \
122 /usr/bin/gcovr -r $srcdir --object-directory $workdir/$1 --gcov-filter-source-errors -s ${GCOV_RUNNER_GCOVR_OPTIONS}
125 # Helper to clean up after runner
126 gcovr_runner_cleanup() {
128 if [ -f $workdir/.runner_tmpdir ]; then
129 tmpdir=$(cat $workdir/.runner_tmpdir)
131 rm -f $workdir/.runner_tmpdir
133 if [ "$workdir" != "/tmp" ]; then
134 rmdir $workdir 2>/dev/null || true
139 OPTS=$(getopt -o +hko:pw:c: --longoptions gcovr-runner,afm-test,command:,help,keep,gcovr-options:,pyagl,workdir: -n "$(basename $0)" -- "$@")
140 if [ $? -ne 0 ]; then
156 --gcovr-runner) runner=true; shift;;
157 --afm-test) afmtest=true; pyagl=false; shift;;
158 -c|--command) cmd="$2"; shift; shift;;
159 -h|--help) usage; exit 0;;
160 -k|--keep) keep=true; shift;;
161 -o|--gcovr-options) options="$2"; shift; shift;;
162 -p|--pyagl) pyagl=true; afmtest=false; shift;;
163 -w|--workdir) workdir="$2"; shift; shift;;
169 # Encode the assumption that a specified command means it runs instead
170 # of any other tests.
171 if [ -s "$cmd" ]; then
176 if [ $# -ne 1 ]; then
177 # Always expect widget name as single non-option argument
182 # Rationalize workdir just in case
183 workdir=$(realpath "$workdir")
185 if [ "$runner" = "true" ]; then
186 if [ "${GCOV_RUNNER_READY}" != "true" ]; then
187 echo "ERROR: gcovr environment not ready!"
191 # If we get here, it'd be an error, so return 1
196 if [ "${1%.wgt}" != "$1" ]; then
197 # User has specified path to a widget file
199 binding=$(basename "${1%-coverage.wgt}")
201 wgt=/usr/AGL/apps/coverage/${binding}-coverage.wgt
203 if [ ! -f $wgt ]; then
204 echo "ERROR: No widget $wgt"
206 elif [ "$afmtest" = "true" -a ! -f /usr/AGL/apps/test/${binding}-test.wgt ]; then
207 echo "ERROR: No test widget for $binding"
211 # Determine starting systemd unit name
212 service=$(systemctl --all |grep afm-service-$binding |sed 's/^[ *] \([^ ]*\).*/\1/')
213 if [ -z "$service" ]; then
214 echo "ERROR: Could not determine systemd service unit for $binding"
218 # Install coverage widget
219 echo "Removing $binding widget"
220 systemctl stop $service
221 afm-util remove $binding
223 echo "Installing $binding coverage widget"
224 afm-util install $wgt
227 wgt_install_dir=/var/local/lib/afm/applications/$binding
228 check_wgt_install ${wgt_install_dir}
229 gcov_src=${wgt_install_dir}/coverage
230 if [ ! -d ${gcov_src} ]; then
231 echo "ERROR: No coverage information in ${gcov_src}"
233 elif [ ! -f ${gcov_src}/gcov.env ]; then
234 echo "ERROR: No gcov environment file at ${gcov_src}/gcov.env"
239 # NOTE: In theory, the coverage data collection could be done inside
240 # the mount namespace / chroot, but the potential for issues
241 # when doing that seems higher than just running gcovr there,
242 # so a conservative approach is taken.
245 # Set up things for the binary to write out gcda data files
247 # Having the matching build directory hierarchy in place and
248 # writeable by the target binary before any restart and testing is
249 # key to things working.
251 # As well, the environment file with the GCOV_PREFIX and
252 # GCOV_PREFIX_STRIP values needs to be present before running so the
253 # gcda files will get written into the relocated build hierarchy.
255 echo "Installing coverage information for $binding"
257 rm -rf $workdir/$binding
258 cp -dr ${gcov_src} $workdir/$binding
259 chsmack -r -a System::Log $workdir
260 chmod -R go+w $workdir
262 # Install the gcov environment file
263 mkdir -p /etc/afm/widget.env.d/$binding
264 if [ "${workdir}" = "/tmp/gcov" ]; then
265 cp ${gcov_src}/gcov.env /etc/afm/widget.env.d/$binding/gcov
267 # Update GCOV_PREFIX to point into workdir
268 sed "s|^GCOV_PREFIX=.*|GCOV_PREFIX=${workdir}/$binding|" ${gcov_src}/gcov.env > /etc/afm/widget.env.d/$binding/gcov
270 chsmack -r -a _ /etc/afm/widget.env.d/$binding
272 # Determine new systemd unit name (version may now be different)
273 service=$(systemctl --all |grep afm-service-$binding |sed 's/^[ *] \([^ ]*\).*/\1/')
274 if [ -z "$service" ]; then
275 echo "ERROR: Could not determine systemd service unit for $binding"
279 # Restart the binding
280 systemctl start $service
283 # Run tests or given command
284 if [ -n "$cmd" ]; then
285 echo "Running command: $cmd"
286 export AGL_AVAILABLE_INTERFACES=${AGL_AVAILABLE_INTERFACES:-ethernet}
288 elif [ "$pyagl" = "true" ]; then
289 echo "Running $binding pyagl tests"
290 export AGL_AVAILABLE_INTERFACES=${AGL_AVAILABLE_INTERFACES:-ethernet}
291 pytest -k "${binding#agl-service-} and not hwrequired" /usr/lib/python3.?/site-packages/pyagl
293 echo "Running $binding test widget"
294 # NOTE: su to agl-driver is required here to avoid fallout from
295 # the "afm-util run" in afm-test seemingly triggering the
296 # start of other per-user bindings for the root user.
297 su -l -c "/usr/bin/afm-test /usr/AGL/apps/test/${binding}-test.wgt" agl-driver
300 # Restart again to trigger data file writing
301 systemctl restart $service
304 # Run ourselves in gcovr runner mode inside a private mount namespace
305 export GCOV_RUNNER_READY=true
306 # NOTE: Passing gcovr options in the environment to avoid quoting hassles
307 export GCOV_RUNNER_GCOVR_OPTIONS="$options"
308 runner_options="--workdir ${workdir}"
309 unshare -m $0 --gcovr-runner ${runner_options} $binding
312 if [ "$keep" != "true" ]; then
313 # Clean up after ourselves
314 gcovr_runner_cleanup $1
315 rm -f /etc/afm/widget.env.d/$1/gcov
316 rmdir /etc/afm/widget.env.d/$1 2>/dev/null || true