From d2f3ea8019580dc716abdf9251c17920443a3a61 Mon Sep 17 00:00:00 2001 From: whitefox Date: Mon, 9 Dec 2024 13:17:19 +0200 Subject: [PATCH 1/2] add test for SELEventsCollector --- collector_sel_events_test.go | 16 ++++++++++++++++ go.mod | 4 ++++ 2 files changed, 20 insertions(+) create mode 100644 collector_sel_events_test.go diff --git a/collector_sel_events_test.go b/collector_sel_events_test.go new file mode 100644 index 0000000..ea22609 --- /dev/null +++ b/collector_sel_events_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "github.com/prometheus-community/ipmi_exporter/freeipmi" + "github.com/prometheus/client_golang/prometheus" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCollectSELEventsCollector(t *testing.T) { + c := SELEventsCollector{} + ch := make(chan prometheus.Metric) + i, err := c.Collect(freeipmi.Result{}, ch, ipmiTarget{}) + assert.Nil(t, err) + assert.Equal(t, 1, i) +} diff --git a/go.mod b/go.mod index f4c905a..c34f4bf 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/prometheus/client_golang v1.20.5 github.com/prometheus/common v0.60.1 github.com/prometheus/exporter-toolkit v0.13.1 + github.com/stretchr/testify v1.9.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -15,12 +16,14 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/mdlayher/socket v0.4.1 // indirect github.com/mdlayher/vsock v1.2.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/xhit/go-str2duration/v2 v2.1.0 // indirect @@ -31,4 +34,5 @@ require ( golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) From e04f48b48986140b5d0fcf1a7185415009cfc157 Mon Sep 17 00:00:00 2001 From: whitefox Date: Mon, 9 Dec 2024 18:01:49 +0200 Subject: [PATCH 2/2] add testCases --- collector_sel_events_test.go | 65 +++++++++++++++++++++++++++++++++--- go.mod | 2 +- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/collector_sel_events_test.go b/collector_sel_events_test.go index ea22609..8f31d46 100644 --- a/collector_sel_events_test.go +++ b/collector_sel_events_test.go @@ -1,16 +1,71 @@ package main import ( + "bytes" + "fmt" "github.com/prometheus-community/ipmi_exporter/freeipmi" "github.com/prometheus/client_golang/prometheus" + dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/assert" + "log" + "log/slog" "testing" ) +type withExemplarsMetric struct { + prometheus.Metric + + exemplars []*dto.Exemplar +} + func TestCollectSELEventsCollector(t *testing.T) { - c := SELEventsCollector{} - ch := make(chan prometheus.Metric) - i, err := c.Collect(freeipmi.Result{}, ch, ipmiTarget{}) - assert.Nil(t, err) - assert.Equal(t, 1, i) + testCases := []struct { + arg string + wantState string + wantGauge float64 + }{ + { + arg: "1,Mar-01-2024,17:00:11,SEL,Event Logging Disabled,Nominal,Log Area Reset/Cleared\n", + wantState: "Nominal", + wantGauge: 1, + }, + { + arg: "2,Aug-05-2024,14:31:52,System Board Intrusion,Physical Security,Critical,General Chassis Intrusion ; Intrusion while system Off\n", + wantState: "Critical", + wantGauge: 1, + }, + } + + var logBuf bytes.Buffer + log.SetOutput(&logBuf) + slogger := slog.New(slog.NewTextHandler(&logBuf, nil)) + + for n, tc := range testCases { + t.Run(fmt.Sprintf("test%d", n), func(t *testing.T) { + ch := make(chan prometheus.Metric) + defer close(ch) + + c := SELEventsCollector{} + + go func() { + i, err := c.Collect( + freeipmi.Execute("/bin/echo", []string{tc.arg}, "", "", slogger), + ch, + ipmiTarget{}, + ) + assert.Nil(t, err) + assert.Equal(t, 1, i) + }() + + metric := <-ch + dm := dto.Metric{} + err := metric.Write(&dm) + + assert.Nil(t, err) + assert.Equal(t, *dm.Gauge.Value, tc.wantGauge) + assert.Equal(t, len(dm.Label), 1) + assert.Equal(t, *dm.Label[0].Name, "state") + assert.Equal(t, *dm.Label[0].Value, tc.wantState) + }) + } } diff --git a/go.mod b/go.mod index c34f4bf..1d04a1e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22 require ( github.com/alecthomas/kingpin/v2 v2.4.0 github.com/prometheus/client_golang v1.20.5 + github.com/prometheus/client_model v0.6.1 github.com/prometheus/common v0.60.1 github.com/prometheus/exporter-toolkit v0.13.1 github.com/stretchr/testify v1.9.0 @@ -24,7 +25,6 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/xhit/go-str2duration/v2 v2.1.0 // indirect golang.org/x/crypto v0.28.0 // indirect