1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/webhooks/policyStatus_test.go

212 lines
6.5 KiB
Go
Raw Normal View History

2020-02-29 22:39:27 +05:30
package webhooks
2020-02-26 13:52:12 +05:30
import (
"encoding/json"
"reflect"
"testing"
"time"
v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/engine/response"
2020-02-26 13:52:12 +05:30
)
2020-02-29 22:39:27 +05:30
func Test_GenerateStats(t *testing.T) {
2020-02-26 13:52:12 +05:30
testCase := struct {
2020-12-23 15:10:07 -08:00
generateStats []*response.EngineResponse
2020-02-29 22:39:27 +05:30
expectedOutput []byte
2020-02-26 13:52:12 +05:30
}{
2020-02-29 22:39:27 +05:30
expectedOutput: []byte(`{"policy1":{"averageExecutionTime":"494ns","rulesFailedCount":1,"rulesAppliedCount":1,"ruleStatus":[{"ruleName":"rule5","averageExecutionTime":"243ns","appliedCount":1},{"ruleName":"rule6","averageExecutionTime":"251ns","failedCount":1}]},"policy2":{"averageExecutionTime":"433ns","rulesFailedCount":1,"rulesAppliedCount":1,"ruleStatus":[{"ruleName":"rule5","averageExecutionTime":"222ns","appliedCount":1},{"ruleName":"rule6","averageExecutionTime":"211ns","failedCount":1}]}}`),
2020-12-23 15:10:07 -08:00
generateStats: []*response.EngineResponse{
2020-02-26 13:52:12 +05:30
{
PolicyResponse: response.PolicyResponse{
Policy: "policy1",
Rules: []response.RuleResponse{
{
2020-02-29 22:39:27 +05:30
Name: "rule5",
2020-02-26 13:52:12 +05:30
Success: true,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 243,
},
},
{
2020-02-29 22:39:27 +05:30
Name: "rule6",
2020-02-26 13:52:12 +05:30
Success: false,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 251,
},
},
},
},
},
{
PolicyResponse: response.PolicyResponse{
Policy: "policy2",
Rules: []response.RuleResponse{
{
2020-02-29 22:39:27 +05:30
Name: "rule5",
2020-02-26 13:52:12 +05:30
Success: true,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 222,
},
},
{
2020-02-29 22:39:27 +05:30
Name: "rule6",
2020-02-26 13:52:12 +05:30
Success: false,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 211,
},
},
},
},
},
},
2020-02-29 22:39:27 +05:30
}
policyNameToStatus := map[string]v1.PolicyStatus{}
2020-02-29 22:39:27 +05:30
for _, generateStat := range testCase.generateStats {
receiver := generateStats{
2020-02-29 22:39:27 +05:30
resp: generateStat,
}
policyNameToStatus[receiver.PolicyName()] = receiver.UpdateStatus(policyNameToStatus[receiver.PolicyName()])
2020-02-29 22:39:27 +05:30
}
output, _ := json.Marshal(policyNameToStatus)
2020-02-29 22:39:27 +05:30
if !reflect.DeepEqual(output, testCase.expectedOutput) {
t.Errorf("\n\nTestcase has failed\nExpected:\n%v\nGot:\n%v\n\n", string(testCase.expectedOutput), string(output))
}
}
func Test_MutateStats(t *testing.T) {
testCase := struct {
2020-12-23 15:10:07 -08:00
mutateStats []*response.EngineResponse
2020-02-29 22:39:27 +05:30
expectedOutput []byte
}{
expectedOutput: []byte(`{"policy1":{"averageExecutionTime":"494ns","rulesFailedCount":1,"rulesAppliedCount":1,"resourcesMutatedCount":1,"ruleStatus":[{"ruleName":"rule1","averageExecutionTime":"243ns","appliedCount":1,"resourcesMutatedCount":1},{"ruleName":"rule2","averageExecutionTime":"251ns","failedCount":1}]},"policy2":{"averageExecutionTime":"433ns","rulesFailedCount":1,"rulesAppliedCount":1,"resourcesMutatedCount":1,"ruleStatus":[{"ruleName":"rule1","averageExecutionTime":"222ns","appliedCount":1,"resourcesMutatedCount":1},{"ruleName":"rule2","averageExecutionTime":"211ns","failedCount":1}]}}`),
2020-12-23 15:10:07 -08:00
mutateStats: []*response.EngineResponse{
2020-02-26 13:52:12 +05:30
{
PolicyResponse: response.PolicyResponse{
2020-02-29 22:39:27 +05:30
Policy: "policy1",
2020-02-26 13:52:12 +05:30
Rules: []response.RuleResponse{
{
2020-02-29 22:39:27 +05:30
Name: "rule1",
2020-02-26 13:52:12 +05:30
Success: true,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 243,
},
},
{
2020-02-29 22:39:27 +05:30
Name: "rule2",
2020-02-26 13:52:12 +05:30
Success: false,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 251,
},
},
},
},
},
{
PolicyResponse: response.PolicyResponse{
Policy: "policy2",
Rules: []response.RuleResponse{
{
2020-02-29 22:39:27 +05:30
Name: "rule1",
2020-02-26 13:52:12 +05:30
Success: true,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 222,
},
},
{
2020-02-29 22:39:27 +05:30
Name: "rule2",
2020-02-26 13:52:12 +05:30
Success: false,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 211,
},
},
},
},
},
},
2020-02-29 22:39:27 +05:30
}
policyNameToStatus := map[string]v1.PolicyStatus{}
2020-02-29 22:39:27 +05:30
for _, mutateStat := range testCase.mutateStats {
receiver := mutateStats{
2020-02-29 22:39:27 +05:30
resp: mutateStat,
}
policyNameToStatus[receiver.PolicyName()] = receiver.UpdateStatus(policyNameToStatus[receiver.PolicyName()])
2020-02-29 22:39:27 +05:30
}
output, _ := json.Marshal(policyNameToStatus)
2020-02-29 22:39:27 +05:30
if !reflect.DeepEqual(output, testCase.expectedOutput) {
t.Errorf("\n\nTestcase has failed\nExpected:\n%v\nGot:\n%v\n\n", string(testCase.expectedOutput), string(output))
}
}
func Test_ValidateStats(t *testing.T) {
testCase := struct {
2020-12-23 15:10:07 -08:00
validateStats []*response.EngineResponse
2020-02-29 22:39:27 +05:30
expectedOutput []byte
}{
expectedOutput: []byte(`{"policy1":{"averageExecutionTime":"494ns","rulesFailedCount":1,"rulesAppliedCount":1,"resourcesBlockedCount":1,"ruleStatus":[{"ruleName":"rule3","averageExecutionTime":"243ns","appliedCount":1},{"ruleName":"rule4","averageExecutionTime":"251ns","failedCount":1,"resourcesBlockedCount":1}]},"policy2":{"averageExecutionTime":"433ns","rulesFailedCount":1,"rulesAppliedCount":1,"ruleStatus":[{"ruleName":"rule3","averageExecutionTime":"222ns","appliedCount":1},{"ruleName":"rule4","averageExecutionTime":"211ns","failedCount":1}]}}`),
2020-12-23 15:10:07 -08:00
validateStats: []*response.EngineResponse{
2020-02-26 13:52:12 +05:30
{
PolicyResponse: response.PolicyResponse{
2020-02-29 22:39:27 +05:30
Policy: "policy1",
ValidationFailureAction: "enforce",
2020-02-26 13:52:12 +05:30
Rules: []response.RuleResponse{
{
2020-02-29 22:39:27 +05:30
Name: "rule3",
2020-02-26 13:52:12 +05:30
Success: true,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 243,
},
},
{
2020-02-29 22:39:27 +05:30
Name: "rule4",
2020-02-26 13:52:12 +05:30
Success: false,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 251,
},
},
},
},
},
{
PolicyResponse: response.PolicyResponse{
Policy: "policy2",
Rules: []response.RuleResponse{
{
2020-02-29 22:39:27 +05:30
Name: "rule3",
2020-02-26 13:52:12 +05:30
Success: true,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 222,
},
},
{
2020-02-29 22:39:27 +05:30
Name: "rule4",
2020-02-26 13:52:12 +05:30
Success: false,
RuleStats: response.RuleStats{
ProcessingTime: time.Nanosecond * 211,
},
},
},
},
},
},
}
policyNameToStatus := map[string]v1.PolicyStatus{}
2020-02-26 13:52:12 +05:30
for _, validateStat := range testCase.validateStats {
receiver := validateStats{
2020-02-26 13:52:12 +05:30
resp: validateStat,
}
policyNameToStatus[receiver.PolicyName()] = receiver.UpdateStatus(policyNameToStatus[receiver.PolicyName()])
2020-02-26 13:52:12 +05:30
}
output, _ := json.Marshal(policyNameToStatus)
2020-02-26 13:52:12 +05:30
if !reflect.DeepEqual(output, testCase.expectedOutput) {
t.Errorf("\n\nTestcase has failed\nExpected:\n%v\nGot:\n%v\n\n", string(testCase.expectedOutput), string(output))
}
}