Merge pull request #10134 from keszybz/test-runner

Some test-related fixed and a test runner for installed tests
This commit is contained in:
Lennart Poettering 2018-10-05 20:35:30 +02:00 committed by GitHub
commit ad191df836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 25 deletions

View File

@ -471,7 +471,7 @@ int bind_remount_recursive_with_mountinfo(const char *prefix, bool ro, char **bl
if (path_startswith(p, *i)) {
blacklisted = true;
log_debug("Not remounting %s, because blacklisted by %s, called for %s", p, *i, cleaned);
log_debug("Not remounting %s blacklisted by %s, called for %s", p, *i, cleaned);
break;
}
}

View File

@ -17,6 +17,7 @@
#include "selinux-util.h"
#include "signal-util.h"
#include "string-util.h"
#include "tests.h"
#include "udev.h"
static int fake_filesystems(void) {
@ -26,27 +27,26 @@ static int fake_filesystems(void) {
const char *error;
bool ignore_mount_error;
} fakefss[] = {
{ "test/tmpfs/sys", "/sys", "failed to mount test /sys", false },
{ "test/tmpfs/dev", "/dev", "failed to mount test /dev", false },
{ "test/run", "/run", "failed to mount test /run", false },
{ "test/run", "/etc/udev/rules.d", "failed to mount empty /etc/udev/rules.d", true },
{ "test/run", UDEVLIBEXECDIR "/rules.d", "failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
{ "test/tmpfs/sys", "/sys", "Failed to mount test /sys", false },
{ "test/tmpfs/dev", "/dev", "Failed to mount test /dev", false },
{ "test/run", "/run", "Failed to mount test /run", false },
{ "test/run", "/etc/udev/rules.d", "Failed to mount empty /etc/udev/rules.d", true },
{ "test/run", UDEVLIBEXECDIR "/rules.d", "Failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
};
unsigned int i;
unsigned i;
if (unshare(CLONE_NEWNS) < 0)
return log_error_errno(errno, "failed to call unshare(): %m");
return log_error_errno(errno, "Failed to call unshare(): %m");
if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
return log_error_errno(errno, "failed to mount / as private: %m");
return log_error_errno(errno, "Failed to mount / as private: %m");
for (i = 0; i < ELEMENTSOF(fakefss); i++) {
for (i = 0; i < ELEMENTSOF(fakefss); i++)
if (mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL) < 0) {
log_full_errno(fakefss[i].ignore_mount_error ? LOG_DEBUG : LOG_ERR, errno, "%s: %m", fakefss[i].error);
if (!fakefss[i].ignore_mount_error)
return -errno;
}
}
return 0;
}
@ -55,48 +55,46 @@ int main(int argc, char *argv[]) {
_cleanup_(udev_event_unrefp) struct udev_event *event = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
_cleanup_(udev_rules_unrefp) struct udev_rules *rules = NULL;
char syspath[UTIL_PATH_SIZE];
const char *devpath;
const char *action;
int err;
const char *devpath, *action;
int r;
log_parse_environment();
log_open();
test_setup_logging(LOG_INFO);
err = fake_filesystems();
if (err < 0)
r = fake_filesystems();
if (r < 0)
return EXIT_FAILURE;
log_debug("version %s", PACKAGE_VERSION);
mac_selinux_init();
action = argv[1];
if (action == NULL) {
if (!action) {
log_error("action missing");
goto out;
}
devpath = argv[2];
if (devpath == NULL) {
if (!devpath) {
log_error("devpath missing");
goto out;
}
rules = udev_rules_new(1);
strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
const char *syspath = strjoina("/sys", devpath);
dev = udev_device_new_from_synthetic_event(NULL, syspath, action);
if (dev == NULL) {
if (!dev) {
log_debug("unknown device '%s'", devpath);
goto out;
}
event = udev_event_new(dev);
assert_se(event);
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
/* do what devtmpfs usually provides us */
if (udev_device_get_devnode(dev) != NULL) {
if (udev_device_get_devnode(dev)) {
mode_t mode = 0600;
if (streq(udev_device_get_subsystem(dev), "block"))
@ -122,5 +120,5 @@ int main(int argc, char *argv[]) {
out:
mac_selinux_finish();
return err ? EXIT_FAILURE : EXIT_SUCCESS;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -230,6 +230,14 @@ endif
############################################################
if install_tests
install_data('run-unit-tests.py',
install_mode : 'rwxr-xr-x',
install_dir : testsdir)
endif
############################################################
# prepare test/sys tree
sys_script_py = find_program('sys-script.py')
custom_target(

61
test/run-unit-tests.py Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/env python3
import argparse
import dataclasses
import glob
import os
import subprocess
import sys
try:
import colorama as c
GREEN = c.Fore.GREEN
YELLOW = c.Fore.YELLOW
RED = c.Fore.RED
RESET_ALL = c.Style.RESET_ALL
BRIGHT = c.Style.BRIGHT
except ImportError:
GREEN = YELLOW = RED = RESET_ALL = BRIGHT = ''
@dataclasses.dataclass
class Total:
total:int
good:int = 0
skip:int = 0
fail:int = 0
def argument_parser():
p = argparse.ArgumentParser()
p.add_argument('-u', '--unsafe', action='store_true',
help='run "unsafe" tests too')
return p
opts = argument_parser().parse_args()
tests = glob.glob('/usr/lib/systemd/tests/test-*')
if opts.unsafe:
tests += glob.glob('/usr/lib/systemd/tests/unsafe/test-*')
total = Total(total=len(tests))
for test in tests:
name = os.path.basename(test)
ex = subprocess.run(test, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if ex.returncode == 0:
print(f'{GREEN}PASS: {name}{RESET_ALL}')
total.good += 1
elif ex.returncode == 77:
print(f'{YELLOW}SKIP: {name}{RESET_ALL}')
total.skip += 1
else:
print(f'{RED}FAIL: {name}{RESET_ALL}')
total.fail += 1
# stdout/stderr might not be valid unicode, let's just dump it to the terminal.
# Also let's reset the style afterwards, in case our output sets something.
sys.stdout.buffer.write(ex.stdout)
print(f'{RESET_ALL}{BRIGHT}')
sys.stdout.buffer.write(ex.stderr)
print(f'{RESET_ALL}')
print(f'{BRIGHT}OK: {total.good} SKIP: {total.skip} FAIL: {total.fail}{RESET_ALL}')
sys.exit(total.fail > 0)