onewire plugin: fix two compiler warnings
[collectd.git] / src / utils_vl_lookup_test.c
index 6265b32..6d23c8c 100644 (file)
@@ -1,6 +1,6 @@
 /**
- * collectd - src/utils_vl_lookup_test.c
- * Copyright (C) 2012  Florian Forster
+ * collectd - src/tests/test_utils_vl_lookup.c
+ * Copyright (C) 2012       Florian Forster
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -25,6 +25,7 @@
  **/
 
 #include "collectd.h"
+#include "testing.h"
 #include "utils_vl_lookup.h"
 
 static _Bool expect_new_obj = 0;
@@ -46,7 +47,8 @@ static int lookup_obj_callback (data_set_t const *ds,
   identifier_t *class = user_class;
   identifier_t *obj = user_obj;
 
-  assert (expect_new_obj == have_new_obj);
+  OK1(expect_new_obj == have_new_obj,
+      (expect_new_obj ? "New obj is created." : "Updating existing obj."));
 
   memcpy (&last_class_ident, class, sizeof (last_class_ident));
   memcpy (&last_obj_ident, obj, sizeof (last_obj_ident));
@@ -79,14 +81,14 @@ static void *lookup_class_callback (data_set_t const *ds,
   return ((void *) obj);
 }
 
-static void checked_lookup_add (lookup_t *obj, /* {{{ */
+static int checked_lookup_add (lookup_t *obj, /* {{{ */
     char const *host,
     char const *plugin, char const *plugin_instance,
-    char const *type, char const *type_instance)
+    char const *type, char const *type_instance,
+    unsigned int group_by)
 {
   identifier_t ident;
   void *user_class;
-  int status;
 
   memset (&ident, 0, sizeof (ident));
   strncpy (ident.host, host, sizeof (ident.host));
@@ -98,9 +100,9 @@ static void checked_lookup_add (lookup_t *obj, /* {{{ */
   user_class = malloc (sizeof (ident));
   memmove (user_class, &ident, sizeof (ident));
 
-  status = lookup_add (obj, &ident, user_class);
-  assert (status == 0);
-} /* }}} void test_add */
+  OK(lookup_add (obj, &ident, group_by, user_class) == 0);
+  return 0;
+} /* }}} int checked_lookup_add */
 
 static int checked_lookup_search (lookup_t *obj,
     char const *host,
@@ -128,22 +130,13 @@ static int checked_lookup_search (lookup_t *obj,
   return (status);
 }
 
-static lookup_t *checked_lookup_create (void)
+DEF_TEST(group_by_specific_host)
 {
-  lookup_t *obj = lookup_create (
-      lookup_class_callback,
-      lookup_obj_callback,
-      (void *) free,
-      (void *) free);
-  assert (obj != NULL);
-  return (obj);
-}
+  lookup_t *obj;
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
 
-static void testcase0 (void)
-{
-  lookup_t *obj = checked_lookup_create ();
-
-  checked_lookup_add (obj, "/any/", "test", "", "test", "/all/");
+  checked_lookup_add (obj, "/.*/", "test", "", "test", "/.*/", LU_GROUP_BY_HOST);
   checked_lookup_search (obj, "host0", "test", "", "test", "0",
       /* expect new = */ 1);
   checked_lookup_search (obj, "host0", "test", "", "test", "1",
@@ -154,13 +147,16 @@ static void testcase0 (void)
       /* expect new = */ 0);
 
   lookup_destroy (obj);
+  return (0);
 }
 
-static void testcase1 (void)
+DEF_TEST(group_by_any_host)
 {
-  lookup_t *obj = checked_lookup_create ();
+  lookup_t *obj;
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
 
-  checked_lookup_add (obj, "/any/", "/all/", "/all/", "test", "/all/");
+  checked_lookup_add (obj, "/.*/", "/.*/", "/.*/", "test", "/.*/", LU_GROUP_BY_HOST);
   checked_lookup_search (obj, "host0", "plugin0", "", "test", "0",
       /* expect new = */ 1);
   checked_lookup_search (obj, "host0", "plugin0", "", "test", "1",
@@ -179,15 +175,19 @@ static void testcase1 (void)
       /* expect new = */ 0);
 
   lookup_destroy (obj);
+  return (0);
 }
 
-static void testcase2 (void)
+DEF_TEST(multiple_lookups)
 {
-  lookup_t *obj = checked_lookup_create ();
+  lookup_t *obj;
   int status;
 
-  checked_lookup_add (obj, "/any/", "plugin0", "", "test", "/all/");
-  checked_lookup_add (obj, "/any/", "/all/", "", "test", "ti0");
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
+
+  checked_lookup_add (obj, "/.*/", "plugin0", "", "test", "/.*/", LU_GROUP_BY_HOST);
+  checked_lookup_add (obj, "/.*/", "/.*/", "", "test", "ti0", LU_GROUP_BY_HOST);
 
   status = checked_lookup_search (obj, "host0", "plugin1", "", "test", "",
       /* expect new = */ 0);
@@ -203,12 +203,46 @@ static void testcase2 (void)
   assert (status == 2);
 
   lookup_destroy (obj);
+  return (0);
+}
+
+DEF_TEST(regex)
+{
+  lookup_t *obj;
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
+
+  checked_lookup_add (obj, "/^db[0-9]\\./", "cpu", "/.*/", "cpu", "/.*/",
+      LU_GROUP_BY_TYPE_INSTANCE);
+  checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "user",
+      /* expect new = */ 1);
+  checked_lookup_search (obj, "db0.example.com", "cpu", "0", "cpu", "idle",
+      /* expect new = */ 1);
+  checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "user",
+      /* expect new = */ 0);
+  checked_lookup_search (obj, "db0.example.com", "cpu", "1", "cpu", "idle",
+      /* expect new = */ 0);
+  checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "user",
+      /* expect new = */ 0);
+  checked_lookup_search (obj, "app0.example.com", "cpu", "0", "cpu", "idle",
+      /* expect new = */ 0);
+  checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "user",
+      /* expect new = */ 0);
+  checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "idle",
+      /* expect new = */ 0);
+  checked_lookup_search (obj, "db1.example.com", "cpu", "0", "cpu", "system",
+      /* expect new = */ 1);
+
+  lookup_destroy (obj);
+  return (0);
 }
 
 int main (int argc, char **argv) /* {{{ */
 {
-  testcase0 ();
-  testcase1 ();
-  testcase2 ();
-  return (EXIT_SUCCESS);
+  RUN_TEST(group_by_specific_host);
+  RUN_TEST(group_by_any_host);
+  RUN_TEST(multiple_lookups);
+  RUN_TEST(regex);
+
+  END_TEST;
 } /* }}} int main */