Skip to content

Commit

Permalink
enhance: add additional explain options to hubtest
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenceJJones committed Aug 2, 2024
1 parent 136dba6 commit b24632f
Showing 1 changed file with 58 additions and 32 deletions.
90 changes: 58 additions & 32 deletions cmd/crowdsec-cli/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (cli *cliHubTest) NewCommand() *cobra.Command {
cmd.AddCommand(cli.NewListCmd())
cmd.AddCommand(cli.NewCoverageCmd())
cmd.AddCommand(cli.NewEvalCmd())
cmd.AddCommand(cli.NewExplainCmd())
cmd.AddCommand(NewCLiHubtestExplain(cli.cfg).NewCommand())

return cmd
}
Expand Down Expand Up @@ -234,7 +234,6 @@ cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios
return cmd
}


func (cli *cliHubTest) run(runAll bool, NucleiTargetHost string, AppSecHost string, args []string) error {
cfg := cli.cfg()

Expand Down Expand Up @@ -271,7 +270,6 @@ func (cli *cliHubTest) run(runAll bool, NucleiTargetHost string, AppSecHost stri
return nil
}


func (cli *cliHubTest) NewRunCmd() *cobra.Command {
var (
noClean bool
Expand Down Expand Up @@ -701,46 +699,74 @@ func (cli *cliHubTest) NewEvalCmd() *cobra.Command {
return cmd
}

func (cli *cliHubTest) NewExplainCmd() *cobra.Command {
type cliHubtestExplain struct {
cfg configGetter
flags struct {
details bool
skipOk bool
}
}

func NewCLiHubtestExplain(cfg configGetter) *cliHubtestExplain {
return &cliHubtestExplain{
cfg: cfg,
}

Check warning on line 713 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L713

Added line #L713 was not covered by tests
}

func (cli *cliHubtestExplain) NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "explain",
Short: "explain [test_name]",
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, args []string) error {
for _, testName := range args {
test, err := HubTest.LoadTestItem(testName)
if err != nil {
return fmt.Errorf("can't load test: %+v", err)
}
err = test.ParserAssert.LoadTest(test.ParserResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}
return cli.run(args)
},

Check warning on line 724 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L723-L724

Added lines #L723 - L724 were not covered by tests
}

if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil {
return fmt.Errorf("unable to load parser result after run: %w", err)
}
}
flags := cmd.Flags()

err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}
flags.BoolVarP(&cli.flags.details, "verbose", "v", false, "Display individual changes")
flags.BoolVar(&cli.flags.skipOk, "failures", false, "Only show failed lines")

if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil {
return fmt.Errorf("unable to load scenario result after run: %w", err)
}
}
opts := dumps.DumpOpts{}
dumps.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts)
return cmd
}

func (cli *cliHubtestExplain) run(args []string) error {
for _, testName := range args {
test, err := HubTest.LoadTestItem(testName)
if err != nil {
return fmt.Errorf("can't load test: %+v", err)
}
err = test.ParserAssert.LoadTest(test.ParserResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)

Check warning on line 744 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L735-L744

Added lines #L735 - L744 were not covered by tests
}

return nil
},
if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil {
return fmt.Errorf("unable to load parser result after run: %w", err)
}

Check warning on line 749 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L747-L749

Added lines #L747 - L749 were not covered by tests
}

err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile)
if err != nil {
if err = test.Run(); err != nil {
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}

Check warning on line 756 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L752-L756

Added lines #L752 - L756 were not covered by tests

if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil {
return fmt.Errorf("unable to load scenario result after run: %w", err)
}

Check warning on line 760 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L758-L760

Added lines #L758 - L760 were not covered by tests
}

opts := dumps.DumpOpts{
Details: cli.flags.details,
SkipOk: cli.flags.skipOk,
}

dumps.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts)

Check warning on line 768 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L763-L768

Added lines #L763 - L768 were not covered by tests
}

return cmd
return nil

Check warning on line 771 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L771

Added line #L771 was not covered by tests
}

0 comments on commit b24632f

Please sign in to comment.