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

Commit

Permalink
test: add test cases for test command (#410)
Browse files Browse the repository at this point in the history
* test: add test cases for test command

Co-authored-by: Roy Hadad <roy@datree.io>
  • Loading branch information
Abhra303 and royhadad authored Apr 3, 2022
1 parent ca00128 commit 3a78d0e
Show file tree
Hide file tree
Showing 10 changed files with 565 additions and 30 deletions.
15 changes: 13 additions & 2 deletions bl/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import (

type UnknownStruct map[string]interface{}

func ExtractFilesConfigurations(paths []string, concurrency int) (chan *extractor.FileConfigurations, chan *extractor.InvalidFile) {
type FilesExtractor struct{}

type FilesExtractorInterface interface {
ExtractFilesConfigurations(paths []string, concurrency int) (chan *extractor.FileConfigurations, chan *extractor.InvalidFile)
ExtractYamlFileToUnknownStruct(path string) (UnknownStruct, error)
}

func New() *FilesExtractor {
return &FilesExtractor{}
}

func (f *FilesExtractor) ExtractFilesConfigurations(paths []string, concurrency int) (chan *extractor.FileConfigurations, chan *extractor.InvalidFile) {
filesConfigurationsChan := make(chan *extractor.FileConfigurations, concurrency)
invalidFilesChan := make(chan *extractor.InvalidFile, concurrency)

Expand All @@ -37,7 +48,7 @@ func ExtractFilesConfigurations(paths []string, concurrency int) (chan *extracto
return filesConfigurationsChan, invalidFilesChan
}

func ExtractYamlFileToUnknownStruct(path string) (UnknownStruct, error) {
func (f *FilesExtractor) ExtractYamlFileToUnknownStruct(path string) (UnknownStruct, error) {
absolutePath, err := extractor.ToAbsolutePath(path)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions bl/files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ func test_directory_file() *toAbsolutePathsTestCase {
}

func TestExtractYamlFileToUnknownStruct(t *testing.T) {
filesExtractor := FilesExtractor{}
t.Run("valid yaml file, should return an unknown struct and no error", func(t *testing.T) {
actualResult, actualErr := ExtractYamlFileToUnknownStruct("../../internal/fixtures/policyAsCode/valid-schema.yaml")
actualResult, actualErr := filesExtractor.ExtractYamlFileToUnknownStruct("../../internal/fixtures/policyAsCode/valid-schema.yaml")
assert.NotEqual(t, nil, actualResult)
assert.Equal(t, nil, actualErr)
})

t.Run("invalid yaml file, should return an error", func(t *testing.T) {
actualResult, actualErr := ExtractYamlFileToUnknownStruct("../../internal/fixtures/policyAsCode/invalid-yaml.yaml")
actualResult, actualErr := filesExtractor.ExtractYamlFileToUnknownStruct("../../internal/fixtures/policyAsCode/invalid-yaml.yaml")
assert.Equal(t, UnknownStruct(nil), actualResult)
assert.NotEqual(t, nil, actualErr)
assert.Equal(t, errors.New("yaml: line 2: did not find expected key"), actualErr)
Expand Down
3 changes: 2 additions & 1 deletion cmd/publish/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type PublishCommandContext struct {
Messager Messager
Printer Printer
PublishCliClient CliClient
FilesExtractor files.FilesExtractorInterface
}

func New(ctx *PublishCommandContext) *cobra.Command {
Expand Down Expand Up @@ -103,7 +104,7 @@ type MessagesContext struct {
}

func publish(ctx *PublishCommandContext, path string, localConfigContent *localConfig.LocalConfig) (*cliClient.PublishFailedResponse, error) {
policiesConfiguration, err := files.ExtractYamlFileToUnknownStruct(path)
policiesConfiguration, err := ctx.FilesExtractor.ExtractYamlFileToUnknownStruct(path)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/publish/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestPublishCommand(t *testing.T) {
Messager: messagerMock,
Printer: printerMock,
PublishCliClient: publishClientMock,
FilesExtractor: &files.FilesExtractor{},
}

localConfigContent, _ := ctx.LocalConfig.GetLocalConfiguration()
Expand Down
21 changes: 13 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/datreeio/datree/bl/evaluation"
"github.com/datreeio/datree/bl/files"
"github.com/datreeio/datree/bl/messager"
"github.com/datreeio/datree/bl/validation"
"github.com/datreeio/datree/cmd/completion"
Expand Down Expand Up @@ -34,14 +35,15 @@ 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,
K8sValidator: app.context.K8sValidator,
CliClient: app.context.CliClient,
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,
CliClient: app.context.CliClient,
FilesExtractor: app.context.FilesExtractor,
}))

rootCmd.AddCommand(kustomize.New(&test.TestCommandContext{
Expand Down Expand Up @@ -74,6 +76,7 @@ func init() {
Messager: app.context.Messager,
Printer: app.context.Printer,
PublishCliClient: app.context.CliClient,
FilesExtractor: app.context.FilesExtractor,
}))

rootCmd.AddCommand(completion.New())
Expand All @@ -98,6 +101,7 @@ type context struct {
K8sValidator *validation.K8sValidator
JSONSchemaValidator *jsonSchemaValidator.JSONSchemaValidator
CommandRunner *executor.CommandRunner
FilesExtractor *files.FilesExtractor
}

type app struct {
Expand All @@ -121,6 +125,7 @@ func startup() *app {
K8sValidator: validation.New(),
JSONSchemaValidator: jsonSchemaValidator.New(),
CommandRunner: executor.CreateNewCommandRunner(),
FilesExtractor: files.New(),
},
}
}
19 changes: 10 additions & 9 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ type TestCommandData struct {
}

type TestCommandContext struct {
CliVersion string
LocalConfig LocalConfig
Evaluator Evaluator
Messager Messager
K8sValidator K8sValidator
Printer EvaluationPrinter
Reader Reader
CliClient CliClient
CliVersion string
LocalConfig LocalConfig
Evaluator Evaluator
Messager Messager
K8sValidator K8sValidator
Printer EvaluationPrinter
Reader Reader
CliClient CliClient
FilesExtractor files.FilesExtractorInterface
}

func LoadVersionMessages(ctx *TestCommandContext, args []string, cmd *cobra.Command) error {
Expand Down Expand Up @@ -416,7 +417,7 @@ func evaluate(ctx *TestCommandContext, filesPaths []string, prerunData *TestComm

concurrency := 100

validYamlConfigurationsChan, invalidYamlFilesChan := files.ExtractFilesConfigurations(filesPaths, concurrency)
validYamlConfigurationsChan, invalidYamlFilesChan := ctx.FilesExtractor.ExtractFilesConfigurations(filesPaths, concurrency)

validationManager.AggregateInvalidYamlFiles(invalidYamlFilesChan)

Expand Down
Loading

0 comments on commit 3a78d0e

Please sign in to comment.