Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for SELEventsCollector #228

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
71 changes: 71 additions & 0 deletions collector_sel_events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +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) {
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)
})
}
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ 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
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -15,13 +17,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/prometheus/client_model v0.6.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // 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
Expand All @@ -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
)