Repository: datreeio/datree
Pull request #372 (2022-01-12 16:00:33)

fix: non interactive mode errors


Patch:
diff --git a/bl/evaluation/evaluator.go b/bl/evaluation/evaluator.go
index 07a9984..a82c701 100644
--- a/bl/evaluation/evaluator.go
+++ b/bl/evaluation/evaluator.go
@@ -130,7 +130,7 @@ func (e *Evaluator) formatNonInteractiveEvaluationResults(evaluationResults *Eva
 			&formattedEvaluationResults,
 		)
 	}
-	nonInteractiveEvaluationResults.PolicySummary = PolicySummary{
+	nonInteractiveEvaluationResults.PolicySummary = &PolicySummary{
 		PolicyName:         policyName,
 		TotalRulesInPolicy: totalRulesInPolicy,
 		TotalRulesFailed:   evaluationResults.Summary.TotalFailedRules,
diff --git a/bl/evaluation/non_interactive_evaluator_types.go b/bl/evaluation/non_interactive_evaluator_types.go
index d4c4b0e..fa9d758 100644
--- a/bl/evaluation/non_interactive_evaluator_types.go
+++ b/bl/evaluation/non_interactive_evaluator_types.go
@@ -5,41 +5,41 @@ import (
 )
 
 type FormattedOutput struct {
-	PolicyValidationResults []*FormattedEvaluationResults   `yaml:"policyValidationResults" json:"policyValidationResults"`
-	PolicySummary           PolicySummary                   `yaml:"policySummary" json:"policySummary"`
-	EvaluationSummary       NonInteractiveEvaluationSummary `yaml:"evaluationSummary" json:"evaluationSummary"`
-	YamlValidationResults   []*validation.InvalidYamlFile   `yaml:"yamlValidationResults" json:"yamlValidationResults"`
-	K8sValidationResults    []*validation.InvalidK8sFile    `yaml:"k8sValidationResults" json:"k8sValidationResults"`
+	PolicyValidationResults []*FormattedEvaluationResults   `yaml:"policyValidationResults" json:"policyValidationResults" xml:"policyValidationResults"`
+	PolicySummary           *PolicySummary                  `yaml:"policySummary" json:"policySummary" xml:"policySummary"`
+	EvaluationSummary       NonInteractiveEvaluationSummary `yaml:"evaluationSummary" json:"evaluationSummary" xml:"evaluationSummary"`
+	YamlValidationResults   []*validation.InvalidYamlFile   `yaml:"yamlValidationResults" json:"yamlValidationResults" xml:"yamlValidationResults"`
+	K8sValidationResults    []*validation.InvalidK8sFile    `yaml:"k8sValidationResults" json:"k8sValidationResults" xml:"k8sValidationResults"`
 }
 
 type NonInteractiveEvaluationResults struct {
 	FormattedEvaluationResults []*FormattedEvaluationResults
-	PolicySummary              PolicySummary
+	PolicySummary              *PolicySummary
 }
 
 type FormattedEvaluationResults struct {
-	FileName    string        `yaml:"fileName" json:"fileName"`
-	RuleResults []*RuleResult `yaml:"ruleResults" json:"ruleResults"`
+	FileName    string        `yaml:"fileName" json:"fileName" xml:"fileName"`
+	RuleResults []*RuleResult `yaml:"ruleResults" json:"ruleResults" xml:"ruleResults"`
 }
 
 type RuleResult struct {
-	Identifier         string              `json:"identifier"`
-	Name               string              `json:"name"`
-	MessageOnFailure   string              `yaml:"messageOnFailure" json:"messageOnFailure"`
-	OccurrencesDetails []OccurrenceDetails `yaml:"occurrencesDetails" json:"occurrencesDetails"`
+	Identifier         string              `yaml:"identifier" json:"identifier" xml:"identifier"`
+	Name               string              `yaml:"name" json:"name" xml:"name"`
+	MessageOnFailure   string              `yaml:"messageOnFailure" json:"messageOnFailure" xml:"messageOnFailure"`
+	OccurrencesDetails []OccurrenceDetails `yaml:"occurrencesDetails" json:"occurrencesDetails" xml:"occurrencesDetails"`
 }
 
 type NonInteractiveEvaluationSummary struct {
-	ConfigsCount                int `yaml:"configsCount" json:"configsCount"`
-	FilesCount                  int `yaml:"filesCount" json:"filesCount"`
-	PassedYamlValidationCount   int `yaml:"passedYamlValidationCount" json:"passedYamlValidationCount"`
-	PassedK8sValidationCount    int `yaml:"passedK8sValidationCount" json:"passedK8sValidationCount"`
-	PassedPolicyValidationCount int `yaml:"passedPolicyValidationCount" json:"passedPolicyValidationCount"`
+	ConfigsCount                int `yaml:"configsCount" json:"configsCount" xml:"configsCount"`
+	FilesCount                  int `yaml:"filesCount" json:"filesCount" xml:"filesCount"`
+	PassedYamlValidationCount   int `yaml:"passedYamlValidationCount" json:"passedYamlValidationCount" xml:"passedYamlValidationCount"`
+	PassedK8sValidationCount    int `yaml:"passedK8sValidationCount" json:"passedK8sValidationCount" xml:"passedK8sValidationCount"`
+	PassedPolicyValidationCount int `yaml:"passedPolicyValidationCount" json:"passedPolicyValidationCount" xml:"passedPolicyValidationCount"`
 }
 
 type PolicySummary struct {
-	PolicyName         string `yaml:"policyName" json:"policyName"`
-	TotalRulesInPolicy int    `yaml:"totalRulesInPolicy" json:"totalRulesInPolicy"`
-	TotalRulesFailed   int    `yaml:"totalRulesFailed"  json:"totalRulesFailed"`
-	TotalPassedCount   int    `yaml:"totalPassedCount"  json:"totalPassedCount"`
+	PolicyName         string `yaml:"policyName" json:"policyName" xml:"policyName"`
+	TotalRulesInPolicy int    `yaml:"totalRulesInPolicy" json:"totalRulesInPolicy" xml:"totalRulesInPolicy"`
+	TotalRulesFailed   int    `yaml:"totalRulesFailed"  json:"totalRulesFailed" xml:"totalRulesFailed"`
+	TotalPassedCount   int    `yaml:"totalPassedCount"  json:"totalPassedCount" xml:"totalPassedCount"`
 }
diff --git a/bl/evaluation/printer.go b/bl/evaluation/printer.go
index f5e2be3..7f149aa 100644
--- a/bl/evaluation/printer.go
+++ b/bl/evaluation/printer.go
@@ -25,7 +25,7 @@ func PrintResults(results ResultType, invalidYamlFiles []*validation.InvalidYaml
 	if outputFormat == "json" || outputFormat == "yaml" || outputFormat == "xml" {
 		nonInteractiveEvaluationResults := results.NonInteractiveEvaluationResults
 		if nonInteractiveEvaluationResults == nil {
-			return fmt.Errorf("results not found")
+			nonInteractiveEvaluationResults = &NonInteractiveEvaluationResults{}
 		}
 		formattedOutput := FormattedOutput{
 			PolicyValidationResults: nonInteractiveEvaluationResults.FormattedEvaluationResults,
diff --git a/bl/evaluation/rule.go b/bl/evaluation/rule.go
index f6a3bcf..bce6e86 100644
--- a/bl/evaluation/rule.go
+++ b/bl/evaluation/rule.go
@@ -12,6 +12,6 @@ func (rp *Rule) GetCount() int {
 }
 
 type OccurrenceDetails struct {
-	MetadataName string `yaml:"metadataName" json:"metadataName"`
-	Kind         string `json:"kind"`
+	MetadataName string `yaml:"metadataName" json:"metadataName" xml:"metadataName"`
+	Kind         string `yaml:"kind" json:"kind" xml:"kind"`
 }
diff --git a/bl/validation/k8sValidator.go b/bl/validation/k8sValidator.go
index e1075ad..b6bef25 100644
--- a/bl/validation/k8sValidator.go
+++ b/bl/validation/k8sValidator.go
@@ -23,8 +23,8 @@ func New() *K8sValidator {
 }
 
 type InvalidFile struct {
-	Path             string  `json:"path"`
-	ValidationErrors []error `yaml:"errors" json:"errors" xml:"Errors"`
+	Path             string  `yaml:"path" json:"path" xml:"path"`
+	ValidationErrors []error `yaml:"errors" json:"errors" xml:"errors"`
 }
 type InvalidYamlFile InvalidFile
 


Test patch:
diff --git a/bl/evaluation/printer_test.go b/bl/evaluation/printer_test.go
index ec9a4f9..ae63bac 100644
--- a/bl/evaluation/printer_test.go
+++ b/bl/evaluation/printer_test.go
@@ -128,7 +128,7 @@ func readOutput(outputFormat string, formattedOutput FormattedOutput) string {
 
 func createFormattedOutput() FormattedOutput {
 	evaluationResults := &NonInteractiveEvaluationResults{
-		PolicySummary: PolicySummary{
+		PolicySummary: &PolicySummary{
 			PolicyName:         "Default",
 			TotalRulesInPolicy: 21,
 			TotalRulesFailed:   4,
@@ -196,7 +196,7 @@ func getExpectedOutputs() expectedOutputs {
 	return expectedOutputs{
 		json: "{\"policyValidationResults\":[{\"fileName\":\"File1\",\"ruleResults\":[{\"identifier\":\"CONTAINERS_MISSING_IMAGE_VALUE_VERSION\",\"name\":\"Ensure each container image has a pinned (tag) version\",\"messageOnFailure\":\"Incorrect value for key `image` - specify an image version to avoid unpleasant \\\"version surprises\\\" in the future\",\"occurrencesDetails\":[{\"metadataName\":\"rss-site\",\"kind\":\"Deployment\"}]},{\"identifier\":\"CONTAINERS_MISSING_MEMORY_LIMIT_KEY\",\"name\":\"Ensure each container has a configured memory limit\",\"messageOnFailure\":\"Missing property object `limits.memory` - value should be within the accepted boundaries recommended by the organization\",\"occurrencesDetails\":[{\"metadataName\":\"rss-site\",\"kind\":\"Deployment\"}]},{\"identifier\":\"WORKLOAD_INVALID_LABELS_VALUE\",\"name\":\"Ensure workload has valid label values\",\"messageOnFailure\":\"Incorrect value for key(s) under `labels` - the vales syntax is not valid so the Kubernetes engine will not accept it\",\"occurrencesDetails\":[{\"metadataName\":\"rss-site\",\"kind\":\"Deployment\"}]},{\"identifier\":\"CONTAINERS_MISSING_LIVENESSPROBE_KEY\",\"name\":\"Ensure each container has a configured liveness probe\",\"messageOnFailure\":\"Missing property object `livenessProbe` - add a properly configured livenessProbe to catch possible deadlocks\",\"occurrencesDetails\":[{\"metadataName\":\"rss-site\",\"kind\":\"Deployment\"}]}]}],\"policySummary\":{\"policyName\":\"Default\",\"totalRulesInPolicy\":21,\"totalRulesFailed\":4,\"totalPassedCount\":0},\"evaluationSummary\":{\"configsCount\":1,\"filesCount\":1,\"passedYamlValidationCount\":1,\"passedK8sValidationCount\":1,\"passedPolicyValidationCount\":0},\"yamlValidationResults\":null,\"k8sValidationResults\":null}\n",
 		yaml: "policyValidationResults:\n- fileName: File1\n  ruleResults:\n  - identifier: CONTAINERS_MISSING_IMAGE_VALUE_VERSION\n    name: Ensure each container image has a pinned (tag) version\n    messageOnFailure: Incorrect value for key `image` - specify an image version to\n      avoid unpleasant \"version surprises\" in the future\n    occurrencesDetails:\n    - metadataName: rss-site\n      kind: Deployment\n  - identifier: CONTAINERS_MISSING_MEMORY_LIMIT_KEY\n    name: Ensure each container has a configured memory limit\n    messageOnFailure: Missing property object `limits.memory` - value should be within\n      the accepted boundaries recommended by the organization\n    occurrencesDetails:\n    - metadataName: rss-site\n      kind: Deployment\n  - identifier: WORKLOAD_INVALID_LABELS_VALUE\n    name: Ensure workload has valid label values\n    messageOnFailure: Incorrect value for key(s) under `labels` - the vales syntax\n      is not valid so the Kubernetes engine will not accept it\n    occurrencesDetails:\n    - metadataName: rss-site\n      kind: Deployment\n  - identifier: CONTAINERS_MISSING_LIVENESSPROBE_KEY\n    name: Ensure each container has a configured liveness probe\n    messageOnFailure: Missing property object `livenessProbe` - add a properly configured\n      livenessProbe to catch possible deadlocks\n    occurrencesDetails:\n    - metadataName: rss-site\n      kind: Deployment\npolicySummary:\n  policyName: Default\n  totalRulesInPolicy: 21\n  totalRulesFailed: 4\n  totalPassedCount: 0\nevaluationSummary:\n  configsCount: 1\n  filesCount: 1\n  passedYamlValidationCount: 1\n  passedK8sValidationCount: 1\n  passedPolicyValidationCount: 0\nyamlValidationResults: []\nk8sValidationResults: []\n\n",
-		xml:  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<FormattedOutput>\n\t<PolicyValidationResults>\n\t\t<FileName>File1</FileName>\n\t\t<RuleResults>\n\t\t\t<Identifier>CONTAINERS_MISSING_IMAGE_VALUE_VERSION</Identifier>\n\t\t\t<Name>Ensure each container image has a pinned (tag) version</Name>\n\t\t\t<MessageOnFailure>Incorrect value for key `image` - specify an image version to avoid unpleasant &#34;version surprises&#34; in the future</MessageOnFailure>\n\t\t\t<OccurrencesDetails>\n\t\t\t\t<MetadataName>rss-site</MetadataName>\n\t\t\t\t<Kind>Deployment</Kind>\n\t\t\t</OccurrencesDetails>\n\t\t</RuleResults>\n\t\t<RuleResults>\n\t\t\t<Identifier>CONTAINERS_MISSING_MEMORY_LIMIT_KEY</Identifier>\n\t\t\t<Name>Ensure each container has a configured memory limit</Name>\n\t\t\t<MessageOnFailure>Missing property object `limits.memory` - value should be within the accepted boundaries recommended by the organization</MessageOnFailure>\n\t\t\t<OccurrencesDetails>\n\t\t\t\t<MetadataName>rss-site</MetadataName>\n\t\t\t\t<Kind>Deployment</Kind>\n\t\t\t</OccurrencesDetails>\n\t\t</RuleResults>\n\t\t<RuleResults>\n\t\t\t<Identifier>WORKLOAD_INVALID_LABELS_VALUE</Identifier>\n\t\t\t<Name>Ensure workload has valid label values</Name>\n\t\t\t<MessageOnFailure>Incorrect value for key(s) under `labels` - the vales syntax is not valid so the Kubernetes engine will not accept it</MessageOnFailure>\n\t\t\t<OccurrencesDetails>\n\t\t\t\t<MetadataName>rss-site</MetadataName>\n\t\t\t\t<Kind>Deployment</Kind>\n\t\t\t</OccurrencesDetails>\n\t\t</RuleResults>\n\t\t<RuleResults>\n\t\t\t<Identifier>CONTAINERS_MISSING_LIVENESSPROBE_KEY</Identifier>\n\t\t\t<Name>Ensure each container has a configured liveness probe</Name>\n\t\t\t<MessageOnFailure>Missing property object `livenessProbe` - add a properly configured livenessProbe to catch possible deadlocks</MessageOnFailure>\n\t\t\t<OccurrencesDetails>\n\t\t\t\t<MetadataName>rss-site</MetadataName>\n\t\t\t\t<Kind>Deployment</Kind>\n\t\t\t</OccurrencesDetails>\n\t\t</RuleResults>\n\t</PolicyValidationResults>\n\t<PolicySummary>\n\t\t<PolicyName>Default</PolicyName>\n\t\t<TotalRulesInPolicy>21</TotalRulesInPolicy>\n\t\t<TotalRulesFailed>4</TotalRulesFailed>\n\t\t<TotalPassedCount>0</TotalPassedCount>\n\t</PolicySummary>\n\t<EvaluationSummary>\n\t\t<ConfigsCount>1</ConfigsCount>\n\t\t<FilesCount>1</FilesCount>\n\t\t<PassedYamlValidationCount>1</PassedYamlValidationCount>\n\t\t<PassedK8sValidationCount>1</PassedK8sValidationCount>\n\t\t<PassedPolicyValidationCount>0</PassedPolicyValidationCount>\n\t</EvaluationSummary>\n</FormattedOutput>\n",
+		xml:  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<FormattedOutput>\n\t<policyValidationResults>\n\t\t<fileName>File1</fileName>\n\t\t<ruleResults>\n\t\t\t<identifier>CONTAINERS_MISSING_IMAGE_VALUE_VERSION</identifier>\n\t\t\t<name>Ensure each container image has a pinned (tag) version</name>\n\t\t\t<messageOnFailure>Incorrect value for key `image` - specify an image version to avoid unpleasant &#34;version surprises&#34; in the future</messageOnFailure>\n\t\t\t<occurrencesDetails>\n\t\t\t\t<metadataName>rss-site</metadataName>\n\t\t\t\t<kind>Deployment</kind>\n\t\t\t</occurrencesDetails>\n\t\t</ruleResults>\n\t\t<ruleResults>\n\t\t\t<identifier>CONTAINERS_MISSING_MEMORY_LIMIT_KEY</identifier>\n\t\t\t<name>Ensure each container has a configured memory limit</name>\n\t\t\t<messageOnFailure>Missing property object `limits.memory` - value should be within the accepted boundaries recommended by the organization</messageOnFailure>\n\t\t\t<occurrencesDetails>\n\t\t\t\t<metadataName>rss-site</metadataName>\n\t\t\t\t<kind>Deployment</kind>\n\t\t\t</occurrencesDetails>\n\t\t</ruleResults>\n\t\t<ruleResults>\n\t\t\t<identifier>WORKLOAD_INVALID_LABELS_VALUE</identifier>\n\t\t\t<name>Ensure workload has valid label values</name>\n\t\t\t<messageOnFailure>Incorrect value for key(s) under `labels` - the vales syntax is not valid so the Kubernetes engine will not accept it</messageOnFailure>\n\t\t\t<occurrencesDetails>\n\t\t\t\t<metadataName>rss-site</metadataName>\n\t\t\t\t<kind>Deployment</kind>\n\t\t\t</occurrencesDetails>\n\t\t</ruleResults>\n\t\t<ruleResults>\n\t\t\t<identifier>CONTAINERS_MISSING_LIVENESSPROBE_KEY</identifier>\n\t\t\t<name>Ensure each container has a configured liveness probe</name>\n\t\t\t<messageOnFailure>Missing property object `livenessProbe` - add a properly configured livenessProbe to catch possible deadlocks</messageOnFailure>\n\t\t\t<occurrencesDetails>\n\t\t\t\t<metadataName>rss-site</metadataName>\n\t\t\t\t<kind>Deployment</kind>\n\t\t\t</occurrencesDetails>\n\t\t</ruleResults>\n\t</policyValidationResults>\n\t<policySummary>\n\t\t<policyName>Default</policyName>\n\t\t<totalRulesInPolicy>21</totalRulesInPolicy>\n\t\t<totalRulesFailed>4</totalRulesFailed>\n\t\t<totalPassedCount>0</totalPassedCount>\n\t</policySummary>\n\t<evaluationSummary>\n\t\t<configsCount>1</configsCount>\n\t\t<filesCount>1</filesCount>\n\t\t<passedYamlValidationCount>1</passedYamlValidationCount>\n\t\t<passedK8sValidationCount>1</passedK8sValidationCount>\n\t\t<passedPolicyValidationCount>0</passedPolicyValidationCount>\n\t</evaluationSummary>\n</FormattedOutput>\n",
 	}
 }
 
@@ -218,7 +218,7 @@ func print_resultst(outputFormat string) *printResultsTestCase {
 					},
 				},
 				NonInteractiveEvaluationResults: &NonInteractiveEvaluationResults{
-					PolicySummary: PolicySummary{
+					PolicySummary: &PolicySummary{
 						PolicyName:         "Default",
 						TotalRulesInPolicy: 0,
 						TotalRulesFailed:   0,