chiark / gitweb /
log: rearrange log function naming
[elogind.git] / src / journal / test-journal-interleaving.c
index c83a1ea981918b3fb09c624e2cb7b2339ef53613..23a26c43f633814042e83cfae38fd691efb77f60 100644 (file)
@@ -4,6 +4,7 @@
   This file is part of systemd.
 
   Copyright 2013 Marius Vollmer
   This file is part of systemd.
 
   Copyright 2013 Marius Vollmer
+  Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
 
   systemd is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by
 
   systemd is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by
 #include <unistd.h>
 #include <fcntl.h>
 
 #include <unistd.h>
 #include <fcntl.h>
 
-#include <systemd/sd-journal.h>
+#include "systemd/sd-journal.h"
 
 #include "journal-file.h"
 #include "journal-internal.h"
 
 #include "journal-file.h"
 #include "journal-internal.h"
+#include "journal-vacuum.h"
 #include "util.h"
 #include "log.h"
 
 /* This program tests skipping around in a multi-file journal.
  */
 
 #include "util.h"
 #include "log.h"
 
 /* This program tests skipping around in a multi-file journal.
  */
 
-static void log_assert_errno(const char *text, int eno, const char *file, int line, const char *func) {
-        log_meta(LOG_CRIT, file, line, func,
-                 "'%s' failed at %s:%u (%s): %s.",
-                 text, file, line, func, strerror(eno));
+static bool arg_keep = false;
+
+noreturn static void log_assert_errno(const char *text, int eno, const char *file, int line, const char *func) {
+        log_internal(LOG_CRIT, 0, file, line, func,
+                     "'%s' failed at %s:%u (%s): %s.",
+                     text, file, line, func, strerror(eno));
         abort();
 }
 
         abort();
 }
 
@@ -46,20 +50,17 @@ static void log_assert_errno(const char *text, int eno, const char *file, int li
                         log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
         } while (false)
 
                         log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
         } while (false)
 
-static JournalFile *test_open (const char *name)
-{
+static JournalFile *test_open(const char *name) {
         JournalFile *f;
         JournalFile *f;
-        assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &f));
+        assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &f));
         return f;
 }
 
         return f;
 }
 
-static void test_close (JournalFile *f)
-{
+static void test_close(JournalFile *f) {
         journal_file_close (f);
 }
 
         journal_file_close (f);
 }
 
-static void test_append_number(JournalFile *f, int n)
-{
+static void append_number(JournalFile *f, int n, uint64_t *seqnum) {
         char *p;
         dual_timestamp ts;
         struct iovec iovec[1];
         char *p;
         dual_timestamp ts;
         struct iovec iovec[1];
@@ -69,14 +70,13 @@ static void test_append_number(JournalFile *f, int n)
         assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
         iovec[0].iov_base = p;
         iovec[0].iov_len = strlen(p);
         assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
         iovec[0].iov_base = p;
         iovec[0].iov_len = strlen(p);
-        assert_ret(journal_file_append_entry(f, &ts, iovec, 1, NULL, NULL, NULL));
-        free (p);
+        assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL));
+        free(p);
 }
 
 }
 
-static void test_check_number (sd_journal *j, int n)
-{
+static void test_check_number (sd_journal *j, int n) {
         const void *d;
         const void *d;
-        char *k;
+        _cleanup_free_ char *k;
         size_t l;
         int x;
 
         size_t l;
         int x;
 
@@ -88,9 +88,10 @@ static void test_check_number (sd_journal *j, int n)
         assert_se(n == x);
 }
 
         assert_se(n == x);
 }
 
