From 80859a9f1879c753c2a12b3de990ad7c49579545 Mon Sep 17 00:00:00 2001 From: Benny Siegert Date: Thu, 13 Jun 2024 20:53:25 +0200 Subject: [PATCH] Do not panic as much in Linux collector tests (#3050) Running "go test" in the collector directory, without the fixtures available, results in multiple panics, including `SIGSEGV`. Most of these are due to incorrect error handling. This cleans them up. Signed-off-by: Benny Siegert --- collector/btrfs_linux_test.go | 5 ++++- collector/diskstats_linux_test.go | 2 +- collector/ethtool_linux_test.go | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/collector/btrfs_linux_test.go b/collector/btrfs_linux_test.go index fcd6f9f9..f9bf7214 100644 --- a/collector/btrfs_linux_test.go +++ b/collector/btrfs_linux_test.go @@ -92,7 +92,10 @@ func checkMetric(exp, got *btrfsMetric) bool { } func TestBtrfs(t *testing.T) { - fs, _ := btrfs.NewFS("fixtures/sys") + fs, err := btrfs.NewFS("fixtures/sys") + if err != nil { + t.Fatal(err) + } collector := &btrfsCollector{fs: fs} stats, err := collector.fs.Stats() diff --git a/collector/diskstats_linux_test.go b/collector/diskstats_linux_test.go index 8e969c3e..88d8c826 100644 --- a/collector/diskstats_linux_test.go +++ b/collector/diskstats_linux_test.go @@ -320,7 +320,7 @@ node_disk_written_bytes_total{device="vda"} 1.0938236928e+11 logger := log.NewLogfmtLogger(os.Stderr) collector, err := NewDiskstatsCollector(logger) if err != nil { - panic(err) + t.Fatal(err) } c, err := NewTestDiskStatsCollector(logger) if err != nil { diff --git a/collector/ethtool_linux_test.go b/collector/ethtool_linux_test.go index cf55c6b1..ae7df0dd 100644 --- a/collector/ethtool_linux_test.go +++ b/collector/ethtool_linux_test.go @@ -257,12 +257,12 @@ func (e *EthtoolFixture) LinkInfo(intf string) (ethtool.EthtoolCmd, error) { func NewEthtoolTestCollector(logger log.Logger) (Collector, error) { collector, err := makeEthtoolCollector(logger) - collector.ethtool = &EthtoolFixture{ - fixturePath: "fixtures/ethtool/", - } if err != nil { return nil, err } + collector.ethtool = &EthtoolFixture{ + fixturePath: "fixtures/ethtool/", + } return collector, nil } @@ -373,7 +373,7 @@ node_network_supported_speed_bytes{device="eth0",duplex="half",mode="10baseT"} 1 logger := log.NewLogfmtLogger(os.Stderr) collector, err := NewEthtoolTestCollector(logger) if err != nil { - panic(err) + t.Fatal(err) } c, err := NewTestEthtoolCollector(logger) if err != nil {