tests: skip tests when cg_pid_get_path fails (#7033)

v2:
- cast the fstype_t type to ull, because it varies between arches.
  Making it long long should be on the safe side.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-10-10 20:55:20 +02:00 committed by GitHub
parent b74023db06
commit 651d47d14b
11 changed files with 59 additions and 18 deletions

View File

@ -2456,8 +2456,11 @@ static int cg_unified_update(void) {
return -ENOMEDIUM;
unified_cache = CGROUP_UNIFIED_NONE;
}
} else
} else {
log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
(unsigned long long) fs.f_type);
return -ENOMEDIUM;
}
return 0;
}

View File

@ -49,7 +49,12 @@ int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
log_notice("cgroupfs not available, skipping tests");
return EXIT_TEST_SKIP;
}
assert_se(set_unit_path(get_testdata_dir("")) >= 0);
assert_se(runtime_dir = setup_fake_runtime_dir());

View File

@ -34,7 +34,11 @@ static int test_cgroup_mask(void) {
FDSet *fdset = NULL;
int r;
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
puts("Skipping test: cgroupfs not available");
return EXIT_TEST_SKIP;
}
/* Prepare the manager. */
assert_se(set_unit_path(get_testdata_dir("")) >= 0);

View File

@ -37,7 +37,11 @@ int main(int argc, char *argv[]) {
Job *j;
int r;
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
log_notice_errno(r, "Skipping test: cgroupfs not available");
return EXIT_TEST_SKIP;
}
/* prepare the test */
assert_se(set_unit_path(get_testdata_dir("")) >= 0);

View File

@ -528,11 +528,15 @@ int main(int argc, char *argv[]) {
/* It is needed otherwise cgroup creation fails */
if (getuid() != 0) {
printf("Skipping test: not root\n");
puts("Skipping test: not root");
return EXIT_TEST_SKIP;
}
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
puts("Skipping test: cgroupfs not available");
return EXIT_TEST_SKIP;
}
assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
assert_se(set_unit_path(get_testdata_dir("/test-execute")) >= 0);

View File

@ -22,20 +22,25 @@
#include "alloc-util.h"
#include "cgroup-util.h"
void enter_cgroup_subroot(void) {
int enter_cgroup_subroot(void) {
_cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL;
CGroupMask supported;
int r;
r = cg_pid_get_path(NULL, 0, &cgroup_root);
if (r == -ENOMEDIUM)
return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m");
assert(r >= 0);
assert_se(cg_pid_get_path(NULL, 0, &cgroup_root) >= 0);
assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0);
assert_se(cg_mask_supported(&supported) >= 0);
/* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if we handle
* any errors at that point. */
if (cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot) < 0)
return;
r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot);
if (r < 0)
return r;
if (cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL) < 0)
return;
return cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL);
}

View File

@ -40,4 +40,4 @@
-ENOMEDIUM /* cannot determine cgroup */ \
)
void enter_cgroup_subroot(void);
int enter_cgroup_subroot(void);

View File

@ -45,7 +45,11 @@ static int setup_test(Manager **m) {
assert_se(m);
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
log_notice_errno(r, "Skipping test: cgroupfs not available");
return EXIT_TEST_SKIP;
}
r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &tmp);
if (MANAGER_SKIP_TEST(r)) {

View File

@ -34,7 +34,11 @@ int main(int argc, char *argv[]) {
FDSet *fdset = NULL;
int r;
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
log_notice_errno(r, "Skipping test: cgroupfs not available");
return EXIT_TEST_SKIP;
}
/* prepare the test */
assert_se(set_unit_path(get_testdata_dir("")) >= 0);

View File

@ -858,7 +858,11 @@ int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
log_notice_errno(r, "Skipping test: cgroupfs not available");
return EXIT_TEST_SKIP;
}
assert_se(runtime_dir = setup_fake_runtime_dir());

View File

@ -465,12 +465,16 @@ static void test_unit_name_path_unescape(void) {
int main(int argc, char* argv[]) {
_cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
int rc = 0;
int r, rc = 0;
log_parse_environment();
log_open();
enter_cgroup_subroot();
r = enter_cgroup_subroot();
if (r == -ENOMEDIUM) {
log_notice_errno(r, "Skipping test: cgroupfs not available");
return EXIT_TEST_SKIP;
}
assert_se(runtime_dir = setup_fake_runtime_dir());