about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHeikki Kallasjoki <fis@zem.fi>2018-12-12 18:30:35 +0000
committerHeikki Kallasjoki <fis+github@zem.fi>2018-12-12 19:10:51 +0000
commit2f1cce70ee79d0f23a1b2f36cd530a7a9fd8c457 (patch)
tree392eefcbfae46b96584fa2825bcdbda5e4f56728
parent35d35c197f2c115feb3b1fcc225b7ac56781b2b1 (diff)
downloadnano-exporter-2f1cce70ee79d0f23a1b2f36cd530a7a9fd8c457.tar.gz
nano-exporter-2f1cce70ee79d0f23a1b2f36cd530a7a9fd8c457.tar.xz
nano-exporter-2f1cce70ee79d0f23a1b2f36cd530a7a9fd8c457.zip
Add basic test for the stat collector.
-rw-r--r--stat.c2
-rw-r--r--test/Makefile2
-rw-r--r--test/stat_test.c55
3 files changed, 57 insertions, 2 deletions
diff --git a/stat.c b/stat.c
index e36579d..a5ac52a 100644
--- a/stat.c
+++ b/stat.c
@@ -56,7 +56,7 @@ static void stat_collect(scrape_req *req, void *ctx) {
 
   // scan /proc/stat for metrics
 
-  f = fopen("/proc/stat", "r");
+  f = fopen(PATH("/proc/stat"), "r");
   if (!f)
     return;
 
diff --git a/test/Makefile b/test/Makefile
index 5df2563..df38c75 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 hwmon meminfo network
+COLLECTOR_TESTS := cpu diskstats filesystem hwmon meminfo network stat
 
 COLLECTOR_TEST_PROGS := $(foreach c,$(COLLECTOR_TESTS),$(c)_test)
 COLLECTOR_TEST_OBJS := $(foreach p,$(COLLECTOR_TEST_PROGS),$(p).o)
diff --git a/test/stat_test.c b/test/stat_test.c
new file mode 100644
index 0000000..4dbe3a7
--- /dev/null
+++ b/test/stat_test.c
@@ -0,0 +1,55 @@
+/*
+ * 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 stat_collector;
+
+TEST(stat_metrics) {
+  test_write_file(
+      env,
+      "proc/stat",
+      "cpu  63127434 327625 23779819 2507475383 43094144 0 6968769 0 0 0\n"
+      "cpu0 15869790 86128 5127633 626530909 11971750 0 3047872 0 0 0\n"
+      "cpu1 16077564 83099 5249139 627646482 11108332 0 2155429 0 0 0\n"
+      "intr 9977823731 9 0 0 0 0 0 0 0 1 0 0 0\n"
+      "ctxt 17392647926\n"
+      "btime 1538002179\n"
+      "processes 9325143\n"
+      "procs_running 1\n"
+      "procs_blocked 0\n"
+      "softirq 4290947107 27801943 1780530729 1705513 249559277 0 0 187155918 1259122343 22702 785048682\n");
+  scrape_req *req = mock_scrape_start(env);
+
+  stat_collector.collect(req, 0);
+
+  mock_scrape_expect(req, "node_intr_total", 0, 9977823731);
+  mock_scrape_expect(req, "node_context_switches_total", 0, 17392647926);
+  mock_scrape_expect(req, "node_boot_time_seconds", 0, 1538002179);
+  mock_scrape_expect(req, "node_forks_total", 0, 9325143);
+  mock_scrape_expect(req, "node_procs_running", 0, 1);
+  mock_scrape_expect(req, "node_procs_blocked", 0, 0);
+  mock_scrape_expect_no_more(req);
+  mock_scrape_free(req);
+}
+
+TEST_SUITE {
+  TEST_SUITE_START;
+  RUN_TEST(stat_metrics);
+  TEST_SUITE_END;
+}