Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/node_exporter/node_exporter_test.go
5295 views
1
//go:build !race && !windows
2
3
package node_exporter //nolint:golint
4
5
import (
6
"io"
7
"net/http"
8
"net/http/httptest"
9
"runtime"
10
"testing"
11
12
"github.com/go-kit/log"
13
"github.com/go-kit/log/level"
14
"github.com/gorilla/mux"
15
"github.com/grafana/agent/pkg/util"
16
"github.com/prometheus/prometheus/model/textparse"
17
"github.com/stretchr/testify/assert"
18
"github.com/stretchr/testify/require"
19
"gopkg.in/alecthomas/kingpin.v2"
20
)
21
22
// TestNodeExporter runs an integration test for node_exporter, doing the
23
// following:
24
//
25
// 1. Enabling all collectors (minus some that cause issues in cross-platform testing)
26
// 2. Creating the integration
27
// 3. Scrape the integration once
28
// 4. Parse the result of the scrape
29
//
30
// This ensures that the flag parsing is correct and that the handler is
31
// set up properly. We do not test the contents of the scrape, just that it
32
// was parsable by Prometheus.
33
func TestNodeExporter(t *testing.T) {
34
cfg := DefaultConfig
35
36
// Enable all collectors except perf
37
cfg.SetCollectors = make([]string, 0, len(Collectors))
38
for c := range Collectors {
39
cfg.SetCollectors = append(cfg.SetCollectors, c)
40
}
41
cfg.DisableCollectors = []string{CollectorPerf, CollectorBuddyInfo}
42
43
// Check that the flags convert and the integration initializes
44
logger := log.NewNopLogger()
45
integration, err := New(logger, &cfg)
46
require.NoError(t, err, "failed to setup node_exporter")
47
48
r := mux.NewRouter()
49
handler, err := integration.MetricsHandler()
50
require.NoError(t, err)
51
r.Handle("/metrics", handler)
52
53
// Invoke /metrics and parse the response
54
srv := httptest.NewServer(r)
55
defer srv.Close()
56
57
res, err := http.Get(srv.URL + "/metrics")
58
require.NoError(t, err)
59
60
body, err := io.ReadAll(res.Body)
61
require.NoError(t, err)
62
63
p := textparse.NewPromParser(body)
64
for {
65
_, err := p.Next()
66
if err == io.EOF {
67
break
68
}
69
require.NoError(t, err)
70
}
71
}
72
73
// TestFTestNodeExporter_IgnoredFlags ensures that flags don't get ignored for
74
// misspellings.
75
func TestNodeExporter_IgnoredFlags(t *testing.T) {
76
l := util.TestLogger(t)
77
cfg := DefaultConfig
78
79
// Enable all collectors except perf
80
cfg.SetCollectors = make([]string, 0, len(Collectors))
81
for c := range Collectors {
82
cfg.SetCollectors = append(cfg.SetCollectors, c)
83
}
84
cfg.DisableCollectors = []string{CollectorPerf}
85
86
_, ignored := MapConfigToNodeExporterFlags(&cfg)
87
var expect []string
88
89
switch runtime.GOOS {
90
case "darwin":
91
expect = []string{
92
"collector.cpu.info",
93
"collector.cpu.guest",
94
"collector.cpu.info.flags-include",
95
"collector.cpu.info.bugs-include",
96
"collector.filesystem.mount-timeout",
97
}
98
}
99
100
if !assert.ElementsMatch(t, expect, ignored) {
101
level.Debug(l).Log("msg", "printing available flags")
102
for _, flag := range kingpin.CommandLine.Model().Flags {
103
level.Debug(l).Log("flag", flag.Name, "hidden", flag.Hidden)
104
}
105
}
106
}
107
108
// TestFlags makes sure that boolean flags and some known non-boolean flags
109
// work as expected
110
func TestFlags(t *testing.T) {
111
var f flags
112
f.add("--path.rootfs", "/")
113
require.Equal(t, []string{"--path.rootfs", "/"}, f.accepted)
114
115
// Set up booleans to use as pointers
116
var (
117
truth = true
118
119
// You know, the opposite of truth?
120
falth = false
121
)
122
123
f = flags{}
124
f.addBools(map[*bool]string{&truth: "collector.textfile"})
125
require.Equal(t, []string{"--collector.textfile"}, f.accepted)
126
127
f = flags{}
128
f.addBools(map[*bool]string{&falth: "collector.textfile"})
129
require.Equal(t, []string{"--no-collector.textfile"}, f.accepted)
130
}
131
132