Skip to content

Commit 426340b

Browse files
committed
WIP
1 parent b255b10 commit 426340b

File tree

5 files changed

+158
-64
lines changed

5 files changed

+158
-64
lines changed

hp/cntlr/controller_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package cntlr
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/NETWAYS/go-check"
7-
"github.com/stretchr/testify/assert"
88
)
99

1010
func TestIlo_GetNagiosStatus(t *testing.T) {
@@ -51,8 +51,14 @@ func TestIlo_GetNagiosStatus(t *testing.T) {
5151
for name, tc := range testcases {
5252
t.Run(name, func(t *testing.T) {
5353
state, output := tc.controller.GetNagiosStatus()
54-
assert.Equal(t, state, tc.expectedState)
55-
assert.Contains(t, output, tc.expectedOutput)
54+
55+
if state != tc.expectedState {
56+
t.Fatalf("expected %v, got %v", tc.expectedState, state)
57+
}
58+
59+
if !strings.Contains(output, tc.expectedOutput) {
60+
t.Fatalf("expected %v, got %v", tc.expectedOutput, output)
61+
}
5662
})
5763
}
5864
}

hp/cntlr/firmware_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package cntlr
22

33
import (
4+
"strings"
45
"testing"
5-
6-
"github.com/stretchr/testify/assert"
76
)
87

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

2524
for fw, expect := range versions {
2625
rc, info := IsAffected(fw)
27-
assert.Equal(t, rc, expect.rc)
28-
assert.Contains(t, info, expect.info)
26+
27+
if rc != expect.rc {
28+
t.Fatalf("expected %v, got %v", expect.rc, rc)
29+
}
30+
31+
if !strings.Contains(info, expect.info) {
32+
t.Fatalf("expected %v, got %v", expect.info, info)
33+
}
2934
}
3035
}

hp/ilo/firmware_test.go

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package ilo
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/NETWAYS/check_hp_firmware/snmp"
78

89
"github.com/NETWAYS/go-check"
9-
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func TestIlo_GetNagiosStatus(t *testing.T) {
@@ -56,49 +56,81 @@ func TestIlo_GetNagiosStatus(t *testing.T) {
5656
for name, tc := range testcases {
5757
t.Run(name, func(t *testing.T) {
5858
state, output := tc.ilo.GetNagiosStatus(1)
59-
assert.Equal(t, state, tc.expectedState)
60-
assert.Contains(t, output, tc.expectedOutput)
59+
if state != tc.expectedState {
60+
t.Fatalf("expected %v, got %v", tc.expectedState, state)
61+
}
62+
63+
if !strings.Contains(output, tc.expectedOutput) {
64+
t.Fatalf("expected %v, got %v", tc.expectedOutput, output)
65+
}
6166
})
6267
}
6368
}
6469

6570
func TestIsNewerVersion(t *testing.T) {
66-
// Compare required Version with current Version
67-
assert.True(t, isNewerVersion("1.0", "1.0"))
68-
assert.True(t, isNewerVersion("1.0", "1.1"))
69-
assert.True(t, isNewerVersion("1.0", "5"))
70-
assert.True(t, isNewerVersion("1.0", "10.1.0"))
71-
72-
assert.False(t, isNewerVersion("1.0", "0.9"))
73-
assert.False(t, isNewerVersion("1.0", "0.9"))
74-
assert.False(t, isNewerVersion("1.0", "0.0"))
75-
assert.False(t, isNewerVersion("1.0", "0"))
76-
77-
assert.False(t, isNewerVersion("1.0", "foobar"))
78-
assert.False(t, isNewerVersion("foobar", "1.0"))
79-
assert.False(t, isNewerVersion("xxx", "xxx"))
71+
tests := []struct {
72+
current string
73+
required string
74+
expected bool
75+
}{
76+
{"1.0", "1.0", true},
77+
{"1.0", "1.1", true},
78+
{"1.0", "5", true},
79+
{"1.0", "10.1.0", true},
80+
{"1.0", "0.9", false},
81+
{"1.0", "0.9", false}, // Duplicate test case (can be removed if not intentional)
82+
{"1.0", "0.0", false},
83+
{"1.0", "0", false},
84+
{"1.0", "foobar", false},
85+
{"foobar", "1.0", false},
86+
{"xxx", "xxx", false},
87+
}
88+
89+
for _, test := range tests {
90+
result := isNewerVersion(test.current, test.required)
91+
if result != test.expected {
92+
t.Errorf("isNewerVersion(%q, %q) = %v, want %v",
93+
test.current, test.required, result, test.expected)
94+
}
95+
}
8096
}
8197

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

85101
i, err := GetIloInformation(c)
86102

87-
assert.NoError(t, err)
103+
if err != nil {
104+
t.Fatalf("expected no error, got %v", err)
105+
}
88106

89-
assert.Equal(t, 11, i.ModelID)
90-
assert.Equal(t, "pciIntegratedLightsOutRemoteInsight5", i.Model)
91-
assert.Equal(t, "3.00", i.RomRevision)
107+
if i.ModelID != 11 {
108+
t.Errorf("Expected ModelID to be %d, got %d", 11, i.ModelID)
109+
}
110+
if i.Model != "pciIntegratedLightsOutRemoteInsight5" {
111+
t.Errorf("Expected Model to be %q, got %q", "pciIntegratedLightsOutRemoteInsight5", i.Model)
112+
}
113+
if i.RomRevision != "3.00" {
114+
t.Errorf("Expected RomRevision to be %q, got %q", "3.00", i.RomRevision)
115+
}
92116
}
93117

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

