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

Commit

Permalink
feat: issue#647 export log to file (#938)
Browse files Browse the repository at this point in the history
* add the option to export log to a local file

* change position

* add validation for json file

* add test for validation

* change help sentence

* capital letter

* fix code bugs and not fail test when dir is not valid

* discard validation

* add test for checking invalid dir

---------

Co-authored-by: Alon Zylberberg <alon@Alons-MacBook-Pro.local>
Co-authored-by: Alon Zylberberg <alon@Alons-MBP.Home>
  • Loading branch information
3 people authored May 28, 2023
1 parent d3917ba commit abcea62
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type K8sValidator interface {

type TestCommandFlags struct {
Output string
SaveResults string
K8sVersion string
ExcludePattern string
IgnoreMissingSchemas bool
Expand All @@ -70,6 +71,7 @@ type TestCommandFlags struct {
func NewTestCommandFlags() *TestCommandFlags {
return &TestCommandFlags{
Output: "",
SaveResults: "",
K8sVersion: "",
ExcludePattern: "",
IgnoreMissingSchemas: false,
Expand Down Expand Up @@ -149,6 +151,7 @@ type CliClient interface {

type TestCommandData struct {
Output string
SaveResults string
K8sVersion string
ExcludePattern string
IgnoreMissingSchemas bool
Expand Down Expand Up @@ -275,6 +278,7 @@ func (flags *TestCommandFlags) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&flags.OnlyK8sFiles, "only-k8s-files", false, "Evaluate only valid yaml files with the properties 'apiVersion' and 'kind'. Ignore everything else")
cmd.Flags().BoolVar(&flags.Verbose, "verbose", false, "Display 'How to Fix' link")
cmd.Flags().BoolVar(&flags.NoRecord, "no-record", false, "Don’t send policy checks metadata to the backend")
cmd.Flags().StringVarP(&flags.SaveResults, "save-results", "", "", "Enter the path to the directory where you want to save the output file")

cmd.Flags().StringVar(&flags.SkipValidation, "skip-validation", "", "Skip validation step. Possible values: 'schema'")

Expand Down Expand Up @@ -349,6 +353,7 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont
}

testCommandOptions := &TestCommandData{Output: testCommandFlags.Output,
SaveResults: testCommandFlags.SaveResults,
K8sVersion: k8sVersion,
ExcludePattern: testCommandFlags.ExcludePattern,
IgnoreMissingSchemas: testCommandFlags.IgnoreMissingSchemas,
Expand Down Expand Up @@ -465,7 +470,7 @@ func test(ctx *TestCommandContext, paths []string, testCommandData *TestCommandD
PassedPolicyCheckCount: passedPolicyCheckCount,
}

err = evaluation.PrintResults(&evaluation.PrintResultsData{
evaluationData := &evaluation.PrintResultsData{
Results: results,
AdditionalJUnitData: evaluationResultData.AdditionalJUnitData,
InvalidYamlFiles: validationManager.InvalidYamlFiles(),
Expand All @@ -481,7 +486,20 @@ func test(ctx *TestCommandContext, paths []string, testCommandData *TestCommandD
CliVersion: ctx.CliVersion,
IsCI: ctx.CiContext.IsCI,
Quiet: testCommandData.Quiet,
})
}
err = evaluation.PrintResults(evaluationData)

if testCommandData.SaveResults != "" {
resultsText, err := evaluation.GetjsonResult(evaluationData)
if err != nil {
return err
}

err = ioutil.WriteFile(testCommandData.SaveResults, []byte(resultsText), 0666)
if err != nil {
return err
}
}

if evaluationResultData.PromptMessage != "" {
ctx.Printer.PrintPromptMessage(evaluationResultData.PromptMessage)
Expand Down
10 changes: 10 additions & 0 deletions cmd/test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ func TestTestCommandFlagsValidation(t *testing.T) {
test_testCommand_output_flags_validation(t, ctx)
test_testCommand_version_flags_validation(t, ctx)
test_testCommand_no_record_flag(t, ctx)
test_testCommand_save_results_flag(t, ctx)
}

func TestTestCommandEmptyDir(t *testing.T) {
Expand Down Expand Up @@ -918,6 +919,15 @@ func test_testCommand_no_record_flag(t *testing.T, ctx *TestCommandContext) {
assert.Equal(t, ViolationsFoundError, err)
}

func test_testCommand_save_results_flag(t *testing.T, ctx *TestCommandContext) {
flags := TestCommandFlags{SaveResults: "./"}
err := flags.Validate()
assert.NoError(t, err)

err = executeTestCommand(ctx, []string{"8/*", "--save-results=" + "../non-exsisted-dir/test.json"})
assert.EqualError(t, err, "open ../non-exsisted-dir/test.json: no such file or directory")
}

func newFilesConfigurationsChan(path string) chan *extractor.FileConfigurations {
filesConfigurationsChan := make(chan *extractor.FileConfigurations, 1)

Expand Down

0 comments on commit abcea62

Please sign in to comment.