2022-09-07 16:01:42 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
v1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2023-01-30 12:41:09 +01:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2022-09-07 16:01:42 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetWarningMessages(t *testing.T) {
|
|
|
|
type args struct {
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses []*engineapi.EngineResponse
|
2022-09-07 16:01:42 +02:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
}{{
|
|
|
|
name: "nil response",
|
|
|
|
args: args{nil},
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "enmpty response",
|
2023-01-30 12:41:09 +01:00
|
|
|
args: args{[]*engineapi.EngineResponse{}},
|
2022-09-07 16:01:42 +02:00
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "warning",
|
2023-01-30 12:41:09 +01:00
|
|
|
args: args{[]*engineapi.EngineResponse{
|
2022-09-07 16:01:42 +02:00
|
|
|
{
|
|
|
|
Policy: &v1.ClusterPolicy{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
},
|
|
|
|
},
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-07 16:01:42 +02:00
|
|
|
{
|
|
|
|
Name: "rule",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusWarn,
|
2022-09-07 16:01:42 +02:00
|
|
|
Message: "message warn",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
want: []string{
|
|
|
|
"policy test.rule: message warn",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "multiple rules",
|
2023-01-30 12:41:09 +01:00
|
|
|
args: args{[]*engineapi.EngineResponse{
|
2022-09-07 16:01:42 +02:00
|
|
|
{
|
|
|
|
Policy: &v1.ClusterPolicy{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
},
|
|
|
|
},
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-07 16:01:42 +02:00
|
|
|
{
|
|
|
|
Name: "rule-pass",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusPass,
|
2022-09-07 16:01:42 +02:00
|
|
|
Message: "message pass",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rule-warn",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusWarn,
|
2022-09-07 16:01:42 +02:00
|
|
|
Message: "message warn",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rule-fail",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusFail,
|
2022-09-07 16:01:42 +02:00
|
|
|
Message: "message fail",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rule-error",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusError,
|
2022-09-07 16:01:42 +02:00
|
|
|
Message: "message error",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rule-skip",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusSkip,
|
2022-09-07 16:01:42 +02:00
|
|
|
Message: "message skip",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
want: []string{
|
|
|
|
"policy test.rule-warn: message warn",
|
|
|
|
"policy test.rule-fail: message fail",
|
|
|
|
"policy test.rule-error: message error",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := GetWarningMessages(tt.args.engineResponses)
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|