]> git.proxmox.com Git - mirror_corosync-qdevice.git/commitdiff
qdevice: Add log test
authorJan Friesse <jfriesse@redhat.com>
Tue, 13 Aug 2019 13:57:09 +0000 (15:57 +0200)
committerJan Friesse <jfriesse@redhat.com>
Tue, 13 Aug 2019 14:09:08 +0000 (16:09 +0200)
Signed-off-by: Jan Friesse <jfriesse@redhat.com>
qdevices/Makefile.am
qdevices/test-log.c [new file with mode: 0644]

index 02f0730d34fb73757879e8e31864fc2c1d70429b..0723963ffd461ae0b60b2134612b7d32be26de9d 100644 (file)
@@ -159,9 +159,11 @@ corosync-qdevice-net-certutil: corosync-qdevice-net-certutil.sh
            $< > $@
 
 TESTS                          = qnetd-cluster-list.test dynar.test dynar-simple-lex.test \
-                                  dynar-getopt-lex.test process-list.test utils.test
+                                  dynar-getopt-lex.test process-list.test utils.test \
+                                  log.test
 check_PROGRAMS                 = qnetd-cluster-list.test dynar.test dynar-simple-lex.test \
-                                  dynar-getopt-lex.test process-list.test utils.test
+                                  dynar-getopt-lex.test process-list.test utils.test \
+                                  log.test
 
 qnetd_cluster_list_test_SOURCES        = qnetd-cluster-list.c test-qnetd-cluster-list.c \
                                   qnetd-cluster.c qnetd-cluster.h \
@@ -176,6 +178,7 @@ dynar_getopt_lex_test_SOURCES       = test-dynar-getopt-lex.c dynar.c dynar-str.c dyna
 process_list_test_SOURCES      = test-process-list.c dynar.c dynar-str.c dynar-simple-lex.c \
                                   process-list.c
 utils_test_SOURCES             = test-utils.c utils.c
+log_test_SOURCES               = test-log.c log.c
 
 endif
 
diff --git a/qdevices/test-log.c b/qdevices/test-log.c
new file mode 100644 (file)
index 0000000..f299c6d
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2019 Red Hat, Inc.
+ *
+ * All rights reserved.
+ *
+ * Author: Jan Friesse (jfriesse@redhat.com)
+ *
+ * This software licensed under BSD license, the text of which follows:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ *   this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * - Neither the name of the Red Hat, Inc. nor the names of its
+ *   contributors may be used to endorse or promote products derived from this
+ *   software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+
+#include "log.h"
+
+#define MAX_LINE_LEN           512
+
+static int openlog_called;
+static int openlog_facility;
+static char vsyslog_buf[MAX_LINE_LEN];
+static int vsyslog_priority;
+static int vsyslog_called;
+static int closelog_called;
+
+void
+openlog(const char *ident, int option, int facility)
+{
+
+       openlog_called = 1;
+       openlog_facility = facility;
+}
+
+void
+vsyslog(int priority, const char *format, va_list ap)
+{
+       va_list ap_copy;
+       int res;
+
+       vsyslog_called = 1;
+       vsyslog_priority = priority;
+
+       va_copy(ap_copy, ap);
+       res = vsnprintf(vsyslog_buf, MAX_LINE_LEN, format, ap_copy);
+       assert(res < MAX_LINE_LEN && res != -1);
+       va_end(ap_copy);
+}
+
+void
+closelog(void)
+{
+
+       closelog_called = 1;
+}
+
+static void
+log_check(int priority, const char *msg, int should_be_called, int expected_priority)
+{
+
+       vsyslog_called = 0;
+       vsyslog_priority = -1;
+       vsyslog_buf[0] = '\0';
+
+       log(priority, "%s", msg);
+
+       if (should_be_called) {
+               assert(vsyslog_called);
+               assert(vsyslog_priority == expected_priority);
+               assert(strcmp(vsyslog_buf, msg) == 0);
+       } else {
+               assert(!vsyslog_called);
+       }
+}
+
+int
+main(void)
+{
+
+       openlog_called = 0;
+       assert(log_init("test", LOG_TARGET_SYSLOG, LOG_DAEMON) == 0);
+       assert(openlog_called);
+       assert(openlog_facility == LOG_DAEMON);
+
+       log_check(LOG_INFO, "test log info", 1, LOG_INFO);
+       log_check(LOG_ERR, "test log err", 1, LOG_ERR);
+       log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG);
+
+       log_set_debug(1);
+       log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG);
+
+       log_set_debug(0);
+       log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG);
+
+       log_set_debug(1);
+       log_set_priority_bump(1);
+       log_check(LOG_ERR, "test log err", 1, LOG_ERR);
+       log_check(LOG_DEBUG, "test log debug", 1, LOG_INFO);
+
+       log_set_priority_bump(0);
+       log_check(LOG_ERR, "test log err", 1, LOG_ERR);
+       log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG);
+
+       closelog_called = 0;
+       openlog_called = 0;
+       log_set_target(LOG_TARGET_SYSLOG, LOG_USER);
+       assert(openlog_called);
+       assert(closelog_called);
+       assert(openlog_facility == LOG_USER);
+
+       closelog_called = 0;
+       log_close();
+       assert(closelog_called);
+
+       return (0);
+}