On 05/12/2011 08:58 AM, Jan Safranek wrote:
> New simple tests, testing
>   - standard (stdout) callback
>   - custom application callback
>   - all loglevels
>   - automatic loglevel extraction from CGROUP_LOGLEVEL
>
If libcgroup is configred with --disable-debug, logger.sh test will be 
broken:
CGROUP_LOGLEVEL=DeBuG,
CGROUP_LOGLEVEL= logger 4 and
CGROUP_LOGLEVEL= logger custom 4
test have hardcoded mandatory debuginfo output which is not produced

> Signed-off-by: Jan Safranek<jsafr...@redhat.com>
> ---
>
>   tests/Makefile.am |    7 ++-
>   tests/logger.c    |   53 +++++++++++++++++++++
>   tests/logger.sh   |  136 
> +++++++++++++++++++++++++++++++++++++++++++++++++++++
>   3 files changed, 193 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 95ff008..e7f2094 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 pathtest walk_test 
> read_stats walk_task get_controller get_mount_point proctest 
> get_all_controller get_variable_names test_named_hierarchy get_procs
> +noinst_PROGRAMS = libcgrouptest01 libcg_ba setuid pathtest walk_test 
> read_stats walk_task get_controller get_mount_point proctest 
> get_all_controller get_variable_names test_named_hierarchy get_procs logger
>
>   libcgrouptest01_SOURCES=libcgrouptest01.c test_functions.c libcgrouptest.h
>   libcg_ba_SOURCES=libcg_ba.cpp
> @@ -20,7 +20,8 @@ get_all_controller_SOURCES=get_all_controller.c
>   get_variable_names_SOURCES=get_variable_names.c
>   test_named_hierarchy_SOURCES=test_named_hierarchy.c
>   get_procs_SOURCES=get_procs.c
> +logger_SOURCES=logger.c
>
> -EXTRA_DIST = pathtest.sh runlibcgrouptest.sh
> +EXTRA_DIST = pathtest.sh runlibcgrouptest.sh logger.sh
>
> -TESTS = runlibcgrouptest.sh
> +TESTS = runlibcgrouptest.sh logger.sh
> diff --git a/tests/logger.c b/tests/logger.c
> new file mode 100644
> index 0000000..392b2f6
> --- /dev/null
> +++ b/tests/logger.c
> @@ -0,0 +1,53 @@
> +/*
> + * Copyright Red Hat Inc., 2011
> + *
> + * 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..2b0c403
> --- /dev/null
> +++ b/tests/logger.sh
> @@ -0,0 +1,136 @@
> +#!/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 WARNINGs only
> +run_logger -1
> +assert_not_grep "^DEBUG message"
> +assert_not_grep "^INFO message"
> +assert_grep "^WARNING message"
> +assert_grep "^ERROR message"
> +rm $LOGFILE
> +
> +# wrong CGROUP_LOGLEVEL leads to WARNINGs only
> +CGROUP_LOGLEVEL=xyz run_logger -1
> +assert_not_grep "^DEBUG message"
> +assert_not_grep "^INFO message"
> +assert_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
>
>
> ------------------------------------------------------------------------------
> Achieve unprecedented app performance and reliability
> What every C/C++ and Fortran developer should know.
> Learn how Intel has extended the reach of its next-generation tools
> to help boost performance applications - inlcuding clusters.
> http://p.sf.net/sfu/intel-dev2devmay
> _______________________________________________
> Libcg-devel mailing list
> Libcg-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/libcg-devel


------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its 
next-generation tools to help Windows* and Linux* C/C++ and Fortran 
developers boost performance applications - including clusters. 
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to