1
1
package analyzer
2
2
3
3
import (
4
+ "encoding/json"
4
5
"testing"
5
6
6
7
"github.com/pkg/errors"
@@ -10,25 +11,30 @@ import (
10
11
"github.com/stretchr/testify/require"
11
12
)
12
13
14
+ func collectorToBytes (t * testing.T , collector any ) []byte {
15
+ jsonData , err := json .Marshal (collector )
16
+ require .NoError (t , err )
17
+ return jsonData
18
+ }
19
+
13
20
func TestAnalyzeHostCollectorResults (t * testing.T ) {
14
21
tests := []struct {
15
22
name string
16
23
outcomes []* troubleshootv1beta2.Outcome
17
24
collectedContent []collectedContent
18
- checkCondition func (string , collectorData ) (bool , error )
19
25
expectResult []* AnalyzeResult
20
26
}{
21
27
{
22
28
name : "pass if ubuntu >= 00.1.2" ,
23
29
collectedContent : []collectedContent {
24
30
{
25
31
NodeName : "node1" ,
26
- Data : collect.HostOSInfo {
32
+ Data : collectorToBytes ( t , collect.HostOSInfo {
27
33
Name : "myhost" ,
28
34
KernelVersion : "5.4.0-1034-gcp" ,
29
35
PlatformVersion : "00.1.2" ,
30
36
Platform : "ubuntu" ,
31
- },
37
+ }) ,
32
38
},
33
39
},
34
40
outcomes : []* troubleshootv1beta2.Outcome {
@@ -44,10 +50,6 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
44
50
},
45
51
},
46
52
},
47
- checkCondition : func (when string , data collectorData ) (bool , error ) {
48
- osInfo := data .(collect.HostOSInfo )
49
- return osInfo .Platform == "ubuntu" && osInfo .PlatformVersion >= "00.1.2" , nil
50
- },
51
53
expectResult : []* AnalyzeResult {
52
54
{
53
55
Title : "Host OS Info - Node node1" ,
@@ -61,21 +63,21 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
61
63
collectedContent : []collectedContent {
62
64
{
63
65
NodeName : "node1" ,
64
- Data : collect.HostOSInfo {
66
+ Data : collectorToBytes ( t , collect.HostOSInfo {
65
67
Name : "myhost" ,
66
68
KernelVersion : "5.4.0-1034-gcp" ,
67
69
PlatformVersion : "11.04" ,
68
70
Platform : "ubuntu" ,
69
- },
71
+ }) ,
70
72
},
71
73
{
72
74
NodeName : "node2" ,
73
- Data : collect.HostOSInfo {
75
+ Data : collectorToBytes ( t , collect.HostOSInfo {
74
76
Name : "myhost" ,
75
77
KernelVersion : "5.4.0-1034-gcp" ,
76
78
PlatformVersion : "11.04" ,
77
79
Platform : "ubuntu" ,
78
- },
80
+ }) ,
79
81
},
80
82
},
81
83
outcomes : []* troubleshootv1beta2.Outcome {
@@ -91,10 +93,6 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
91
93
},
92
94
},
93
95
},
94
- checkCondition : func (when string , data collectorData ) (bool , error ) {
95
- osInfo := data .(collect.HostOSInfo )
96
- return osInfo .Platform == "ubuntu" && osInfo .PlatformVersion <= "11.04" , nil
97
- },
98
96
expectResult : []* AnalyzeResult {
99
97
{
100
98
Title : "Host OS Info - Node node1" ,
@@ -113,12 +111,12 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
113
111
collectedContent : []collectedContent {
114
112
{
115
113
NodeName : "" ,
116
- Data : collect.HostOSInfo {
114
+ Data : collectorToBytes ( t , collect.HostOSInfo {
117
115
Name : "myhost" ,
118
116
KernelVersion : "5.4.0-1034-gcp" ,
119
117
PlatformVersion : "20.04" ,
120
118
Platform : "ubuntu" ,
121
- },
119
+ }) ,
122
120
},
123
121
},
124
122
outcomes : []* troubleshootv1beta2.Outcome {
@@ -134,10 +132,6 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
134
132
},
135
133
},
136
134
},
137
- checkCondition : func (when string , data collectorData ) (bool , error ) {
138
- osInfo := data .(collect.HostOSInfo )
139
- return osInfo .Platform == "ubuntu" && osInfo .PlatformVersion >= "20.04" , nil
140
- },
141
135
expectResult : []* AnalyzeResult {
142
136
{
143
137
Title : "Host OS Info" , // Ensuring the title does not include node name if it's empty
@@ -150,8 +144,9 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
150
144
151
145
for _ , test := range tests {
152
146
t .Run (test .name , func (t * testing.T ) {
147
+ a := AnalyzeHostOS {}
153
148
// Call the new analyzeHostCollectorResults function with the test data
154
- result , err := analyzeHostCollectorResults (test .collectedContent , test .outcomes , test . checkCondition , "Host OS Info" )
149
+ result , err := analyzeHostCollectorResults (test .collectedContent , test .outcomes , a . CheckCondition , "Host OS Info" )
155
150
require .NoError (t , err )
156
151
assert .Equal (t , test .expectResult , result )
157
152
})
@@ -162,8 +157,8 @@ func TestEvaluateOutcomes(t *testing.T) {
162
157
tests := []struct {
163
158
name string
164
159
outcomes []* troubleshootv1beta2.Outcome
165
- checkCondition func (string , collectorData ) (bool , error )
166
- data collectorData
160
+ checkCondition func (string , [] byte ) (bool , error )
161
+ data [] byte
167
162
expectedResult []* AnalyzeResult
168
163
}{
169
164
{
@@ -176,11 +171,11 @@ func TestEvaluateOutcomes(t *testing.T) {
176
171
},
177
172
},
178
173
},
179
- checkCondition : func (when string , data collectorData ) (bool , error ) {
174
+ checkCondition : func (when string , data [] byte ) (bool , error ) {
180
175
// Return true if the condition being checked matches "failCondition"
181
176
return when == "failCondition" , nil
182
177
},
183
- data : "someData" ,
178
+ data : [] byte ( "someData" ) ,
184
179
expectedResult : []* AnalyzeResult {
185
180
{
186
181
Title : "Test Title" ,
@@ -199,11 +194,11 @@ func TestEvaluateOutcomes(t *testing.T) {
199
194
},
200
195
},
201
196
},
202
- checkCondition : func (when string , data collectorData ) (bool , error ) {
197
+ checkCondition : func (when string , data [] byte ) (bool , error ) {
203
198
// Return true if the condition being checked matches "warnCondition"
204
199
return when == "warnCondition" , nil
205
200
},
206
- data : "someData" ,
201
+ data : [] byte ( "someData" ) ,
207
202
expectedResult : []* AnalyzeResult {
208
203
{
209
204
Title : "Test Title" ,
@@ -222,11 +217,11 @@ func TestEvaluateOutcomes(t *testing.T) {
222
217
},
223
218
},
224
219
},
225
- checkCondition : func (when string , data collectorData ) (bool , error ) {
220
+ checkCondition : func (when string , data [] byte ) (bool , error ) {
226
221
// Return true if the condition being checked matches "passCondition"
227
222
return when == "passCondition" , nil
228
223
},
229
- data : "someData" ,
224
+ data : [] byte ( "someData" ) ,
230
225
expectedResult : []* AnalyzeResult {
231
226
{
232
227
Title : "Test Title" ,
@@ -253,11 +248,11 @@ func TestEvaluateOutcomes(t *testing.T) {
253
248
},
254
249
},
255
250
},
256
- checkCondition : func (when string , data collectorData ) (bool , error ) {
251
+ checkCondition : func (when string , data [] byte ) (bool , error ) {
257
252
// Always return false to simulate no condition matching
258
253
return false , nil
259
254
},
260
- data : "someData" ,
255
+ data : [] byte ( "someData" ) ,
261
256
expectedResult : nil , // No condition matches, so we expect no results
262
257
},
263
258
{
@@ -270,11 +265,11 @@ func TestEvaluateOutcomes(t *testing.T) {
270
265
},
271
266
},
272
267
},
273
- checkCondition : func (when string , data collectorData ) (bool , error ) {
268
+ checkCondition : func (when string , data [] byte ) (bool , error ) {
274
269
// Simulate an error occurring during condition evaluation
275
270
return false , errors .New ("mock error" )
276
271
},
277
- data : "someData" ,
272
+ data : [] byte ( "someData" ) ,
278
273
expectedResult : []* AnalyzeResult {
279
274
{
280
275
Title : "Test Title" ,
0 commit comments