about summary refs log tree commit diff
path: root/test
diff options
context:
space:
mode:
authorHeikki Kallasjoki <fis@zem.fi>2018-12-12 16:10:34 +0000
committerHeikki Kallasjoki <fis+github@zem.fi>2018-12-12 19:10:51 +0000
commit79b14a8f79b63f79a282551c913f82ca4d1ed22d (patch)
tree504da4094d5d17678faf6b72174338597bab56f1 /test
parent5ce4474f7e6cb2256fee2619b4695772768b65d3 (diff)
downloadnano-exporter-79b14a8f79b63f79a282551c913f82ca4d1ed22d.tar.gz
nano-exporter-79b14a8f79b63f79a282551c913f82ca4d1ed22d.tar.xz
nano-exporter-79b14a8f79b63f79a282551c913f82ca4d1ed22d.zip
Add basic test for the hwmon collector.
Diffstat (limited to 'test')
-rw-r--r--test/Makefile2
-rw-r--r--test/harness.c13
-rw-r--r--test/harness.h1
-rw-r--r--test/hwmon_test.c79
-rw-r--r--test/mock_scrape.c38
-rw-r--r--test/mock_scrape.h1
6 files changed, 133 insertions, 1 deletions
diff --git a/test/Makefile b/test/Makefile
index 0c3374a..a6d5ae6 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-COLLECTOR_TESTS := cpu diskstats filesystem
+COLLECTOR_TESTS := cpu diskstats filesystem hwmon
 
 COLLECTOR_TEST_PROGS := $(foreach c,$(COLLECTOR_TESTS),$(c)_test)
 COLLECTOR_TEST_OBJS := $(foreach p,$(COLLECTOR_TEST_PROGS),$(p).o)
diff --git a/test/harness.c b/test/harness.c
index d88a092..9d80196 100644
--- a/test/harness.c
+++ b/test/harness.c
@@ -68,6 +68,19 @@ void test_write_file(test_env *env, const char *path, const char *contents) {
   close(fd);
 }
 
+void test_add_link(test_env *env, const char *path, const char *target) {
+  if (*path == '/')
+    test_fail(env, "test_write_file path is absolute: %s", path);
+  const char *file = strrchr(path, '/');
+  file = file ? file + 1 : path;
+
+  testdir_setup(env);
+  int dir_fd = testdir_getdir(env, path);
+  if (symlinkat(target, dir_fd, file) == -1)
+    test_fail(env, "in %s: %s: unable to create a symlink to %s: %s", path, file, target, strerror(errno));
+  testdir_closedir(env, dir_fd);
+}
+
 void test_fail(test_env *env, const char *err, ...) {
   va_list ap;
 
diff --git a/test/harness.h b/test/harness.h
index e7013a7..43d163b 100644
--- a/test/harness.h
+++ b/test/harness.h
@@ -26,6 +26,7 @@ typedef struct test_env test_env;
 // functions for use from tests
 
 void test_write_file(test_env *env, const char *path, const char *contents);
+void test_add_link(test_env *env, const char *path, const char *target);
 
 void test_fail(test_env *env, const char *err, ...);
 
diff --git a/test/hwmon_test.c b/test/hwmon_test.c
new file mode 100644
index 0000000..0df510b
--- /dev/null
+++ b/test/hwmon_test.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2018 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "harness.h"
+#include "mock_scrape.h"
+#include "../collector.h"
+
+extern const struct collector hwmon_collector;
+
+TEST(hwmon_metrics) {
+  test_write_file(env, "sys/devices/virtual/hwmon/hwmon0/name", "acpitz\n");
+  test_write_file(env, "sys/devices/virtual/hwmon/hwmon0/temp1_input", "27800\n");
+  test_write_file(env, "sys/devices/virtual/hwmon/hwmon0/temp2_input", "29800\n");
+  test_add_link(env, "sys/class/hwmon/hwmon0", "../../devices/virtual/hwmon/hwmon0");
+  test_write_file(env, "sys/devices/platform/it87.2608/hwmon/hwmon1/name", "unused_name\n");
+  test_write_file(env, "sys/devices/platform/it87.2608/hwmon/hwmon1/fan1_input", "1305\n");
+  test_write_file(env, "sys/devices/platform/it87.2608/hwmon/hwmon1/fan1_min", "500\n");
+  test_write_file(env, "sys/devices/platform/it87.2608/hwmon/hwmon1/fan1_alarm", "0\n");
+  test_write_file(env, "sys/devices/platform/it87.2608/hwmon/hwmon1/fan2_input", "0\n");
+  test_write_file(env, "sys/devices/platform/it87.2608/hwmon/hwmon1/fan2_min", "500\n");
+  test_write_file(env, "sys/devices/platform/it87.2608/hwmon/hwmon1/fan2_alarm", "1\n");
+  test_add_link(env, "sys/class/hwmon/hwmon1", "../../devices/platform/it87.2608/hwmon/hwmon1");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in0_input", "3020\n");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in0_min", "2700\n");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in0_max", "3300\n");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in0_alarm", "0\n");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in1_input", "2109\n");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in1_min", "2700\n");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in1_max", "3300\n");
+  test_write_file(env, "sys/class/hwmon/hwmon2/in1_alarm", "1\n");
+  scrape_req *req = mock_scrape_start(env);
+
+  hwmon_collector.collect(req, 0);
+
+  struct label *labels_acpitz_temp1 = LABEL_LIST({"chip", "hwmon/acpitz"}, {"sensor", "temp1"});
+  struct label *labels_acpitz_temp2 = LABEL_LIST({"chip", "hwmon/acpitz"}, {"sensor", "temp2"});
+  struct label *labels_it87_fan1 = LABEL_LIST({"chip", "platform/it87.2608"}, {"sensor", "fan1"});
+  struct label *labels_it87_fan2 = LABEL_LIST({"chip", "platform/it87.2608"}, {"sensor", "fan2"});
+  struct label *labels_unknown_in0 = LABEL_LIST({"chip", "unknown"}, {"sensor", "in0"});
+  struct label *labels_unknown_in1 = LABEL_LIST({"chip", "unknown"}, {"sensor", "in1"});
+  mock_scrape_sort(req);
+  mock_scrape_expect(req, "node_hwmon_fan_alarm", labels_it87_fan1, 0);
+  mock_scrape_expect(req, "node_hwmon_fan_alarm", labels_it87_fan2, 1);
+  mock_scrape_expect(req, "node_hwmon_fan_min_rpm", labels_it87_fan1, 500);
+  mock_scrape_expect(req, "node_hwmon_fan_min_rpm", labels_it87_fan2, 500);
+  mock_scrape_expect(req, "node_hwmon_fan_rpm", labels_it87_fan1, 1305);
+  mock_scrape_expect(req, "node_hwmon_fan_rpm", labels_it87_fan2, 0);
+  mock_scrape_expect(req, "node_hwmon_in_alarm", labels_unknown_in0, 0);
+  mock_scrape_expect(req, "node_hwmon_in_alarm", labels_unknown_in1, 1);
+  mock_scrape_expect(req, "node_hwmon_in_max_volts", labels_unknown_in0, 3.3);
+  mock_scrape_expect(req, "node_hwmon_in_max_volts", labels_unknown_in1, 3.3);
+  mock_scrape_expect(req, "node_hwmon_in_min_volts", labels_unknown_in0, 2.7);
+  mock_scrape_expect(req, "node_hwmon_in_min_volts", labels_unknown_in1, 2.7);
+  mock_scrape_expect(req, "node_hwmon_in_volts", labels_unknown_in0, 3.02);
+  mock_scrape_expect(req, "node_hwmon_in_volts", labels_unknown_in1, 2.109);
+  mock_scrape_expect(req, "node_hwmon_temp_celsius", labels_acpitz_temp1, 27.8);
+  mock_scrape_expect(req, "node_hwmon_temp_celsius", labels_acpitz_temp2, 29.8);
+  mock_scrape_expect_no_more(req);
+  mock_scrape_free(req);
+}
+
+TEST_SUITE {
+  TEST_SUITE_START;
+  RUN_TEST(hwmon_metrics);
+  TEST_SUITE_END;
+}
diff --git a/test/mock_scrape.c b/test/mock_scrape.c
index 82a17d7..01a681e 100644
--- a/test/mock_scrape.c
+++ b/test/mock_scrape.c
@@ -15,6 +15,7 @@
  */
 
 #include <math.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "harness.h"
