From 48b5ba7cea3716faa493116d4c54c1e266917c4a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 25 Apr 2014 15:34:49 +0200 Subject: [PATCH] nmtst: add NMTST_DEBUG=no-expect-message for debugging assert_message tests Some tests want to assert against the messages logged using g_test_expect_message(). In this mode, nmtst will not log anything itself. Interpret the option no-expect-message which turns g_test_expect_message() into a NOP and turns logging on. The use of this is for debugging such tests, without asserting against the messages but printing them instead. For tests that are not in the assert_message mode, the option has no effect. Example: NMTST_DEBUG=debug,no-expect-message make -C src/settings/plugins/keyfile/tests/ check Signed-off-by: Thomas Haller --- include/nm-test-utils.h | 33 +++++++++++++++++-- libnm-util/tests/test-general.c | 10 ++---- .../ifcfg-rh/tests/test-ifcfg-rh-utils.c | 4 +++ .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 23 +++---------- src/settings/plugins/ifnet/tests/test_all.c | 8 ++--- .../tests/test-supplicant-config.c | 10 ++---- src/tests/test-dhcp-options.c | 15 ++------- 7 files changed, 52 insertions(+), 51 deletions(-) diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h index ca1508b108..83c4080fe0 100644 --- a/include/nm-test-utils.h +++ b/include/nm-test-utils.h @@ -43,6 +43,7 @@ struct __nmtst_internal GRand *rand; gboolean is_debug; gboolean assert_logging; + gboolean no_expect_message; char *sudo_cmd; char **orig_argv; }; @@ -62,7 +63,7 @@ nmtst_initialized (void) #define __NMTST_LOG(cmd, fmt, ...) \ G_STMT_START { \ g_assert (nmtst_initialized ()); \ - if (!__nmtst_internal.assert_logging) { \ + if (!__nmtst_internal.assert_logging || __nmtst_internal.no_expect_message) { \ cmd (fmt, __VA_ARGS__); \ } else { \ printf (fmt "\n", __VA_ARGS__); \ @@ -146,6 +147,7 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ char *sudo_cmd = NULL; GArray *debug_messages = g_array_new (TRUE, FALSE, sizeof (char *)); int i; + gboolean no_expect_message = FALSE; g_assert (!nmtst_initialized ()); @@ -160,6 +162,10 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ /* g_test_init() is a variadic function, so we cannot pass it * (variadic) arguments. If you need to pass additional parameters, * call nmtst_init() with argc==NULL and call g_test_init() yourself. */ + + /* g_test_init() sets g_log_set_always_fatal() for G_LOG_LEVEL_WARNING + * and G_LOG_LEVEL_CRITICAL. So, beware that the test will fail if you + * have any WARN or ERR log messages -- unless you g_test_expect_message(). */ g_test_init (argc, argv, NULL); } @@ -200,6 +206,8 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ } else if (!g_ascii_strncasecmp (debug, "sudo-cmd=", strlen ("sudo-cmd="))) { g_free (sudo_cmd); sudo_cmd = g_strdup (&debug[strlen ("sudo-cmd=")]); + } else if (!g_ascii_strcasecmp (debug, "no-expect-message")) { + no_expect_message = TRUE; } else { char *msg = g_strdup_printf (">>> nmtst: ignore unrecognized NMTST_DEBUG option \"%s\"", debug); @@ -225,6 +233,7 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ __nmtst_internal.is_debug = is_debug; __nmtst_internal.rand0 = g_rand_new_with_seed (0); __nmtst_internal.sudo_cmd = sudo_cmd; + __nmtst_internal.no_expect_message = no_expect_message; if (!log_level && log_domains) { /* if the log level is not specified (but the domain is), we assume @@ -238,6 +247,11 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ success = nm_logging_setup (log_level, log_domains, NULL, NULL); #endif g_assert (success); + } else if (__nmtst_internal.no_expect_message) { + /* We have a test that would be assert_logging, but the user specified no_expect_message. + * This transforms g_test_expect_message() into a NOP, but we also have to relax + * g_log_set_always_fatal(), which was set by g_test_init(). */ + g_log_set_always_fatal (G_LOG_FATAL_MASK); } else { #if GLIB_CHECK_VERSION(2,34,0) /* We were called not to set logging levels. This means, that the user @@ -249,7 +263,7 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ #endif } - if (!__nmtst_internal.assert_logging && + if ((!__nmtst_internal.assert_logging || (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message)) && (is_debug || (log_level && !g_ascii_strcasecmp (log_level, "DEBUG"))) && !g_getenv ("G_MESSAGES_DEBUG")) { @@ -306,6 +320,21 @@ nmtst_is_debug (void) return __nmtst_internal.is_debug; } +#if GLIB_CHECK_VERSION(2,34,0) +#undef g_test_expect_message +#define g_test_expect_message(...) \ + G_STMT_START { \ + g_assert (nmtst_initialized ()); \ + if (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message) { \ + g_debug ("nmtst: swallow g_test_expect_message %s", G_STRINGIFY ((__VA_ARGS__))); \ + } else { \ + G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ + g_test_expect_message (__VA_ARGS__); \ + G_GNUC_END_IGNORE_DEPRECATIONS \ + } \ + } G_STMT_END +#endif + inline static GRand * nmtst_get_rand0 () { diff --git a/libnm-util/tests/test-general.c b/libnm-util/tests/test-general.c index bc63251754..5a54c558cb 100644 --- a/libnm-util/tests/test-general.c +++ b/libnm-util/tests/test-general.c @@ -2484,17 +2484,13 @@ test_setting_old_uuid (void) g_assert (success == TRUE); } +NMTST_DEFINE (); + int main (int argc, char **argv) { - GError *error = NULL; char *base; -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - - if (!nm_utils_init (&error)) - FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message); + nmtst_init (&argc, &argv, TRUE); /* The tests */ test_setting_vpn_items (); diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh-utils.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh-utils.c index cdb1f1f274..39c81027fe 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh-utils.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh-utils.c @@ -113,10 +113,14 @@ test_ignored (const char *desc, const char *path, gboolean expected_ignored) ASSERT (result == expected_ignored, desc, "unexpected ignore result for path '%s'", path); } +NMTST_DEFINE (); + int main (int argc, char **argv) { char *base; + nmtst_init (&argc, &argv, TRUE); + /* The tests */ test_get_ifcfg_name ("get-ifcfg-name-bad", "/foo/bar/adfasdfadf", FALSE, NULL); test_get_ifcfg_name ("get-ifcfg-name-good", "/foo/bar/ifcfg-FooBar", FALSE, "FooBar"); diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 5d91b2add3..9e0706def6 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -54,6 +54,7 @@ #include "reader.h" #include "writer.h" #include "utils.h" +#include "nm-logging.h" #include "nm-test-utils.h" @@ -14377,27 +14378,11 @@ test_svUnescape () #define TPATH "/settings/plugins/ifcfg-rh/" +NMTST_DEFINE (); + int main (int argc, char **argv) { - GError *error = NULL; - gboolean success; - - g_test_init (&argc, &argv, NULL); -#if GLIB_CHECK_VERSION(2,34,0) - /* consider even unexpected g_message()s to be fatal */ - g_log_set_always_fatal (G_LOG_LEVEL_MASK); -#else - /* g_test_expect_message() is dummied out, so allow warnings */ - g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL); -#endif - -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - - success = nm_utils_init (&error); - g_assert_no_error (error); - g_assert (success); + nmtst_init_assert_logging (&argc, &argv); g_test_add_func (TPATH "svUnescape", test_svUnescape); diff --git a/src/settings/plugins/ifnet/tests/test_all.c b/src/settings/plugins/ifnet/tests/test_all.c index dc93dcccd6..c320ed5f8f 100644 --- a/src/settings/plugins/ifnet/tests/test_all.c +++ b/src/settings/plugins/ifnet/tests/test_all.c @@ -451,16 +451,16 @@ test_missing_config () "get connection should fail with 'Unknown config for eth8'"); } +NMTST_DEFINE (); + int main (int argc, char **argv) { char *f; -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - nm_linux_platform_setup (); + + nmtst_init_assert_logging (&argc, &argv); nm_logging_setup ("WARN", "DEFAULT", NULL, NULL); f = g_build_filename (argv[1], "net", NULL); diff --git a/src/supplicant-manager/tests/test-supplicant-config.c b/src/supplicant-manager/tests/test-supplicant-config.c index 2b917625a6..ce884a8dd5 100644 --- a/src/supplicant-manager/tests/test-supplicant-config.c +++ b/src/supplicant-manager/tests/test-supplicant-config.c @@ -494,17 +494,13 @@ test_wifi_wpa_psk_types (void) test_wifi_wpa_psk ("wifi-wep-psk-passphrase", TYPE_STRING, key2, (gconstpointer) key2, strlen (key2)); } +NMTST_DEFINE (); + int main (int argc, char **argv) { - GError *error = NULL; char *base; -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - - if (!nm_utils_init (&error)) - FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message); + nmtst_init (&argc, &argv, TRUE); /* The tests */ test_wifi_open (); diff --git a/src/tests/test-dhcp-options.c b/src/tests/test-dhcp-options.c index 96e9b5c822..5df17e587c 100644 --- a/src/tests/test-dhcp-options.c +++ b/src/tests/test-dhcp-options.c @@ -837,24 +837,15 @@ test_ip4_prefix_classless (gconstpointer test_data) g_hash_table_destroy (options); } +NMTST_DEFINE (); + int main (int argc, char **argv) { - GError *error = NULL; char *path; const char *clients[2][2] = { {DHCLIENT_PATH, "dhclient"}, {DHCPCD_PATH, "dhcpcd"} }; guint32 i; - g_test_init (&argc, &argv, NULL); -#if !GLIB_CHECK_VERSION(2,34,0) - g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL); -#endif - -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - - if (!nm_utils_init (&error)) - FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message); + nmtst_init_assert_logging (&argc, &argv); nm_logging_setup ("WARN", "DEFAULT", NULL, NULL); for (i = 0; i < 2; i++) {