1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/cmd/cli/kubectl-kyverno/apply/apply_command_test.go
Sandesh More 01b1ece704
added kubeconfig and context flag to kyverno apply (#4524)
Signed-off-by: Sandesh More <sandesh.more@infracloud.io>
2022-09-20 19:05:18 +05:30

90 lines
2.4 KiB
Go

package apply
import (
"testing"
preport "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
"gotest.tools/assert"
)
func Test_Apply(t *testing.T) {
type TestCase struct {
PolicyPaths []string
ResourcePaths []string
expectedPolicyReports []preport.PolicyReport
config ApplyCommandConfig
}
testcases := []TestCase{
{
config: ApplyCommandConfig{
PolicyPaths: []string{"../../../../test/best_practices/disallow_latest_tag.yaml"},
ResourcePaths: []string{"../../../../test/resources/pod_with_version_tag.yaml"},
PolicyReport: true,
},
expectedPolicyReports: []preport.PolicyReport{
{
Summary: preport.PolicyReportSummary{
Pass: 2,
Fail: 0,
Skip: 0,
Error: 0,
Warn: 0,
},
},
},
},
{
config: ApplyCommandConfig{
PolicyPaths: []string{"../../../../test/best_practices/disallow_latest_tag.yaml"},
ResourcePaths: []string{"../../../../test/resources/pod_with_latest_tag.yaml"},
PolicyReport: true,
},
expectedPolicyReports: []preport.PolicyReport{
{
Summary: preport.PolicyReportSummary{
Pass: 1,
Fail: 1,
Skip: 0,
Error: 0,
Warn: 0,
},
},
},
},
{
config: ApplyCommandConfig{
PolicyPaths: []string{"../../../../test/cli/apply/policies"},
ResourcePaths: []string{"../../../../test/cli/apply/resource"},
PolicyReport: true,
},
expectedPolicyReports: []preport.PolicyReport{
{
Summary: preport.PolicyReportSummary{
Pass: 1,
Fail: 1,
Skip: 8,
Error: 0,
Warn: 2,
},
},
},
},
}
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))
}
for _, tc := range testcases {
_, _, _, info, _ := tc.config.applyCommandHelper()
resps := buildPolicyReports(info)
for i, resp := range resps {
compareSummary(tc.expectedPolicyReports[i].Summary, resp.UnstructuredContent()["summary"].(map[string]interface{}))
}
}
}