Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #45 from datreeio/fix-k8s-version-print
Browse files Browse the repository at this point in the history
fix: correct k8s schema version summary printing
  • Loading branch information
Yishay Mendelsohn authored May 24, 2021
2 parents bea9dcc + 5b7ecc9 commit c72681f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
26 changes: 11 additions & 15 deletions bl/evaluation/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,29 @@ 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"
)

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)
}
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -88,7 +84,7 @@ func parseToPrinterWarnings(results *EvaluationResults, invalidFiles []*validati
ValidationInfo: printer.ValidationInfo{
IsValid: false,
ValidationErrors: invalidFile.ValidationErrors,
K8sVersion: "1.18.0",
K8sVersion: k8sVersion,
},
})
}
Expand All @@ -114,7 +110,7 @@ func parseToPrinterWarnings(results *EvaluationResults, invalidFiles []*validati
ValidationInfo: printer.ValidationInfo{
IsValid: true,
ValidationErrors: []error{},
K8sVersion: "1.18.0",
K8sVersion: k8sVersion,
},
})
}
Expand Down
7 changes: 4 additions & 3 deletions bl/evaluation/printer_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions cmd/test/main_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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{}

Expand Down
4 changes: 2 additions & 2 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c72681f

Please sign in to comment.