2017-03-04 14:46:20 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-03-13 19:55:19 -07:00
|
|
|
"io/ioutil"
|
2017-03-04 14:46:20 -08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-03-13 19:55:19 -07:00
|
|
|
"path/filepath"
|
2017-03-04 14:46:20 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/procfs"
|
|
|
|
)
|
|
|
|
|
2017-03-13 19:55:19 -07:00
|
|
|
const (
|
|
|
|
binary = "./node_exporter"
|
|
|
|
address = "localhost:19100"
|
|
|
|
)
|
2017-03-04 14:46:20 -08:00
|
|
|
|
2017-03-13 19:55:19 -07:00
|
|
|
func TestFileDescriptorLeak(t *testing.T) {
|
2017-03-04 14:46:20 -08:00
|
|
|
if _, err := os.Stat(binary); err != nil {
|
|
|
|
t.Skipf("node_exporter binary not available, try to run `make build` first: %s", err)
|
|
|
|
}
|
|
|
|
if _, err := procfs.NewStat(); err != nil {
|
|
|
|
t.Skipf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exporter := exec.Command(binary, "-web.listen-address", address)
|
2017-03-13 19:55:19 -07:00
|
|
|
test := func(pid int) error {
|
|
|
|
if err := queryExporter(address); err != nil {
|
|
|
|
return err
|
2017-03-04 14:46:20 -08:00
|
|
|
}
|
|
|
|
proc, err := procfs.NewProc(pid)
|
|
|
|
if err != nil {
|
2017-03-13 19:55:19 -07:00
|
|
|
return err
|
2017-03-04 14:46:20 -08:00
|
|
|
}
|
|
|
|
fdsBefore, err := proc.FileDescriptors()
|
|
|
|
if err != nil {
|
2017-03-13 19:55:19 -07:00
|
|
|
return err
|
2017-03-04 14:46:20 -08:00
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
2017-03-13 19:55:19 -07:00
|
|
|
if err := queryExporter(address); err != nil {
|
|
|
|
return err
|
2017-03-04 14:46:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fdsAfter, err := proc.FileDescriptors()
|
|
|
|
if err != nil {
|
2017-03-13 19:55:19 -07:00
|
|
|
return err
|
2017-03-04 14:46:20 -08:00
|
|
|
}
|
|
|
|
if want, have := len(fdsBefore), len(fdsAfter); want != have {
|
2017-03-13 19:55:19 -07:00
|
|
|
return fmt.Errorf("want %d open file descriptors after metrics scrape, have %d", want, have)
|
2017-03-04 14:46:20 -08:00
|
|
|
}
|
2017-03-13 19:55:19 -07:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-04 14:46:20 -08:00
|
|
|
|
2017-03-13 19:55:19 -07:00
|
|
|
if err := runCommandAndTests(exporter, test); err != nil {
|
|
|
|
t.Error(err)
|
2017-03-04 14:46:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 19:55:19 -07:00
|
|
|
func TestHandlingOfDuplicatedMetrics(t *testing.T) {
|
|
|
|
if _, err := os.Stat(binary); err != nil {
|
|
|
|
t.Skipf("node_exporter binary not available, try to run `make build` first: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "node-exporter")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
content := []byte("dummy_metric 1\n")
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(dir, "a.prom"), content, 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(dir, "b.prom"), content, 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exporter := exec.Command(binary, "-web.listen-address", address, "-collector.textfile.directory", dir)
|
|
|
|
test := func(_ int) error {
|
|
|
|
return queryExporter(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runCommandAndTests(exporter, test); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryExporter(address string) error {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://%s/metrics", address))
|
2017-03-04 14:46:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-13 19:55:19 -07:00
|
|
|
if want, have := http.StatusOK, resp.StatusCode; want != have {
|
2017-03-04 14:46:20 -08:00
|
|
|
return fmt.Errorf("want /metrics status code %d, have %d", want, have)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-13 19:55:19 -07:00
|
|
|
|
|
|
|
func runCommandAndTests(cmd *exec.Cmd, fn func(pid int) error) error {
|
|
|
|
errc := make(chan error)
|
|
|
|
go func() {
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
errc <- fmt.Errorf("execution of command failed: %s", err)
|
|
|
|
} else {
|
|
|
|
errc <- nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Allow the process to start before running any tests.
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
return err
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(pid int) {
|
|
|
|
errc <- fn(pid)
|
|
|
|
}(cmd.Process.Pid)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if cmd.Process != nil {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|