@@ -82,6 +83,43 @@ void mock_scrape_free(scrape_req *req) {
   free(req);
 }
 
+static int metric_cmp(const void *a_ptr, const void *b_ptr) {
+  const struct scrape_metric *a = a_ptr;
+  const struct scrape_metric *b = b_ptr;
+  int c;
+
+  c = strcmp(a->metric, b->metric);
+  if (c != 0)
+    return c;
+
+  unsigned a_labels = 0, b_labels = 0;
+  for (const struct label *p = a->labels; p && p->key; p++) a_labels++;
+  for (const struct label *p = b->labels; p && p->key; p++) b_labels++;
+  if (a_labels < b_labels)
+    return -1;
+  else if (a_labels > b_labels)
+    return 1;
+  for (const struct label *pa = a->labels, *pb = b->labels; pa && pa->key; pa++, pb++) {
+    c = strcmp(pa->key, pb->key);
+    if (c != 0)
+      return c;
+    c = strcmp(pa->value, pb->value);
+    if (c != 0)
+      return c;
+  }
+
+  if (a->value < b->value)
+    return -1;
+  else if (a->value > b->value)
+    return 1;
+  return 0;
+}
+
+void mock_scrape_sort(scrape_req *req) {
+  if (req->metrics_written > 0)
+    qsort(req->metrics, req->metrics_written, sizeof *req->metrics, metric_cmp);
+}
+
 void mock_scrape_expect(scrape_req *req, const char *metric, const struct label *labels, double value) {
   if (req->metrics_tested >= req->metrics_written) {
     dump_metrics(req);
diff --git a/test/mock_scrape.h b/test/mock_scrape.h
index bbb3767..57b201f 100644
--- a/test/mock_scrape.h
+++ b/test/mock_scrape.h
@@ -23,6 +23,7 @@
 scrape_req *mock_scrape_start(test_env *env);
 void mock_scrape_free(scrape_req *req);
 
+void mock_scrape_sort(scrape_req *req);
 void mock_scrape_expect(scrape_req *req, const char *metric, const struct label *labels, double value);
 void mock_scrape_expect_raw(scrape_req *req, const char *str);
 void mock_scrape_expect_no_more(scrape_req *req);