Skip to content

Commit bcbf753

Browse files
authored
Merge pull request #147 from jackby03/test/fleet-hascritical-coverage-13630435664046139039
🧪 test: implement unit testing for fleet HasCritical
2 parents 1293c2b + bbb47e1 commit bcbf753

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

internal/fleet/fleet_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (C) 2024 Jack (jackby03)
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published
5+
// by the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
package fleet_test
17+
18+
import (
19+
"errors"
20+
"testing"
21+
22+
"github.com/hardbox-io/hardbox/internal/fleet"
23+
)
24+
25+
func TestHasCritical(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
results []fleet.HostResult
29+
want bool
30+
}{
31+
{
32+
name: "empty results",
33+
results: []fleet.HostResult{},
34+
want: false,
35+
},
36+
{
37+
name: "no critical string present",
38+
results: []fleet.HostResult{
39+
{Output: `{"status": "ok"}`},
40+
},
41+
want: false,
42+
},
43+
{
44+
name: "critical is zero without space",
45+
results: []fleet.HostResult{
46+
{Output: `{"critical":0}`},
47+
},
48+
want: false,
49+
},
50+
{
51+
name: "critical is zero with space",
52+
results: []fleet.HostResult{
53+
{Output: `{"critical": 0}`},
54+
},
55+
want: false,
56+
},
57+
{
58+
name: "critical is greater than zero",
59+
results: []fleet.HostResult{
60+
{Output: `{"critical": 1}`},
61+
},
62+
want: true,
63+
},
64+
{
65+
name: "critical is greater than zero with space",
66+
results: []fleet.HostResult{
67+
{Output: `{"critical": 5}`},
68+
},
69+
want: true,
70+
},
71+
{
72+
name: "result with error is skipped",
73+
results: []fleet.HostResult{
74+
{Output: `{"critical": 1}`, Err: errors.New("failed")},
75+
},
76+
want: false,
77+
},
78+
{
79+
name: "mixed results where one is critical",
80+
results: []fleet.HostResult{
81+
{Output: `{"critical": 0}`},
82+
{Output: `{"critical": 1}`, Err: errors.New("failed")},
83+
{Output: `{"critical": 2}`},
84+
},
85+
want: true,
86+
},
87+
{
88+
name: "only non-critical findings in multiple results",
89+
results: []fleet.HostResult{
90+
{Output: `{"critical": 0}`},
91+
{Output: `{"critical": 0}`},
92+
{Output: `{"status": "ok"}`},
93+
},
94+
want: false,
95+
},
96+
}
97+
98+
for _, tc := range tests {
99+
t.Run(tc.name, func(t *testing.T) {
100+
got := fleet.HasCritical(tc.results)
101+
if got != tc.want {
102+
t.Errorf("HasCritical() = %v, want %v", got, tc.want)
103+
}
104+
})
105+
}
106+
}

0 commit comments

Comments
 (0)