Skip to content
Merged
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
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ require (
github.com/NETWAYS/go-check v0.6.2
github.com/gosnmp/gosnmp v1.42.1
github.com/hashicorp/go-version v1.7.0
github.com/stretchr/testify v1.11.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require github.com/spf13/pflag v1.0.5 // indirect
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
12 changes: 9 additions & 3 deletions hp/cntlr/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cntlr

import (
"strings"
"testing"

"github.com/NETWAYS/go-check"
"github.com/stretchr/testify/assert"
)

func TestIlo_GetNagiosStatus(t *testing.T) {
Expand Down Expand Up @@ -51,8 +51,14 @@ func TestIlo_GetNagiosStatus(t *testing.T) {
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)

if state != tc.expectedState {
t.Fatalf("expected %v, got %v", tc.expectedState, state)
}

if !strings.Contains(output, tc.expectedOutput) {
t.Fatalf("expected %v, got %v", tc.expectedOutput, output)
}
})
}
}
13 changes: 9 additions & 4 deletions hp/cntlr/firmware_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cntlr

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

type testInfo struct {
Expand All @@ -24,7 +23,13 @@ func TestIsAffected(t *testing.T) {

for fw, expect := range versions {
rc, info := IsAffected(fw)
assert.Equal(t, rc, expect.rc)
assert.Contains(t, info, expect.info)

if rc != expect.rc {
t.Fatalf("expected %v, got %v", expect.rc, rc)
}

if !strings.Contains(info, expect.info) {
t.Fatalf("expected %v, got %v", expect.info, info)
}
}
}
47 changes: 38 additions & 9 deletions hp/drive/drive_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package drive

import (
"regexp"
"testing"

"github.com/NETWAYS/go-check"
"github.com/stretchr/testify/assert"
)

const affectedDrive = "VO0480JFDGT"
const affectedDriveFixed = "HPD8"

func matchRegexp(rx string, str string) bool {
r := regexp.MustCompile(rx)

return r.MatchString(str)
}

func TestPhysicalDrive_GetNagiosStatus(t *testing.T) {
drive := &PhysicalDrive{
ID: "1.1",
Expand All @@ -27,25 +33,48 @@ func TestPhysicalDrive_GetNagiosStatus(t *testing.T) {

// good
status, info = drive.GetNagiosStatus()
assert.Equal(t, check.OK, status)
assert.Regexp(t, `\(1\.1 \) model=\w+ serial=ABC123 firmware=HPD1 hours=1337`, info)

if check.OK != status {
t.Fatalf("expected %v, got %v", check.OK, status)
}

if !matchRegexp(`\(1\.1 \) model=\w+ serial=ABC123 firmware=HPD1 hours=1337`, info) {
t.Fatalf("%s did not match regex", info)
}

// failed
drive.Status = "failed"
status, info = drive.GetNagiosStatus()
assert.Equal(t, check.Critical, status)
assert.Regexp(t, `\(1\.1 \) model=\w+ serial=ABC123 firmware=HPD1 hours=1337 - status: failed`, info)

if check.Critical != status {
t.Fatalf("expected %v, got %v", check.Critical, status)
}

if !matchRegexp(`\(1\.1 \) model=\w+ serial=ABC123 firmware=HPD1 hours=1337 - status: failed`, info) {
t.Fatalf("%s did not match regex", info)
}

// affected
drive.Status = "ok"
drive.Model = affectedDrive
status, info = drive.GetNagiosStatus()
assert.Equal(t, check.Critical, status)
assert.Regexp(t, `\(1\.1 \) model=\w+ serial=ABC123 firmware=HPD1 hours=1337 - affected`, info)

if check.Critical != status {
t.Fatalf("expected %v, got %v", check.Critical, status)
}

if !matchRegexp(`\(1\.1 \) model=\w+ serial=ABC123 firmware=HPD1 hours=1337 - affected`, info) {
t.Fatalf("%s did not match regex", info)
}

// affected but fixed
drive.FwRev = affectedDriveFixed
status, info = drive.GetNagiosStatus()
assert.Equal(t, check.OK, status)
assert.Regexp(t, `\(1\.1 \) model=\w+ serial=ABC123 firmware=\w+ hours=1337 - .*applied`, info)
if check.OK != status {
t.Fatalf("expected %v, got %v", check.OK, status)
}

if !matchRegexp(`\(1\.1 \) model=\w+ serial=ABC123 firmware=\w+ hours=1337 - .*applied`, info) {
t.Fatalf("%s did not match regex", info)
}
}
61 changes: 47 additions & 14 deletions hp/drive/firmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,44 @@ package drive

import (
"testing"

"github.com/stretchr/testify/assert"
)

const testModelA = "VO0480JFDGT"
const testModelB = "EK0800JVYPN"

func TestSplitFirmware(t *testing.T) {
prefix, version, err := SplitFirmware("HPD5")
assert.Equal(t, "HPD", prefix)
assert.Equal(t, 5, version)
assert.Nil(t, err)

if "HPD" != prefix {
t.Fatalf("expected %s, got %s", "HPD", prefix)
}

if 5 != version {
t.Fatalf("expected %d, got %d", 5, version)
}

if err != nil {
t.Fatalf("expected no error, got %v", err)
}

prefix, version, err = SplitFirmware("HPD10")
assert.Equal(t, "HPD", prefix)
assert.Equal(t, 10, version)
assert.Nil(t, err)

if "HPD" != prefix {
t.Fatalf("expected %s, got %s", "HPD", prefix)
}

if 10 != version {
t.Fatalf("expected %d, got %d", 10, version)
}

if err != nil {
t.Fatalf("expected no error, got %v", err)
}
_, _, err = SplitFirmware("1HPD5")
assert.Error(t, err)

if err == nil {
t.Fatalf("expected error, got none")
}
}

func TestIsFirmwareFixed(t *testing.T) {
Expand All @@ -37,8 +55,14 @@ func TestIsFirmwareFixed(t *testing.T) {

for fw, expect := range testsA {
ok, err := IsFirmwareFixed(modelA, fw)
assert.Equal(t, expect, ok)
assert.NoError(t, err)

if ok != expect {
t.Fatalf("expected %v, got %v", ok, expect)
}

if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}

testsB := map[string]bool{
Expand All @@ -52,11 +76,20 @@ func TestIsFirmwareFixed(t *testing.T) {

for fw, expect := range testsB {
ok, err := IsFirmwareFixed(modelB, fw)
assert.Equal(t, expect, ok)
assert.NoError(t, err)

if ok != expect {
t.Fatalf("expected %v, got %v", ok, expect)
}

if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}

otherModel := &AffectedModel{"ABC", "nothing", "HPX11"}
_, err := IsFirmwareFixed(otherModel, "HPD5")
assert.Error(t, err)

if err == nil {
t.Fatalf("expected error, got none")
}
}
82 changes: 57 additions & 25 deletions hp/ilo/firmware_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ilo

import (
"strings"
"testing"

"github.com/NETWAYS/check_hp_firmware/snmp"

"github.com/NETWAYS/go-check"
"github.com/stretchr/testify/assert"
)

func TestIlo_GetNagiosStatus(t *testing.T) {
Expand Down Expand Up @@ -56,49 +56,81 @@ func TestIlo_GetNagiosStatus(t *testing.T) {
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
state, output := tc.ilo.GetNagiosStatus(1)
assert.Equal(t, state, tc.expectedState)
assert.Contains(t, output, tc.expectedOutput)
if state != tc.expectedState {
t.Fatalf("expected %v, got %v", tc.expectedState, state)
}

if !strings.Contains(output, tc.expectedOutput) {
t.Fatalf("expected %v, got %v", tc.expectedOutput, output)
}
})
}
}

func TestIsNewerVersion(t *testing.T) {
// Compare required Version with current Version
assert.True(t, isNewerVersion("1.0", "1.0"))
assert.True(t, isNewerVersion("1.0", "1.1"))
assert.True(t, isNewerVersion("1.0", "5"))
assert.True(t, isNewerVersion("1.0", "10.1.0"))

assert.False(t, isNewerVersion("1.0", "0.9"))
assert.False(t, isNewerVersion("1.0", "0.9"))
assert.False(t, isNewerVersion("1.0", "0.0"))
assert.False(t, isNewerVersion("1.0", "0"))

assert.False(t, isNewerVersion("1.0", "foobar"))
assert.False(t, isNewerVersion("foobar", "1.0"))
assert.False(t, isNewerVersion("xxx", "xxx"))
tests := []struct {
current string
required string
expected bool
}{
{"1.0", "1.0", true},
{"1.0", "1.1", true},
{"1.0", "5", true},
{"1.0", "10.1.0", true},
{"1.0", "0.9", false},
{"1.0", "0.9", false}, // Duplicate test case (can be removed if not intentional)
{"1.0", "0.0", false},
{"1.0", "0", false},
{"1.0", "foobar", false},
{"foobar", "1.0", false},
{"xxx", "xxx", false},
}

for _, test := range tests {
result := isNewerVersion(test.current, test.required)
if result != test.expected {
t.Errorf("isNewerVersion(%q, %q) = %v, want %v",
test.current, test.required, result, test.expected)
}
}
}

func TestGetIloInformation_ilo5(t *testing.T) {
c, _ := snmp.NewFileHandlerFromFile("../../testdata/ilo5.txt")

i, err := GetIloInformation(c)

assert.NoError(t, err)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

assert.Equal(t, 11, i.ModelID)
assert.Equal(t, "pciIntegratedLightsOutRemoteInsight5", i.Model)
assert.Equal(t, "3.00", i.RomRevision)
if i.ModelID != 11 {
t.Errorf("Expected ModelID to be %d, got %d", 11, i.ModelID)
}
if i.Model != "pciIntegratedLightsOutRemoteInsight5" {
t.Errorf("Expected Model to be %q, got %q", "pciIntegratedLightsOutRemoteInsight5", i.Model)
}
if i.RomRevision != "3.00" {
t.Errorf("Expected RomRevision to be %q, got %q", "3.00", i.RomRevision)
}
}

func TestGetIloInformation_ilo6(t *testing.T) {
c, _ := snmp.NewFileHandlerFromFile("../../testdata/ilo6.txt")

i, err := GetIloInformation(c)

assert.NoError(t, err)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

assert.Equal(t, 12, i.ModelID)
assert.Equal(t, "pciIntegratedLightsOutRemoteInsight6", i.Model)
assert.Equal(t, "1.55", i.RomRevision)
if i.ModelID != 12 {
t.Errorf("Expected ModelID to be %d, got %d", 12, i.ModelID)
}
if i.Model != "pciIntegratedLightsOutRemoteInsight6" {
t.Errorf("Expected Model to be %q, got %q", "pciIntegratedLightsOutRemoteInsight6", i.Model)
}
if i.RomRevision != "1.55" {
t.Errorf("Expected RomRevision to be %q, got %q", "1.55", i.RomRevision)
}
}
Loading