Home > front end >  meson: determine running tests as root
meson: determine running tests as root

Time:11-12

In tests for ping from iputils certain tests should fail for non-root but pass for root. Thus I need a detection whether user running tests is root or not. Current code:

run_as_root = false
r = run_command('id', '-u')
if r.stdout().strip().to_int() == 0
  message('running as root')
  run_as_root = true
else
  message('running as normal user')
endif

...
test(name, cmd, args : args, should_fail : not run_as_root)

is not working, because test is done during build phase:

$ meson builddir
The Meson build system
Version: 0.59.4
...
Program xsltproc found: YES (/usr/bin/xsltproc)
Message: running as normal user

and not for running tests because root user is not properly detected:

# cd builddir/ && meson test
[21/21] Linking target ninfod/ninfod
  1/36 arping -V                             OK              0.03s
...
32/36 ping -c1 -i0.001 127.0.0.1            UNEXPECTEDPASS  0.02s
>>> ./builddir/ping/ping -c1 -i0.001 127.0.0.1

33/36 ping -c1 -i0.001 __1                  UNEXPECTEDPASS  0.02s

What to do to evaluate user when running tests?

CodePudding user response:

This is really a case for skipping rather than expected failure. It would be easy to wrap your tests in a small shell or python script that checks the effective UID and returns the magic exit code 127 (which meson interprets as skip)

Something like:

#!/bin/bash

if [ "$(id -u)" -ne 0 ]; then
    echo "User does not have root, cannot run"
    exit 127
fi
exec "$@"

This will cause meson test to return a status of SKIP if the tests are not run as root.

  • Related