2019-03-25 11:47:57 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-04 22:46:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-27 18:59:39 +00:00
|
|
|
"io"
|
2017-03-04 22:46:20 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-03-14 02:55:19 +00:00
|
|
|
"path/filepath"
|
2017-03-04 22:46:20 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/procfs"
|
|
|
|
)
|
|
|
|
|
2017-08-19 09:37:57 +00:00
|
|
|
var (
|
|
|
|
binary = filepath.Join(os.Getenv("GOPATH"), "bin/node_exporter")
|
|
|
|
)
|
2017-08-21 16:45:42 +00:00
|
|
|
|
2017-03-14 02:55:19 +00:00
|
|
|
const (
|
|
|
|
address = "localhost:19100"
|
|
|
|
)
|
2017-03-04 22:46:20 +00:00
|
|
|
|
2017-03-14 02:55:19 +00:00
|
|
|
func TestFileDescriptorLeak(t *testing.T) {
|
2017-03-04 22:46:20 +00:00
|
|
|
if _, err := os.Stat(binary); err != nil {
|
|
|
|
t.Skipf("node_exporter binary not available, try to run `make build` first: %s", err)
|
|
|
|
}
|
2019-06-12 18:47:16 +00:00
|
|
|
fs, err := procfs.NewDefaultFS()
|
|
|
|
if err != nil {
|
2017-03-04 22:46:20 +00:00
|
|
|
t.Skipf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err)
|
|
|
|
}
|
2019-06-12 18:47:16 +00:00
|
|
|
if _, err := fs.Stat(); err != nil {
|
|
|
|
t.Errorf("unable to read process stats: %s", err)
|
|
|
|
}
|
2017-03-04 22:46:20 +00:00
|
|
|
|
2017-08-12 13:07:24 +00:00
|
|
|
exporter := exec.Command(binary, "--web.listen-address", address)
|
2017-03-14 02:55:19 +00:00
|
|
|
test := func(pid int) error {
|
|
|
|
if err := queryExporter(address); err != nil {
|
|
|
|
return err
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
|
|
|
proc, err := procfs.NewProc(pid)
|
|
|
|
if err != nil {
|
2017-03-14 02:55:19 +00:00
|
|
|
return err
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
|
|
|
fdsBefore, err := proc.FileDescriptors()
|
|
|
|
if err != nil {
|
2017-03-14 02:55:19 +00:00
|
|
|
return err
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
2017-03-14 02:55:19 +00:00
|
|
|
if err := queryExporter(address); err != nil {
|
|
|
|
return err
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fdsAfter, err := proc.FileDescriptors()
|
|
|
|
if err != nil {
|
2017-03-14 02:55:19 +00:00
|
|
|
return err
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
|
|
|
if want, have := len(fdsBefore), len(fdsAfter); want != have {
|
2017-03-14 02:55:19 +00:00
|
|
|
return fmt.Errorf("want %d open file descriptors after metrics scrape, have %d", want, have)
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
2017-03-14 02:55:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-04 22:46:20 +00:00
|
|
|
|
2017-08-19 09:37:57 +00:00
|
|
|
if err := runCommandAndTests(exporter, address, test); err != nil {
|
2017-03-14 02:55:19 +00:00
|
|
|
t.Error(err)
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 02:55:19 +00: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)
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:59:39 +00:00
|
|
|
dir, err := os.MkdirTemp("", "node-exporter")
|
2017-03-14 02:55:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
content := []byte("dummy_metric 1\n")
|
2022-07-27 18:59:39 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "a.prom"), content, 0600); err != nil {
|
2017-03-14 02:55:19 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-07-27 18:59:39 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "b.prom"), content, 0600); err != nil {
|
2017-03-14 02:55:19 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-12 13:07:24 +00:00
|
|
|
exporter := exec.Command(binary, "--web.listen-address", address, "--collector.textfile.directory", dir)
|
2017-03-14 02:55:19 +00:00
|
|
|
test := func(_ int) error {
|
|
|
|
return queryExporter(address)
|
|
|
|
}
|
|
|
|
|
2017-08-19 09:37:57 +00:00
|
|
|
if err := runCommandAndTests(exporter, address, test); err != nil {
|
2017-03-14 02:55:19 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryExporter(address string) error {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://%s/metrics", address))
|
2017-03-04 22:46:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-27 18:59:39 +00:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2017-07-05 07:51:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-04 22:46:20 +00:00
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-14 02:55:19 +00:00
|
|
|
if want, have := http.StatusOK, resp.StatusCode; want != have {
|
2017-07-05 07:51:27 +00:00
|
|
|
return fmt.Errorf("want /metrics status code %d, have %d. Body:\n%s", want, have, b)
|
2017-03-04 22:46:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-14 02:55:19 +00:00
|
|
|
|
2017-08-19 09:37:57 +00:00
|
|
|
func runCommandAndTests(cmd *exec.Cmd, address string, fn func(pid int) error) error {
|
2017-07-08 18:15:40 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return fmt.Errorf("failed to start command: %s", err)
|
|
|
|
}
|
2017-08-19 09:37:57 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if err := queryExporter(address); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2017-08-21 16:45:42 +00:00
|
|
|
if cmd.Process == nil || i == 9 {
|
2017-08-19 09:37:57 +00:00
|
|
|
return fmt.Errorf("can't start command")
|
2017-03-14 02:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 09:37:57 +00:00
|
|
|
errc := make(chan error)
|
2017-03-14 02:55:19 +00:00
|
|
|
go func(pid int) {
|
|
|
|
errc <- fn(pid)
|
|
|
|
}(cmd.Process.Pid)
|
|
|
|
|
2017-10-18 05:38:48 +00:00
|
|
|
err := <-errc
|
|
|
|
if cmd.Process != nil {
|
|
|
|
cmd.Process.Kill()
|
2017-03-14 02:55:19 +00:00
|
|
|
}
|
2017-10-18 05:38:48 +00:00
|
|
|
return err
|
2017-03-14 02:55:19 +00:00
|
|
|
}
|