[systemd-devel] [PATCH] Skip tests that depend on /etc/machine-id if it is not present
Ramkumar Ramachandra
artagnon at gmail.com
Fri Nov 30 05:17:42 PST 2012
The following tests fail if /etc/machine-id is not present:
$ ./test-id128
random: a08ea8ed34594d4bbd953dd182ec86f9
Assertion 'sd_id128_get_machine(&id) == 0' failed at
src/test/test-id128.c:41, function main(). Aborting.
[1] 8017 abort (core dumped) ./test-id128
$ ./test-journal
Assertion 'journal_file_open("test.journal", O_RDWR|O_CREAT, 0666,
true, true, NULL, NULL, NULL, &f) == 0' failed at
src/journal/test-journal.c:46, function main(). Aborting.
[1] 8059 abort (core dumped) ./test-journal
$ ./test-journal-stream
Assertion 'journal_file_open("one.journal", O_RDWR|O_CREAT, 0666,
true, false, NULL, NULL, NULL, &one) == 0' failed at
src/journal/test-journal-stream.c:88, function main(). Aborting.
[1] 8107 abort (core dumped) ./test-journal-stream
$ ./test-journal-verify
Generating...
Assertion 'journal_file_open("test.journal", O_RDWR|O_CREAT, 0666,
true, !!verification_key, NULL, NULL, NULL, &f) == 0' failed at
src/journal/test-journal-verify.c:87, function main(). Aborting.
[1] 8154 abort (core dumped) ./test-journal-verify
This is because they call sd_id128_get_machine() which barfs if
/etc/machine-id can't be open()'ed. Treat this as a special case and
skip the dependent tests instead of failing them.
Signed-off-by: Ramkumar Ramachandra <artagnon at gmail.com>
---
Ramkumar Ramachandra wrote:
> Kay Sievers wrote:
>> On Fri, Nov 30, 2012 at 1:28 PM, Zbigniew Jędrzejewski-Szmek
>> <zbyszek at in.waw.pl> wrote:
>>> I haven't looked at the details of the patch, but
>>> I think that it would be nice to support testing before the first run
>>> with systemd: the tests should support missing /etc/machine-id.
>>
>> Don't know if it matters that much. Failing the test with a proper
>> error message sounds reasonable good enough to me.
>
> How do you propose we do that? /etc/machine-id is hardcoded in
> src/libsystemd-id128/sd-id128.c:107. Instead of returning -errno, we
> can return a special error value when /etc/machine-id can't be
> open()'ed, and cascade that value upwards from callers, finally
> handling it in the test.
This is a patch that does exactly that.
src/journal/test-journal-stream.c | 5 +++++
src/journal/test-journal-verify.c | 5 +++++
src/journal/test-journal.c | 5 +++++
src/libsystemd-id128/sd-id128.c | 2 +-
src/test/test-id128.c | 8 ++++++--
5 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c
index b3e816d..eaf9daa 100644
--- a/src/journal/test-journal-stream.c
+++ b/src/journal/test-journal-stream.c
@@ -85,6 +85,11 @@ int main(int argc, char *argv[]) {
assert_se(mkdtemp(t));
assert_se(chdir(t) >= 0);
+ if (journal_file_open("one.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &one) == -128) {
+ printf("skipping test: /etc/machine-id not preset\n");
+ return 0;
+ }
+
assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &one) == 0);
assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &two) == 0);
assert_se(journal_file_open("three.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &three) == 0);
diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c
index b667721..00068fb 100644
--- a/src/journal/test-journal-verify.c
+++ b/src/journal/test-journal-verify.c
@@ -84,6 +84,11 @@ int main(int argc, char *argv[]) {
log_info("Generating...");
+ if (journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, !!verification_key, NULL, NULL, NULL, &f) == -128) {
+ printf("skipping test: /etc/machine-id not preset\n");
+ return 0;
+ }
+
assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, !!verification_key, NULL, NULL, NULL, &f) == 0);
for (n = 0; n < N_ENTRIES; n++) {
diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c
index f4dc52c..73c7554 100644
--- a/src/journal/test-journal.c
+++ b/src/journal/test-journal.c
@@ -43,6 +43,11 @@ int main(int argc, char *argv[]) {
assert_se(mkdtemp(t));
assert_se(chdir(t) >= 0);
+ if (journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, true, NULL, NULL, NULL, &f) == -128) {
+ printf("skipping test: /etc/machine-id not preset\n");
+ return 0;
+ }
+
assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, true, NULL, NULL, NULL, &f) == 0);
dual_timestamp_get(&ts);
diff --git a/src/libsystemd-id128/sd-id128.c b/src/libsystemd-id128/sd-id128.c
index 4286ae7..4e5aaad 100644
--- a/src/libsystemd-id128/sd-id128.c
+++ b/src/libsystemd-id128/sd-id128.c
@@ -106,7 +106,7 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) {
fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
- return -errno;
+ return -128; /* Special return value */
k = loop_read(fd, buf, 32, false);
close_nointr_nofail(fd);
diff --git a/src/test/test-id128.c b/src/test/test-id128.c
index bfd743e..ea621f9 100644
--- a/src/test/test-id128.c
+++ b/src/test/test-id128.c
@@ -38,8 +38,12 @@ int main(int argc, char *argv[]) {
assert_se(sd_id128_from_string(t, &id2) == 0);
assert_se(sd_id128_equal(id, id2));
- assert_se(sd_id128_get_machine(&id) == 0);
- printf("machine: %s\n", sd_id128_to_string(id, t));
+ if (sd_id128_get_machine(&id) == -128)
+ printf("skipping test: /etc/machine-id not preset\n");
+ else {
+ assert_se(sd_id128_get_machine(&id) == 0);
+ printf("machine: %s\n", sd_id128_to_string(id, t));
+ }
assert_se(sd_id128_get_boot(&id) == 0);
printf("boot: %s\n", sd_id128_to_string(id, t));
--
1.7.8.1.362.g5d6df.dirty
More information about the systemd-devel
mailing list