New simple tests, testing
 - standard (stdout) callback
 - custom application callback
 - all loglevels
 - automatic loglevel extraction from CGROUP_LOGLEVEL

Signed-off-by: Peter Schiffer <pschi...@redhat.com>
---
 tests/Makefile.am |    7 ++-
 tests/logger.c    |   53 +++++++++++++++++++++
 tests/logger.sh   |  137 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 194 insertions(+), 3 deletions(-)
 create mode 100644 tests/logger.c
 create mode 100755 tests/logger.sh

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0b77b42..4d556aa 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,7 +4,7 @@ INCLUDES = -I$(top_srcdir)/include
 LDADD = $(top_builddir)/src/.libs/libcgroup.la
 
 # compile the tests, but do not install them
-noinst_PROGRAMS = libcgrouptest01 libcg_ba setuid walk_test read_stats 
walk_task get_controller get_mount_point proctest get_all_controller 
get_variable_names test_named_hierarchy get_procs wrapper_test
+noinst_PROGRAMS = libcgrouptest01 libcg_ba setuid walk_test read_stats 
walk_task get_controller get_mount_point proctest get_all_controller 
get_variable_names test_named_hierarchy get_procs wrapper_test logger
 
 libcgrouptest01_SOURCES=libcgrouptest01.c test_functions.c libcgrouptest.h
 libcg_ba_SOURCES=libcg_ba.cpp
@@ -20,7 +20,8 @@ get_variable_names_SOURCES=get_variable_names.c
 test_named_hierarchy_SOURCES=test_named_hierarchy.c
 get_procs_SOURCES=get_procs.c
 wrapper_test_SOURCES=wrapper_test.c
+logger_SOURCES=logger.c
 
-EXTRA_DIST = runlibcgrouptest.sh
+EXTRA_DIST = runlibcgrouptest.sh logger.sh
 
-TESTS = wrapper_test runlibcgrouptest.sh
+TESTS = wrapper_test runlibcgrouptest.sh logger.sh
diff --git a/tests/logger.c b/tests/logger.c
new file mode 100644
index 0000000..b7f1b70
--- /dev/null
+++ b/tests/logger.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright Red Hat Inc., 2012
+ *
+ * Author:     Jan Safranek <jsafr...@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Description: This file contains the test code for libcgroup logging.
+ */
+
+#include "config.h"
+#include "libcgroup.h"
+#include "../src/libcgroup-internal.h"
+#include <string.h>
+#include <stdlib.h>
+
+static void mylogger(void *userdata, int loglevel, const char *fmt, va_list ap)
+{
+       printf("custom: ");
+       vprintf(fmt, ap);
+}
+
+int main(int argc, char **argv)
+{
+       int custom = 0;
+       int loglevel = -1;
+       int i;
+
+       for (i = 1; i < argc; i++) {
+               if (strcmp("custom", argv[i]) == 0)
+                       custom = 1;
+               else
+                       loglevel = atoi(argv[i]);
+       }
+
+       if (custom)
+               cgroup_set_logger(mylogger, loglevel, NULL);
+       else
+               cgroup_set_default_logger(loglevel);
+
+       cgroup_dbg("DEBUG message\n");
+       cgroup_info("INFO message\n");
+       cgroup_warn("WARNING message\n");
+       cgroup_err("ERROR message\n");
+
+       return 0;
+}
diff --git a/tests/logger.sh b/tests/logger.sh
new file mode 100755
index 0000000..b3c82a5
--- /dev/null
+++ b/tests/logger.sh
@@ -0,0 +1,137 @@
+#!/bin/bash
+# Test various log levels
+
+export LOGFILE=
+export RET=0
+unset CGROUP_LOGLEVEL
+
+function run_logger()
+{
+       LOGFILE=`mktemp`
+       echo "Running CGROUP_LOGLEVEL=$CGROUP_LOGLEVEL logger $* >$LOGFILE"
+       ./logger $* >$LOGFILE
+}
+
+function assert_grep()
+{
+       if ! grep "$@" <$LOGFILE >/dev/null; then
+               echo "Error: expecting $* in output"
+               RET=1
+       fi
+}
+
+function assert_not_grep()
+{
+       if grep "$@" <$LOGFILE >/dev/null; then
+               echo "Error: unexptected $* in output"
+               RET=1
+       fi
+}
+
+# CGROUP_LOGLEVEL is case-insensitive
+CGROUP_LOGLEVEL=DeBuG run_logger -1
+assert_grep "^DEBUG message"
+assert_grep "^INFO message"
+assert_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# missing CGROUP_LOGLEVEL leads to ERRORs only
+run_logger -1
+assert_not_grep "^DEBUG message"
+assert_not_grep "^INFO message"
+assert_not_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# wrong CGROUP_LOGLEVEL leads to ERRORs only
+CGROUP_LOGLEVEL=xyz run_logger -1
+assert_not_grep "^DEBUG message"
+assert_not_grep "^INFO message"
+assert_not_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# numeric CGROUP_LOGLEVEL
+CGROUP_LOGLEVEL=3 run_logger -1
+assert_not_grep "^DEBUG message"
+assert_grep "^INFO message"
+assert_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# errors only CGROUP_LOGLEVEL
+CGROUP_LOGLEVEL=ERROR run_logger -1
+assert_not_grep "^DEBUG message"
+assert_not_grep "^INFO message"
+assert_not_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# no CGROUP_LOGLEVEL -> DEBUG
+run_logger 4
+assert_grep "^DEBUG message"
+assert_grep "^INFO message"
+assert_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# no CGROUP_LOGLEVEL -> INFO
+run_logger 3
+assert_not_grep "^DEBUG message"
+assert_grep "^INFO message"
+assert_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# no CGROUP_LOGLEVEL -> WARN
+run_logger 2
+assert_not_grep "^DEBUG message"
+assert_not_grep "^INFO message"
+assert_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# no CGROUP_LOGLEVEL -> ERROR
+run_logger 1
+assert_not_grep "^DEBUG message"
+assert_not_grep "^INFO message"
+assert_not_grep "^WARNING message"
+assert_grep "^ERROR message"
+rm $LOGFILE
+
+# no CGROUP_LOGLEVEL -> nothing
+run_logger 0
+assert_not_grep "^DEBUG message"
+assert_not_grep "^INFO message"
+assert_not_grep "^WARNING message"
+assert_not_grep "^ERROR message"
+rm $LOGFILE
+
+
+# custom logger -> DEBUG
+run_logger custom 4
+assert_grep "^custom: DEBUG message"
+assert_grep "^custom: INFO message"
+assert_grep "^custom: WARNING message"
+assert_grep "^custom: ERROR message"
+rm $LOGFILE
+
+# custom logger -> INFO
+run_logger custom 3
+assert_not_grep "^custom: DEBUG message"
+assert_grep "^custom: INFO message"
+assert_grep "^custom: WARNING message"
+assert_grep "^custom: ERROR message"
+rm $LOGFILE
+
+# custom logger -> WARN
+run_logger custom 2
+assert_not_grep "^custom: DEBUG message"
+assert_not_grep "^custom: INFO message"
+assert_grep "^custom: WARNING message"
+assert_grep "^custom: ERROR message"
+rm $LOGFILE
+
+exit $RET
+


------------------------------------------------------------------------------
Own the Future-Intel(R) Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest. Compete 
for recognition, cash, and the chance to get your game on Steam. 
$5K grand prize plus 10 genre and skill prizes. Submit your demo 
by 6/6/13. http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to