]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
write_redis plugin: Add unit test.
authorFlorian Forster <octo@collectd.org>
Wed, 24 Jan 2024 15:19:17 +0000 (16:19 +0100)
committerFlorian Forster <octo@collectd.org>
Thu, 25 Jan 2024 19:53:29 +0000 (20:53 +0100)
Makefile.am
src/write_redis_test.c [new file with mode: 0644]

index 305ba2a74e943ff182f63a9f001975f3e20d7185..d8b0841bdf7b46d5daf8b8efe672b11637ef78dc 100644 (file)
@@ -2388,6 +2388,13 @@ write_redis_la_SOURCES = src/write_redis.c
 write_redis_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBHIREDIS_CPPFLAGS)
 write_redis_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBHIREDIS_LDFLAGS)
 write_redis_la_LIBADD = libformat_json.la -lhiredis
+
+test_plugin_write_redis_SOURCES = src/write_redis_test.c
+test_plugin_write_redis_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBHIREDIS_CPPFLAGS)
+test_plugin_write_redis_LDFLAGS = $(AM_LDFLAGS) $(BUILD_WITH_LIBMICROHTTPD_LDFLAGS)
+test_plugin_write_redis_LDADD = libformat_json.la libplugin_mock.la -lhiredis
+check_PROGRAMS += test_plugin_write_redis
+TESTS += test_plugin_write_redis
 endif
 
 if BUILD_PLUGIN_WRITE_RIEMANN
diff --git a/src/write_redis_test.c b/src/write_redis_test.c
new file mode 100644 (file)
index 0000000..86682ba
--- /dev/null
@@ -0,0 +1,136 @@
+/**
+ * collectd - src/write_redis.c
+ * Copyright (C) 2024       Florian 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 Forster <ff at octo.it>
+ **/
+
+#include "testing.h"
+#include "write_redis.c" /* sic */
+
+char **got_commands = NULL;
+size_t got_commands_num = 0;
+
+static int fake_execute(wr_node_t *node, int argc, char const **argv) {
+  strbuf_t cmd = STRBUF_CREATE;
+  for (int i = 0; i < argc; i++) {
+    if (i != 0) {
+      strbuf_print(&cmd, " ");
+    }
+    strbuf_print(&cmd, argv[i]);
+  }
+
+  got_commands = realloc(got_commands, sizeof(*got_commands) * (got_commands_num + 1));
+  got_commands[got_commands_num] = strdup(cmd.ptr);
+  got_commands_num++;
+
+  STRBUF_DESTROY(cmd);
+
+  return 0;
+}
+
+static void fake_disconnect(wr_node_t *node) {
+  for (size_t i = 0; i < got_commands_num; i++) {
+    free(got_commands[i]);
+  }
+  free(got_commands);
+  got_commands = NULL;
+  got_commands_num = 0;
+}
+
+static int fake_reconnect(wr_node_t *node) { return 0; }
+
+DEF_TEST(usage_rate) {
+  metric_family_t fam = {
+      .name = "unit.test",
+      .type = METRIC_TYPE_GAUGE,
+      .resource =
+          {
+              .ptr =
+                  (label_pair_t[]){
+                      {"test", "usage_rate"},
+                  },
+              .num = 1,
+          },
+      .metric =
+          (metric_list_t){
+              .ptr =
+                  (metric_t[]){
+                      {
+                          .label =
+                              {
+                                  .ptr =
+                                      (label_pair_t[]){
+                                          {"metric.type", "gauge"},
+                                      },
+                                  .num = 1,
+                              },
+                          .value = (value_t){.gauge = 42},
+                          .time = TIME_T_TO_CDTIME_T(100),
+                      },
+                  },
+              .num = 1,
+          },
+  };
+
+  for (size_t i = 0; i < fam.metric.num; i++) {
+    fam.metric.ptr[i].family = &fam;
+  }
+
+  wr_node_t node = {
+      .store_rates = false,
+
+      .reconnect = fake_reconnect,
+      .disconnect = fake_disconnect,
+      .execute = fake_execute,
+
+      .lock = PTHREAD_MUTEX_INITIALIZER,
+  };
+
+  user_data_t ud = {
+      .data = &node,
+  };
+
+  CHECK_ZERO(wr_write(&fam, &ud));
+
+#define METRIC_ID                                                              \
+  "{\"name\":\"unit.test\",\"resource\":{\"test\":\"usage_rate\"},\"labels\":" \
+  "{\"metric.type\":\"gauge\"}}"
+  char *want_commands[] = {
+      "ZADD " METRIC_ID " 100.000000000 100.000:42",
+      "SADD values " METRIC_ID,
+  };
+  size_t want_commands_num = STATIC_ARRAY_SIZE(want_commands);
+
+  for (size_t i = 0; i < want_commands_num && i < got_commands_num; i++) {
+    EXPECT_EQ_STR(want_commands[i], got_commands[i]);
+  }
+  EXPECT_EQ_INT(want_commands_num, got_commands_num);
+
+  return 0;
+}
+
+int main(void) {
+  RUN_TEST(usage_rate);
+
+  END_TEST;
+}