1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/cmd/cli/kubectl-kyverno/apply/apply_command_test.go

81 lines
2.2 KiB
Go
Raw Normal View History

2020-12-20 02:37:09 +05:30
package apply
import (
"testing"
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{
{
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{
{
2020-12-20 02:37:09 +05:30
Summary: preport.PolicyReportSummary{
Pass: 2,
Fail: 0,
Skip: 0,
Error: 0,
Warn: 0,
},
},
},
},
{
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{
{
2020-12-20 02:37:09 +05:30
Summary: preport.PolicyReportSummary{
Pass: 1,
2020-12-20 02:37:09 +05:30
Fail: 1,
Skip: 0,
Error: 0,
Warn: 0,
},
},
},
},
{
PolicyPaths: []string{"../../../../test/cli/apply/policies"},
ResourcePaths: []string{"../../../../test/cli/apply/resource"},
expectedPolicyReports: []preport.PolicyReport{
{
Summary: preport.PolicyReportSummary{
Pass: 1,
Fail: 1,
Skip: 8,
Error: 0,
Warn: 2,
},
},
},
},
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{}) {
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 {
_, _, _, info, _ := applyCommandHelper(tc.ResourcePaths, "", false, true, "", "", "", "", tc.PolicyPaths, false, false)
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
}
}
}