mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2024-12-03 05:18:12 +00:00
Do not log lines whose check.State is in --status
This commit is contained in:
parent
0feaa5d75f
commit
fe50154f17
@ -157,8 +157,29 @@ func parseSkipIds(skipIds string) map[string]bool {
|
||||
return skipIdMap
|
||||
}
|
||||
|
||||
func parseStatus(statusList string) map[check.State]bool {
|
||||
var statusMap = make(map[check.State]bool, 0)
|
||||
if statusList != "" {
|
||||
for _, status := range strings.Split(statusList, ",") {
|
||||
statusMap[check.State(strings.ToUpper(strings.Trim(status, " ")))] = true
|
||||
}
|
||||
}
|
||||
return statusMap
|
||||
}
|
||||
|
||||
func printStatus(state check.State) bool {
|
||||
if statusList == "" {
|
||||
return true
|
||||
}
|
||||
statusMap := parseStatus(statusList)
|
||||
return statusMap[state]
|
||||
}
|
||||
|
||||
// colorPrint outputs the state in a specific colour, along with a message string
|
||||
func colorPrint(state check.State, s string) {
|
||||
if !printStatus(state) {
|
||||
return
|
||||
}
|
||||
colors[state].Printf("[%s] ", state)
|
||||
fmt.Printf("%s", s)
|
||||
}
|
||||
|
@ -750,6 +750,55 @@ func TestWriteStdoutOutputTotal(t *testing.T) {
|
||||
assert.Contains(t, string(out), "49 checks PASS")
|
||||
}
|
||||
|
||||
func TestWriteStdoutOutputStatusList(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
statusList string
|
||||
|
||||
notContains []string
|
||||
}
|
||||
testCases := []testCase{
|
||||
{
|
||||
name: "statusList PASS",
|
||||
statusList: "PASS",
|
||||
notContains: []string{"INFO", "WARN", "ERRO"},
|
||||
},
|
||||
{
|
||||
name: "statusList PASS,INFO",
|
||||
statusList: "PASS,INFO",
|
||||
notContains: []string{"WARN", "ERRO"},
|
||||
},
|
||||
{
|
||||
name: "statusList empty",
|
||||
statusList: "",
|
||||
notContains: nil,
|
||||
},
|
||||
}
|
||||
|
||||
controlsCollection, err := parseControlsJsonFile("./testdata/controlsCollection.json")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
for _, tt := range testCases {
|
||||
rescueStdout := os.Stdout
|
||||
|
||||
r, w, _ := os.Pipe()
|
||||
|
||||
os.Stdout = w
|
||||
statusList = tt.statusList
|
||||
writeStdoutOutput(controlsCollection)
|
||||
w.Close()
|
||||
out, _ := ioutil.ReadAll(r)
|
||||
|
||||
os.Stdout = rescueStdout
|
||||
|
||||
for _, n := range tt.notContains {
|
||||
assert.NotContains(t, string(out), fmt.Sprintf("[%s]", n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseControlsJsonFile(filepath string) ([]*check.Controls, error) {
|
||||
var result []*check.Controls
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user