enhancement: Hubtest respect patterndir option set via config.yaml (#3386)

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

* pass patternDir to Run()

---------

Co-authored-by: marco <marco@crowdsec.net>
This commit is contained in:
Laurence Jones 2025-01-02 14:21:03 +00:00 committed by GitHub
parent 4e6e6dec65
commit f938b0c602
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 17 additions and 17 deletions

View file

@ -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)
}
@ -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)
}

View file

@ -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)
}

View file

@ -91,7 +91,6 @@ func loadConfigFor(command string) (*csconfig.Config, string, error) {
"help",
"completion",
"version",
"hubtest",
}
if !slices.Contains(noNeedConfig, command) {

View file

@ -25,8 +25,8 @@ type HubTest struct {
NucleiTargetHost string
AppSecHost string
HubIndex *cwhub.Hub
Tests []*HubTestItem
HubIndex *cwhub.Hub
Tests []*HubTestItem
}
const (

View file

@ -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)
@ -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
@ -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
@ -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
@ -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()
}