Skip to content

Commit

Permalink
enhancement: Hubtest respect patterndir option set via config.yaml (#…
Browse files Browse the repository at this point in the history
…3386)

* enhancement: Hubtest respect patterndir option set via config.yaml

* pass patternDir to Run()

---------

Co-authored-by: marco <[email protected]>
  • Loading branch information
LaurenceJJones and mmetc authored Jan 2, 2025
1 parent 4e6e6de commit f938b0c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
7 changes: 5 additions & 2 deletions cmd/crowdsec-cli/clihubtest/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ func (cli *cliHubTest) explain(testName string, details bool, skipOk bool) error
return fmt.Errorf("can't load test: %+v", err)
}

cfg := cli.cfg()
patternDir := cfg.ConfigPaths.PatternDir

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

Expand All @@ -27,7 +30,7 @@ func (cli *cliHubTest) explain(testName string, details bool, skipOk bool) error

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

Expand Down
4 changes: 3 additions & 1 deletion cmd/crowdsec-cli/clihubtest/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ func (cli *cliHubTest) run(runAll bool, nucleiTargetHost string, appSecHost stri
// set timezone to avoid DST issues
os.Setenv("TZ", "UTC")

patternDir := cfg.ConfigPaths.PatternDir

for _, test := range hubPtr.Tests {
if cfg.Cscli.Output == "human" {
log.Infof("Running test '%s'", test.Name)
}

err := test.Run()
err := test.Run(patternDir)
if err != nil {
log.Errorf("running test '%s' failed: %+v", test.Name, err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func loadConfigFor(command string) (*csconfig.Config, string, error) {
"help",
"completion",
"version",
"hubtest",
}

if !slices.Contains(noNeedConfig, command) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/hubtest/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type HubTest struct {
NucleiTargetHost string
AppSecHost string

HubIndex *cwhub.Hub
Tests []*HubTestItem
HubIndex *cwhub.Hub
Tests []*HubTestItem
}

const (
Expand Down
18 changes: 7 additions & 11 deletions pkg/hubtest/hubtest_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func createDirs(dirs []string) error {
return nil
}

func (t *HubTestItem) RunWithLogFile() error {
func (t *HubTestItem) RunWithLogFile(patternDir string) error {
testPath := filepath.Join(t.HubTestPath, t.Name)
if _, err := os.Stat(testPath); os.IsNotExist(err) {
return fmt.Errorf("test '%s' doesn't exist in '%s', exiting", t.Name, t.HubTestPath)
Expand Down Expand Up @@ -417,11 +417,9 @@ func (t *HubTestItem) RunWithLogFile() error {
return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateSimulationPath, t.RuntimeSimulationFilePath, err)
}

crowdsecPatternsFolder := csconfig.DefaultConfigPath("patterns")

// copy template patterns folder to runtime folder
if err = CopyDir(crowdsecPatternsFolder, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", crowdsecPatternsFolder, t.RuntimePatternsPath, err)
if err = CopyDir(patternDir, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", patternDir, t.RuntimePatternsPath, err)
}

// install the hub in the runtime folder
Expand Down Expand Up @@ -566,7 +564,7 @@ func (t *HubTestItem) RunWithLogFile() error {
return nil
}

func (t *HubTestItem) Run() error {
func (t *HubTestItem) Run(patternDir string) error {
var err error

t.Success = false
Expand Down Expand Up @@ -596,11 +594,9 @@ func (t *HubTestItem) Run() error {
return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateSimulationPath, t.RuntimeSimulationFilePath, err)
}

crowdsecPatternsFolder := csconfig.DefaultConfigPath("patterns")

// copy template patterns folder to runtime folder
if err = CopyDir(crowdsecPatternsFolder, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", crowdsecPatternsFolder, t.RuntimePatternsPath, err)
if err = CopyDir(patternDir, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", patternDir, t.RuntimePatternsPath, err)
}

// create the appsec-configs dir
Expand Down Expand Up @@ -634,7 +630,7 @@ func (t *HubTestItem) Run() error {
}

if t.Config.LogFile != "" {
return t.RunWithLogFile()
return t.RunWithLogFile(patternDir)
} else if t.Config.NucleiTemplate != "" {
return t.RunWithNucleiTemplate()
}
Expand Down

0 comments on commit f938b0c

Please sign in to comment.