2020-10-19 15:07:28 -07:00
|
|
|
package apply
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-29 18:13:20 +02:00
|
|
|
report "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
|
2020-10-19 15:07:28 -07:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_mergeClusterReport(t *testing.T) {
|
|
|
|
reports := []*unstructured.Unstructured{
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
2020-11-11 15:09:07 -08:00
|
|
|
"apiVersion": report.SchemeGroupVersion.String(),
|
2020-10-19 15:07:28 -07:00
|
|
|
"kind": "PolicyReport",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "ns-polr-1",
|
|
|
|
"namespace": "ns-polr",
|
|
|
|
},
|
|
|
|
"results": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "ns-polr-1",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusPass,
|
2020-10-19 15:07:28 -07:00
|
|
|
"resources": make([]interface{}, 10),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
2020-11-11 15:09:07 -08:00
|
|
|
"apiVersion": report.SchemeGroupVersion.String(),
|
2020-10-19 15:07:28 -07:00
|
|
|
"kind": "PolicyReport",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "ns-polr-2",
|
|
|
|
},
|
|
|
|
"results": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "ns-polr-2",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusPass,
|
2020-10-19 15:07:28 -07:00
|
|
|
"resources": make([]interface{}, 5),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "polr-3",
|
|
|
|
},
|
|
|
|
"results": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "polr-3",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusPass,
|
2020-10-19 15:07:28 -07:00
|
|
|
"resources": make([]interface{}, 1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
2020-11-11 15:09:07 -08:00
|
|
|
"apiVersion": report.SchemeGroupVersion.String(),
|
2020-10-19 15:07:28 -07:00
|
|
|
"kind": "ClusterPolicyReport",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "cpolr-4",
|
|
|
|
},
|
|
|
|
"results": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "cpolr-4",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusFail,
|
2020-10-19 15:07:28 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
2020-11-11 15:09:07 -08:00
|
|
|
"apiVersion": report.SchemeGroupVersion.String(),
|
2020-10-19 15:07:28 -07:00
|
|
|
"kind": "ClusterPolicyReport",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "cpolr-5",
|
|
|
|
},
|
|
|
|
"results": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "cpolr-5",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusFail,
|
2020-10-19 15:07:28 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResults := []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "ns-polr-2",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusPass,
|
2020-10-19 15:07:28 -07:00
|
|
|
"resources": make([]interface{}, 5),
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "polr-3",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusPass,
|
2020-10-19 15:07:28 -07:00
|
|
|
"resources": make([]interface{}, 1),
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "cpolr-4",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusFail,
|
2020-10-19 15:07:28 -07:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"policy": "cpolr-5",
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusFail,
|
2020-10-19 15:07:28 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cpolr, err := mergeClusterReport(reports)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2020-11-11 15:09:07 -08:00
|
|
|
assert.Assert(t, cpolr.GetAPIVersion() == report.SchemeGroupVersion.String(), cpolr.GetAPIVersion())
|
2020-10-19 15:07:28 -07:00
|
|
|
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)
|
2020-11-04 15:22:12 +05:30
|
|
|
assert.Assert(t, summary[report.StatusPass].(int64) == 2, summary[report.StatusPass])
|
2020-10-30 18:01:46 -07:00
|
|
|
assert.Assert(t, summary[report.StatusFail].(int64) == 2, summary[report.StatusFail])
|
2020-10-19 15:07:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_updateSummary(t *testing.T) {
|
|
|
|
results := []interface{}{
|
|
|
|
map[string]interface{}{
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusPass,
|
2020-10-19 15:07:28 -07:00
|
|
|
"resources": make([]interface{}, 5),
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusFail,
|
2020-10-19 15:07:28 -07:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusFail,
|
2020-10-19 15:07:28 -07:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2021-08-21 19:35:17 +02:00
|
|
|
"result": report.StatusFail,
|
2020-10-19 15:07:28 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
summary := updateSummary(results)
|
2020-11-04 15:22:12 +05:30
|
|
|
assert.Assert(t, summary[report.StatusPass].(int64) == 1, summary[report.StatusPass])
|
2020-10-30 18:01:46 -07:00
|
|
|
assert.Assert(t, summary[report.StatusFail].(int64) == 3, summary[report.StatusFail])
|
2020-10-19 15:07:28 -07:00
|
|
|
}
|