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"
|
2023-02-10 09:11:21 +01:00
|
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-02-10 15:04:41 +01:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2022-09-08 10:36:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2023-02-13 13:27:40 +01:00
|
|
|
auditPolicy := &kyvernov1.ClusterPolicy{
|
2023-02-10 15:04:41 +01:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
},
|
2023-02-13 13:27:40 +01:00
|
|
|
Spec: kyvernov1.Spec{
|
|
|
|
ValidationFailureAction: kyvernov1.Audit,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
enforcePolicy := &kyvernov1.ClusterPolicy{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
},
|
|
|
|
Spec: kyvernov1.Spec{
|
|
|
|
ValidationFailureAction: kyvernov1.Enforce,
|
|
|
|
},
|
2023-02-10 15:04:41 +01:00
|
|
|
}
|
|
|
|
resource := unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "foo",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"namespace": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-08 10:36:31 +02:00
|
|
|
type args struct {
|
2023-03-23 13:58:52 +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-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, enforcePolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.RuleFail("rule-fail", engineapi.Validation, "message fail"),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Fail,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "failure - audit",
|
|
|
|
args: args{
|
2023-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, auditPolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.RuleFail("rule-fail", engineapi.Validation, "message fail"),
|
2023-02-10 09:11:21 +01:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Fail,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "error - fail",
|
|
|
|
args: args{
|
2023-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, auditPolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.RuleError("rule-error", engineapi.Validation, "message error", nil),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Fail,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
name: "error - ignore",
|
|
|
|
args: args{
|
2023-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, auditPolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.RuleError("rule-error", engineapi.Validation, "message error", nil),
|
2023-02-10 09:11:21 +01:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Ignore,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "warning - ignore",
|
|
|
|
args: args{
|
2023-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, auditPolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.NewRuleResponse("rule-warning", engineapi.Validation, "message warning", engineapi.RuleStatusWarn),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
failurePolicy: kyvernov1.Ignore,
|
|
|
|
log: logr.Discard(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
name: "warning - fail",
|
|
|
|
args: args{
|
2023-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, auditPolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.NewRuleResponse("rule-warning", engineapi.Validation, "message warning", engineapi.RuleStatusWarn),
|
2023-02-10 09:11:21 +01:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
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) {
|
2023-02-13 13:27:40 +01:00
|
|
|
enforcePolicy := &kyvernov1.ClusterPolicy{
|
2023-02-10 15:04:41 +01:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
},
|
2023-02-13 13:27:40 +01:00
|
|
|
Spec: kyvernov1.Spec{
|
|
|
|
ValidationFailureAction: kyvernov1.Enforce,
|
|
|
|
},
|
2023-02-10 15:04:41 +01:00
|
|
|
}
|
|
|
|
resource := unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "foo",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"namespace": "bar",
|
|
|
|
"name": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-08 10:36:31 +02:00
|
|
|
type args struct {
|
2023-03-23 13:58:52 +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-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, enforcePolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.RuleFail("rule-fail", engineapi.Validation, "message fail"),
|
2023-02-10 09:11:21 +01:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "\n\npolicy foo/bar/baz for resource violation: \n\ntest:\n rule-fail: message fail\n",
|
|
|
|
}, {
|
|
|
|
name: "error - enforce",
|
|
|
|
args: args{
|
2023-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, enforcePolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.RuleError("rule-error", engineapi.Validation, "message error", nil),
|
2023-02-10 09:11:21 +01:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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-03-23 13:58:52 +01:00
|
|
|
engineResponses: []engineapi.EngineResponse{
|
2023-04-05 19:07:04 +02:00
|
|
|
engineapi.NewEngineResponse(resource, enforcePolicy, nil).WithPolicyResponse(engineapi.PolicyResponse{
|
2023-02-10 15:04:41 +01:00
|
|
|
Rules: []engineapi.RuleResponse{
|
2023-04-05 12:35:38 +02:00
|
|
|
*engineapi.RuleFail("rule-fail", engineapi.Validation, "message fail"),
|
|
|
|
*engineapi.RuleError("rule-error", engineapi.Validation, "message error", nil),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
2023-04-05 19:07:04 +02:00
|
|
|
}),
|
2022-09-08 10:36:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|