From: Vsevolod Stakhov Date: Sat, 15 Dec 2012 00:17:10 +0000 (+0400) Subject: * Add simple test for rrd. X-Git-Tag: 0.5.4~43 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fbcc7af8a6bfb2c82d17180358bfdadf0228cddd;p=rspamd.git * Add simple test for rrd. Several fixes to RRD API. TODO: fix test building TODO: improve update of rrd files TODO: rework shared libraries again --- diff --git a/src/rrd.c b/src/rrd.c index f24b4c3d5..e6abf8b18 100644 --- a/src/rrd.c +++ b/src/rrd.c @@ -30,6 +30,100 @@ rrd_error_quark (void) return g_quark_from_static_string ("rrd-error"); } +/** + * Convert rrd dst type from string to numeric value + */ +enum rrd_dst_type +rrd_dst_from_string (const gchar *str) +{ + if (g_ascii_strcasecmp (str, "counter") == 0) { + return RRD_DST_COUNTER; + } + else if (g_ascii_strcasecmp (str, "absolute") == 0) { + return RRD_DST_ABSOLUTE; + } + else if (g_ascii_strcasecmp (str, "gauge") == 0) { + return RRD_DST_GAUGE; + } + else if (g_ascii_strcasecmp (str, "cdef") == 0) { + return RRD_DST_CDEF; + } + else if (g_ascii_strcasecmp (str, "derive") == 0) { + return RRD_DST_DERIVE; + } + return -1; +} + +/** + * Convert numeric presentation of dst to string + */ +const gchar* +rrd_dst_to_string (enum rrd_dst_type type) +{ + switch (type) { + case RRD_DST_COUNTER: + return "COUNTER"; + case RRD_DST_ABSOLUTE: + return "ABSOLUTE"; + case RRD_DST_GAUGE: + return "GAUGE"; + case RRD_DST_CDEF: + return "CDEF"; + case RRD_DST_DERIVE: + return "DERIVE"; + default: + return "U"; + } + + return "U"; +} + +/** + * Convert rrd consolidation function type from string to numeric value + */ +enum rrd_cf_type +rrd_cf_from_string (const gchar *str) +{ + if (g_ascii_strcasecmp (str, "average") == 0) { + return RRD_CF_AVERAGE; + } + else if (g_ascii_strcasecmp (str, "minimum") == 0) { + return RRD_CF_MINIMUM; + } + else if (g_ascii_strcasecmp (str, "maximum") == 0) { + return RRD_CF_MAXIMUM; + } + else if (g_ascii_strcasecmp (str, "last") == 0) { + return RRD_CF_LAST; + } + /* XXX: add other CF functions supported by rrd */ + return -1; +} + +/** + * Convert numeric presentation of cf to string + */ +const gchar* +rrd_cf_to_string (enum rrd_cf_type type) +{ + switch (type) { + case RRD_CF_AVERAGE: + return "AVERAGE"; + case RRD_CF_MINIMUM: + return "MINIMUM"; + case RRD_CF_MAXIMUM: + return "MAXIMUM"; + case RRD_CF_LAST: + return "LAST"; + default: + return "U"; + } + + /* XXX: add other CF functions supported by rrd */ + + return "U"; +} + /** * Check rrd file for correctness (size, cookies, etc) */ @@ -107,7 +201,7 @@ rspamd_rrd_check_file (const gchar *filename, gboolean need_data, GError **err) close (fd); return FALSE; } - head_size += rra.row_cnt * head.ds_cnt; + head_size += rra.row_cnt * head.ds_cnt * sizeof (gdouble); } if (st.st_size != head_size) { @@ -357,7 +451,7 @@ gboolean rspamd_rrd_add_ds (struct rspamd_rrd_file *file, GArray *ds, GError **err) { - if (file == NULL || file->stat_head->ds_cnt != ds->len * sizeof (struct rrd_ds_def)) { + if (file == NULL || file->stat_head->ds_cnt * sizeof (struct rrd_ds_def) != ds->len) { g_set_error (err, rrd_error_quark (), EINVAL, "rrd add ds failed: wrong arguments"); return FALSE; } @@ -378,7 +472,7 @@ rspamd_rrd_add_ds (struct rspamd_rrd_file *file, GArray *ds, GError **err) gboolean rspamd_rrd_add_rra (struct rspamd_rrd_file *file, GArray *rra, GError **err) { - if (file == NULL || file->stat_head->rra_cnt != rra->len * sizeof (struct rrd_rra_def)) { + if (file == NULL || file->stat_head->rra_cnt * sizeof (struct rrd_rra_def) != rra->len) { g_set_error (err, rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments"); return FALSE; } @@ -399,7 +493,8 @@ gboolean rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) { gint fd; - guint i, count = 0; + guint i; + gint count = 0; gdouble vbuf[1024]; struct stat st; @@ -439,7 +534,7 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) while (count > 0) { /* Write values in buffered matter */ - if (write (fd, vbuf, MIN (G_N_ELEMENTS (vbuf), count) * sizeof (gdouble)) == -1) { + if (write (fd, vbuf, MIN ((gint)G_N_ELEMENTS (vbuf), count) * sizeof (gdouble)) == -1) { g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); close (fd); return FALSE; @@ -465,6 +560,8 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) /* Adjust pointers */ rspamd_rrd_adjust_pointers (file, TRUE); + file->finalized = TRUE; + return TRUE; } @@ -482,7 +579,7 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, guint rra_idx, GArray *poin gdouble *row; guint i; - if (file == NULL || file->stat_head->ds_cnt != points->len * sizeof (gdouble) || rra_idx >= file->stat_head->rra_cnt) { + if (file == NULL || file->stat_head->ds_cnt * sizeof (gdouble) != points->len || rra_idx >= file->stat_head->rra_cnt) { g_set_error (err, rrd_error_quark (), EINVAL, "rrd add points failed: wrong arguments"); return FALSE; } @@ -502,7 +599,7 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, guint rra_idx, GArray *poin } /* Write data */ - memcpy (row, points, points->len); + memcpy (row, points->data, points->len); return TRUE; } diff --git a/src/rrd.h b/src/rrd.h index 214f36d03..b1420a72a 100644 --- a/src/rrd.h +++ b/src/rrd.h @@ -340,4 +340,25 @@ gboolean rspamd_rrd_add_record (struct rspamd_rrd_file* file, guint rra_idx, GAr */ gint rspamd_rrd_close (struct rspamd_rrd_file* file); +/* + * Conversion functions + */ + +/** + * Convert rrd dst type from string to numeric value + */ +enum rrd_dst_type rrd_dst_from_string (const gchar *str); +/** + * Convert numeric presentation of dst to string + */ +const gchar* rrd_dst_to_string (enum rrd_dst_type type); +/** + * Convert rrd consolidation function type from string to numeric value + */ +enum rrd_cf_type rrd_cf_from_string (const gchar *str); +/** + * Convert numeric presentation of cf to string + */ +const gchar* rrd_cf_to_string (enum rrd_cf_type type); + #endif /* RRD_H_ */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 322229cad..acf80497a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,17 +7,21 @@ SET(TESTSRC rspamd_expression_test.c rspamd_url_test.c rspamd_dns_test.c rspamd_async_test.c - rspamd_dkim_test.c) + rspamd_dkim_test.c + rspamd_rrd_test.c) ADD_EXECUTABLE(rspamd-test EXCLUDE_FROM_ALL ${TESTSRC}) SET_TARGET_PROPERTIES(rspamd-test PROPERTIES LINKER_LANGUAGE C) SET_TARGET_PROPERTIES(rspamd-test PROPERTIES COMPILE_FLAGS "-DRSPAMD_TEST") -TARGET_LINK_LIBRARIES(rspamd-test rspamd_lua) -TARGET_LINK_LIBRARIES(rspamd-test rspamdserver) + TARGET_LINK_LIBRARIES(rspamd-test event) TARGET_LINK_LIBRARIES(rspamd-test m) TARGET_LINK_LIBRARIES(rspamd-test ${GLIB2_LIBRARIES}) TARGET_LINK_LIBRARIES(rspamd-test ${CMAKE_REQUIRED_LIBRARIES}) +TARGET_LINK_LIBRARIES(rspamd-test rspamd-mime) +TARGET_LINK_LIBRARIES(rspamd-test rspamd-server) +TARGET_LINK_LIBRARIES(rspamd-test rspamd-util) +TARGET_LINK_LIBRARIES(rspamd-test hiredis) IF(HAVE_LIBEVENT2) TARGET_LINK_LIBRARIES(rspamd-test event_pthreads) ENDIF(HAVE_LIBEVENT2) diff --git a/test/rspamd_rrd_test.c b/test/rspamd_rrd_test.c new file mode 100644 index 000000000..04d523e6c --- /dev/null +++ b/test/rspamd_rrd_test.c @@ -0,0 +1,76 @@ +/* Copyright (c) 2012, Vsevolod Stakhov + * All rights reserved. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED ''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 AUTHOR 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 "config.h" +#include "tests.h" +#include "rrd.h" +#include "main.h" + +void +rspamd_rrd_test_func () +{ + gchar tmpfile[PATH_MAX]; + struct rrd_rra_def rra; + struct rrd_ds_def ds; + GArray ar; + struct rspamd_rrd_file *rrd; + gint fd, i; + gdouble t; + + rspamd_snprintf (tmpfile, sizeof (tmpfile), "/tmp/rspamd_rrd.rrd"); + + /* Create sample rrd */ + g_assert ((rrd = rspamd_rrd_create (tmpfile, 1, 1, 5, NULL)) != NULL); + /* Add RRA */ + rspamd_strlcpy (rra.cf_nam, rrd_cf_to_string (RRD_CF_AVERAGE), sizeof (rra.cf_nam)); + rra.pdp_cnt = 1; + rra.row_cnt = 100; + ar.data = &rra; + ar.len = sizeof (rra); + g_assert (rspamd_rrd_add_rra (rrd, &ar, NULL)); + /* Add DS */ + rspamd_strlcpy (ds.dst, rrd_dst_to_string (RRD_DST_ABSOLUTE), sizeof (ds.dst)); + rspamd_strlcpy (ds.ds_nam, "test", sizeof (ds.ds_nam)); + ar.data = &ds; + ar.len = sizeof (ds); + g_assert (rspamd_rrd_add_ds (rrd, &ar, NULL)); + /* Finalize */ + g_assert (rspamd_rrd_finalize (rrd, NULL)); + /* Close */ + rspamd_rrd_close (rrd); + + /* Reopen */ + g_assert ((rrd = rspamd_rrd_open (tmpfile, NULL)) != NULL); + + /* Add some points */ + for (i = 0; i < 200; i ++) { + t = i; + ar.data = &t; + ar.len = sizeof (gdouble); + g_assert (rspamd_rrd_add_record (rrd, 0, &ar, NULL)); + } + + /* Finish */ + rspamd_rrd_close (rrd); + /* unlink (tmpfile); */ +} diff --git a/test/rspamd_statfile_test.c b/test/rspamd_statfile_test.c index 7a36a848d..f8e8a5294 100644 --- a/test/rspamd_statfile_test.c +++ b/test/rspamd_statfile_test.c @@ -17,7 +17,7 @@ rspamd_statfile_test_func () p = memory_pool_new (memory_pool_get_size ()); umask (S_IWGRP | S_IWOTH); - pool = statfile_pool_new (p, 10 * 1024 * 1024); + pool = statfile_pool_new (p, 10 * 1024 * 1024, TRUE); now = time (NULL); /* Fill random array */ diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index 283cae57d..b1f273cba 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -70,6 +70,7 @@ main (int argc, char **argv) g_test_add_func ("/rspamd/dns", rspamd_dns_test_func); g_test_add_func ("/rspamd/aio", rspamd_async_test_func); g_test_add_func ("/rspamd/dkim", rspamd_dkim_test_func); + g_test_add_func ("/rspamd/rrd", rspamd_rrd_test_func); g_test_run (); diff --git a/test/tests.h b/test/tests.h index 135bfd163..8fb22b656 100644 --- a/test/tests.h +++ b/test/tests.h @@ -6,30 +6,33 @@ */ /* URL parser test */ -void rspamd_url_test_func (); +void rspamd_url_test_func (void); /* Memcached library test */ -void rspamd_memcached_test_func (); +void rspamd_memcached_test_func (void); /* Memory pools */ -void rspamd_mem_pool_test_func (); +void rspamd_mem_pool_test_func (void); /* Expressions */ -void rspamd_expression_test_func (); +void rspamd_expression_test_func (void); /* Fuzzy hashes */ -void rspamd_fuzzy_test_func (); +void rspamd_fuzzy_test_func (void); /* Stat file */ -void rspamd_statfile_test_func (); +void rspamd_statfile_test_func (void); /* DNS resolving */ -void rspamd_dns_test_func (); +void rspamd_dns_test_func (void); /* Async IO */ -void rspamd_async_test_func (); +void rspamd_async_test_func (void); /* DKIM test */ -void rspamd_dkim_test_func (); +void rspamd_dkim_test_func (void); + +/* RRD test */ +void rspamd_rrd_test_func (void); #endif