Repository: datreeio/datree
Pull request #48 (2021-05-25 12:15:51)

Dat 3254 receive k8s version


Patch:
diff --git a/bl/evaluation/evaluator.go b/bl/evaluation/evaluator.go
index 65d7e1d..d9eb96a 100644
--- a/bl/evaluation/evaluator.go
+++ b/bl/evaluation/evaluator.go
@@ -8,7 +8,7 @@ import (
 
 type CLIClient interface {
 	RequestEvaluation(request *cliClient.EvaluationRequest) (*cliClient.EvaluationResponse, error)
-	CreateEvaluation(request *cliClient.CreateEvaluationRequest) (int, error)
+	CreateEvaluation(request *cliClient.CreateEvaluationRequest) (*cliClient.CreateEvaluationResponse, error)
 	UpdateEvaluationValidation(request *cliClient.UpdateEvaluationValidationRequest) error
 }
 
@@ -40,9 +40,9 @@ type Error struct {
 	Filename string
 }
 
-func (e *Evaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string) (int, error) {
-	evaluationId, err := e.cliClient.CreateEvaluation(&cliClient.CreateEvaluationRequest{
-		K8sVersion: k8sVersion,
+func (e *Evaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string) (*cliClient.CreateEvaluationResponse, error) {
+	createEvaluationResponse, err := e.cliClient.CreateEvaluation(&cliClient.CreateEvaluationRequest{
+		K8sVersion: &k8sVersion,
 		CliId:      cliId,
 		Metadata: &cliClient.Metadata{
 			CliVersion:      cliVersion,
@@ -52,7 +52,7 @@ func (e *Evaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion
 		},
 	})
 
-	return evaluationId, err
+	return createEvaluationResponse, err
 }
 
 func (e *Evaluator) Evaluate(validFilesPathsChan chan string, invalidFilesPathsChan chan *validation.InvalidFile, evaluationId int) (*EvaluationResults, []*validation.InvalidFile, []*extractor.FileConfiguration, []*Error, error) {
diff --git a/bl/validation/k8sValidator.go b/bl/validation/k8sValidator.go
index b9b8e9a..8a01a04 100644
--- a/bl/validation/k8sValidator.go
+++ b/bl/validation/k8sValidator.go
@@ -17,11 +17,8 @@ type K8sValidator struct {
 	validationClient ValidationClient
 }
 
-func New(k8sVersion string) *K8sValidator {
-	kubconformClient := newKubconformValidator(k8sVersion)
-	return &K8sValidator{
-		validationClient: kubconformClient,
-	}
+func New() *K8sValidator {
+	return &K8sValidator{}
 }
 
 type InvalidFile struct {
@@ -29,6 +26,10 @@ type InvalidFile struct {
 	ValidationErrors []error
 }
 
+func (val *K8sValidator) InitClient(k8sVersion string) {
+	val.validationClient = newKubconformValidator(k8sVersion)
+}
+
 func (val *K8sValidator) ValidateResources(paths []string) (chan string, chan *InvalidFile, chan error) {
 	pathsChan := files.ToAbsolutePaths(paths)
 
diff --git a/cmd/root.go b/cmd/root.go
index 77b378c..4bdcfad 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -3,6 +3,7 @@ package cmd
 import (
 	"github.com/datreeio/datree/bl/evaluation"
 	"github.com/datreeio/datree/bl/messager"
+	"github.com/datreeio/datree/bl/validation"
 	"github.com/datreeio/datree/cmd/test"
 	"github.com/datreeio/datree/cmd/version"
 	"github.com/datreeio/datree/pkg/cliClient"
@@ -25,12 +26,13 @@ func init() {
 	app := startup()
 
 	rootCmd.AddCommand(test.New(&test.TestCommandContext{
-		CliVersion:  CliVersion,
-		Evaluator:   app.context.Evaluator,
-		LocalConfig: app.context.LocalConfig,
-		Messager:    app.context.Messager,
-		Printer:     app.context.Printer,
-		Reader:      app.context.Reader,
+		CliVersion:   CliVersion,
+		Evaluator:    app.context.Evaluator,
+		LocalConfig:  app.context.LocalConfig,
+		Messager:     app.context.Messager,
+		Printer:      app.context.Printer,
+		Reader:       app.context.Reader,
+		K8sValidator: app.context.K8sValidator,
 	}))
 
 	rootCmd.AddCommand(version.New(&version.VersionCommandContext{
@@ -45,12 +47,13 @@ func Execute() error {
 }
 
 type context struct {
-	LocalConfig *localConfig.LocalConfiguration
-	Evaluator   *evaluation.Evaluator
-	CliClient   *cliClient.CliClient
-	Messager    *messager.Messager
-	Printer     *printer.Printer
-	Reader      *fileReader.FileReader
+	LocalConfig  *localConfig.LocalConfiguration
+	Evaluator    *evaluation.Evaluator
+	CliClient    *cliClient.CliClient
+	Messager     *messager.Messager
+	Printer      *printer.Printer
+	Reader       *fileReader.FileReader
+	K8sValidator *validation.K8sValidator
 }
 
 type app struct {
@@ -64,12 +67,13 @@ func startup() *app {
 
 	return &app{
 		context: &context{
-			LocalConfig: config,
-			Evaluator:   evaluation.New(cliClient),
-			CliClient:   cliClient,
-			Messager:    messager.New(cliClient),
-			Printer:     printer,
-			Reader:      fileReader.CreateFileReader(nil),
+			LocalConfig:  config,
+			Evaluator:    evaluation.New(cliClient),
+			CliClient:    cliClient,
+			Messager:     messager.New(cliClient),
+			Printer:      printer,
+			Reader:       fileReader.CreateFileReader(nil),
+			K8sValidator: validation.New(),
 		},
 	}
 }
diff --git a/pkg/cliClient/evaluation.go b/pkg/cliClient/evaluation.go
index d473c57..785984a 100644
--- a/pkg/cliClient/evaluation.go
+++ b/pkg/cliClient/evaluation.go
@@ -2,8 +2,9 @@ package cliClient
 
 import (
 	"encoding/json"
-	"github.com/datreeio/datree/pkg/extractor"
 	"net/http"
+
+	"github.com/datreeio/datree/pkg/extractor"
 )
 
 type Metadata struct {
@@ -16,26 +17,27 @@ type Metadata struct {
 type CreateEvaluationRequest struct {
 	CliId      string    `json:"cliId"`
 	Metadata   *Metadata `json:"metadata"`
-	K8sVersion string    `json:"k8sVersion"`
+	K8sVersion *string   `json:"k8sVersion"`
 }
 
 type CreateEvaluationResponse struct {
-	EvaluationId int `json:"evaluationId"`
+	EvaluationId int    `json:"evaluationId"`
+	K8sVersion   string `json:"k8sVersion"`
 }
 
-func (c *CliClient) CreateEvaluation(request *CreateEvaluationRequest) (int, error) {
+func (c *CliClient) CreateEvaluation(request *CreateEvaluationRequest) (*CreateEvaluationResponse, error) {
 	httpRes, err := c.httpClient.Request(http.MethodPost, "/cli/evaluation/create", request, nil)
 	if err != nil {
-		return 0, err
+		return nil, err
 	}
 
 	var res = &CreateEvaluationResponse{}
 	err = json.Unmarshal(httpRes.Body, &res)
 	if err != nil {
-		return 0, err
+		return nil, err
 	}
 
-	return res.EvaluationId, nil
+	return res, nil
 }
 
 type Match struct {


Test patch:
diff --git a/bl/evaluation/evaluator_test.go b/bl/evaluation/evaluator_test.go
index 1ebb56b..1d7e01c 100644
--- a/bl/evaluation/evaluator_test.go
+++ b/bl/evaluation/evaluator_test.go
@@ -1,10 +1,11 @@
 package evaluation
 
 import (
-	"github.com/datreeio/datree/bl/validation"
 	"path/filepath"
 	"testing"
 
+	"github.com/datreeio/datree/bl/validation"
+
 	"github.com/datreeio/datree/pkg/cliClient"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/mock"
@@ -14,9 +15,9 @@ type mockCliClient struct {
 	mock.Mock
 }
 
-func (m *mockCliClient) CreateEvaluation(createEvaluationRequest *cliClient.CreateEvaluationRequest) (int, error) {
+func (m *mockCliClient) CreateEvaluation(createEvaluationRequest *cliClient.CreateEvaluationRequest) (*cliClient.CreateEvaluationResponse, error) {
 	args := m.Called(createEvaluationRequest)
-	return args.Get(0).(int), args.Error(1)
+	return args.Get(0).(*cliClient.CreateEvaluationResponse), args.Error(1)
 }
 
 func (m *mockCliClient) RequestEvaluation(evaluationRequest *cliClient.EvaluationRequest) (*cliClient.EvaluationResponse, error) {
@@ -37,6 +38,7 @@ func (m *mockCliClient) GetVersionMessage(cliVersion string, timeout int) (*cliC
 type cliClientMockTestCase struct {
 	createEvaluation struct {
 		evaluationId int
+		k8sVersion   string
 		err          error
 	}
 	requestEvaluation struct {
@@ -62,7 +64,7 @@ func TestEvaluate(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
 			mockedCliClient := &mockCliClient{}
 
-			mockedCliClient.On("CreateEvaluation", mock.Anything).Return(tt.mock.cliClient.createEvaluation.evaluationId, tt.mock.cliClient.createEvaluation.err)
+			mockedCliClient.On("CreateEvaluation", mock.Anything).Return(tt.mock.cliClient.createEvaluation, tt.mock.cliClient.createEvaluation.err)
 			mockedCliClient.On("UpdateEvaluationValidation", mock.Anything).Return(nil)
 
 			evaluator := &Evaluator{
@@ -163,6 +165,7 @@ func request_evaluation_all_valid() *evaluateTestCase {
 			cliClient: &cliClientMockTestCase{
 				createEvaluation: struct {
 					evaluationId int
+					k8sVersion   string
 					err          error
 				}{
 					evaluationId: 1,
@@ -230,6 +233,7 @@ func request_evaluation_all_invalid() *evaluateTestCase {
 			cliClient: &cliClientMockTestCase{
 				createEvaluation: struct {
 					evaluationId int
+					k8sVersion   string
 					err          error
 				}{
 					evaluationId: 1,
diff --git a/cmd/test/main.go b/cmd/test/main.go
index bdafd3a..ee9a2bc 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"time"
 
+	"github.com/datreeio/datree/pkg/cliClient"
 	"github.com/datreeio/datree/pkg/extractor"
 
 	"github.com/briandowns/spinner"
@@ -17,7 +18,7 @@ import (
 
 type Evaluator interface {
 	Evaluate(validFilesPathsChan chan string, invalidFilesPaths chan *validation.InvalidFile, evaluationId int) (*evaluation.EvaluationResults, []*validation.InvalidFile, []*extractor.FileConfiguration, []*evaluation.Error, error)
-	CreateEvaluation(cliId string, cliVersion string, k8sVersion string) (int, error)
+	CreateEvaluation(cliId string, cliVersion string, k8sVersion string) (*cliClient.CreateEvaluationResponse, error)
 }
 
 type Messager interface {
@@ -26,6 +27,7 @@ type Messager interface {
 
 type K8sValidator interface {
 	ValidateResources(paths []string) (chan string, chan *validation.InvalidFile, chan error)
+	InitClient(k8sVersion string)
 }
 
 type TestCommandFlags struct {
@@ -73,7 +75,6 @@ func New(ctx *TestCommandContext) *cobra.Command {
 			}
 
 			testCommandFlags := TestCommandFlags{Output: outputFlag, K8sVersion: k8sVersion}
-			ctx.K8sValidator = validation.New(k8sVersion)
 			return test(ctx, args, testCommandFlags)
 		},
 		SilenceUsage:  true,
@@ -81,7 +82,7 @@ func New(ctx *TestCommandContext) *cobra.Command {
 	}
 
 	testCommand.Flags().StringP("output", "o", "", "Define output format")
-	testCommand.Flags().StringP("schema-version", "s", "1.18.0", "Set kubernetes version to validate against. Defaults to 1.18.0")
+	testCommand.Flags().StringP("schema-version", "s", "", "Set kubernetes version to validate against. Defaults to 1.18.0")
 	return testCommand
 }
 
@@ -109,12 +110,13 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
 	spinner := createSpinner(" Loading...", "cyan")
 	spinner.Start()
 
-	evaluationId, err := ctx.Evaluator.CreateEvaluation(ctx.LocalConfig.CliId, ctx.CliVersion, flags.K8sVersion)
+	createEvaluationResponse, err := ctx.Evaluator.CreateEvaluation(ctx.LocalConfig.CliId, ctx.CliVersion, flags.K8sVersion)
 	if err != nil {
 		fmt.Println(err.Error())
 		return err
 	}
 
+	ctx.K8sValidator.InitClient(createEvaluationResponse.K8sVersion)
 	validFilesPathsChan, invalidFilesPathsChan, errorsChan := ctx.K8sValidator.ValidateResources(paths)
 	go func() {
 		for err := range errorsChan {
@@ -122,7 +124,7 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
 		}
 	}()
 
-	results, invalidFiles, filesConfigurations, errors, err := ctx.Evaluator.Evaluate(validFilesPathsChan, invalidFilesPathsChan, evaluationId)
+	results, invalidFiles, filesConfigurations, errors, err := ctx.Evaluator.Evaluate(validFilesPathsChan, invalidFilesPathsChan, createEvaluationResponse.EvaluationId)
 
 	spinner.Stop()
 
@@ -138,7 +140,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, flags.K8sVersion)
+	err = evaluation.PrintResults(results, invalidFiles, evaluationSummary, fmt.Sprintf("https://app.datree.io/login?cliId=%s", ctx.LocalConfig.CliId), flags.Output, ctx.Printer, createEvaluationResponse.K8sVersion)
 
 	var invocationFailedErr error = nil
 
diff --git a/cmd/test/main_test.go b/cmd/test/main_test.go
index bf3fab0..889772b 100644
--- a/cmd/test/main_test.go
+++ b/cmd/test/main_test.go
@@ -4,6 +4,7 @@ import (
 	"testing"
 
 	"github.com/datreeio/datree/bl/validation"
+	"github.com/datreeio/datree/pkg/cliClient"
 	"github.com/datreeio/datree/pkg/extractor"
 	"github.com/datreeio/datree/pkg/printer"
 
@@ -22,9 +23,9 @@ func (m *mockEvaluator) Evaluate(validFilesPathsChan chan string, invalidFilesPa
 	return args.Get(0).(*evaluation.EvaluationResults), args.Get(1).([]*validation.InvalidFile), args.Get(2).([]*extractor.FileConfiguration), args.Get(3).([]*evaluation.Error), args.Error(4)
 }
 
-func (m *mockEvaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string) (int, error) {
+func (m *mockEvaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string) (*cliClient.CreateEvaluationResponse, error) {
 	args := m.Called(cliId, cliVersion, k8sVersion)
-	return args.Get(0).(int), args.Error(1)
+	return args.Get(0).(*cliClient.CreateEvaluationResponse), args.Error(1)
 }
 
 type mockMessager struct {
@@ -56,6 +57,9 @@ func (kv *K8sValidatorMock) ValidateResources(paths []string) (chan string, chan
 	return args.Get(0).(chan string), args.Get(1).(chan *validation.InvalidFile), args.Get(2).(chan error)
 }
 
+func (kv *K8sValidatorMock) InitClient(k8sVersion string) {
+}
+
 type PrinterMock struct {
 	mock.Mock
 }
@@ -103,8 +107,7 @@ func TestTestCommand(t *testing.T) {
 
 	mockedEvaluator := &mockEvaluator{}
 	mockedEvaluator.On("Evaluate", mock.Anything, mock.Anything, mock.Anything).Return(evaluationResults, invalidFiles, fileConfigurations, evaluationErrors, nil)
-	mockedEvaluator.On("CreateEvaluation", mock.Anything, mock.Anything, mock.Anything).Return(evaluationId, nil)
-
+	mockedEvaluator.On("CreateEvaluation", mock.Anything, mock.Anything, mock.Anything).Return(&cliClient.CreateEvaluationResponse{EvaluationId: evaluationId, K8sVersion: "1.18.0"}, nil)
 	messager := &mockMessager{}
 	messager.On("LoadVersionMessages", mock.Anything, mock.Anything)
 
@@ -114,6 +117,7 @@ func TestTestCommand(t *testing.T) {
 	invalidFilesChan := newInvalidFilesChan()
 
 	k8sValidatorMock.On("ValidateResources", mock.Anything).Return(validFilesPathChan, invalidFilesChan, newErrorsChan())
+	k8sValidatorMock.On("InitClient", mock.Anything).Return()
 
 	printerMock := &PrinterMock{}
 	printerMock.On("PrintWarnings", mock.Anything)
@@ -140,7 +144,7 @@ func TestTestCommand(t *testing.T) {
 }
 
 func test_testCommand_no_flags(t *testing.T, evaluator *mockEvaluator, validFilesPathChan chan string, invalidFilesChan chan *validation.InvalidFile, evaluationId int, ctx *TestCommandContext) {
-	test(ctx, []string{"8/*"}, TestCommandFlags{})
+	test(ctx, []string{"8/*"}, TestCommandFlags{K8sVersion: "", Output: ""})
 
 	evaluator.AssertCalled(t, "Evaluate", validFilesPathChan, invalidFilesChan, evaluationId)
 	evaluator.AssertNotCalled(t, "PrintFileParsingErrors")
diff --git a/pkg/cliClient/cliClient_test.go b/pkg/cliClient/cliClient_test.go
index 27d750a..0ee3a97 100644
--- a/pkg/cliClient/cliClient_test.go
+++ b/pkg/cliClient/cliClient_test.go
@@ -3,12 +3,13 @@ package cliClient
 import (
 	"bytes"
 	"encoding/json"
-	"gopkg.in/yaml.v3"
 	"io/ioutil"
 	"net/http"
 	"path/filepath"
 	"testing"
 
+	"gopkg.in/yaml.v3"
+
 	"github.com/datreeio/datree/pkg/extractor"
 	"github.com/datreeio/datree/pkg/httpClient"
 	"github.com/stretchr/testify/assert"
@@ -25,7 +26,7 @@ func (c *mockHTTPClient) Request(method string, resourceURI string, body interfa
 	return args.Get(0).(httpClient.Response), args.Error(1)
 }
 
-func (c *mockHTTPClient) name()  {
+func (c *mockHTTPClient) name() {
 
 }
 
@@ -140,11 +141,10 @@ func TestCreateRequestEvaluation(t *testing.T) {
 				httpClient: &httpClientMock,
 			}
 
-			actualEvaluationId, _ := client.CreateEvaluation(tt.args.createEvaluationRequest)
+			res, _ := client.CreateEvaluation(tt.args.createEvaluationRequest)
 
 			httpClientMock.AssertCalled(t, "Request", tt.expected.request.method, tt.expected.request.uri, tt.expected.request.body, tt.expected.request.headers)
-			assert.Equal(t, tt.expected.response.EvaluationId, actualEvaluationId)
-
+			assert.Equal(t, tt.expected.response, res)
 		})
 	}
 }
@@ -161,9 +161,8 @@ func TestGetVersionMessage(t *testing.T) {
 			mockedHTTPResponse := httpClient.Response{StatusCode: tt.mock.response.status, Body: body}
 			httpClientMock.On("Request", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mockedHTTPResponse, nil)
 
-
 			client := &CliClient{
-				baseUrl:    "http://cli-service.test.io",
+				baseUrl:       "http://cli-service.test.io",
 				timeoutClient: &httpClientMock,
 			}
 
@@ -312,13 +311,16 @@ func test_requestEvaluation_success() *RequestEvaluationTestCase {
 }
 
 func test_createEvaluation_success() *CreateEvaluationTestCase {
+	k8sVersion := "1.18.0"
+
 	return &CreateEvaluationTestCase{
 		name: "success - create evaluation",
 		args: struct {
 			createEvaluationRequest *CreateEvaluationRequest
 		}{
 			createEvaluationRequest: &CreateEvaluationRequest{
-				CliId: "cli_id",
+				K8sVersion: &k8sVersion,
+				CliId:      "cli_id",
 				Metadata: &Metadata{
 					CliVersion:      "0.0.1",
 					Os:              "darwin",
@@ -340,6 +342,7 @@ func test_createEvaluation_success() *CreateEvaluationTestCase {
 				status: http.StatusOK,
 				body: &CreateEvaluationResponse{
 					EvaluationId: 123,
+					K8sVersion:   k8sVersion,
 				},
 			},
 		},
@@ -361,7 +364,8 @@ func test_createEvaluation_success() *CreateEvaluationTestCase {
 				method: http.MethodPost,
 				uri:    "/cli/evaluation/create",
 				body: &CreateEvaluationRequest{
-					CliId: "cli_id",
+					K8sVersion: &k8sVersion,
+					CliId:      "cli_id",
 					Metadata: &Metadata{
 						CliVersion:      "0.0.1",
 						Os:              "darwin",
@@ -373,6 +377,7 @@ func test_createEvaluation_success() *CreateEvaluationTestCase {
 			},
 			response: &CreateEvaluationResponse{
 				EvaluationId: 123,
+				K8sVersion:   k8sVersion,
 			},
 		},
 	}