forked from tomasro27/node_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_exporter_test.go
154 lines (137 loc) · 3.8 KB
/
node_exporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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.
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/prometheus/procfs"
)
var (
binary = filepath.Join(os.Getenv("GOPATH"), "bin/node_exporter")
)
const (
address = "localhost:19100"
)
func TestFileDescriptorLeak(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)
}
fs, err := procfs.NewDefaultFS()
if err != nil {
t.Skipf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err)
}
if _, err := fs.Stat(); err != nil {
t.Errorf("unable to read process stats: %s", err)
}
exporter := exec.Command(binary, "--web.listen-address", address)
test := func(pid int) error {
if err := queryExporter(address); err != nil {
return err
}
proc, err := procfs.NewProc(pid)
if err != nil {
return err
}
fdsBefore, err := proc.FileDescriptors()
if err != nil {
return err
}
for i := 0; i < 5; i++ {
if err := queryExporter(address); err != nil {
return err
}
}
fdsAfter, err := proc.FileDescriptors()
if err != nil {
return err
}
if want, have := len(fdsBefore), len(fdsAfter); want != have {
return fmt.Errorf("want %d open file descriptors after metrics scrape, have %d", want, have)
}
return nil
}
if err := runCommandAndTests(exporter, address, test); err != nil {
t.Error(err)
}
}
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 := os.MkdirTemp("", "node-exporter")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
content := []byte("dummy_metric 1\n")
if err := os.WriteFile(filepath.Join(dir, "a.prom"), content, 0600); err != nil {
t.Fatal(err)
}
if err := os.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, address, test); err != nil {
t.Error(err)
}
}
func queryExporter(address string) error {
resp, err := http.Get(fmt.Sprintf("http://%s/metrics", address))
if err != nil {
return err
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if err := resp.Body.Close(); err != nil {
return err
}
if want, have := http.StatusOK, resp.StatusCode; want != have {
return fmt.Errorf("want /metrics status code %d, have %d. Body:\n%s", want, have, b)
}
return nil
}
func runCommandAndTests(cmd *exec.Cmd, address string, fn func(pid int) error) error {
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start command: %s", err)
}
time.Sleep(50 * time.Millisecond)
for i := 0; i < 10; i++ {
if err := queryExporter(address); err == nil {
break
}
time.Sleep(500 * time.Millisecond)
if cmd.Process == nil || i == 9 {
return fmt.Errorf("can't start command")
}
}
errc := make(chan error)
go func(pid int) {
errc <- fn(pid)
}(cmd.Process.Pid)
err := <-errc
if cmd.Process != nil {
cmd.Process.Kill()
}
return err
}