[systemd-devel] [PATCH v2] Skip tests that depend on /etc/machine-id if it is not present

Ramkumar Ramachandra artagnon at gmail.com
Fri Nov 30 06:44:20 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.
---
 Zbigniew Jędrzejewski-Szmek wrote:
 > On Fri, Nov 30, 2012 at 06:47:42PM +0530, Ramkumar Ramachandra wrote:
 >> The following tests fail if /etc/machine-id is not present:
 >> +        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,Hm, this seems fairly intrusive. What about just adding a simple
 > test in the Makefile, which adds this test to the list of tests
 > only if /etc/machine-id does not exist?
 
 I wouldn't like that.  I don't want to have to regenerate my Makefile
 after installing /etc/machine-id.  I don't think it's elegant to have
 a different number of tests depending on different conditions.
 
 How about this patch as an alternative?  I call sd_id_get_machine()
 before anything else happens.

 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..b3dd140 100644
--- a/src/journal/test-journal-stream.c
+++ b/src/journal/test-journal-stream.c
@@ -80,6 +80,11 @@ int main(int argc, char *argv[]) {
         const void *data;
         size_t l;
 
+        if (sd_id128_get_machine(&id) == -128) {
+                printf("skipping test: /etc/machine-id not preset\n");
+                return 0;
+        }
+
         log_set_max_level(LOG_DEBUG);
 
         assert_se(mkdtemp(t));
diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c
index b667721..54dff9b 100644
--- a/src/journal/test-journal-verify.c
+++ b/src/journal/test-journal-verify.c
@@ -77,6 +77,11 @@ int main(int argc, char *argv[]) {
         struct stat st;
         uint64_t p;
 
+        if (sd_id128_get_machine(&id) == -128) {
+                printf("skipping test: /etc/machine-id not preset\n");
+                return 0;
+        }
+
         log_set_max_level(LOG_DEBUG);
 
         assert_se(mkdtemp(t));
diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c
index f4dc52c..c23aca0 100644
--- a/src/journal/test-journal.c
+++ b/src/journal/test-journal.c
@@ -38,6 +38,11 @@ int main(int argc, char *argv[]) {
         uint64_t p;
         char t[] = "/tmp/journal-XXXXXX";
 
+        if (sd_id128_get_machine(&id) == -128) {
+                printf("skipping test: /etc/machine-id not preset\n");
+                return 0;
+        }
+
         log_set_max_level(LOG_DEBUG);
 
         assert_se(mkdtemp(t));
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