2022-09-08 10:36:31 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
kyvernov1 "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-08 10:36:31 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_getAction(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
hasViolations bool
|
|
|
|
i int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
name: "violation",
|
|
|
|
args: args{true, 1},
|
|
|
|
want: "violation",
|
|
|
|
}, {
|
|
|
|
name: "violations",
|
|
|
|
args: args{true, 5},
|
|
|
|
want: "violations",
|
|
|
|
}, {
|
|
|
|
name: "error",
|
|
|
|
args: args{false, 1},
|
|
|
|
want: "error",
|
|
|
|
}, {
|
|
|
|
name: "errors",
|
|
|
|
args: args{false, 5},
|
|
|
|
want: "errors",
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := getAction(tt.args.hasViolations, tt.args.i)
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockRequest(t *testing.T) {
|
|
|
|
type args struct {
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses []*engineapi.EngineResponse
|
2022-09-08 10:36:31 +02:00
|
|
|
failurePolicy kyvernov1.FailurePolicyType
|
|
|
|
log logr.Logger
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
}{{
|
|
|
|
name: "failure - enforce",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Enforce",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-fail",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusFail,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message fail",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Fail,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "failure - audit",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Audit",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-fail",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusFail,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message fail",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Fail,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "error - fail",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Audit",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-error",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusError,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Fail,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "error - ignore",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Audit",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-error",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusError,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Ignore,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "warning - ignore",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Audit",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-warning",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusWarn,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message warning",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Ignore,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "warning - fail",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Audit",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-warning",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusWarn,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message warning",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Fail,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := BlockRequest(tt.args.engineResponses, tt.args.failurePolicy, tt.args.log)
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockedMessages(t *testing.T) {
|
|
|
|
type args struct {
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses []*engineapi.EngineResponse
|
2022-09-08 10:36:31 +02:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
name: "failure - enforce",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
|
|
|
Policy: engineapi.PolicySpec{
|
2022-09-08 10:36:31 +02:00
|
|
|
Name: "test",
|
|
|
|
},
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Enforce",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-fail",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusFail,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message fail",
|
|
|
|
},
|
|
|
|
},
|
2023-01-30 12:41:09 +01:00
|
|
|
Resource: engineapi.ResourceSpec{
|
2022-09-08 10:36:31 +02:00
|
|
|
Kind: "foo",
|
|
|
|
Namespace: "bar",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "\n\npolicy foo/bar/baz for resource violation: \n\ntest:\n rule-fail: message fail\n",
|
|
|
|
}, {
|
|
|
|
name: "error - enforce",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
|
|
|
Policy: engineapi.PolicySpec{
|
2022-09-08 10:36:31 +02:00
|
|
|
Name: "test",
|
|
|
|
},
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Enforce",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-error",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusError,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message error",
|
|
|
|
},
|
|
|
|
},
|
2023-01-30 12:41:09 +01:00
|
|
|
Resource: engineapi.ResourceSpec{
|
2022-09-08 10:36:31 +02:00
|
|
|
Kind: "foo",
|
|
|
|
Namespace: "bar",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "\n\npolicy foo/bar/baz for resource error: \n\ntest:\n rule-error: message error\n",
|
|
|
|
}, {
|
|
|
|
name: "error and failure - enforce",
|
|
|
|
args: args{
|
2023-01-30 12:41:09 +01:00
|
|
|
engineResponses: []*engineapi.EngineResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
2023-01-30 12:41:09 +01:00
|
|
|
PolicyResponse: engineapi.PolicyResponse{
|
|
|
|
Policy: engineapi.PolicySpec{
|
2022-09-08 10:36:31 +02:00
|
|
|
Name: "test",
|
|
|
|
},
|
2022-11-01 09:56:52 +00:00
|
|
|
ValidationFailureAction: "Enforce",
|
2023-01-30 12:41:09 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2022-09-08 10:36:31 +02:00
|
|
|
{
|
|
|
|
Name: "rule-fail",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusFail,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message fail",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rule-error",
|
2023-01-30 12:41:09 +01:00
|
|
|
Status: engineapi.RuleStatusError,
|
2022-09-08 10:36:31 +02:00
|
|
|
Message: "message error",
|
|
|
|
},
|
|
|
|
},
|
2023-01-30 12:41:09 +01:00
|
|
|
Resource: engineapi.ResourceSpec{
|
2022-09-08 10:36:31 +02:00
|
|
|
Kind: "foo",
|
|
|
|
Namespace: "bar",
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "\n\npolicy foo/bar/baz for resource violation: \n\ntest:\n rule-error: message error\n rule-fail: message fail\n",
|
|
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := GetBlockedMessages(tt.args.engineResponses)
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|