void __test_seeks(Journald *journald) { gint result = journald_seek_head(journald); assert_gint(result, 0, ASSERTION_ERROR("Can't seek in empty journald mock")); result = journald_next(journald); assert_gint(result, 0, ASSERTION_ERROR("Bad next step result")); MockEntry *entry = mock_entry_new("test_data1"); mock_entry_add_data(entry, "MESSAGE=test message"); mock_entry_add_data(entry, "KEY=VALUE"); mock_entry_add_data(entry, "HOST=testhost"); journald_mock_add_entry(journald, entry); entry = mock_entry_new("test_data2"); mock_entry_add_data(entry, "MESSAGE=test message2"); mock_entry_add_data(entry, "KEY=VALUE2"); mock_entry_add_data(entry, "HOST=testhost2"); journald_mock_add_entry(journald, entry); result = journald_seek_head(journald); assert_gint(result, 0, ASSERTION_ERROR("Can't seek in journald mock")); result = journald_next(journald); assert_gint(result, 1, ASSERTION_ERROR("Bad next step result")); }
Test(systemd_journal, test_journald_helper) { const gchar *persist_file = "test_systemd_journal2.persist"; _init_cfg_with_persist_file(persist_file); Journald *journald = journald_mock_new(); journald_open(journald, 0); MockEntry *entry = mock_entry_new("test_data1"); mock_entry_add_data(entry, "MESSAGE=test message"); mock_entry_add_data(entry, "KEY=VALUE"); mock_entry_add_data(entry, "HOST=testhost"); journald_mock_add_entry(journald, entry); journald_seek_head(journald); journald_next(journald); GHashTable *result = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); journald_foreach_data(journald, __helper_test, result); gchar *message = g_hash_table_lookup(result, "MESSAGE"); gchar *key = g_hash_table_lookup(result, "KEY"); gchar *host = g_hash_table_lookup(result, "HOST"); cr_assert_str_eq(message, "test message", "%s", "Bad item"); cr_assert_str_eq(key, "VALUE", "%s", "Bad item"); cr_assert_str_eq(host, "testhost", "%s", "Bad item"); journald_close(journald); journald_free(journald); g_hash_table_unref(result); _deinit_cfg(persist_file); }
void test_journald_helper() { Journald *journald = journald_mock_new(); journald_open(journald, 0); MockEntry *entry = mock_entry_new("test_data1"); mock_entry_add_data(entry, "MESSAGE=test message"); mock_entry_add_data(entry, "KEY=VALUE"); mock_entry_add_data(entry, "HOST=testhost"); journald_mock_add_entry(journald, entry); journald_seek_head(journald); journald_next(journald); GHashTable *result = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); journald_foreach_data(journald, __helper_test, result); gchar *message = g_hash_table_lookup(result, "MESSAGE"); gchar *key = g_hash_table_lookup(result, "KEY"); gchar *host = g_hash_table_lookup(result, "HOST"); assert_string(message, "test message", ASSERTION_ERROR("Bad item")); assert_string(key, "VALUE", ASSERTION_ERROR("Bad item")); assert_string(host, "testhost", ASSERTION_ERROR("Bad item")); journald_close(journald); journald_free(journald); g_hash_table_unref(result); }
static inline gboolean _seek_to_head(JournalReader *self) { gint rc = journald_seek_head(self->journal); if (rc != 0) { msg_error("Failed to seek to the start position of the journal", evt_tag_errno("error", errno)); return FALSE; } else { msg_debug("Seeking the journal to the start position"); } return TRUE; }
void __test_cursors(Journald *journald) { gchar *cursor; journald_seek_head(journald); journald_next(journald); gint result = journald_get_cursor(journald, &cursor); cr_assert_str_eq(cursor, "test_data1", "%s", "Bad cursor fetched"); \ g_free(cursor); result = journald_next(journald); cr_assert_eq(result, 1, "%s", "Bad next step result"); result = journald_get_cursor(journald, &cursor); cr_assert_str_eq(cursor, "test_data2", "%s", "Bad cursor fetched"); g_free(cursor); result = journald_next(journald); cr_assert_eq(result, 0, "%s", "Should not contain more elements"); result = journald_seek_cursor(journald, "test_data1"); cr_assert_eq(result, 0, "%s", "Should find cursor"); result = journald_next(journald); cr_assert_eq(result, 1, "%s", "Bad next step result"); result = journald_get_cursor(journald, &cursor); cr_assert_str_eq(cursor, "test_data1", "%s", "Bad cursor fetched"); g_free(cursor); result = journald_seek_cursor(journald, "test_data2"); cr_assert_eq(result, 0, "%s", "Should find cursor"); result = journald_next(journald); cr_assert_eq(result, 1, "%s", "Bad next step result"); result = journald_get_cursor(journald, &cursor); cr_assert_str_eq(cursor, "test_data2", "%s", "Bad cursor fetched"); g_free(cursor); }