Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 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 collector

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
5 changes: 5 additions & 0 deletions collector/diskstats_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"log/slog"
"strings"
"sync"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -329,7 +330,10 @@ node_disk_written_bytes_total{device="vda"} 1.0938236928e+11
reg.MustRegister(c)

sink := make(chan prometheus.Metric)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = collector.Update(sink)
if err != nil {
panic(fmt.Errorf("failed to update collector: %s", err))
Expand All @@ -341,4 +345,5 @@ node_disk_written_bytes_total{device="vda"} 1.0938236928e+11
if err != nil {
t.Fatal(err)
}
wg.Wait()
}
5 changes: 5 additions & 0 deletions collector/ethtool_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"testing"

Expand Down Expand Up @@ -389,7 +390,10 @@ node_network_supported_speed_bytes{device="eth0",duplex="half",mode="10baseT"} 1
reg.MustRegister(c)

sink := make(chan prometheus.Metric)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = collector.Update(sink)
if err != nil {
panic(fmt.Errorf("failed to update collector: %s", err))
Expand All @@ -401,4 +405,5 @@ node_network_supported_speed_bytes{device="eth0",duplex="half",mode="10baseT"} 1
if err != nil {
t.Fatal(err)
}
wg.Wait()
}
11 changes: 11 additions & 0 deletions collector/ipvs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http/httptest"
"os"
"strings"
"sync"
"testing"

"github.com/alecthomas/kingpin/v2"
Expand Down Expand Up @@ -128,18 +129,28 @@ func TestIPVSCollector(t *testing.T) {
}

sink := make(chan prometheus.Metric)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = collector.Update(sink)
if err != nil {
panic(fmt.Sprintf("failed to update collector: %v", err))
}
close(sink)
}()
for _, expected := range test.expects {
got := (<-sink).Desc().String()
if expected != got {
t.Fatalf("Expected '%s' but got '%s'", expected, got)
}
}
// Drain remaining metrics so the goroutine can finish.
go func() {
for range sink {
}
}()
wg.Wait()
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions collector/mdadm_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"log/slog"
"os"
"strings"
"sync"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -278,7 +279,10 @@ func TestMdadmStats(t *testing.T) {
reg.MustRegister(c)

sink := make(chan prometheus.Metric)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := collector.Update(sink)
if err != nil {
panic(err)
Expand All @@ -290,4 +294,5 @@ func TestMdadmStats(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wg.Wait()
}
5 changes: 5 additions & 0 deletions collector/watchdog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"log/slog"
"strings"
"sync"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -76,7 +77,10 @@ func TestWatchdogStats(t *testing.T) {
reg.MustRegister(&testWatchdogCollector{wc: c})

sink := make(chan prometheus.Metric)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = c.Update(sink)
if err != nil {
panic(fmt.Errorf("failed to update collector: %s", err))
Expand All @@ -88,4 +92,5 @@ func TestWatchdogStats(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wg.Wait()
}
10 changes: 0 additions & 10 deletions collector/xfrm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package collector

import (
"fmt"
"io"
"log/slog"
"strings"
Expand Down Expand Up @@ -134,15 +133,6 @@ func TestXfrmStats(t *testing.T) {
reg := prometheus.NewRegistry()
reg.MustRegister(&testXfrmCollector{xc: c})

sink := make(chan prometheus.Metric)
go func() {
err = c.Update(sink)
if err != nil {
panic(fmt.Errorf("failed to update collector: %s", err))
}
close(sink)
}()

err = testutil.GatherAndCompare(reg, strings.NewReader(testcase))
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/prometheus/exporter-toolkit v0.16.0
github.com/prometheus/procfs v0.20.1
github.com/safchain/ethtool v0.7.0
go.uber.org/goleak v1.3.0
golang.org/x/sys v0.42.0
howett.net/plist v1.0.1
)
Expand Down
Loading