diff --git a/pkg/kyverno/test/test_command.go b/pkg/kyverno/test/test_command.go index 50ffbf9b43..d669edfc0d 100644 --- a/pkg/kyverno/test/test_command.go +++ b/pkg/kyverno/test/test_command.go @@ -71,11 +71,12 @@ type Test struct { } type TestResults struct { - Policy string `json:"policy"` - Rule string `json:"rule"` - Result report.PolicyResult `json:"result"` - Status report.PolicyResult `json:"status"` - Resource string `json:"resource"` + Policy string `json:"policy"` + Rule string `json:"rule"` + Result report.PolicyResult `json:"result"` + Status report.PolicyResult `json:"status"` + Resource string `json:"resource"` + AutoGeneratedRule bool `json:"auto_generated_rule"` } type ReportResult struct { @@ -234,9 +235,10 @@ func getLocalDirTestFiles(fs billy.Filesystem, path, fileName, valuesFile string return errors } -func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResults, infos []policyreport.Info) map[string]report.PolicyReportResult { +func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResults, infos []policyreport.Info) (map[string]report.PolicyReportResult, []TestResults) { results := make(map[string]report.PolicyReportResult) now := metav1.Timestamp{Seconds: time.Now().Unix()} + for _, resp := range resps { policyName := resp.PolicyResponse.Policy.Name resourceName := resp.PolicyResponse.Resource.Name @@ -253,10 +255,15 @@ func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResu }, }, } - for _, test := range testResults { + for i, test := range testResults { if test.Policy == policyName && test.Resource == resourceName { if !util.ContainsString(rules, test.Rule) { - result.Result = report.StatusSkip + if !util.ContainsString(rules, "autogen-"+test.Rule) { + result.Result = report.StatusSkip + } else { + testResults[i].AutoGeneratedRule = true + test.Rule = "autogen-" + test.Rule + } } resultsKey := fmt.Sprintf("%s-%s-%s", test.Policy, test.Rule, test.Resource) if _, ok := results[resultsKey]; !ok { @@ -265,9 +272,11 @@ func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResu } } } + for _, info := range infos { for _, infoResult := range info.Results { for _, rule := range infoResult.Rules { + if rule.Type != utils.Validation.String() { continue } @@ -280,14 +289,16 @@ func buildPolicyResults(resps []*response.EngineResponse, testResults []TestResu } result.Rule = rule.Name result.Result = report.PolicyResult(rule.Check) + result.Source = policyreport.SourceValue result.Timestamp = now results[resultsKey] = result + } } } - return results + return results, testResults } func getPolicyResourceFullPath(path []string, policyResourcePath string, isGit bool) []string { @@ -404,8 +415,9 @@ func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, valuesFile s pvInfos = append(pvInfos, info) } } - resultsMap := buildPolicyResults(validateEngineResponses, values.Results, pvInfos) - resultErr := printTestResult(resultsMap, values.Results, rc) + resultsMap, testResults := buildPolicyResults(validateEngineResponses, values.Results, pvInfos) + + resultErr := printTestResult(resultsMap, testResults, rc) if resultErr != nil { return sanitizederror.NewWithError("Unable to genrate result. Error:", resultErr) } @@ -425,7 +437,16 @@ func printTestResult(resps map[string]report.PolicyReportResult, testResults []T res.Policy = boldFgCyan.Sprintf(v.Policy) res.Rule = boldFgCyan.Sprintf(v.Rule) res.Resource = boldFgCyan.Sprintf(v.Resource) - resultKey := fmt.Sprintf("%s-%s-%s", v.Policy, v.Rule, v.Resource) + + var ruleNameInResultKey string + if v.AutoGeneratedRule { + ruleNameInResultKey = fmt.Sprintf("autogen-%s", v.Rule) + } else { + ruleNameInResultKey = v.Rule + } + + resultKey := fmt.Sprintf("%s-%s-%s", v.Policy, ruleNameInResultKey, v.Resource) + var testRes report.PolicyReportResult if val, ok := resps[resultKey]; ok { testRes = val