about summary refs log tree commit diff
path: root/filesystem.c
diff options
context:
space:
mode:
authorHeikki Kallasjoki <fis@zem.fi>2018-12-12 15:13:50 +0000
committerHeikki Kallasjoki <fis+github@zem.fi>2018-12-12 19:10:51 +0000
commit5ce4474f7e6cb2256fee2619b4695772768b65d3 (patch)
tree7e22a7930a4f330d898c530ff5d73e723105b020 /filesystem.c
parent6adc487e0887ad8dfb5ce7024b448fafd0bed943 (diff)
downloadnano-exporter-5ce4474f7e6cb2256fee2619b4695772768b65d3.tar.gz
nano-exporter-5ce4474f7e6cb2256fee2619b4695772768b65d3.tar.xz
nano-exporter-5ce4474f7e6cb2256fee2619b4695772768b65d3.zip
Add basic test for the filesystem collector.
Diffstat (limited to 'filesystem.c')
-rw-r--r--filesystem.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/filesystem.c b/filesystem.c
index 19b2e4e..aa16421 100644
--- a/filesystem.c
+++ b/filesystem.c
@@ -42,6 +42,7 @@ struct filesystem_context {
   struct slist *exclude_mount;
   struct slist *include_type;
   struct slist *exclude_type;
+  int (*statvfs_func)(const char *path, struct statvfs *buf);
 };
 
 static void *filesystem_init(int argc, char *argv[]) {
@@ -73,6 +74,7 @@ static void *filesystem_init(int argc, char *argv[]) {
     }
   }
 
+  ctx->statvfs_func = statvfs;
   return ctx;
 }
 
@@ -98,7 +100,7 @@ static void filesystem_collect(scrape_req *req, void *ctx_ptr) {
 
   // loop over /proc/mounts to get visible mounts
 
-  f = fopen("/proc/mounts", "r");
+  f = fopen(PATH("/proc/mounts"), "r");
   if (!f)
     return;
 
@@ -142,7 +144,7 @@ static void filesystem_collect(scrape_req *req, void *ctx_ptr) {
 
     // report metrics from statfs
 
-    if (statvfs(*mount, &fs) != 0)
+    if (ctx->statvfs_func(*mount, &fs) != 0)
       continue;
 
     double bs = fs.f_frsize;
@@ -156,3 +158,9 @@ static void filesystem_collect(scrape_req *req, void *ctx_ptr) {
 
   fclose(f);
 }
+
+// exposed only for testing
+
+void filesystem_test_override_statvfs(void *ctx, int (*statvfs_func)(const char *path, struct statvfs *buf)) {
+  ((struct filesystem_context *) ctx)->statvfs_func = statvfs_func;
+}