nmtst: add new mode where tests assert against logged messages (assert_messages)

In this mode, nmtst itself will not log anything and not set the logging
level. Also, it will set g_log_set_always_fatal().

This is for tests that want to assert against all logged messages via
g_test_expect_message().

In this mode also setting the logging level via NMTST_DEBUG variable has
no effect. The test is expected to manage the logging level itself and
changing the logging level might interfere with the test.

As a showcase, move keyfile/tests/test-keyfile.c to nmtst.

Signed-off-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
Thomas Haller 2014-04-24 21:01:58 +02:00
parent ae863a6954
commit 8ce1eb3837
4 changed files with 55 additions and 17 deletions

View file

@ -42,6 +42,7 @@ struct __nmtst_internal
guint32 rand_seed;
GRand *rand;
gboolean is_debug;
gboolean assert_logging;
char *sudo_cmd;
char **orig_argv;
};
@ -58,6 +59,16 @@ nmtst_initialized (void)
return !!__nmtst_internal.rand0;
}
#define __NMTST_LOG(cmd, fmt, ...) \
G_STMT_START { \
g_assert (nmtst_initialized ()); \
if (!__nmtst_internal.assert_logging) { \
cmd (fmt, __VA_ARGS__); \
} else { \
printf (fmt "\n", __VA_ARGS__); \
} \
} G_STMT_END
/* split the string inplace at specific delimiters, allowing escaping with '\\'.
* Returns a zero terminated array of pointers into @str.
*
@ -125,7 +136,7 @@ nmtst_free (void)
}
inline static void
nmtst_init (int *argc, char ***argv, const char *log_level, const char *log_domains)
__nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_level, const char *log_domains)
{
static gsize atexit_registered = 0;
GError *error = NULL;
@ -140,6 +151,7 @@ nmtst_init (int *argc, char ***argv, const char *log_level, const char *log_doma
g_assert (!((!!argc) ^ (!!argv)));
g_assert (!argc || (g_strv_length (*argv) == *argc));
g_assert (!assert_logging || (!log_level && !log_domains));
if (argc)
__nmtst_internal.orig_argv = g_strdupv (*argv);
@ -151,6 +163,8 @@ nmtst_init (int *argc, char ***argv, const char *log_level, const char *log_doma
g_test_init (argc, argv, NULL);
}
__nmtst_internal.assert_logging = !!assert_logging;
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
@ -218,12 +232,21 @@ nmtst_init (int *argc, char ***argv, const char *log_level, const char *log_doma
log_level = is_debug ? "DEBUG" : "WARN";
}
if (log_level || log_domains) {
gboolean success = FALSE;
if (!__nmtst_internal.assert_logging) {
gboolean success = TRUE;
#ifdef NM_LOGGING_H
success = nm_logging_setup (log_level, log_domains, NULL, NULL);
#endif
g_assert (success);
} else {
#if GLIB_CHECK_VERSION(2,34,0)
/* We were called not to set logging levels. This means, that the user
* expects to assert against (all) messages. Any uncought message is fatal. */
g_log_set_always_fatal (G_LOG_LEVEL_MASK);
#else
/* g_test_expect_message() is a NOP, so allow any messages */
g_log_set_always_fatal (G_LOG_FATAL_MASK);
#endif
}
if (!nm_utils_init (&error))
@ -232,7 +255,7 @@ nmtst_init (int *argc, char ***argv, const char *log_level, const char *log_doma
/* Delay messages until we setup logging. */
for (i = 0; i < debug_messages->len; i++)
g_message ("%s", g_array_index (debug_messages, const char *, i));
__NMTST_LOG (g_message, "%s", g_array_index (debug_messages, const char *, i));
g_strfreev ((char **) g_array_free (debug_messages, FALSE));
g_free (c_log_level);
@ -244,6 +267,25 @@ nmtst_init (int *argc, char ***argv, const char *log_level, const char *log_doma
}
}
#ifdef NM_LOGGING_H
inline static void
nmtst_init_with_logging (int *argc, char ***argv, const char *log_level, const char *log_domains)
{
__nmtst_init (argc, argv, FALSE, log_level, log_domains);
}
inline static void
nmtst_init_assert_logging (int *argc, char ***argv)
{
__nmtst_init (argc, argv, TRUE, NULL, NULL);
}
#else
inline static void
nmtst_init (int *argc, char ***argv, gboolean assert_logging)
{
__nmtst_init (argc, argv, assert_logging, NULL, NULL);
}
#endif
inline static gboolean
nmtst_is_debug (void)
{
@ -284,7 +326,7 @@ nmtst_get_rand ()
}
__nmtst_internal.rand_seed = seed;
g_message (">> initialize nmtst_get_rand() with seed=%u", seed);
__NMTST_LOG (g_message, ">> initialize nmtst_get_rand() with seed=%u", seed);
}
return __nmtst_internal.rand;
}
@ -311,7 +353,7 @@ nmtst_reexec_sudo (void)
return;
str = g_strjoinv (" ", __nmtst_internal.orig_argv);
g_message (">> exec %s %s", __nmtst_internal.sudo_cmd, str);
__NMTST_LOG (g_message, ">> exec %s %s", __nmtst_internal.sudo_cmd, str);
argv = g_new0 (char *, 1 + g_strv_length (__nmtst_internal.orig_argv) + 1);
argv[0] = __nmtst_internal.sudo_cmd;

View file

@ -147,7 +147,7 @@ main (int argc, char **argv)
int result;
const char *program = *argv;
nmtst_init (&argc, &argv, NULL, "ALL");
nmtst_init_with_logging (&argc, &argv, NULL, "ALL");
if (SETUP == nm_linux_platform_setup && getuid() != 0) {
/* Try to exec as sudo, this function does not return, if a sudo-cmd is set. */

View file

@ -40,6 +40,7 @@
#include <nm-setting-gsm.h>
#include <nm-setting-8021x.h>
#include <nm-setting-infiniband.h>
#include <nm-logging.h>
#include "reader.h"
#include "writer.h"
@ -3409,19 +3410,13 @@ test_read_missing_vlan_setting (void)
g_object_unref (connection);
}
NMTST_DEFINE ();
int main (int argc, char **argv)
{
GError *error = NULL;
char *base;
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
g_log_set_always_fatal (G_LOG_LEVEL_MASK);
if (!nm_utils_init (&error))
FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
nmtst_init_assert_logging (&argc, &argv);
/* The tests */
test_read_valid_wired_connection ();

View file

@ -24,6 +24,7 @@
#include "nm-ip6-config.h"
#include "nm-logging.h"
#include "nm-test-utils.h"
static NMIP6Config *
@ -324,7 +325,7 @@ NMTST_DEFINE();
int
main (int argc, char **argv)
{
nmtst_init (&argc, &argv, NULL, NULL);
nmtst_init_with_logging (&argc, &argv, NULL, "ALL");
g_test_add_func ("/ip6-config/subtract", test_subtract);
g_test_add_func ("/ip6-config/compare-with-source", test_compare_with_source);