Merge pull request #25120 from bluca/test_machineid

unit tests: do not fail when machine-id is missing
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-10-26 07:43:48 +02:00
committed by GitHub
12 changed files with 54 additions and 26 deletions

View File

@@ -63,6 +63,11 @@ for phase in "${PHASES[@]}"; do
else
MESON_ARGS+=(-Dmode=release --optimization=2)
fi
# Some variation: remove machine-id, like on Debian builders to ensure unit tests still work.
if [ -w /etc/machine-id ]; then
mv /etc/machine-id /etc/machine-id.bak
fi
fi
# The install_tag feature introduced in 0.60 causes meson to fail with fatal-meson-warnings
# "Project targeting '>= 0.53.2' but tried to use feature introduced in '0.60.0': install_tag arg in custom_target"
@@ -113,6 +118,9 @@ for phase in "${PHASES[@]}"; do
;;
CLEANUP)
info "Cleanup phase"
if [ ! -f /etc/machine-id ] && [ -w /etc/machine-id.bak ]; then
mv /etc/machine-id.bak /etc/machine-id
fi
;;
*)
echo >&2 "Unknown phase '$phase'"

View File

@@ -342,7 +342,7 @@ TEST(sd_device_enumerator_filter_subsystem) {
/* The test test_sd_device_enumerator_filter_subsystem_trial() is quite racy. Let's run the function
* several times after the udev queue becomes empty. */
if (!udev_available()) {
if (!udev_available() || (access("/run/udev", F_OK) < 0 && errno == ENOENT)) {
assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
return;
}

View File

@@ -247,8 +247,12 @@ TEST(condition_test_host) {
_cleanup_free_ char *hostname = NULL;
Condition *condition;
sd_id128_t id;
int r;
assert_se(sd_id128_get_machine(&id) >= 0);
r = sd_id128_get_machine(&id);
if (IN_SET(r, -ENOENT, -ENOMEDIUM))
return (void) log_tests_skipped("/etc/machine-id missing");
assert_se(r >= 0);
condition = condition_new(CONDITION_HOST, SD_ID128_TO_STRING(id), false, false);
assert_se(condition);

View File

@@ -216,7 +216,7 @@ TEST(chase_symlinks) {
result = mfree(result);
r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result, NULL);
assert_se(r == -ENOTDIR);
assert_se(IN_SET(r, -ENOTDIR, -ENOENT));
result = mfree(result);
/* Path that loops back to self */

View File

@@ -38,7 +38,7 @@ TEST(id128) {
assert_se(!sd_id128_in_set(id, ID128_WALDI));
assert_se(!sd_id128_in_set(id, ID128_WALDI, ID128_WALDI));
if (sd_booted() > 0) {
if (sd_booted() > 0 && access("/etc/machine-id", F_OK) >= 0) {
assert_se(sd_id128_get_machine(&id) == 0);
printf("machine: %s\n", sd_id128_to_string(id, t));
@@ -145,11 +145,13 @@ TEST(id128) {
assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id) >= 0);
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), &id2) >= 0);
assert_se(!sd_id128_equal(id, id2));
if (sd_booted() > 0 && access("/etc/machine-id", F_OK) >= 0) {
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id) >= 0);
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), &id2) >= 0);
assert_se(!sd_id128_equal(id, id2));
}
}
TEST(sd_id128_get_invocation) {
@@ -168,6 +170,9 @@ TEST(benchmark_sd_id128_get_machine_app_specific) {
unsigned iterations = slow_tests_enabled() ? 1000000 : 1000;
usec_t t, q;
if (access("/etc/machine-id", F_OK) < 0 && errno == ENOENT)
return (void) log_tests_skipped("/etc/machine-id does not exist");
log_info("/* %s (%u iterations) */", __func__, iterations);
sd_id128_t id = ID128_WALDI, id2;

View File

@@ -518,8 +518,10 @@ TEST(install_printf, .sd_booted = true) {
_cleanup_free_ char *mid = NULL, *bid = NULL, *host = NULL, *gid = NULL, *group = NULL, *uid = NULL, *user = NULL;
assert_se(specifier_machine_id('m', NULL, NULL, NULL, &mid) >= 0 && mid);
assert_se(specifier_boot_id('b', NULL, NULL, NULL, &bid) >= 0 && bid);
if (access("/etc/machine-id", F_OK) >= 0)
assert_se(specifier_machine_id('m', NULL, NULL, NULL, &mid) >= 0 && mid);
if (sd_booted() > 0)
assert_se(specifier_boot_id('b', NULL, NULL, NULL, &bid) >= 0 && bid);
assert_se(host = gethostname_malloc());
assert_se(group = gid_to_name(getgid()));
assert_se(asprintf(&gid, UID_FMT, getgid()) >= 0);

View File

@@ -74,7 +74,7 @@ TEST(specifier_printf) {
assert_se(streq(w, "xxx a=AAAA b=BBBB e= yyy"));
free(w);
r = specifier_printf("machine=%m, boot=%b, host=%H, pretty=%q, version=%v, arch=%a, empty=%e", SIZE_MAX, table, NULL, NULL, &w);
r = specifier_printf("boot=%b, host=%H, pretty=%q, version=%v, arch=%a, empty=%e", SIZE_MAX, table, NULL, NULL, &w);
assert_se(r >= 0);
assert_se(w);
puts(w);
@@ -127,13 +127,18 @@ TEST(specifier_real_path_missing_file) {
}
TEST(specifiers) {
int r;
for (const Specifier *s = specifier_table; s->specifier; s++) {
char spec[3];
_cleanup_free_ char *resolved = NULL;
xsprintf(spec, "%%%c", s->specifier);
assert_se(specifier_printf(spec, SIZE_MAX, specifier_table, NULL, NULL, &resolved) >= 0);
r = specifier_printf(spec, SIZE_MAX, specifier_table, NULL, NULL, &resolved);
if (s->specifier == 'm' && IN_SET(r, -ENOENT, -ENOMEDIUM)) /* machine-id might be missing in build chroots */
continue;
assert_se(r >= 0);
log_info("%%%c → %s", s->specifier, resolved);
}

View File

@@ -235,9 +235,9 @@ TEST(unit_name_mangle) {
TEST_RET(unit_printf, .sd_booted = true) {
_cleanup_free_ char
*architecture, *os_image_version, *boot_id, *os_build_id,
*architecture, *os_image_version, *boot_id = NULL, *os_build_id,
*hostname, *short_hostname, *pretty_hostname,
*machine_id, *os_image_id, *os_id, *os_version_id, *os_variant_id,
*machine_id = NULL, *os_image_id, *os_id, *os_version_id, *os_variant_id,
*user, *group, *uid, *gid, *home, *shell,
*tmp_dir, *var_tmp_dir;
_cleanup_(manager_freep) Manager *m = NULL;
@@ -252,16 +252,20 @@ TEST_RET(unit_printf, .sd_booted = true) {
assert_se(specifier_architecture('a', NULL, NULL, NULL, &architecture) >= 0);
assert_se(architecture);
assert_se(specifier_os_image_version('A', NULL, NULL, NULL, &os_image_version) >= 0);
assert_se(specifier_boot_id('b', NULL, NULL, NULL, &boot_id) >= 0);
assert_se(boot_id);
if (sd_booted() > 0) {
assert_se(specifier_boot_id('b', NULL, NULL, NULL, &boot_id) >= 0);
assert_se(boot_id);
}
assert_se(specifier_os_build_id('B', NULL, NULL, NULL, &os_build_id) >= 0);
assert_se(hostname = gethostname_malloc());
assert_se(specifier_short_hostname('l', NULL, NULL, NULL, &short_hostname) == 0);
assert_se(short_hostname);
assert_se(specifier_pretty_hostname('q', NULL, NULL, NULL, &pretty_hostname) == 0);
assert_se(pretty_hostname);
assert_se(specifier_machine_id('m', NULL, NULL, NULL, &machine_id) >= 0);
assert_se(machine_id);
if (access("/etc/machine-id", F_OK) >= 0) {
assert_se(specifier_machine_id('m', NULL, NULL, NULL, &machine_id) >= 0);
assert_se(machine_id);
}
assert_se(specifier_os_image_id('M', NULL, NULL, NULL, &os_image_id) >= 0);
assert_se(specifier_os_id('o', NULL, NULL, NULL, &os_id) >= 0);
assert_se(specifier_os_version_id('w', NULL, NULL, NULL, &os_version_id) >= 0);
@@ -312,12 +316,14 @@ TEST_RET(unit_printf, .sd_booted = true) {
/* normal unit */
expect(u, "%a", architecture);
expect(u, "%A", os_image_version);
expect(u, "%b", boot_id);
if (boot_id)
expect(u, "%b", boot_id);
expect(u, "%B", os_build_id);
expect(u, "%H", hostname);
expect(u, "%l", short_hostname);
expect(u, "%q", pretty_hostname);
expect(u, "%m", machine_id);
if (machine_id)
expect(u, "%m", machine_id);
expect(u, "%M", os_image_id);
expect(u, "%o", os_id);
expect(u, "%w", os_version_id);

View File

@@ -27,7 +27,6 @@ ExecStart=sh -c 'test %g = $$(id -gn)'
ExecStart=sh -c 'test %G = $$(id -g)'
ExecStart=test %h = /root
ExecStart=sh -c 'test -x %s'
ExecStart=sh -c 'test %m = $$(cat /etc/machine-id)'
ExecStart=sh -c 'test %b = $$(cat /proc/sys/kernel/random/boot_id | sed -e 's/-//g')'
ExecStart=sh -c 'test %H = $$(uname -n)'
ExecStart=sh -c 'test %v = $$(uname -r)'

View File

@@ -24,7 +24,6 @@ ExecStart=sh -c 'test %g = $$(id -gn)'
ExecStart=sh -c 'test %G = $$(id -g)'
ExecStart=test %h = /root
ExecStart=sh -c 'test -x %s'
ExecStart=sh -c 'test %m = $$(cat /etc/machine-id)'
ExecStart=sh -c 'test %b = $$(cat /proc/sys/kernel/random/boot_id | sed -e 's/-//g')'
ExecStart=sh -c 'test %H = $$(uname -n)'
ExecStart=sh -c 'test %v = $$(uname -r)'

View File

@@ -87,7 +87,7 @@ def test_content(line, expected, *, user, extra={}, subpath='/arg', path_cb=None
def test_valid_specifiers(*, user):
test_content('f {} - - - - two words', 'two words', user=user)
if id128:
if id128 and os.path.isfile('/etc/machine-id'):
try:
test_content('f {} - - - - %m', '{}'.format(id128.get_machine().hex), user=user)
except AssertionError as e:

View File

@@ -1980,8 +1980,8 @@ EOF
not_exp_links => ["notthere"],
}],
rules => <<EOF
TEST=="/etc/machine-id", SYMLINK+="there"
TEST!="/etc/machine-id", SYMLINK+="notthere"
TEST=="/etc/passwd", SYMLINK+="there"
TEST!="/etc/passwd", SYMLINK+="notthere"
EOF
},
{