]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
write_prometheus plugin: Add two more simple unit test cases.
authorFlorian Forster <octo@collectd.org>
Thu, 21 Dec 2023 09:53:07 +0000 (10:53 +0100)
committerFlorian Forster <octo@collectd.org>
Thu, 21 Dec 2023 11:59:59 +0000 (12:59 +0100)
src/write_prometheus_test.c

index fcf8e92aff99a653c6cf8273cf52e50d4f7af8fa..2f8d77617e0dfa5eaf64674cf26e7c7cd2d1fab4 100644 (file)
 
 #include "collectd.h"
 
-#include "testing.h"
 #include "daemon/metric.h"
+#include "testing.h"
+#include "utils/common/common.h"
 
 void format_metric_family(strbuf_t *buf, metric_family_t const *prom_fam);
 
 DEF_TEST(format_metric_family) {
-  strbuf_t got = STRBUF_CREATE;
-
-  metric_family_t fam = {
-    .name = "unit.test",
+  struct {
+    char const *name;
+    metric_family_t fam;
+    char const *want;
+  } cases[] = {
+      {
+          .name = "metrics is empty",
+          .fam =
+              {
+                  .name = "unit.test",
+              },
+          .want = NULL,
+      },
+      {
+          .name = "metric without labels",
+          .fam =
+              {
+                  .name = "unittest",
+                  .type = METRIC_TYPE_COUNTER,
+                  .metric =
+                      {
+                          .ptr =
+                              &(metric_t){
+                                  .value = (value_t){
+                                    .counter = 42,
+                                  },
+                              },
+                          .num = 1,
+                      },
+              },
+          .want = "# HELP unittest\n"
+              "# TYPE unittest counter\n"
+              "unittest 42\n",
+      },
+      {
+          .name = "metric with one label",
+          .fam =
+              {
+                  .name = "unittest",
+                  .type = METRIC_TYPE_COUNTER,
+                  .metric =
+                      {
+                          .ptr =
+                              &(metric_t){
+                                  .label =
+                                      {
+                                          .ptr =
+                                              &(label_pair_t){
+                                                  .name = "foo",
+                                                  .value = "bar",
+                                              },
+                                          .num = 1,
+                                      },
+                                  .value = (value_t){
+                                    .counter = 42,
+                                  },
+                              },
+                          .num = 1,
+                      },
+              },
+          .want = "# HELP unittest\n"
+              "# TYPE unittest counter\n"
+              "unittest{foo=\"bar\"} 42\n",
+      },
   };
 
-  format_metric_family(&got, &fam);
-  EXPECT_EQ_INT(0, got.pos);
+  for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
+    printf("# Case %zu: %s\n", i, cases[i].name);
+    strbuf_t got = STRBUF_CREATE;
+
+    metric_family_t *fam = &cases[i].fam;
+    for (size_t j = 0; j < fam->metric.num; j++) {
+      fam->metric.ptr[j].family = fam;
+    }
+
+    format_metric_family(&got, &cases[i].fam);
+    EXPECT_EQ_STR(cases[i].want, got.ptr);
+
+    STRBUF_DESTROY(got);
+  }
 
-  STRBUF_DESTROY(got);
   return 0;
 }