2020-10-16 16:27:04 -07:00
package apply
import (
2021-09-02 02:24:04 +05:30
"encoding/json"
2020-10-16 16:27:04 -07:00
"testing"
2021-10-29 18:13:20 +02:00
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
preport "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
2022-04-14 17:50:18 +05:30
kyvCommon "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
2021-09-02 02:24:04 +05:30
"github.com/kyverno/kyverno/pkg/engine/response"
"github.com/kyverno/kyverno/pkg/policyreport"
2020-10-16 16:27:04 -07:00
"gotest.tools/assert"
v1 "k8s.io/api/core/v1"
)
2021-09-02 02:24:04 +05:30
var rawPolicy = [ ] byte ( `
{
"apiVersion" : "kyverno.io/v1" ,
"kind" : "ClusterPolicy" ,
"metadata" : {
"name" : "pod-requirements" ,
"annotations" : {
"pod-policies.kyverno.io/autogen-controllers" : "none"
}
} ,
"spec" : {
"background" : false ,
"validationFailureAction" : "audit" ,
"rules" : [
{
"name" : "pods-require-account" ,
"match" : {
"resources" : {
"kinds" : [
"Pod"
]
}
} ,
"validate" : {
"message" : "User pods must include an account for charging" ,
"pattern" : {
"metadata" : {
"labels" : {
"account" : "*?"
}
}
}
}
} ,
{
"name" : "pods-require-limits" ,
"match" : {
"resources" : {
"kinds" : [
"Pod"
]
}
} ,
"validate" : {
"message" : "CPU and memory resource requests and limits are required for user pods" ,
"pattern" : {
"spec" : {
"containers" : [
{
"resources" : {
"requests" : {
"memory" : "?*" ,
"cpu" : "?*"
} ,
"limits" : {
"memory" : "?*" ,
"cpu" : "?*"
}
}
}
]
}
}
}
}
]
}
}
` )
2021-09-30 00:04:13 -07:00
var rawEngRes = [ ] byte ( ` { "PatchedResource": { "apiVersion":"v1","kind":"Pod","metadata": { "name":"nginx1","namespace":"default"},"spec": { "containers":[ { "image":"nginx","imagePullPolicy":"IfNotPresent","name":"nginx","resources": { "limits": { "cpu":"200m","memory":"100Mi"},"requests": { "cpu":"100m","memory":"50Mi"}}}]}},"PolicyResponse": { "policy": { "name":"pod-requirements","namespace":""},"resource": { "kind":"Pod","apiVersion":"v1","namespace":"default","name":"nginx1","uid":""},"processingTime":974958,"rulesAppliedCount":2,"policyExecutionTimestamp":1630527712,"rules":[ { "name":"pods-require-account","type":"Validation","message":"validation error: User pods must include an account for charging. Rule pods-require-account failed at path /metadata/labels/","status":"fail","processingTime":28833,"ruleExecutionTimestamp":1630527712}, { "name":"pods-require-limits","type":"Validation","message":"validation rule 'pods-require-limits' passed.","status":"pass","processingTime":578625,"ruleExecutionTimestamp":1630527712}],"ValidationFailureAction":"audit"}} ` )
2021-09-02 02:24:04 +05:30
func Test_buildPolicyReports ( t * testing . T ) {
rc := & kyvCommon . ResultCounts { }
var pvInfos [ ] policyreport . Info
var policy kyverno . ClusterPolicy
err := json . Unmarshal ( rawPolicy , & policy )
assert . NilError ( t , err )
var er response . EngineResponse
err = json . Unmarshal ( rawEngRes , & er )
assert . NilError ( t , err )
2021-09-03 12:32:12 +05:30
info := kyvCommon . ProcessValidateEngineResponse ( & policy , & er , "" , rc , true )
2021-09-02 02:24:04 +05:30
pvInfos = append ( pvInfos , info )
reports := buildPolicyReports ( pvInfos )
assert . Assert ( t , len ( reports ) == 1 , len ( reports ) )
for _ , report := range reports {
if report . GetNamespace ( ) == "" {
assert . Assert ( t , report . GetName ( ) == clusterpolicyreport )
assert . Assert ( t , report . GetKind ( ) == "ClusterPolicyReport" )
assert . Assert ( t , len ( report . UnstructuredContent ( ) [ "results" ] . ( [ ] interface { } ) ) == 2 )
assert . Assert ( t ,
report . UnstructuredContent ( ) [ "summary" ] . ( map [ string ] interface { } ) [ preport . StatusPass ] . ( int64 ) == 1 ,
report . UnstructuredContent ( ) [ "summary" ] . ( map [ string ] interface { } ) [ preport . StatusPass ] . ( int64 ) )
} else {
assert . Assert ( t , report . GetName ( ) == "policyreport-ns-default" )
assert . Assert ( t , report . GetKind ( ) == "PolicyReport" )
assert . Assert ( t , len ( report . UnstructuredContent ( ) [ "results" ] . ( [ ] interface { } ) ) == 2 )
2021-09-26 21:15:13 -07:00
summary := report . UnstructuredContent ( ) [ "summary" ] . ( map [ string ] interface { } )
assert . Assert ( t , summary [ preport . StatusPass ] . ( int64 ) == 1 , summary [ preport . StatusPass ] . ( int64 ) )
2021-09-02 02:24:04 +05:30
}
}
}
func Test_buildPolicyResults ( t * testing . T ) {
rc := & kyvCommon . ResultCounts { }
var pvInfos [ ] policyreport . Info
var policy kyverno . ClusterPolicy
err := json . Unmarshal ( rawPolicy , & policy )
assert . NilError ( t , err )
var er response . EngineResponse
err = json . Unmarshal ( rawEngRes , & er )
assert . NilError ( t , err )
2021-09-03 12:32:12 +05:30
info := kyvCommon . ProcessValidateEngineResponse ( & policy , & er , "" , rc , true )
2021-09-02 02:24:04 +05:30
pvInfos = append ( pvInfos , info )
2021-09-02 02:43:07 +05:30
results := buildPolicyResults ( pvInfos )
for _ , result := range results {
assert . Assert ( t , len ( result ) == 2 , len ( result ) )
for _ , r := range result {
switch r . Rule {
case "pods-require-limits" :
2021-10-29 18:13:20 +02:00
assert . Assert ( t , r . Result == preport . StatusPass )
2021-09-02 02:43:07 +05:30
case "pods-require-account" :
2021-10-29 18:13:20 +02:00
assert . Assert ( t , r . Result == preport . StatusFail )
2021-09-02 02:43:07 +05:30
}
}
}
2021-09-02 02:24:04 +05:30
}
2020-10-16 16:27:04 -07:00
func Test_calculateSummary ( t * testing . T ) {
2022-04-28 11:11:14 +02:00
results := [ ] preport . PolicyReportResult {
2020-10-16 16:27:04 -07:00
{
2022-04-28 11:11:14 +02:00
Resources : make ( [ ] v1 . ObjectReference , 5 ) ,
2021-10-29 18:13:20 +02:00
Result : preport . PolicyResult ( preport . StatusPass ) ,
2020-10-16 16:27:04 -07:00
} ,
2021-10-29 18:13:20 +02:00
{ Result : preport . PolicyResult ( preport . StatusFail ) } ,
{ Result : preport . PolicyResult ( preport . StatusFail ) } ,
{ Result : preport . PolicyResult ( preport . StatusFail ) } ,
2020-10-16 16:27:04 -07:00
{
2022-04-28 11:11:14 +02:00
Resources : make ( [ ] v1 . ObjectReference , 1 ) ,
2021-10-29 18:13:20 +02:00
Result : preport . PolicyResult ( preport . StatusPass ) } ,
2020-10-16 16:27:04 -07:00
{
2022-04-28 11:11:14 +02:00
Resources : make ( [ ] v1 . ObjectReference , 4 ) ,
2021-10-29 18:13:20 +02:00
Result : preport . PolicyResult ( preport . StatusPass ) ,
2020-10-16 16:27:04 -07:00
} ,
}
summary := calculateSummary ( results )
2020-11-04 15:22:12 +05:30
assert . Assert ( t , summary . Pass == 3 )
2020-10-16 16:27:04 -07:00
assert . Assert ( t , summary . Fail == 3 )
}