From 391d60f153fe075fa584a25aed1be88553f356f4 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Wed, 16 Aug 2023 15:26:25 +0200 Subject: [PATCH] WIP: tests --- test/event-log.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 228 insertions(+), 1 deletion(-) diff --git a/test/event-log.c b/test/event-log.c index 02115213e..c7832fee3 100644 --- a/test/event-log.c +++ b/test/event-log.c @@ -1,6 +1,217 @@ #include +#include -#include "event-log.h" +#include +#include + +typedef struct { + gchar *tmpdir; +} EventLogFixture; + +static void event_log_fixture_set_up(EventLogFixture *fixture, + gconstpointer user_data) +{ + fixture->tmpdir = g_dir_make_tmp("rauc-event_log-XXXXXX", NULL); + g_assert_nonnull(fixture->tmpdir); + + r_context_conf()->configpath = g_strdup("test/test.conf"); + r_context(); +} + +static void config_file_fixture_tear_down(EventLogFixture *fixture, + gconstpointer user_data) +{ + g_assert_true(rm_tree(fixture->tmpdir, NULL)); + g_free(fixture->tmpdir); + r_context_clean(); +} + +/* Test setting up a logger */ +static void event_log_test_setup_logger(EventLogFixture *fixture, + gconstpointer user_data) +{ + g_autoptr(REventLogger) logger = NULL; + + logger = g_new0(REventLogger, 1); + logger->name = g_strdup("testlogger"); + logger->filename = g_build_filename(fixture->tmpdir, "testfile.log", NULL); + + r_event_log_setup_logger(logger); + + g_assert_true(logger->configured); + g_assert_cmpint(logger->filesize, ==, 0); +} + +/* Test setting up a readable (default) logger and writing a simple log message to the logfile */ +static void event_log_test_log_write_simple(EventLogFixture *fixture, + gconstpointer user_data) +{ + g_autoptr(REventLogger) logger = NULL; + GLogField fields[] = { + {"MESSAGE", "This is a test (mark) log message", -1 }, + {"MESSAGE_ID", "1d1b7a5a-a908-4c3a-9004-650c9d2ce850", -1 }, + {"GLIB_DOMAIN", R_EVENT_LOG_DOMAIN, -1}, + {"RAUC_EVENT_TYPE", "mark", -1}, + {"RAUC_SLOT", "rootfs.0", -1}, + {"BUNDLE_HASH", "b970468f-89e4-4793-9904-06c922902b25", -1}, + {"SLOT_BOOTNAME", "A", -1}, + }; + g_autofree gchar *contents = NULL; + + logger = g_new0(REventLogger, 1); + logger->name = g_strdup("testlogger"); + logger->filename = g_build_filename(fixture->tmpdir, "testfile.log", NULL); + + r_event_log_setup_logger(logger); + + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + + g_file_get_contents(logger->filename, &contents, NULL, NULL); + + g_assert_nonnull(strstr(contents, "This is a test (mark) log message")); + g_assert_nonnull(strstr(contents, "bundle hash: b970468f-89e4-4793-9904-06c922902b25")); +} + +/* Test setting up a json format logger and writing a log message to the logfile */ +static void event_log_test_log_write_json(EventLogFixture *fixture, + gconstpointer user_data) +{ + g_autoptr(REventLogger) logger = NULL; + GLogField fields[] = { + {"MESSAGE", "This is a test (mark) log message", -1 }, + {"MESSAGE_ID", "1d1b7a5a-a908-4c3a-9004-650c9d2ce850", -1 }, + {"GLIB_DOMAIN", R_EVENT_LOG_DOMAIN, -1}, + {"RAUC_EVENT_TYPE", "mark", -1}, + {"RAUC_SLOT", "rootfs.0", -1}, + {"BUNDLE_HASH", "b970468f-89e4-4793-9904-06c922902b25", -1}, + {"SLOT_BOOTNAME", "A", -1}, + }; + g_autofree gchar *contents = NULL; + + logger = g_new0(REventLogger, 1); + logger->name = g_strdup("testlogger"); + logger->format = R_EVENT_LOGFMT_JSON; + logger->filename = g_build_filename(fixture->tmpdir, "testfile.log", NULL); + + r_event_log_setup_logger(logger); + + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + + g_file_get_contents(logger->filename, &contents, NULL, NULL); + + g_assert_nonnull(strstr(contents, "\"MESSAGE\":\"This is a test (mark) log message\"")); + g_assert_nonnull(strstr(contents, "\"MESSAGE_ID\":\"1d1b7a5a-a908-4c3a-9004-650c9d2ce850\"")); +} + +/* Test setting a maxsize and logging until exceeding it */ +static void event_log_test_max_size(EventLogFixture *fixture, + gconstpointer user_data) +{ + g_autoptr(REventLogger) logger = NULL; + GLogField fields[] = { + {"MESSAGE", "This is a test (mark) log message", -1 }, + {"MESSAGE_ID", "1d1b7a5a-a908-4c3a-9004-650c9d2ce850", -1 }, + {"GLIB_DOMAIN", R_EVENT_LOG_DOMAIN, -1}, + {"RAUC_EVENT_TYPE", "mark", -1}, + {"RAUC_SLOT", "rootfs.0", -1}, + {"BUNDLE_HASH", "b970468f-89e4-4793-9904-06c922902b25", -1}, + {"SLOT_BOOTNAME", "A", -1}, + }; + g_autofree gchar *rotatefile = NULL; + + logger = g_new0(REventLogger, 1); + logger->name = g_strdup("testlogger"); + logger->filename = g_build_filename(fixture->tmpdir, "testfile.log", NULL); + logger->maxsize = 500; + + rotatefile = g_build_filename(fixture->tmpdir, "testfile.log.0", NULL); + + r_event_log_setup_logger(logger); + + /* Message size is 128 bytes, thus the 4th write should exceed the + * configured 500 bytes maxsize and create the rotation file. */ + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + g_assert_false(g_file_test(rotatefile, G_FILE_TEST_EXISTS)); + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + g_assert_false(g_file_test(rotatefile, G_FILE_TEST_EXISTS)); + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + g_assert_false(g_file_test(rotatefile, G_FILE_TEST_EXISTS)); + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + g_assert_true(g_file_test(rotatefile, G_FILE_TEST_EXISTS)); +} + +/* Test setting a maxsize and logging until exceeding it */ +static void event_log_test_max_files_rotation(EventLogFixture *fixture, + gconstpointer user_data) +{ + g_autoptr(REventLogger) logger = NULL; + GLogField fields[] = { + {"MESSAGE", "This is a test (mark) log message", -1 }, + {"MESSAGE_ID", "1d1b7a5a-a908-4c3a-9004-650c9d2ce850", -1 }, + {"GLIB_DOMAIN", R_EVENT_LOG_DOMAIN, -1}, + {"RAUC_EVENT_TYPE", "mark", -1}, + {"RAUC_SLOT", "rootfs.0", -1}, + {"BUNDLE_HASH", "b970468f-89e4-4793-9904-06c922902b25", -1}, + {"SLOT_BOOTNAME", "A", -1}, + }; + g_autofree gchar *rotatefile = NULL; + const gchar *rotate_content = "This string will walk through the files"; + g_autofree gchar *compare_content = NULL; + + logger = g_new0(REventLogger, 1); + logger->name = g_strdup("testlogger"); + logger->filename = g_build_filename(fixture->tmpdir, "testfile.log", NULL); + logger->maxsize = 256; + logger->maxfiles = 3; + + /* create file with content to rotate */ + rotatefile = g_build_filename(fixture->tmpdir, "testfile.log.0", NULL); + g_assert_true(g_file_set_contents(rotatefile, rotate_content, -1, NULL)); + + r_event_log_setup_logger(logger); + + /* Pre-fill logfile with 128 bytes */ + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + + /* search string must be in .0 file */ + g_assert_true(g_file_get_contents(rotatefile, &compare_content, NULL, NULL)); + g_assert_cmpstr(rotate_content, ==, compare_content); + + /* file size was 0, message size is 128 bytes, will rotate on 3rd message */ + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + + /* search string must be in .1 file (but not in .0) */ + g_assert_true(g_file_get_contents(rotatefile, &compare_content, NULL, NULL)); + g_assert_cmpstr(rotate_content, !=, compare_content); + g_free(rotatefile); + rotatefile = g_build_filename(fixture->tmpdir, "testfile.log.1", NULL); + g_assert_true(g_file_get_contents(rotatefile, &compare_content, NULL, NULL)); + g_assert_cmpstr(rotate_content, ==, compare_content); + + /* file size was 128, message size is 128 bytes, will rotate on 2nd message */ + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + + /* search string must be in .2 file (but no in .1) */ + g_assert_true(g_file_get_contents(rotatefile, &compare_content, NULL, NULL)); + g_assert_cmpstr(rotate_content, !=, compare_content); + g_free(rotatefile); + rotatefile = g_build_filename(fixture->tmpdir, "testfile.log.2", NULL); + g_assert_true(g_file_get_contents(rotatefile, &compare_content, NULL, NULL)); + g_assert_cmpstr(rotate_content, ==, compare_content); + + /* file size was 128, message size is 128 bytes, will rotate on 2nd message */ + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + logger->writer(logger, fields, G_N_ELEMENTS(fields)); + + /* .3 file must not be created and search string must not be in .2 */ + g_assert_true(g_file_get_contents(rotatefile, &compare_content, NULL, NULL)); + g_assert_cmpstr(rotate_content, !=, compare_content); + g_free(rotatefile); + rotatefile = g_build_filename(fixture->tmpdir, "testfile.log.3", NULL); + g_assert_false(g_file_get_contents(rotatefile, &compare_content, NULL, NULL)); +} int main(int argc, char *argv[]) { @@ -8,5 +219,21 @@ int main(int argc, char *argv[]) g_test_init(&argc, &argv, NULL); + g_test_add("/event-log/setup-logger", EventLogFixture, NULL, + event_log_fixture_set_up, event_log_test_setup_logger, + config_file_fixture_tear_down); + g_test_add("/event-log/log-writer/simple", EventLogFixture, NULL, + event_log_fixture_set_up, event_log_test_log_write_simple, + config_file_fixture_tear_down); + g_test_add("/event-log/log-writer/json", EventLogFixture, NULL, + event_log_fixture_set_up, event_log_test_log_write_json, + config_file_fixture_tear_down); + g_test_add("/event-log/logger-max-size", EventLogFixture, NULL, + event_log_fixture_set_up, event_log_test_max_size, + config_file_fixture_tear_down); + g_test_add("/event-log/logger-max-files", EventLogFixture, NULL, + event_log_fixture_set_up, event_log_test_max_files_rotation, + config_file_fixture_tear_down); + return g_test_run(); }