97121
i, err := GetIloInformation(c)
98122

99-
assert.NoError(t, err)
123+
if err != nil {
124+
t.Fatalf("expected no error, got %v", err)
125+
}
100126

101-
assert.Equal(t, 12, i.ModelID)
102-
assert.Equal(t, "pciIntegratedLightsOutRemoteInsight6", i.Model)
103-
assert.Equal(t, "1.55", i.RomRevision)
127+
if i.ModelID != 12 {
128+
t.Errorf("Expected ModelID to be %d, got %d", 12, i.ModelID)
129+
}
130+
if i.Model != "pciIntegratedLightsOutRemoteInsight6" {
131+
t.Errorf("Expected Model to be %q, got %q", "pciIntegratedLightsOutRemoteInsight6", i.Model)
132+
}
133+
if i.RomRevision != "1.55" {
134+
t.Errorf("Expected RomRevision to be %q, got %q", "1.55", i.RomRevision)
135+
}
104136
}

snmp/table_test.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package snmp
22

33
import (
44
"os"
5+
"reflect"
56
"testing"
6-
7-
"github.com/stretchr/testify/assert"
87
)
98

109
func TestSnmpTable_Walk(t *testing.T) {
@@ -19,22 +18,38 @@ func TestSnmpTable_Walk(t *testing.T) {
1918
Oid: ".1.3.6.1.2.1.2.2", // IF-MIB::ifTable
2019
}
2120

22-
assert.NoError(t, table.Walk())
21+
err := table.Walk()
22+
if err != nil {
23+
t.Fatalf("expected no error, got %v", err)
24+
}
2325

24-
assert.NotEmpty(t, table.Columns, "expect at least the default columns")
25-
assert.NotEmpty(t, table.Values, "expect at least some rows, even a basic container should have localhost")
26+
// assert.NotEmpty(t, table.Columns, "expect at least the default columns")
27+
// assert.NotEmpty(t, table.Values, "expect at least some rows, even a basic container should have localhost")
2628
}
2729

