1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/policyreport/policyreport_test.go
Marcus Noble 8690f8b142
Handle reports with missing result property (#2696)
* Handle reports with missing result property

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>

* Make use of type structs

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>

* Fix import

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>

* Fix cast from map to struct

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
2021-11-09 12:03:15 +01:00

70 lines
1.7 KiB
Go

package policyreport
import (
"testing"
report "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
)
var validReportStatuses = []string{"pass", "fail", "error", "skip", "warn"}
func TestUpdateSummary_Successful(t *testing.T) {
results := []report.PolicyReportResult{}
for _, status := range validReportStatuses {
results = append(results, report.PolicyReportResult{
Result: report.PolicyResult(status),
Policy: "TestUpdateSummary_Successful",
Source: "Kyverno",
})
}
summary := updateSummary(results)
if summary.Pass != 1 {
t.Errorf("Was expecting status pass to have a count of 1")
}
if summary.Error != 1 {
t.Errorf("Was expecting status error to have a count of 1")
}
if summary.Fail != 1 {
t.Errorf("Was expecting status fail to have a count of 1")
}
if summary.Skip != 1 {
t.Errorf("Was expecting status skip to have a count of 1")
}
if summary.Warn != 1 {
t.Errorf("Was expecting status warn to have a count of 1")
}
}
func TestUpdateSummary_MissingResultField(t *testing.T) {
results := []report.PolicyReportResult{
{
Policy: "TestUpdateSummary_MissingResultField",
Source: "Kyverno",
},
}
defer func() {
if r := recover(); r != nil {
t.Error("Function should not cause a panic")
}
}()
summary := updateSummary(results)
if summary.Pass != 0 {
t.Errorf("Was expecting status pass to have a count of 0")
}
if summary.Error != 0 {
t.Errorf("Was expecting status error to have a count of 0")
}
if summary.Fail != 0 {
t.Errorf("Was expecting status fail to have a count of 0")
}
if summary.Skip != 0 {
t.Errorf("Was expecting status skip to have a count of 0")
}
if summary.Warn != 0 {
t.Errorf("Was expecting status warn to have a count of 0")
}
}