mirror of
https://github.com/prometheus-community/windows_exporter
synced 2024-12-24 23:42:08 +00:00
(#1236) textfile: more tests added
Signed-off-by: Dinifarb <andreas.vogt89@bluewin.ch>
This commit is contained in:
parent
f5b9ba35d0
commit
f9361bb684
@ -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)
|
||||
}
|
||||
}
|
||||
|
3
tools/textfile-test/duplicate-filename/file.prom
Normal file
3
tools/textfile-test/duplicate-filename/file.prom
Normal file
@ -0,0 +1,3 @@
|
||||
# HELP windows_test Some Test
|
||||
# TYPE windows_test gauge
|
||||
windows_test{flag="file"} 1
|
3
tools/textfile-test/duplicate-filename/sub/file.prom
Normal file
3
tools/textfile-test/duplicate-filename/sub/file.prom
Normal file
@ -0,0 +1,3 @@
|
||||
# HELP windows_test Some Test
|
||||
# TYPE windows_test gauge
|
||||
windows_test{flag="sub_file"} 2
|
3
tools/textfile-test/multiple-dirs/dir1/dir1.prom
Normal file
3
tools/textfile-test/multiple-dirs/dir1/dir1.prom
Normal file
@ -0,0 +1,3 @@
|
||||
# HELP windows_test Some Test
|
||||
# TYPE windows_test gauge
|
||||
windows_test{flag="dir1"} 1
|
3
tools/textfile-test/multiple-dirs/dir2/dir2.prom
Normal file
3
tools/textfile-test/multiple-dirs/dir2/dir2.prom
Normal file
@ -0,0 +1,3 @@
|
||||
# HELP windows_test Some Test
|
||||
# TYPE windows_test gauge
|
||||
windows_test{flag="dir2"} 2
|
3
tools/textfile-test/multiple-dirs/dir3/dir3.prom
Normal file
3
tools/textfile-test/multiple-dirs/dir3/dir3.prom
Normal file
@ -0,0 +1,3 @@
|
||||
# HELP windows_test Some Test
|
||||
# TYPE windows_test gauge
|
||||
windows_test{flag="dir3"} 3
|
@ -0,0 +1,3 @@
|
||||
# HELP windows_test Some Test
|
||||
# TYPE windows_test gauge
|
||||
windows_test{flag="dir3sub"} 3
|
Loading…
Reference in New Issue
Block a user