From ce2fb80a17bf452d8b5bf4660453ffee510e0868 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 26 Nov 2015 10:33:07 +0100 Subject: [PATCH] utils_time.[ch]: Control mocking by preprocessor define. This allows us to test rfc3339() while mocking out cdtime(). --- src/Makefile.am | 6 +++--- src/daemon/Makefile.am | 10 ++++++---- src/daemon/utils_time.c | 21 +++++++++++++++++---- src/daemon/utils_time.h | 6 ++++++ src/daemon/utils_time_mock.c | 33 --------------------------------- src/testing.h | 5 +++++ 6 files changed, 37 insertions(+), 44 deletions(-) delete mode 100644 src/daemon/utils_time_mock.c diff --git a/src/Makefile.am b/src/Makefile.am index d3b1acd3..6267a310 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,7 +28,7 @@ liblatency_la_SOURCES = utils_latency.c utils_latency.h check_PROGRAMS += test_utils_latency TESTS += test_utils_latency test_utils_latency_SOURCES = utils_latency_test.c testing.h -test_utils_latency_LDADD = liblatency.la daemon/libcommon.la daemon/libplugin_mock.la -lm +test_utils_latency_LDADD = liblatency.la daemon/libplugin_mock.la -lm noinst_LTLIBRARIES += liblookup.la liblookup_la_SOURCES = utils_vl_lookup.c utils_vl_lookup.h @@ -36,7 +36,7 @@ liblookup_la_LIBADD = daemon/libavltree.la check_PROGRAMS += test_utils_vl_lookup TESTS += test_utils_vl_lookup test_utils_vl_lookup_SOURCES = utils_vl_lookup_test.c testing.h -test_utils_vl_lookup_LDADD = liblookup.la daemon/libcommon.la daemon/libplugin_mock.la +test_utils_vl_lookup_LDADD = liblookup.la daemon/libplugin_mock.la if BUILD_WITH_LIBKSTAT test_utils_vl_lookup_LDADD += -lkstat endif @@ -46,7 +46,7 @@ libmount_la_SOURCES = utils_mount.c utils_mount.h check_PROGRAMS += test_utils_mount TESTS += test_utils_mount test_utils_mount_SOURCES = utils_mount_test.c testing.h -test_utils_mount_LDADD = libmount.la daemon/libcommon.la daemon/libplugin_mock.la +test_utils_mount_LDADD = libmount.la daemon/libplugin_mock.la if BUILD_WITH_LIBKSTAT test_utils_mount_LDADD += -lkstat endif diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index 3d50029b..cdd927bc 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -48,8 +48,10 @@ libheap_la_SOURCES = utils_heap.c utils_heap.h libmetadata_la_SOURCES = meta_data.c meta_data.h -libplugin_mock_la_SOURCES = plugin_mock.c utils_cache_mock.c utils_time_mock.c -libplugin_mock_la_LIBADD = $(COMMON_LIBS) +libplugin_mock_la_SOURCES = plugin_mock.c utils_cache_mock.c \ + utils_time.c utils_time.h +libplugin_mock_la_CPPFLAGS = $(AM_CPPFLAGS) -DMOCK_TIME +libplugin_mock_la_LIBADD = $(COMMON_LIBS) libcommon.la collectd_SOURCES = collectd.c collectd.h \ configfile.c configfile.h \ @@ -94,7 +96,7 @@ check_PROGRAMS = test_common test_meta_data test_utils_avltree test_utils_heap t TESTS = test_common test_meta_data test_utils_avltree test_utils_heap test_utils_time test_utils_subst test_common_SOURCES = common_test.c ../testing.h -test_common_LDADD = libcommon.la libplugin_mock.la +test_common_LDADD = libplugin_mock.la test_meta_data_SOURCES = meta_data_test.c ../testing.h test_meta_data_LDADD = libmetadata.la libplugin_mock.la @@ -109,4 +111,4 @@ test_utils_time_SOURCES = utils_time_test.c ../testing.h test_utils_subst_SOURCES = utils_subst_test.c ../testing.h \ utils_subst.c utils_subst.h -test_utils_subst_LDADD = libcommon.la libplugin_mock.la +test_utils_subst_LDADD = libplugin_mock.la diff --git a/src/daemon/utils_time.c b/src/daemon/utils_time.c index f500e138..7f482da7 100644 --- a/src/daemon/utils_time.c +++ b/src/daemon/utils_time.c @@ -1,6 +1,6 @@ /** * collectd - src/utils_time.c - * Copyright (C) 2010 Florian octo Forster + * Copyright (C) 2010-2015 Florian octo Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -21,7 +21,7 @@ * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian octo Forster + * Florian octo Forster **/ #include "collectd.h" @@ -29,7 +29,19 @@ #include "plugin.h" #include "common.h" -#if HAVE_CLOCK_GETTIME +#ifndef DEFAULT_MOCK_TIME +# define DEFAULT_MOCK_TIME 1542455354518929408ULL +#endif + +#ifdef MOCK_TIME +cdtime_t cdtime_mock = (cdtime_t) MOCK_TIME; + +cdtime_t cdtime (void) +{ + return cdtime_mock; +} +#else /* !MOCK_TIME */ +# if HAVE_CLOCK_GETTIME cdtime_t cdtime (void) /* {{{ */ { int status; @@ -46,7 +58,7 @@ cdtime_t cdtime (void) /* {{{ */ return (TIMESPEC_TO_CDTIME_T (&ts)); } /* }}} cdtime_t cdtime */ -#else +# else /* !HAVE_CLOCK_GETTIME */ /* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */ cdtime_t cdtime (void) /* {{{ */ { @@ -64,6 +76,7 @@ cdtime_t cdtime (void) /* {{{ */ return (TIMEVAL_TO_CDTIME_T (&tv)); } /* }}} cdtime_t cdtime */ +# endif #endif /* format_zone reads time zone information from "extern long timezone", exported diff --git a/src/daemon/utils_time.h b/src/daemon/utils_time.h index 07e560af..9e980406 100644 --- a/src/daemon/utils_time.h +++ b/src/daemon/utils_time.h @@ -29,6 +29,12 @@ #include "collectd.h" +#ifdef TESTING_H +/* cdtime_mock is the time returned by cdtime() when build with + * -DMOCK_TIME */ +extern cdtime_t cdtime_mock; +#endif + /* * "cdtime_t" is a 64bit unsigned integer. The time is stored at a 2^-30 second * resolution, i.e. the most significant 34 bit are used to store the time in diff --git a/src/daemon/utils_time_mock.c b/src/daemon/utils_time_mock.c deleted file mode 100644 index 217515eb..00000000 --- a/src/daemon/utils_time_mock.c +++ /dev/null @@ -1,33 +0,0 @@ -/** - * collectd - src/tests/mock/utils_time.c - * Copyright (C) 2013 Florian octo Forster - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - * Authors: - * Florian octo Forster - */ - -#include "utils_time.h" - -cdtime_t cdtime (void) -{ - return ((cdtime_t) 1542455354518929408ULL); -} - diff --git a/src/testing.h b/src/testing.h index 0c415e48..c0a9e88a 100644 --- a/src/testing.h +++ b/src/testing.h @@ -24,6 +24,9 @@ * Florian octo Forster */ +#ifndef TESTING_H +#define TESTING_H 1 + #include static int fail_count__ = 0; @@ -109,3 +112,5 @@ static int check_count__ = 0; status_ = (long) (expr); \ OK1(status_ == 0L, #expr); \ } while (0) + +#endif /* TESTING_H */ -- 2.30.2