-static void test_check_numbers_down (sd_journal *j, int count)
-{
-        for (int i = 1; i <= count; i++) {
+static void test_check_numbers_down (sd_journal *j, int count) {
+        int i;
+
+        for (i = 1; i <= count; i++) {
                 int r;
                 test_check_number(j, i);
                 assert_ret(r = sd_journal_next(j));
                 int r;
                 test_check_number(j, i);
                 assert_ret(r = sd_journal_next(j));
@@ -102,8 +103,7 @@ static void test_check_numbers_down (sd_journal *j, int count)
 
 }
 
 
 }
 
-static void test_check_numbers_up (sd_journal *j, int count)
-{
+static void test_check_numbers_up (sd_journal *j, int count) {
         for (int i = count; i >= 1; i--) {
                 int r;
                 test_check_number(j, i);
         for (int i = count; i >= 1; i--) {
                 int r;
                 test_check_number(j, i);
@@ -120,10 +120,10 @@ static void setup_sequential(void) {
         JournalFile *one, *two;
         one = test_open("one.journal");
         two = test_open("two.journal");
         JournalFile *one, *two;
         one = test_open("one.journal");
         two = test_open("two.journal");
-        test_append_number(one, 1);
-        test_append_number(one, 2);
-        test_append_number(two, 3);
-        test_append_number(two, 4);
+        append_number(one, 1, NULL);
+        append_number(one, 2, NULL);
+        append_number(two, 3, NULL);
+        append_number(two, 4, NULL);
         test_close(one);
         test_close(two);
 }
         test_close(one);
         test_close(two);
 }
@@ -132,22 +132,19 @@ static void setup_interleaved(void) {
         JournalFile *one, *two;
         one = test_open("one.journal");
         two = test_open("two.journal");
         JournalFile *one, *two;
         one = test_open("one.journal");
         two = test_open("two.journal");
-        test_append_number(one, 1);
-        test_append_number(two, 2);
-        test_append_number(one, 3);
-        test_append_number(two, 4);
+        append_number(one, 1, NULL);
+        append_number(two, 2, NULL);
+        append_number(one, 3, NULL);
+        append_number(two, 4, NULL);
         test_close(one);
         test_close(two);
 }
 
         test_close(one);
         test_close(two);
 }
 
-static void test_skip(void (*setup)(void))
-{
+static void test_skip(void (*setup)(void)) {
         char t[] = "/tmp/journal-skip-XXXXXX";
         sd_journal *j;
         int r;
 
         char t[] = "/tmp/journal-skip-XXXXXX";
         sd_journal *j;
         int r;
 
-        log_set_max_level(LOG_DEBUG);
-
         assert_se(mkdtemp(t));
         assert_se(chdir(t) >= 0);
 
         assert_se(mkdtemp(t));
         assert_se(chdir(t) >= 0);
 
@@ -187,12 +184,115 @@ static void test_skip(void (*setup)(void))
         test_check_numbers_up(j, 4);
         sd_journal_close(j);
 
         test_check_numbers_up(j, 4);
         sd_journal_close(j);
 
-        assert_ret(rm_rf_dangerous(t, false, true, false));
+        log_info("Done...");
+
+        if (arg_keep)
+                log_info("Not removing %s", t);
+        else {
+                journal_directory_vacuum(".", 3000000, 0, NULL, true);
+
+                assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
+        }
+
+        puts("------------------------------------------------------------");
+}
+
+static void test_sequence_numbers(void) {
+
+        char t[] = "/tmp/journal-seq-XXXXXX";
+        JournalFile *one, *two;
+        uint64_t seqnum = 0;
+        sd_id128_t seqnum_id;
+
+        assert_se(mkdtemp(t));
+        assert_se(chdir(t) >= 0);
+
+        assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0644,
+                                    true, false, NULL, NULL, NULL, &one) == 0);
+
+        append_number(one, 1, &seqnum);
+        printf("seqnum=%"PRIu64"\n", seqnum);
+        assert(seqnum == 1);
+        append_number(one, 2, &seqnum);
+        printf("seqnum=%"PRIu64"\n", seqnum);
+        assert(seqnum == 2);
+
+        assert(one->header->state == STATE_ONLINE);
+        assert(!sd_id128_equal(one->header->file_id, one->header->machine_id));
+        assert(!sd_id128_equal(one->header->file_id, one->header->boot_id));
+        assert(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
+
+        memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
+
+        assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0644,
+                                    true, false, NULL, NULL, one, &two) == 0);
+
+        assert(two->header->state == STATE_ONLINE);
+        assert(!sd_id128_equal(two->header->file_id, one->header->file_id));
+        assert(sd_id128_equal(one->header->machine_id, one->header->machine_id));
+        assert(sd_id128_equal(one->header->boot_id, one->header->boot_id));
+        assert(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
+
+        append_number(two, 3, &seqnum);
+        printf("seqnum=%"PRIu64"\n", seqnum);
+        assert(seqnum == 3);
+        append_number(two, 4, &seqnum);
+        printf("seqnum=%"PRIu64"\n", seqnum);
+        assert(seqnum == 4);
+
+        test_close(two);
+
+        append_number(one, 5, &seqnum);
+        printf("seqnum=%"PRIu64"\n", seqnum);
+        assert(seqnum == 5);
+
+        append_number(one, 6, &seqnum);
+        printf("seqnum=%"PRIu64"\n", seqnum);
+        assert(seqnum == 6);
+
+        test_close(one);
+
+        /* restart server */
+        seqnum = 0;
+
+        assert_se(journal_file_open("two.journal", O_RDWR, 0,
+                                    true, false, NULL, NULL, NULL, &two) == 0);
+
+        assert(sd_id128_equal(two->header->seqnum_id, seqnum_id));
+
+        append_number(two, 7, &seqnum);
+        printf("seqnum=%"PRIu64"\n", seqnum);
+        assert(seqnum == 5);
+
+        /* So..., here we have the same seqnum in two files with the
+         * same seqnum_id. */
+
+        test_close(two);
+
+        log_info("Done...");
+
+        if (arg_keep)
+                log_info("Not removing %s", t);
+        else {
+                journal_directory_vacuum(".", 3000000, 0, NULL, true);
+
+                assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
+        }
 }
 
 int main(int argc, char *argv[]) {
 }
 
 int main(int argc, char *argv[]) {
+        log_set_max_level(LOG_DEBUG);
+
+        /* journal_file_open requires a valid machine id */
+        if (access("/etc/machine-id", F_OK) != 0)
+                return EXIT_TEST_SKIP;
+
+        arg_keep = argc > 1;
+
         test_skip(setup_sequential);
         test_skip(setup_interleaved);
 
         test_skip(setup_sequential);
         test_skip(setup_interleaved);
 
+        test_sequence_numbers();
+
         return 0;
 }
         return 0;
 }