Skip to content

Commit 6f59025

Browse files
committed
Introduce new statuses: SKIP, MANU, ERRO
1 parent cc619e5 commit 6f59025

File tree

11 files changed

+233
-62
lines changed

11 files changed

+233
-62
lines changed

check/check.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ const (
3838
WARN State = "WARN"
3939
// INFO informational message
4040
INFO State = "INFO"
41-
4241
// SKIP for when a check should be skipped.
43-
SKIP = "skip"
42+
SKIP State = "SKIP"
43+
// MANU for when a check is manual.
44+
MANU State = "MANU"
45+
// ERRO for errors in tests.
46+
ERRO State = "ERRO"
4447

4548
// MASTER a master node
4649
MASTER NodeType = "master"
@@ -58,8 +61,11 @@ const (
5861
// MANAGEDSERVICES a node to run managedservices from
5962
MANAGEDSERVICES = "managedservices"
6063

61-
// MANUAL Check Type
62-
MANUAL string = "manual"
64+
// TypeSkip is skip check type.
65+
TypeSkip = "skip"
66+
67+
// TypeManual is manual check type.
68+
TypeManual = "manual"
6369
)
6470

6571
// Check contains information about a recommendation in the
@@ -118,18 +124,18 @@ func (c *Check) run() State {
118124
return c.State
119125
}
120126

121-
// If check type is skip, force result to INFO
122-
if c.Type == SKIP {
127+
// If check type is skip, force result to SKIP
128+
if c.Type == TypeSkip {
123129
c.Reason = "Test marked as skip"
124-
c.State = INFO
130+
c.State = SKIP
125131
glog.V(3).Info(c.Reason)
126132
return c.State
127133
}
128134

129-
// If check type is manual force result to WARN
130-
if c.Type == MANUAL {
135+
// If check type is manual, force result to MANU
136+
if c.Type == TypeManual {
131137
c.Reason = "Test marked as a manual test"
132-
c.State = WARN
138+
c.State = MANU
133139
glog.V(3).Info(c.Reason)
134140
return c.State
135141
}
@@ -172,11 +178,7 @@ func (c *Check) run() State {
172178

173179
if err != nil {
174180
c.Reason = err.Error()
175-
if c.Scored {
176-
c.State = FAIL
177-
} else {
178-
c.State = WARN
179-
}
181+
c.State = ERRO
180182
glog.V(3).Info(c.Reason)
181183
}
182184

check/check_test.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,27 @@ func TestCheck_Run(t *testing.T) {
2727
}
2828

2929
testCases := []TestCase{
30-
{name: "Manual check should WARN", check: Check{Type: MANUAL}, Expected: WARN},
31-
{name: "Skip check should INFO", check: Check{Type: "skip"}, Expected: INFO},
32-
{name: "Unscored check (with no type) should WARN on failure", check: Check{Scored: false}, Expected: WARN},
30+
{
31+
name: "Manual check should MANU",
32+
check: Check{
33+
Type: TypeManual,
34+
},
35+
Expected: MANU,
36+
},
37+
{
38+
name: "Skip check should SKIP",
39+
check: Check{
40+
Type: TypeSkip,
41+
},
42+
Expected: SKIP,
43+
},
44+
{
45+
name: "Unscored check (with no type) should WARN on failure",
46+
check: Check{
47+
Scored: false,
48+
},
49+
Expected: WARN,
50+
},
3351
{
3452
name: "Unscored check that pass should PASS",
3553
check: Check{
@@ -42,9 +60,21 @@ func TestCheck_Run(t *testing.T) {
4260
},
4361
Expected: PASS,
4462
},
45-
46-
{name: "Check with no tests should WARN", check: Check{Scored: true}, Expected: WARN},
47-
{name: "Scored check with empty tests should FAIL", check: Check{Scored: true, Tests: &tests{}}, Expected: FAIL},
63+
{
64+
name: "Check with no tests should WARN",
65+
check: Check{
66+
Scored: true,
67+
},
68+
Expected: WARN,
69+
},
70+
{
71+
name: "Scored check with empty tests should FAIL",
72+
check: Check{
73+
Scored: true,
74+
Tests: &tests{},
75+
},
76+
Expected: FAIL,
77+
},
4878
{
4979
name: "Scored check that doesn't pass should FAIL",
5080
check: Check{

check/controls.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type Group struct {
6464
Fail int `json:"fail"`
6565
Warn int `json:"warn"`
6666
Info int `json:"info"`
67+
Skip int `json:"skip"`
68+
Manu int `json:"manu"`
69+
Erro int `json:"erro"`
6770
Text string `json:"desc"`
6871
Checks []*Check `json:"results"`
6972
}
@@ -74,6 +77,9 @@ type Summary struct {
7477
Fail int `json:"total_fail"`
7578
Warn int `json:"total_warn"`
7679
Info int `json:"total_info"`
80+
Skip int `json:"total_skip"`
81+
Manu int `json:"total_manu"`
82+
Erro int `json:"total_erro"`
7783
}
7884

7985
// Predicate a predicate on the given Group and Check arguments.
@@ -99,7 +105,7 @@ func NewControls(t NodeType, in []byte, detectedVersion string) (*Controls, erro
99105
func (controls *Controls) RunChecks(runner Runner, filter Predicate, skipIDMap map[string]bool) Summary {
100106
var g []*Group
101107
m := make(map[string]*Group)
102-
controls.Summary.Pass, controls.Summary.Fail, controls.Summary.Warn, controls.Info = 0, 0, 0, 0
108+
controls.Summary = Summary{}
103109

104110
for _, group := range controls.Groups {
105111
for _, check := range group.Checks {
@@ -111,8 +117,8 @@ func (controls *Controls) RunChecks(runner Runner, filter Predicate, skipIDMap m
111117
_, groupSkippedViaCmd := skipIDMap[group.ID]
112118
_, checkSkippedViaCmd := skipIDMap[check.ID]
113119

114-
if group.Type == SKIP || groupSkippedViaCmd || checkSkippedViaCmd {
115-
check.Type = SKIP
120+
if group.Type == TypeSkip || groupSkippedViaCmd || checkSkippedViaCmd {
121+
check.Type = TypeSkip
116122
}
117123

118124
state := runner.Run(check)
@@ -158,8 +164,14 @@ func (controls *Controls) JUnit() ([]byte, error) {
158164
suite := reporters.JUnitTestSuite{
159165
Name: controls.Text,
160166
TestCases: []reporters.JUnitTestCase{},
161-
Tests: controls.Summary.Pass + controls.Summary.Fail + controls.Summary.Info + controls.Summary.Warn,
162-
Failures: controls.Summary.Fail,
167+
Tests: controls.Summary.Pass +
168+
controls.Summary.Fail +
169+
controls.Summary.Info +
170+
controls.Summary.Warn +
171+
controls.Summary.Skip +
172+
controls.Summary.Manu +
173+
controls.Summary.Erro,
174+
Failures: controls.Summary.Fail,
163175
}
164176
for _, g := range controls.Groups {
165177
for _, check := range g.Checks {
@@ -179,11 +191,10 @@ func (controls *Controls) JUnit() ([]byte, error) {
179191
}
180192

181193
switch check.State {
182-
case FAIL:
194+
case FAIL, ERRO:
183195
tc.FailureMessage = &reporters.JUnitFailureMessage{Message: check.Remediation}
184-
case WARN, INFO:
185-
// WARN and INFO are two different versions of skipped tests. Either way it would be a false positive/negative to report
186-
// it any other way.
196+
case WARN, INFO, SKIP, MANU:
197+
// Different versions of skipped tests. It would be a false positive/negative to report it any other way.
187198
tc.Skipped = &reporters.JUnitSkipped{}
188199
case PASS:
189200
default:
@@ -226,7 +237,7 @@ func (controls *Controls) ASFF() ([]*securityhub.AwsSecurityFinding, error) {
226237
tf := ti.Format(time.RFC3339)
227238
for _, g := range controls.Groups {
228239
for _, check := range g.Checks {
229-
if check.State == FAIL || check.State == WARN {
240+
if check.State == FAIL || check.State == WARN || check.State == MANU || check.State == ERRO {
230241
// ASFF ProductFields['Actual result'] can't be longer than 1024 characters
231242
actualValue := check.ActualValue
232243
remediation := check.Remediation
@@ -292,6 +303,7 @@ func getConfig(name string) (string, error) {
292303
}
293304
return r, nil
294305
}
306+
295307
func summarize(controls *Controls, state State) {
296308
switch state {
297309
case PASS:
@@ -302,6 +314,12 @@ func summarize(controls *Controls, state State) {
302314
controls.Summary.Warn++
303315
case INFO:
304316
controls.Summary.Info++
317+
case SKIP:
318+
controls.Summary.Skip++
319+
case MANU:
320+
controls.Summary.Manu++
321+
case ERRO:
322+
controls.Summary.Erro++
305323
default:
306324
glog.Warningf("Unrecognized state %s", state)
307325
}
@@ -317,6 +335,12 @@ func summarizeGroup(group *Group, state State) {
317335
group.Warn++
318336
case INFO:
319337
group.Info++
338+
case SKIP:
339+
group.Skip++
340+
case MANU:
341+
group.Manu++
342+
case ERRO:
343+
group.Erro++
320344
default:
321345
glog.Warningf("Unrecognized state %s", state)
322346
}

check/controls_test.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ groups:
132132
controls.RunChecks(normalRunner, allChecks, skipMap)
133133

134134
G1 := controls.Groups[0]
135-
assertEqualGroupSummary(t, 0, 0, 3, 0, G1)
135+
assertEqualGroupSummary(t, 0, 0, 0, 0, 3, 0, 0, G1)
136136

137137
G2 := controls.Groups[1]
138-
assertEqualGroupSummary(t, 0, 0, 2, 0, G2)
138+
assertEqualGroupSummary(t, 0, 0, 0, 0, 2, 0, 0, G2)
139139
})
140140
}
141141

@@ -163,7 +163,7 @@ groups:
163163
controls.RunChecks(normalRunner, allChecks, emptySkipList)
164164

165165
G1 := controls.Groups[0]
166-
assertEqualGroupSummary(t, 0, 0, 1, 0, G1)
166+
assertEqualGroupSummary(t, 0, 0, 0, 0, 1, 0, 0, G1)
167167
})
168168
}
169169

@@ -214,7 +214,7 @@ groups:
214214
G1 := controls.Groups[0]
215215
assert.Equal(t, "G1", G1.ID)
216216
assert.Equal(t, "G1/C1", G1.Checks[0].ID)
217-
assertEqualGroupSummary(t, 1, 0, 0, 0, G1)
217+
assertEqualGroupSummary(t, 1, 0, 0, 0, 0, 0, 0, G1)
218218
// and
219219
G2 := controls.Groups[1]
220220
assert.Equal(t, "G2", G2.ID)
@@ -225,12 +225,15 @@ groups:
225225
assert.Equal(t, "SomeSampleFlag=true", G2.Checks[0].Tests.TestItems[0].Flag)
226226
assert.Equal(t, "Edit the config file /this/is/a/file/path and set SomeSampleFlag to true.\n", G2.Checks[0].Remediation)
227227
assert.Equal(t, true, G2.Checks[0].Scored)
228-
assertEqualGroupSummary(t, 0, 1, 0, 0, G2)
228+
assertEqualGroupSummary(t, 0, 1, 0, 0, 0, 0, 0, G2)
229229
// and
230230
assert.Equal(t, 1, controls.Summary.Pass)
231231
assert.Equal(t, 1, controls.Summary.Fail)
232232
assert.Equal(t, 0, controls.Summary.Info)
233233
assert.Equal(t, 0, controls.Summary.Warn)
234+
assert.Equal(t, 0, controls.Summary.Skip)
235+
assert.Equal(t, 0, controls.Summary.Manu)
236+
assert.Equal(t, 0, controls.Summary.Erro)
234237
// and
235238
runner.AssertExpectations(t)
236239
})
@@ -267,6 +270,9 @@ func TestControls_JUnitIncludesJSON(t *testing.T) {
267270
Pass: 100,
268271
Warn: 101,
269272
Info: 102,
273+
Skip: 0,
274+
Manu: 0,
275+
Erro: 0,
270276
},
271277
Groups: []*Group{
272278
{
@@ -283,7 +289,7 @@ func TestControls_JUnitIncludesJSON(t *testing.T) {
283289
</testcase>
284290
</testsuite>`),
285291
}, {
286-
desc: "Warn and Info are considered skips and failed tests properly reported",
292+
desc: "WARN, INFO, SKIP, MANU are considered skips and failed tests properly reported",
287293
input: &Controls{
288294
Groups: []*Group{
289295
{
@@ -293,6 +299,9 @@ func TestControls_JUnitIncludesJSON(t *testing.T) {
293299
{ID: "check2id", Text: "check2text", State: INFO},
294300
{ID: "check3id", Text: "check3text", State: WARN},
295301
{ID: "check4id", Text: "check4text", State: FAIL},
302+
{ID: "check5id", Text: "check5text", State: SKIP},
303+
{ID: "check6id", Text: "check6text", State: MANU},
304+
{ID: "check7id", Text: "check7text", State: ERRO},
296305
},
297306
},
298307
},
@@ -313,6 +322,18 @@ func TestControls_JUnitIncludesJSON(t *testing.T) {
313322
<failure type=""></failure>
314323
<system-out>{&#34;test_number&#34;:&#34;check4id&#34;,&#34;test_desc&#34;:&#34;check4text&#34;,&#34;audit&#34;:&#34;&#34;,&#34;AuditEnv&#34;:&#34;&#34;,&#34;AuditConfig&#34;:&#34;&#34;,&#34;type&#34;:&#34;&#34;,&#34;remediation&#34;:&#34;&#34;,&#34;test_info&#34;:null,&#34;status&#34;:&#34;FAIL&#34;,&#34;actual_value&#34;:&#34;&#34;,&#34;scored&#34;:false,&#34;IsMultiple&#34;:false,&#34;expected_result&#34;:&#34;&#34;}</system-out>
315324
</testcase>
325+
<testcase name="check5id check5text" classname="" time="0">
326+
<skipped></skipped>
327+
<system-out>{&#34;test_number&#34;:&#34;check5id&#34;,&#34;test_desc&#34;:&#34;check5text&#34;,&#34;audit&#34;:&#34;&#34;,&#34;AuditEnv&#34;:&#34;&#34;,&#34;AuditConfig&#34;:&#34;&#34;,&#34;type&#34;:&#34;&#34;,&#34;remediation&#34;:&#34;&#34;,&#34;test_info&#34;:null,&#34;status&#34;:&#34;SKIP&#34;,&#34;actual_value&#34;:&#34;&#34;,&#34;scored&#34;:false,&#34;IsMultiple&#34;:false,&#34;expected_result&#34;:&#34;&#34;}</system-out>
328+
</testcase>
329+
<testcase name="check6id check6text" classname="" time="0">
330+
<skipped></skipped>
331+
<system-out>{&#34;test_number&#34;:&#34;check6id&#34;,&#34;test_desc&#34;:&#34;check6text&#34;,&#34;audit&#34;:&#34;&#34;,&#34;AuditEnv&#34;:&#34;&#34;,&#34;AuditConfig&#34;:&#34;&#34;,&#34;type&#34;:&#34;&#34;,&#34;remediation&#34;:&#34;&#34;,&#34;test_info&#34;:null,&#34;status&#34;:&#34;MANU&#34;,&#34;actual_value&#34;:&#34;&#34;,&#34;scored&#34;:false,&#34;IsMultiple&#34;:false,&#34;expected_result&#34;:&#34;&#34;}</system-out>
332+
</testcase>
333+
<testcase name="check7id check7text" classname="" time="0">
334+
<failure type=""></failure>
335+
<system-out>{&#34;test_number&#34;:&#34;check7id&#34;,&#34;test_desc&#34;:&#34;check7text&#34;,&#34;audit&#34;:&#34;&#34;,&#34;AuditEnv&#34;:&#34;&#34;,&#34;AuditConfig&#34;:&#34;&#34;,&#34;type&#34;:&#34;&#34;,&#34;remediation&#34;:&#34;&#34;,&#34;test_info&#34;:null,&#34;status&#34;:&#34;ERRO&#34;,&#34;actual_value&#34;:&#34;&#34;,&#34;scored&#34;:false,&#34;IsMultiple&#34;:false,&#34;expected_result&#34;:&#34;&#34;}</system-out>
336+
</testcase>
316337
</testsuite>`),
317338
},
318339
}
@@ -355,12 +376,15 @@ func TestControls_JUnitIncludesJSON(t *testing.T) {
355376
}
356377
}
357378

358-
func assertEqualGroupSummary(t *testing.T, pass, fail, info, warn int, actual *Group) {
379+
func assertEqualGroupSummary(t *testing.T, pass, fail, info, warn, skip, manu, erro int, actual *Group) {
359380
t.Helper()
360381
assert.Equal(t, pass, actual.Pass)
361382
assert.Equal(t, fail, actual.Fail)
362383
assert.Equal(t, info, actual.Info)
363384
assert.Equal(t, warn, actual.Warn)
385+
assert.Equal(t, skip, actual.Skip)
386+
assert.Equal(t, manu, actual.Manu)
387+
assert.Equal(t, erro, actual.Erro)
364388
}
365389

366390
func TestControls_ASFF(t *testing.T) {
@@ -388,6 +412,9 @@ func TestControls_ASFF(t *testing.T) {
388412
Pass: 100,
389413
Warn: 101,
390414
Info: 102,
415+
Skip: 0,
416+
Manu: 0,
417+
Erro: 0,
391418
},
392419
Groups: []*Group{
393420
{

0 commit comments

Comments
 (0)