Path: blob/main/pkg/integrations/node_exporter/node_exporter_test.go
5295 views
//go:build !race && !windows12package node_exporter //nolint:golint34import (5"io"6"net/http"7"net/http/httptest"8"runtime"9"testing"1011"github.com/go-kit/log"12"github.com/go-kit/log/level"13"github.com/gorilla/mux"14"github.com/grafana/agent/pkg/util"15"github.com/prometheus/prometheus/model/textparse"16"github.com/stretchr/testify/assert"17"github.com/stretchr/testify/require"18"gopkg.in/alecthomas/kingpin.v2"19)2021// TestNodeExporter runs an integration test for node_exporter, doing the22// following:23//24// 1. Enabling all collectors (minus some that cause issues in cross-platform testing)25// 2. Creating the integration26// 3. Scrape the integration once27// 4. Parse the result of the scrape28//29// This ensures that the flag parsing is correct and that the handler is30// set up properly. We do not test the contents of the scrape, just that it31// was parsable by Prometheus.32func TestNodeExporter(t *testing.T) {33cfg := DefaultConfig3435// Enable all collectors except perf36cfg.SetCollectors = make([]string, 0, len(Collectors))37for c := range Collectors {38cfg.SetCollectors = append(cfg.SetCollectors, c)39}40cfg.DisableCollectors = []string{CollectorPerf, CollectorBuddyInfo}4142// Check that the flags convert and the integration initializes43logger := log.NewNopLogger()44integration, err := New(logger, &cfg)45require.NoError(t, err, "failed to setup node_exporter")4647r := mux.NewRouter()48handler, err := integration.MetricsHandler()49require.NoError(t, err)50r.Handle("/metrics", handler)5152// Invoke /metrics and parse the response53srv := httptest.NewServer(r)54defer srv.Close()5556res, err := http.Get(srv.URL + "/metrics")57require.NoError(t, err)5859body, err := io.ReadAll(res.Body)60require.NoError(t, err)6162p := textparse.NewPromParser(body)63for {64_, err := p.Next()65if err == io.EOF {66break67}68require.NoError(t, err)69}70}7172// TestFTestNodeExporter_IgnoredFlags ensures that flags don't get ignored for73// misspellings.74func TestNodeExporter_IgnoredFlags(t *testing.T) {75l := util.TestLogger(t)76cfg := DefaultConfig7778// Enable all collectors except perf79cfg.SetCollectors = make([]string, 0, len(Collectors))80for c := range Collectors {81cfg.SetCollectors = append(cfg.SetCollectors, c)82}83cfg.DisableCollectors = []string{CollectorPerf}8485_, ignored := MapConfigToNodeExporterFlags(&cfg)86var expect []string8788switch runtime.GOOS {89case "darwin":90expect = []string{91"collector.cpu.info",92"collector.cpu.guest",93"collector.cpu.info.flags-include",94"collector.cpu.info.bugs-include",95"collector.filesystem.mount-timeout",96}97}9899if !assert.ElementsMatch(t, expect, ignored) {100level.Debug(l).Log("msg", "printing available flags")101for _, flag := range kingpin.CommandLine.Model().Flags {102level.Debug(l).Log("flag", flag.Name, "hidden", flag.Hidden)103}104}105}106107// TestFlags makes sure that boolean flags and some known non-boolean flags108// work as expected109func TestFlags(t *testing.T) {110var f flags111f.add("--path.rootfs", "/")112require.Equal(t, []string{"--path.rootfs", "/"}, f.accepted)113114// Set up booleans to use as pointers115var (116truth = true117118// You know, the opposite of truth?119falth = false120)121122f = flags{}123f.addBools(map[*bool]string{&truth: "collector.textfile"})124require.Equal(t, []string{"--collector.textfile"}, f.accepted)125126f = flags{}127f.addBools(map[*bool]string{&falth: "collector.textfile"})128require.Equal(t, []string{"--no-collector.textfile"}, f.accepted)129}130131132