28-
func TestSortOIDS(t *testing.T) {
29-
assert.Equal(t,
30-
[]string{"1.2.3", "4.5.6"},
31-
SortOIDs([]string{"4.5.6", "1.2.3"}))
32-
33-
assert.Equal(t,
34-
[]string{"1.2.3", "1.2.10", "1.2.14"},
35-
SortOIDs([]string{"1.2.14", "1.2.10", "1.2.3"}))
30+
func TestSortOIDs(t *testing.T) {
31+
tests := []struct {
32+
input []string
33+
expected []string
34+
}{
35+
{
36+
input: []string{"4.5.6", "1.2.3"},
37+
expected: []string{"1.2.3", "4.5.6"},
38+
},
39+
{
40+
input: []string{"1.2.14", "1.2.10", "1.2.3"},
41+
expected: []string{"1.2.3", "1.2.10", "1.2.14"},
42+
},
43+
{
44+
input: []string{"1.2.14", "1.2.10", "1.2.3.4.5.6"},
45+
expected: []string{"1.2.3.4.5.6", "1.2.10", "1.2.14"},
46+
},
47+
}
3648

37-
assert.Equal(t,
38-
[]string{"1.2.3.4.5.6", "1.2.10", "1.2.14"},
39-
SortOIDs([]string{"1.2.14", "1.2.10", "1.2.3.4.5.6"}))
49+
for _, test := range tests {
50+
result := SortOIDs(test.input)
51+
if !reflect.DeepEqual(result, test.expected) {
52+
t.Errorf("SortOIDs(%v) = %v, want %v", test.input, result, test.expected)
53+
}
54+
}
4055
}

snmp/util_test.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,67 @@ import (
55
"testing"
66

77
"github.com/gosnmp/gosnmp"
8-
"github.com/stretchr/testify/assert"
98
)
109

1110
func TestIsOid(t *testing.T) {
12-
assert.False(t, IsOid(""))
13-
assert.False(t, IsOid(".x"))
14-
assert.False(t, IsOid(".1.2.3.x"))
15-
assert.False(t, IsOid(".1.2.3..4"))
16-
assert.False(t, IsOid(".1.2.3.4."))
17-
18-
assert.True(t, IsOid(".1.2.3.4"))
19-
assert.True(t, IsOid(".1.2.311.44"))
11+
tests := []struct {
12+
input string
13+
expected bool
14+
}{
15+
{"", false},
16+
{".x", false},
17+
{".1.2.3.x", false},
18+
{".1.2.3..4", false},
19+
{".1.2.3.4.", false},
20+
{".1.2.3.4", true},
21+
{".1.2.311.44", true},
22+
}
23+
24+
for _, test := range tests {
25+
result := IsOid(test.input)
26+
if result != test.expected {
27+
t.Errorf("IsOid(%q) = %v, want %v", test.input, result, test.expected)
28+
}
29+
}
2030
}
2131

2232
func TestIsOidPartOf(t *testing.T) {
23-
assert.False(t, IsOidPartOf("", ""))
24-
assert.False(t, IsOidPartOf(".1.2.3.4", ".1.1"))
25-
assert.True(t, IsOidPartOf(".1.2", ".1"))
26-
assert.True(t, IsOidPartOf(".1.2", ".1.2"))
33+
tests := []struct {
34+
oid string
35+
prefix string
36+
expected bool
37+
}{
38+
{"", "", false},
39+
{".1.2.3.4", ".1.1", false},
40+
{".1.2", ".1", true},
41+
{".1.2", ".1.2", true},
42+
}
43+
44+
for _, test := range tests {
45+
result := IsOidPartOf(test.oid, test.prefix)
46+
if result != test.expected {
47+
t.Errorf("IsOidPartOf(%q, %q) = %v, want %v", test.oid, test.prefix, result, test.expected)
48+
}
49+
}
2750
}
2851

2952
func TestGetSubOid(t *testing.T) {
30-
assert.Equal(t, GetSubOid(".1.2.3.4", ".1.2.5"), "")
31-
assert.Equal(t, GetSubOid(".1.2.3.4", ".1.2.3"), "4")
32-
assert.Equal(t, GetSubOid(".1.2.3.4.5.6.7", ".1.2.3"), "4.5.6.7")
53+
tests := []struct {
54+
oid string
55+
prefix string
56+
expected string
57+
}{
58+
{".1.2.3.4", ".1.2.5", ""},
59+
{".1.2.3.4", ".1.2.3", "4"},
60+
{".1.2.3.4.5.6.7", ".1.2.3", "4.5.6.7"},
61+
}
62+
63+
for _, test := range tests {
64+
result := GetSubOid(test.oid, test.prefix)
65+
if result != test.expected {
66+
t.Errorf("GetSubOid(%q, %q) = %q, want %q", test.oid, test.prefix, result, test.expected)
67+
}
68+
}
3369
}
3470

3571
func getEnvDefault(key string, defaultValue string) string {

0 commit comments

Comments
 (0)