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
  • Loading branch information
LaurenceJJones committed Dec 29, 2024
1 parent 5c0c4f9 commit 01a1456
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
5 changes: 3 additions & 2 deletions cmd/crowdsec-cli/clihubtest/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ func (cli *cliHubTest) NewCommand() *cobra.Command {
DisableAutoGenTag: true,
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
var err error
HubTest, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, false)
cfg := cli.cfg()
HubTest, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, false, cfg.ConfigPaths.PatternDir)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihubtest/hubtest.go#L46-L47

Added lines #L46 - L47 were not covered by tests
if err != nil {
return fmt.Errorf("unable to load hubtest: %+v", err)
}

HubAppsecTests, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, true)
HubAppsecTests, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, true, cfg.ConfigPaths.PatternDir)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihubtest/hubtest.go#L52

Added line #L52 was not covered by tests
if err != nil {
return fmt.Errorf("unable to load appsec specific hubtest: %+v", 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
9 changes: 6 additions & 3 deletions pkg/hubtest/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type HubTest struct {
NucleiTargetHost string
AppSecHost string

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

const (
Expand Down Expand Up @@ -58,7 +59,7 @@ http:
`
)

func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool) (HubTest, error) {
func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool, patternDir string) (HubTest, error) {

Check warning on line 62 in pkg/hubtest/hubtest.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest.go#L62

Added line #L62 was not covered by tests
hubPath, err := filepath.Abs(hubPath)
if err != nil {
return HubTest{}, fmt.Errorf("can't get absolute path of hub: %+v", err)
Expand Down Expand Up @@ -116,6 +117,7 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
NucleiTargetHost: DefaultNucleiTarget,
AppSecHost: DefaultAppsecHost,
HubIndex: hub,
PatternDir: patternDir,

Check warning on line 120 in pkg/hubtest/hubtest.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest.go#L120

Added line #L120 was not covered by tests
}, nil
}

Expand Down Expand Up @@ -149,6 +151,7 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
TemplateProfilePath: filepath.Join(HubTestPath, templateProfileFile),
TemplateSimulationPath: filepath.Join(HubTestPath, templateSimulationFile),
HubIndex: hub,
PatternDir: patternDir,

Check warning on line 154 in pkg/hubtest/hubtest.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest.go#L154

Added line #L154 was not covered by tests
}, nil
}

Expand Down
14 changes: 6 additions & 8 deletions pkg/hubtest/hubtest_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type HubTestItem struct {

NucleiTargetHost string
AppSecHost string
PatternDir string
}

const (
Expand Down Expand Up @@ -159,6 +160,7 @@ func NewTest(name string, hubTest *HubTest) (*HubTestItem, error) {
CustomItemsLocation: []string{hubTest.HubPath, testPath},
NucleiTargetHost: hubTest.NucleiTargetHost,
AppSecHost: hubTest.AppSecHost,
PatternDir: hubTest.PatternDir,

Check warning on line 163 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L163

Added line #L163 was not covered by tests
}, nil
}

Expand Down Expand Up @@ -417,11 +419,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(t.PatternDir, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", t.PatternDir, t.RuntimePatternsPath, err)

Check warning on line 424 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L423-L424

Added lines #L423 - L424 were not covered by tests
}

// install the hub in the runtime folder
Expand Down Expand Up @@ -596,11 +596,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(t.PatternDir, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", t.PatternDir, t.RuntimePatternsPath, err)

Check warning on line 601 in pkg/hubtest/hubtest_item.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L600-L601

Added lines #L600 - L601 were not covered by tests
}

// create the appsec-configs dir
Expand Down

0 comments on commit 01a1456

Please sign in to comment.