From 594ed85b0d4002e59533b164e977c3dbc0d33bac Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Thu, 22 Feb 2024 13:40:50 +0100 Subject: [PATCH] Extend tests --- hp/cntlr/controller.go | 2 ++ hp/cntlr/controller_test.go | 58 +++++++++++++++++++++++++++++++++++++ hp/cntlr/firmware.go | 1 + hp/drive/drive.go | 2 ++ hp/ilo/firmware_test.go | 1 - 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 hp/cntlr/controller_test.go diff --git a/hp/cntlr/controller.go b/hp/cntlr/controller.go index ed94fae..6053947 100644 --- a/hp/cntlr/controller.go +++ b/hp/cntlr/controller.go @@ -76,6 +76,8 @@ func GetControllersFromTable(t *CpqDaCntlrTable) ([]*Controller, error) { return controllers, nil } +// GetNagiosStatus validates the Controller's data against the known models +// in this plugin. func (d *Controller) GetNagiosStatus() (int, string) { description := fmt.Sprintf("controller (%s) model=%s serial=%s firmware=%s", d.ID, d.Model, strings.TrimSpace(d.Serial), d.FwRev) diff --git a/hp/cntlr/controller_test.go b/hp/cntlr/controller_test.go new file mode 100644 index 0000000..7119d28 --- /dev/null +++ b/hp/cntlr/controller_test.go @@ -0,0 +1,58 @@ +package cntlr + +import ( + "testing" + + "github.com/NETWAYS/go-check" + "github.com/stretchr/testify/assert" +) + +func TestIlo_GetNagiosStatus(t *testing.T) { + testcases := map[string]struct { + controller Controller + expectedState int + expectedOutput string + }{ + "status-ok": { + controller: Controller{ + ID: "id123", + Model: "model", + FwRev: "revision", + Serial: "12345", + Status: "ok", + }, + expectedState: check.OK, + expectedOutput: "controller (id123) model=model serial=12345 firmware=revision", + }, + "status-not-ok-not-affected": { + controller: Controller{ + ID: "id123", + Model: "model", + FwRev: "revision", + Serial: "12345", + Status: "not-ok", + }, + expectedState: check.Critical, + expectedOutput: "controller (id123) model=model serial=12345 firmware=revision", + }, + "status-not-ok-affected": { + controller: Controller{ + ID: "id123", + Model: "e208i-p", + FwRev: "1.98", + Serial: "12345", + Status: "ok", + }, + expectedState: check.Critical, + expectedOutput: "controller (id123) model=e208i-p serial=12345 firmware=1.98 - if you have RAID 5/6/50/60 - update immediately!", + }, + } + + for name, tc := range testcases { + t.Run(name, func(t *testing.T) { + state, output := tc.controller.GetNagiosStatus() + assert.Equal(t, state, tc.expectedState) + assert.Contains(t, output, tc.expectedOutput) + }) + } +} diff --git a/hp/cntlr/firmware.go b/hp/cntlr/firmware.go index fc027b6..f869282 100644 --- a/hp/cntlr/firmware.go +++ b/hp/cntlr/firmware.go @@ -24,6 +24,7 @@ func init() { } } +// IsAffected validates the given version against known affected versions. // Note: we can't validate against existing logical drives at the moment func IsAffected(firmware string) (int, string) { firmwareVersion, _ := version.NewVersion(firmware) diff --git a/hp/drive/drive.go b/hp/drive/drive.go index 67ae5e0..865ad29 100644 --- a/hp/drive/drive.go +++ b/hp/drive/drive.go @@ -76,6 +76,8 @@ func GetPhysicalDrivesFromTable(t *CpqDaPhyDrvTable) ([]*PhysicalDrive, error) { return drives, nil } +// GetNagiosStatus validates the drive's data against the known models +// in this plugin. func (d *PhysicalDrive) GetNagiosStatus() (int, string) { description := fmt.Sprintf("physical drive (%-4s) model=%s serial=%s firmware=%s hours=%d", d.ID, d.Model, d.Serial, d.FwRev, d.Hours) diff --git a/hp/ilo/firmware_test.go b/hp/ilo/firmware_test.go index 21a0933..61a0d4c 100644 --- a/hp/ilo/firmware_test.go +++ b/hp/ilo/firmware_test.go @@ -58,7 +58,6 @@ func TestIlo_GetNagiosStatus(t *testing.T) { assert.Contains(t, output, tc.expectedOutput) }) } - } func TestIsNewerVersion(t *testing.T) {