diff --git a/collector/textfile_test.go b/collector/textfile_test.go index 0945d9c2..91ab4642 100644 --- a/collector/textfile_test.go +++ b/collector/textfile_test.go @@ -1,19 +1,25 @@ package collector import ( - "io/ioutil" + "fmt" + "io" + "os" "strings" "testing" "github.com/dimchansky/utfbom" + "github.com/go-kit/log" + "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" ) +var baseDir = "../tools/textfile-test" + func TestCRFilter(t *testing.T) { sr := strings.NewReader("line 1\r\nline 2") cr := carriageReturnFilteringReader{r: sr} - b, err := ioutil.ReadAll(cr) + b, err := io.ReadAll(cr) if err != nil { t.Error(err) } @@ -153,3 +159,67 @@ func TestDuplicateMetricEntry(t *testing.T) { t.Errorf("Unexpected duplicate found in differentValues") } } + +func TestMultipleDirectories(t *testing.T) { + testDir := baseDir + "/multiple-dirs" + testDirs := fmt.Sprintf("%[1]s/dir1,%[1]s/dir2,%[1]s/dir3", testDir) + collector := &textFileCollector{ + logger: log.NewLogfmtLogger(os.Stdout), + directories: testDirs, + } + scrapeContext, err := PrepareScrapeContext([]string{"textfile_test"}) + if err != nil { + t.Errorf("Unexpected error %s", err) + } + metrics := make(chan prometheus.Metric) + got := "" + go func() { + for { + var metric dto.Metric + val := <-metrics + val.Write(&metric) + got += metric.String() + } + }() + err = collector.Collect(scrapeContext, metrics) + if err != nil { + t.Errorf("Unexpected error %s", err) + } + for _, f := range []string{"dir1", "dir2", "dir3", "dir3sub"} { + if !strings.Contains(got, f) { + t.Errorf("Unexpected output %s: %q", f, got) + } + } +} + +func TestDuplicateFileName(t *testing.T) { + testDir := baseDir + "/duplicate-filename" + collector := &textFileCollector{ + logger: log.NewLogfmtLogger(os.Stdout), + directories: testDir, + } + scrapeContext, err := PrepareScrapeContext([]string{"textfile_test"}) + if err != nil { + t.Errorf("Unexpected error %s", err) + } + metrics := make(chan prometheus.Metric) + got := "" + go func() { + for { + var metric dto.Metric + val := <-metrics + val.Write(&metric) + got += metric.String() + } + }() + err = collector.Collect(scrapeContext, metrics) + if err != nil { + t.Errorf("Unexpected error %s", err) + } + if !strings.Contains(got, "file") { + t.Errorf("Unexpected output %q", got) + } + if strings.Contains(got, "sub_file") { + t.Errorf("Unexpected output %q", got) + } +} diff --git a/tools/textfile-test/duplicate-filename/file.prom b/tools/textfile-test/duplicate-filename/file.prom new file mode 100644 index 00000000..c18aad00 --- /dev/null +++ b/tools/textfile-test/duplicate-filename/file.prom @@ -0,0 +1,3 @@ +# HELP windows_test Some Test +# TYPE windows_test gauge +windows_test{flag="file"} 1 diff --git a/tools/textfile-test/duplicate-filename/sub/file.prom b/tools/textfile-test/duplicate-filename/sub/file.prom new file mode 100644 index 00000000..35839e1d --- /dev/null +++ b/tools/textfile-test/duplicate-filename/sub/file.prom @@ -0,0 +1,3 @@ +# HELP windows_test Some Test +# TYPE windows_test gauge +windows_test{flag="sub_file"} 2 diff --git a/tools/textfile-test/multiple-dirs/dir1/dir1.prom b/tools/textfile-test/multiple-dirs/dir1/dir1.prom new file mode 100644 index 00000000..5ee71d79 --- /dev/null +++ b/tools/textfile-test/multiple-dirs/dir1/dir1.prom @@ -0,0 +1,3 @@ +# HELP windows_test Some Test +# TYPE windows_test gauge +windows_test{flag="dir1"} 1 diff --git a/tools/textfile-test/multiple-dirs/dir2/dir2.prom b/tools/textfile-test/multiple-dirs/dir2/dir2.prom new file mode 100644 index 00000000..c30afdfb --- /dev/null +++ b/tools/textfile-test/multiple-dirs/dir2/dir2.prom @@ -0,0 +1,3 @@ +# HELP windows_test Some Test +# TYPE windows_test gauge +windows_test{flag="dir2"} 2 diff --git a/tools/textfile-test/multiple-dirs/dir3/dir3.prom b/tools/textfile-test/multiple-dirs/dir3/dir3.prom new file mode 100644 index 00000000..ca287f04 --- /dev/null +++ b/tools/textfile-test/multiple-dirs/dir3/dir3.prom @@ -0,0 +1,3 @@ +# HELP windows_test Some Test +# TYPE windows_test gauge +windows_test{flag="dir3"} 3 diff --git a/tools/textfile-test/multiple-dirs/dir3/dir3sub/dir3sub.prom b/tools/textfile-test/multiple-dirs/dir3/dir3sub/dir3sub.prom new file mode 100644 index 00000000..40e972e1 --- /dev/null +++ b/tools/textfile-test/multiple-dirs/dir3/dir3sub/dir3sub.prom @@ -0,0 +1,3 @@ +# HELP windows_test Some Test +# TYPE windows_test gauge +windows_test{flag="dir3sub"} 3