1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-10 09:56:55 +00:00
kyverno/pkg/kyverno/apply/generate_test.go
shuting 5e07ecc5f3
Add Policy Report (#1229)
* add report in cli

* policy report crd added

* policy report added

* configmap added

* added jobs

* added jobs

* bug fixed

* added logic for cli

* common function added

* sub command added for policy report

* subcommand added for report

* common package changed

* configmap added

* added logic for kyverno cli

* added logic for jobs

* added logic for jobs

* added logic for jobs

* added logic for cli

* buf fix

* cli changes

* count bug fix

* docs added for command

* go fmt

* refactor codebase

* remove policy controller for policyreport

* policy report removed

* bug fixes

* bug fixes

* added job trigger if needed

* job deletation logic added

* build failed fix

* fixed e2e test

* remove hard coded variables

* packages adde

* improvment added in jobs sheduler

* policy report yaml added

* cronjob added

* small fixes

* remove background sync

* documentation added for report command

* remove extra log

* small improvement

* tested policy report

* revert hardcoded changes

* changes for demo

* demo changes

* resource aggrigation added

* More changes

* More changes

* - resolve PR comments; - refactor jobs controller

* set rbac for jobs

* add clean up in job controller

* add short names

* remove application scope for policyreport

* move job controller to policyreport

* add report logic in command apply

* - update policy report types;  - upgrade k8s library; - update code gen

* temporarily comment out code to pass CI build

* generate / update policyreport to cluster

* add unit test for CLI report

* add test for apply - generate policy report

* fix unit test

* - remove job controller; - remove in-memory configmap; - clean up kustomize manifest

* remove dependency

* add reportRequest / clusterReportRequest

* clean up policy report

* generate report request

* update crd clusterReportRequest

* - update json tag of report summary; - update definition manifests; -  fix dclient creation

* aggregate reportRequest into policy report

* fix unit tests

* - update report summary to optional; - generate clusterPolicyReport; - remove reportRequests after merged to report

* remove

* generate reportRequest in kyverno namespace

* update resource filter in helm chart

* - rename reportRequest to reportChangeRequest; -rename clusterReportRequest to clusterReportChangeRequest

* generate policy report in background scan

* skip generating report change request if there's entry results

* fix results entry removal when policy / rule gets deleted

* rename apiversion from policy.kubernetes.io to policy.k8s.io

* update summary.* to lower case

* move reportChangeRequest to kyverno.io/v1alpha1

* remove policy report flag

* fix report update

* clean up policy violation CRD

* remove violation CRD from manifest

* clean up policy violation code - remove pvGenerator

* change severity fields to lower case

* update import library

* set report category

Co-authored-by: Yuvraj <yuvraj.yad001@gmail.com>
Co-authored-by: Yuvraj <10830562+evalsocket@users.noreply.github.com>
Co-authored-by: Jim Bugwadia <jim@nirmata.com>
2020-11-09 11:26:12 -08:00

151 lines
3.8 KiB
Go

package apply
import (
"reflect"
"testing"
report "github.com/kyverno/kyverno/pkg/api/policyreport/v1alpha1"
"gotest.tools/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func Test_mergeClusterReport(t *testing.T) {
reports := []*unstructured.Unstructured{
{
Object: map[string]interface{}{
"apiVersion": "policy.k8s.io/v1alpha1",
"kind": "PolicyReport",
"metadata": map[string]interface{}{
"name": "ns-polr-1",
"namespace": "ns-polr",
},
"results": []interface{}{
map[string]interface{}{
"policy": "ns-polr-1",
"status": report.StatusPass,
"resources": make([]interface{}, 10),
},
},
},
},
{
Object: map[string]interface{}{
"apiVersion": "policy.k8s.io/v1alpha1",
"kind": "PolicyReport",
"metadata": map[string]interface{}{
"name": "ns-polr-2",
},
"results": []interface{}{
map[string]interface{}{
"policy": "ns-polr-2",
"status": report.StatusPass,
"resources": make([]interface{}, 5),
},
},
},
},
{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
"name": "polr-3",
},
"results": []interface{}{
map[string]interface{}{
"policy": "polr-3",
"status": report.StatusPass,
"resources": make([]interface{}, 1),
},
},
},
},
{
Object: map[string]interface{}{
"apiVersion": "policy.k8s.io/v1alpha1",
"kind": "ClusterPolicyReport",
"metadata": map[string]interface{}{
"name": "cpolr-4",
},
"results": []interface{}{
map[string]interface{}{
"policy": "cpolr-4",
"status": report.StatusFail,
},
},
},
},
{
Object: map[string]interface{}{
"apiVersion": "policy.k8s.io/v1alpha1",
"kind": "ClusterPolicyReport",
"metadata": map[string]interface{}{
"name": "cpolr-5",
},
"results": []interface{}{
map[string]interface{}{
"policy": "cpolr-5",
"status": report.StatusFail,
},
},
},
},
}
expectedResults := []interface{}{
map[string]interface{}{
"policy": "ns-polr-2",
"status": report.StatusPass,
"resources": make([]interface{}, 5),
},
map[string]interface{}{
"policy": "polr-3",
"status": report.StatusPass,
"resources": make([]interface{}, 1),
},
map[string]interface{}{
"policy": "cpolr-4",
"status": report.StatusFail,
},
map[string]interface{}{
"policy": "cpolr-5",
"status": report.StatusFail,
},
}
cpolr, err := mergeClusterReport(reports)
assert.NilError(t, err)
assert.Assert(t, cpolr.GetAPIVersion() == "policy.k8s.io/v1alpha1", cpolr.GetAPIVersion())
assert.Assert(t, cpolr.GetKind() == "ClusterPolicyReport", cpolr.GetKind())
entries, _, err := unstructured.NestedSlice(cpolr.UnstructuredContent(), "results")
assert.NilError(t, err)
assert.Assert(t, reflect.DeepEqual(entries, expectedResults), entries...)
summary, _, err := unstructured.NestedMap(cpolr.UnstructuredContent(), "summary")
assert.NilError(t, err)
assert.Assert(t, summary[report.StatusPass].(int64) == 2, summary[report.StatusPass])
assert.Assert(t, summary[report.StatusFail].(int64) == 2, summary[report.StatusFail])
}
func Test_updateSummary(t *testing.T) {
results := []interface{}{
map[string]interface{}{
"status": report.StatusPass,
"resources": make([]interface{}, 5),
},
map[string]interface{}{
"status": report.StatusFail,
},
map[string]interface{}{
"status": report.StatusFail,
},
map[string]interface{}{
"status": report.StatusFail,
},
}
summary := updateSummary(results)
assert.Assert(t, summary[report.StatusPass].(int64) == 1, summary[report.StatusPass])
assert.Assert(t, summary[report.StatusFail].(int64) == 3, summary[report.StatusFail])
}