(#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
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/dimchansky/utfbom"
|
"github.com/dimchansky/utfbom"
|
||||||
|
"github.com/go-kit/log"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var baseDir = "../tools/textfile-test"
|
||||||
|
|
||||||
func TestCRFilter(t *testing.T) {
|
func TestCRFilter(t *testing.T) {
|
||||||
sr := strings.NewReader("line 1\r\nline 2")
|
sr := strings.NewReader("line 1\r\nline 2")
|
||||||
cr := carriageReturnFilteringReader{r: sr}
|
cr := carriageReturnFilteringReader{r: sr}
|
||||||
b, err := ioutil.ReadAll(cr)
|
b, err := io.ReadAll(cr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -153,3 +159,67 @@ func TestDuplicateMetricEntry(t *testing.T) {
|
||||||
t.Errorf("Unexpected duplicate found in differentValues")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
# HELP windows_test Some Test
|
||||||
|
# TYPE windows_test gauge
|
||||||
|
windows_test{flag="file"} 1
|
|
@ -0,0 +1,3 @@
|
||||||
|
# HELP windows_test Some Test
|
||||||
|
# TYPE windows_test gauge
|
||||||
|
windows_test{flag="sub_file"} 2
|
|
@ -0,0 +1,3 @@
|
||||||
|
# HELP windows_test Some Test
|
||||||
|
# TYPE windows_test gauge
|
||||||
|
windows_test{flag="dir1"} 1
|
|
@ -0,0 +1,3 @@
|
||||||
|
# HELP windows_test Some Test
|
||||||
|
# TYPE windows_test gauge
|
||||||
|
windows_test{flag="dir2"} 2
|
|
@ -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