summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-10-24 11:51:57 +0200
committerThomas Haller <thaller@redhat.com>2018-10-31 13:47:17 +0100
commit35cecd32fd9c27f67d3151247692d0d38185f149 (patch)
tree948a57dc49b4cbff3154f0021615cfe043af7d3a
parent5f4d8ffa79d574c86b108a83735e58e6f1bb6ab5 (diff)
downloadNetworkManager-35cecd32fd9c27f67d3151247692d0d38185f149.tar.gz
core/tests: allow temporarily suppressing logging during tests
Often, during tests we want to assert against the logged messages. In fact, most tests enable assertions for all logging and enforce them with g_test_assert_expected_messages(). So, this is common. However, sometimes it can be cumbersome to understand which logging lines will be produced. For example, the next commits will call nm_dhcp_manager_get() during the tests, which initializes NMDhcpManager and logs a message which plugin was selected (or an additional warning, if the selected plugin was not found). The availability of the DHCP plugin depends on searching the path for "/usr/bin/dhclient", so from testing code it's hard to determine what will be logged. Instead, add a way to temporarily disable logging during testing.
-rw-r--r--shared/nm-utils/nm-test-utils.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h
index 4a329f1534..18b42888e8 100644
--- a/shared/nm-utils/nm-test-utils.h
+++ b/shared/nm-utils/nm-test-utils.h
@@ -1347,6 +1347,42 @@ _nmtst_assert_resolve_relative_path_equals (const char *f1, const char *f2, cons
/*****************************************************************************/
+#ifdef __NETWORKMANAGER_LOGGING_H__
+static inline gpointer
+nmtst_logging_disable (gboolean always)
+{
+ gpointer p;
+
+ g_assert (nmtst_initialized ());
+ if (!always && __nmtst_internal.no_expect_message) {
+ /* The caller does not want to @always suppress logging. Instead,
+ * the caller wants to suppress unexpected log messages that would
+ * fail assertions (since we possibly assert against all unexpected
+ * log messages).
+ *
+ * If the test is run with no-expect-message, then don't suppress
+ * the loggings, because they also wouldn't fail assertions. */
+ return NULL;
+ }
+
+ p = g_memdup (_nm_logging_enabled_state, sizeof (_nm_logging_enabled_state));
+ memset (_nm_logging_enabled_state, 0, sizeof (_nm_logging_enabled_state));
+ return p;
+}
+
+static inline void
+nmtst_logging_reenable (gpointer old_state)
+{
+ g_assert (nmtst_initialized ());
+ if (old_state) {
+ memcpy (_nm_logging_enabled_state, old_state, sizeof (_nm_logging_enabled_state));
+ g_free (old_state);
+ }
+}
+#endif
+
+/*****************************************************************************/
+
#ifdef NM_SETTING_IP_CONFIG_H
static inline void
nmtst_setting_ip_config_add_address (NMSettingIPConfig *s_ip,