Commit eab59cd1 authored by shmuelsa's avatar shmuelsa
Browse files

fix: set simple format as default for CI env

parent 62e84471
Showing with 22 additions and 30 deletions
+22 -30
......@@ -17,14 +17,12 @@ type CLIClient interface {
type Evaluator struct {
cliClient CLIClient
osInfo *OSInfo
ciContext *ciContext.CIContext
}
func New(c CLIClient) *Evaluator {
return &Evaluator{
cliClient: c,
osInfo: NewOSInfo(),
ciContext: ciContext.Extract(),
}
}
......@@ -43,7 +41,7 @@ type EvaluationResults struct {
}
}
func (e *Evaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string, policyName string) (*cliClient.CreateEvaluationResponse, error) {
func (e *Evaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string, policyName string, ciContext *ciContext.CIContext) (*cliClient.CreateEvaluationResponse, error) {
createEvaluationResponse, err := e.cliClient.CreateEvaluation(&cliClient.CreateEvaluationRequest{
K8sVersion: &k8sVersion,
CliId: cliId,
......@@ -53,7 +51,7 @@ func (e *Evaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion
Os: e.osInfo.OS,
PlatformVersion: e.osInfo.PlatformVersion,
KernelVersion: e.osInfo.KernelVersion,
CIContext: e.ciContext,
CIContext: ciContext,
},
})
......
......@@ -73,21 +73,21 @@ func TestCreateEvaluation(t *testing.T) {
PlatformVersion: "1.2.3",
KernelVersion: "4.5.6",
},
ciContext: &ciContext.CIContext{
IsCI: true,
CIEnv: "travis",
},
}
cliId := "test_token"
cliVersion := "0.0.7"
k8sVersion := "1.18.1"
policyName := "Default"
ciContext := &ciContext.CIContext{
IsCI: true,
CIEnv: "travis",
}
mockedCliClient.On("CreateEvaluation", mock.Anything).Return(&cliClient.CreateEvaluationResponse{EvaluationId: 1, K8sVersion: k8sVersion}, nil)
expectedCreateEvaluationResponse := &cliClient.CreateEvaluationResponse{EvaluationId: 1, K8sVersion: k8sVersion}
createEvaluationResponse, _ := evaluator.CreateEvaluation(cliId, cliVersion, k8sVersion, policyName)
createEvaluationResponse, _ := evaluator.CreateEvaluation(cliId, cliVersion, k8sVersion, policyName, ciContext)
mockedCliClient.AssertCalled(t, "CreateEvaluation", mock.Anything)
assert.Equal(t, expectedCreateEvaluationResponse, createEvaluationResponse)
......@@ -109,7 +109,6 @@ func TestEvaluate(t *testing.T) {
evaluator := &Evaluator{
cliClient: mockedCliClient,
osInfo: tt.args.osInfo,
ciContext: tt.args.ciContext,
}
// TODO: define and check the rest of the values
......@@ -129,7 +128,6 @@ func TestEvaluate(t *testing.T) {
type evaluateArgs struct {
validFilesConfigurations []*extractor.FileConfigurations
osInfo *OSInfo
ciContext *ciContext.CIContext
isInteractiveMode bool
rulesCount int
response *cliClient.CreateEvaluationResponse
......@@ -167,10 +165,6 @@ func request_evaluation_all_valid() *evaluateTestCase {
PlatformVersion: "1.2.3",
KernelVersion: "4.5.6",
},
ciContext: &ciContext.CIContext{
IsCI: true,
CIEnv: "travis",
},
isInteractiveMode: true,
},
mock: &evaluatorMock{
......@@ -240,10 +234,6 @@ func request_evaluation_all_invalid() *evaluateTestCase {
PlatformVersion: "1.2.3",
KernelVersion: "4.5.6",
},
ciContext: &ciContext.CIContext{
IsCI: true,
CIEnv: "travis",
},
isInteractiveMode: true,
},
mock: &evaluatorMock{
......
......@@ -26,7 +26,7 @@ import (
type Evaluator interface {
Evaluate(filesConfigurations []*extractor.FileConfigurations, evaluationResponse *cliClient.CreateEvaluationResponse, isInteractiveMode bool) (evaluation.ResultType, error)
CreateEvaluation(cliId string, cliVersion string, k8sVersion string, policyName string) (*cliClient.CreateEvaluationResponse, error)
CreateEvaluation(cliId string, cliVersion string, k8sVersion string, policyName string, ciContext *ciContext.CIContext) (*cliClient.CreateEvaluationResponse, error)
UpdateFailedYamlValidation(invalidFiles []*validation.InvalidYamlFile, evaluationId int, stopEvaluation bool) error
UpdateFailedK8sValidation(invalidFiles []*validation.InvalidK8sFile, evaluationId int, stopEvaluation bool) error
}
......@@ -73,12 +73,6 @@ func (flags *TestCommandFlags) Validate() error {
}
func (flags *TestCommandFlags) Transform() {
if flags.Output == "" && ciContext.Extract().IsCI {
flags.Output = "simple"
}
}
type EvaluationPrinter interface {
PrintWarnings(warnings []printer.Warning)
PrintSummaryTable(summary printer.Summary)
......@@ -170,12 +164,15 @@ func New(ctx *TestCommandContext) *cobra.Command {
testCommandFlags := TestCommandFlags{Output: outputFlag, K8sVersion: k8sVersion, IgnoreMissingSchemas: ignoreMissingSchemas, PolicyName: policy, SchemaLocations: schemaLocations, OnlyK8sFiles: onlyK8sFiles}
err = testCommandFlags.Validate()
testCommandFlags.Transform()
if err != nil {
return err
}
if testCommandFlags.Output == "" && ciContext.Extract().IsCI {
testCommandFlags.Output = "simple"
}
err = test(ctx, args, testCommandFlags)
if err != nil {
return err
......@@ -184,7 +181,11 @@ func New(ctx *TestCommandContext) *cobra.Command {
},
}
testCommand.Flags().StringP("output", "o", "", "Define output format")
if ciContext.Extract().IsCI {
testCommand.Flags().StringP("output", "o", "simple", "Define output format")
} else {
testCommand.Flags().StringP("output", "o", "", "Define output format")
}
testCommand.Flags().StringP("schema-version", "s", "", "Set kubernetes version to validate against. Defaults to 1.18.0")
testCommand.Flags().StringP("policy", "p", "", "Policy name to run against")
testCommand.Flags().Bool("only-k8s-files", false, "Evaluate only valid yaml files with the properties 'apiVersion' and 'kind'. Ignore everything else")
......@@ -324,7 +325,9 @@ func evaluate(ctx *TestCommandContext, filesPaths []string, flags TestCommandFla
validationManager := &ValidationManager{}
filesPathsLen := len(filesPaths)
createEvaluationResponse, err := ctx.Evaluator.CreateEvaluation(cliId, ctx.CliVersion, flags.K8sVersion, flags.PolicyName)
ciContext := ciContext.Extract()
createEvaluationResponse, err := ctx.Evaluator.CreateEvaluation(cliId, ctx.CliVersion, flags.K8sVersion, flags.PolicyName, ciContext)
if err != nil {
return validationManager, nil, evaluation.ResultType{}, err
}
......
......@@ -4,6 +4,7 @@ import (
"testing"
"github.com/datreeio/datree/bl/validation"
"github.com/datreeio/datree/pkg/ciContext"
"github.com/datreeio/datree/pkg/cliClient"
"github.com/datreeio/datree/pkg/extractor"
"github.com/datreeio/datree/pkg/printer"
......@@ -24,7 +25,7 @@ func (m *mockEvaluator) Evaluate(filesConfigurationsChan []*extractor.FileConfig
return args.Get(0).(evaluation.ResultType), args.Error(1)
}
func (m *mockEvaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string, policyName string) (*cliClient.CreateEvaluationResponse, error) {
func (m *mockEvaluator) CreateEvaluation(cliId string, cliVersion string, k8sVersion string, policyName string, ciContext *ciContext.CIContext) (*cliClient.CreateEvaluationResponse, error) {
args := m.Called(cliId, cliVersion, k8sVersion, policyName)
return args.Get(0).(*cliClient.CreateEvaluationResponse), args.Error(1)
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment