Repository: datreeio/datree
Pull request #45 (2021-05-23 16:07:13)

fix: correct k8s schema version summary printing


Patch:
diff --git a/bl/evaluation/printer.go b/bl/evaluation/printer.go
index c66188c..22f0c0e 100644
--- a/bl/evaluation/printer.go
+++ b/bl/evaluation/printer.go
@@ -3,10 +3,11 @@ package evaluation
 import (
 	"encoding/json"
 	"fmt"
-	"github.com/datreeio/datree/bl/validation"
 	"os"
 	"path/filepath"
 
+	"github.com/datreeio/datree/bl/validation"
+
 	"github.com/datreeio/datree/pkg/printer"
 	"gopkg.in/yaml.v2"
 )
@@ -14,22 +15,17 @@ import (
 type Printer interface {
 	PrintWarnings(warnings []printer.Warning)
 	PrintSummaryTable(summary printer.Summary)
-	PrintEvaluationSummary(summary printer.EvaluationSummary)
+	PrintEvaluationSummary(summary printer.EvaluationSummary, k8sVersion string)
 }
 
-// func PrintAllResults(results *EvaluationResults /*, invalidFiles*/) {
-// 	// TODO: foreach invalid file
-// }
-
-// url := "https://app.datree.io/login?cliId=" + cliId
-func PrintResults(results *EvaluationResults, invalidFiles []*validation.InvalidFile, evaluationSummary printer.EvaluationSummary, loginURL string, outputFormat string, printer Printer) error {
+func PrintResults(results *EvaluationResults, invalidFiles []*validation.InvalidFile, evaluationSummary printer.EvaluationSummary, loginURL string, outputFormat string, printer Printer, k8sVersion string) error {
 	switch {
 	case outputFormat == "json":
 		return jsonOutput(results)
 	case outputFormat == "yaml":
 		return yamlOutput(results)
 	default:
-		return textOutput(results, invalidFiles, evaluationSummary, loginURL, printer)
+		return textOutput(results, invalidFiles, evaluationSummary, loginURL, printer, k8sVersion)
 	}
 }
 
@@ -55,13 +51,13 @@ func yamlOutput(results *EvaluationResults) error {
 	return nil
 }
 
-func textOutput(results *EvaluationResults, invalidFiles []*validation.InvalidFile, evaluationSummary printer.EvaluationSummary, url string, printer Printer) error {
+func textOutput(results *EvaluationResults, invalidFiles []*validation.InvalidFile, evaluationSummary printer.EvaluationSummary, url string, printer Printer, k8sVersion string) error {
 	pwd, err := os.Getwd()
 	if err != nil {
 		return err
 	}
 
-	warnings, err := parseToPrinterWarnings(results, invalidFiles, pwd)
+	warnings, err := parseToPrinterWarnings(results, invalidFiles, pwd, k8sVersion)
 	if err != nil {
 		fmt.Println(err)
 		return err
@@ -71,14 +67,14 @@ func textOutput(results *EvaluationResults, invalidFiles []*validation.InvalidFi
 
 	summary := parseEvaluationResultsToSummary(results, evaluationSummary, url)
 
-	printer.PrintEvaluationSummary(evaluationSummary)
+	printer.PrintEvaluationSummary(evaluationSummary, k8sVersion)
 
 	printer.PrintSummaryTable(summary)
 
 	return nil
 }
 
-func parseToPrinterWarnings(results *EvaluationResults, invalidFiles []*validation.InvalidFile, pwd string) ([]printer.Warning, error) {
+func parseToPrinterWarnings(results *EvaluationResults, invalidFiles []*validation.InvalidFile, pwd string, k8sVersion string) ([]printer.Warning, error) {
 	var warnings = []printer.Warning{}
 
 	for _, invalidFile := range invalidFiles {
@@ -88,7 +84,7 @@ func parseToPrinterWarnings(results *EvaluationResults, invalidFiles []*validati
 			ValidationInfo: printer.ValidationInfo{
 				IsValid:          false,
 				ValidationErrors: invalidFile.ValidationErrors,
-				K8sVersion:       "1.18.0",
+				K8sVersion:       k8sVersion,
 			},
 		})
 	}
@@ -114,7 +110,7 @@ func parseToPrinterWarnings(results *EvaluationResults, invalidFiles []*validati
 				ValidationInfo: printer.ValidationInfo{
 					IsValid:          true,
 					ValidationErrors: []error{},
-					K8sVersion:       "1.18.0",
+					K8sVersion:       k8sVersion,
 				},
 			})
 		}
diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go
index f140c04..50e24b5 100644
--- a/pkg/printer/printer.go
+++ b/pkg/printer/printer.go
@@ -98,13 +98,13 @@ type EvaluationSummary struct {
 	PassedPolicyCheckCount    int
 }
 
-func (p *Printer) PrintEvaluationSummary(summary EvaluationSummary) {
+func (p *Printer) PrintEvaluationSummary(summary EvaluationSummary, k8sVersion string) {
 	p.printInColor("(Summary)\n", p.theme.Colors.White)
 	fmt.Println()
 
 	fmt.Printf("- Passing YAML validation: %v/%v\n", summary.PassedYamlValidationCount, summary.FilesCount)
 	fmt.Println()
-	fmt.Printf("- Passing Kubernetes (1.18) schema validation: %v/%v\n", summary.PassedK8sValidationCount, summary.FilesCount)
+	fmt.Printf("- Passing Kubernetes (%s) schema validation: %v/%v\n", k8sVersion, summary.PassedK8sValidationCount, summary.FilesCount)
 	fmt.Println()
 	fmt.Printf("- Passing policy check: %v/%v\n", summary.PassedPolicyCheckCount, summary.FilesCount)
 	fmt.Println()


Test patch:
diff --git a/bl/evaluation/printer_test.go b/bl/evaluation/printer_test.go
index 6444106..dcd01b9 100644
--- a/bl/evaluation/printer_test.go
+++ b/bl/evaluation/printer_test.go
@@ -1,10 +1,11 @@
 package evaluation
 
 import (
-	"github.com/datreeio/datree/bl/validation"
 	"os"
 	"testing"
 
+	"github.com/datreeio/datree/bl/validation"
+
 	"github.com/datreeio/datree/pkg/printer"
 	"github.com/stretchr/testify/mock"
 )
@@ -44,7 +45,7 @@ func TestPrintResults(t *testing.T) {
 		mockedPrinter.On("PrintWarnings", mock.Anything)
 		mockedPrinter.On("PrintSummaryTable", mock.Anything)
 		t.Run(tt.name, func(t *testing.T) {
-			PrintResults(tt.args.results, tt.args.invalidFiles, tt.args.evaluationSummary, tt.args.loginURL, tt.args.outputFormat, tt.args.printer)
+			PrintResults(tt.args.results, tt.args.invalidFiles, tt.args.evaluationSummary, tt.args.loginURL, tt.args.outputFormat, tt.args.printer, "1.18.0")
 
 			if tt.args.outputFormat == "json" {
 				mockedPrinter.AssertNotCalled(t, "PrintWarnings")
@@ -55,7 +56,7 @@ func TestPrintResults(t *testing.T) {
 
 			} else {
 				pwd, _ := os.Getwd()
-				warnings, _ := parseToPrinterWarnings(tt.args.results, tt.args.invalidFiles, pwd)
+				warnings, _ := parseToPrinterWarnings(tt.args.results, tt.args.invalidFiles, pwd, "1.18.0")
 				mockedPrinter.AssertCalled(t, "PrintWarnings", warnings)
 				mockedPrinter.AssertCalled(t, "PrintSummaryTable", tt.args.results, tt.args.loginURL)
 			}
diff --git a/cmd/test/main.go b/cmd/test/main.go
index cb8879d..e80cfce 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -2,9 +2,10 @@ package test
 
 import (
 	"fmt"
-	"github.com/datreeio/datree/pkg/extractor"
 	"time"
 
+	"github.com/datreeio/datree/pkg/extractor"
+
 	"github.com/briandowns/spinner"
 	"github.com/datreeio/datree/bl/evaluation"
 	"github.com/datreeio/datree/bl/messager"
@@ -36,7 +37,7 @@ type EvaluationPrinter interface {
 	PrintWarnings(warnings []printer.Warning)
 	PrintSummaryTable(summary printer.Summary)
 	PrintMessage(messageText string, messageColor string)
-	PrintEvaluationSummary(evaluationSummary printer.EvaluationSummary)
+	PrintEvaluationSummary(evaluationSummary printer.EvaluationSummary, k8sVersion string)
 }
 
 type Reader interface {
@@ -134,7 +135,7 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
 		PassedPolicyCheckCount:    passedPolicyCheckCount,
 	}
 
-	err = evaluation.PrintResults(results, invalidFiles, evaluationSummary, fmt.Sprintf("https://app.datree.io/login?cliId=%s", ctx.LocalConfig.CliId), flags.Output, ctx.Printer)
+	err = evaluation.PrintResults(results, invalidFiles, evaluationSummary, fmt.Sprintf("https://app.datree.io/login?cliId=%s", ctx.LocalConfig.CliId), flags.Output, ctx.Printer, flags.K8sVersion)
 
 	var invocationFailedErr error = nil
 
diff --git a/cmd/test/main_test.go b/cmd/test/main_test.go
index dfe9b69..1f42809 100644
--- a/cmd/test/main_test.go
+++ b/cmd/test/main_test.go
@@ -1,10 +1,11 @@
 package test
 
 import (
+	"testing"
+
 	"github.com/datreeio/datree/bl/validation"
 	"github.com/datreeio/datree/pkg/extractor"
 	"github.com/datreeio/datree/pkg/printer"
-	"testing"
 
 	"github.com/datreeio/datree/bl/evaluation"
 	"github.com/datreeio/datree/bl/messager"
@@ -71,7 +72,7 @@ func (p *PrinterMock) PrintMessage(messageText string, messageColor string) {
 	p.Called(messageText, messageColor)
 }
 
-func (p *PrinterMock) PrintEvaluationSummary(evaluationSummary printer.EvaluationSummary) {
+func (p *PrinterMock) PrintEvaluationSummary(evaluationSummary printer.EvaluationSummary, k8sVersion string) {
 	p.Called(evaluationSummary)
 }
 
@@ -118,7 +119,7 @@ func TestTestCommand(t *testing.T) {
 	printerMock.On("PrintWarnings", mock.Anything)
 	printerMock.On("PrintSummaryTable", mock.Anything)
 	printerMock.On("PrintMessage", mock.Anything, mock.Anything)
-	printerMock.On("PrintEvaluationSummary", mock.Anything)
+	printerMock.On("PrintEvaluationSummary", mock.Anything, mock.Anything)
 
 	readerMock := &ReaderMock{}