2020-12-20 02:37:09 +05:30
|
|
|
package apply
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-10-29 18:13:20 +02:00
|
|
|
preport "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
|
2020-12-20 02:37:09 +05:30
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Apply(t *testing.T) {
|
|
|
|
type TestCase struct {
|
|
|
|
PolicyPaths []string
|
|
|
|
ResourcePaths []string
|
|
|
|
expectedPolicyReports []preport.PolicyReport
|
|
|
|
}
|
|
|
|
|
|
|
|
testcases := []TestCase{
|
2021-01-08 16:45:39 -08:00
|
|
|
{
|
2022-04-14 17:50:18 +05:30
|
|
|
PolicyPaths: []string{"../../../../test/best_practices/disallow_latest_tag.yaml"},
|
|
|
|
ResourcePaths: []string{"../../../../test/resources/pod_with_version_tag.yaml"},
|
2020-12-20 02:37:09 +05:30
|
|
|
expectedPolicyReports: []preport.PolicyReport{
|
2021-01-08 16:45:39 -08:00
|
|
|
{
|
2020-12-20 02:37:09 +05:30
|
|
|
Summary: preport.PolicyReportSummary{
|
|
|
|
Pass: 2,
|
|
|
|
Fail: 0,
|
|
|
|
Skip: 0,
|
|
|
|
Error: 0,
|
|
|
|
Warn: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-08 16:45:39 -08:00
|
|
|
{
|
2022-04-14 17:50:18 +05:30
|
|
|
PolicyPaths: []string{"../../../../test/best_practices/disallow_latest_tag.yaml"},
|
|
|
|
ResourcePaths: []string{"../../../../test/resources/pod_with_latest_tag.yaml"},
|
2020-12-20 02:37:09 +05:30
|
|
|
expectedPolicyReports: []preport.PolicyReport{
|
2021-01-08 16:45:39 -08:00
|
|
|
{
|
2020-12-20 02:37:09 +05:30
|
|
|
Summary: preport.PolicyReportSummary{
|
2021-09-02 01:06:29 +05:30
|
|
|
Pass: 1,
|
2020-12-20 02:37:09 +05:30
|
|
|
Fail: 1,
|
|
|
|
Skip: 0,
|
|
|
|
Error: 0,
|
|
|
|
Warn: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-15 13:30:59 +05:30
|
|
|
{
|
2022-04-14 17:50:18 +05:30
|
|
|
PolicyPaths: []string{"../../../../test/cli/apply/policies"},
|
|
|
|
ResourcePaths: []string{"../../../../test/cli/apply/resource"},
|
2022-03-15 13:30:59 +05:30
|
|
|
expectedPolicyReports: []preport.PolicyReport{
|
|
|
|
{
|
|
|
|
Summary: preport.PolicyReportSummary{
|
|
|
|
Pass: 1,
|
|
|
|
Fail: 1,
|
2022-05-07 15:33:50 +02:00
|
|
|
Skip: 8,
|
2022-03-15 13:30:59 +05:30
|
|
|
Error: 0,
|
2022-05-07 15:33:50 +02:00
|
|
|
Warn: 2,
|
2022-03-15 13:30:59 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-20 02:37:09 +05:30
|
|
|
}
|
|
|
|
|
2020-12-20 02:43:07 +05:30
|
|
|
compareSummary := func(expected preport.PolicyReportSummary, actual map[string]interface{}) {
|
2022-03-28 16:01:27 +02:00
|
|
|
assert.Equal(t, actual[preport.StatusPass].(int64), int64(expected.Pass))
|
|
|
|
assert.Equal(t, actual[preport.StatusFail].(int64), int64(expected.Fail))
|
|
|
|
assert.Equal(t, actual[preport.StatusSkip].(int64), int64(expected.Skip))
|
|
|
|
assert.Equal(t, actual[preport.StatusWarn].(int64), int64(expected.Warn))
|
|
|
|
assert.Equal(t, actual[preport.StatusError].(int64), int64(expected.Error))
|
2020-12-20 02:43:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
2022-04-12 09:30:49 +05:30
|
|
|
_, _, _, info, _ := applyCommandHelper(tc.ResourcePaths, "", false, true, "", "", "", "", tc.PolicyPaths, false, false)
|
2021-09-02 01:06:29 +05:30
|
|
|
resps := buildPolicyReports(info)
|
2020-12-20 02:37:09 +05:30
|
|
|
for i, resp := range resps {
|
2020-12-20 02:43:07 +05:30
|
|
|
compareSummary(tc.expectedPolicyReports[i].Summary, resp.UnstructuredContent()["summary"].(map[string]interface{}))
|
2020-12-20 02:37:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|