Skip to content

Commit

Permalink
pass patternDir to Run()
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 2, 2025
1 parent 01a1456 commit 0946611
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 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

Check warning on line 19 in cmd/crowdsec-cli/clihubtest/explain.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihubtest/explain.go#L17-L19

Added lines #L17 - L19 were not covered by tests
err = test.ParserAssert.LoadTest(test.ParserResultFile)
if err != nil {
if err = test.Run(); err != nil {
if err = test.Run(patternDir); err != nil {

Check warning on line 22 in cmd/crowdsec-cli/clihubtest/explain.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihubtest/explain.go#L22

Added line #L22 was not covered by tests
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 {

Check warning on line 33 in cmd/crowdsec-cli/clihubtest/explain.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihubtest/explain.go#L33

Added line #L33 was not covered by tests
return fmt.Errorf("running test '%s' failed: %+v", test.Name, err)
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/crowdsec-cli/clihubtest/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ func (cli *cliHubTest) NewCommand() *cobra.Command {
DisableAutoGenTag: true,
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
var err error
cfg := cli.cfg()
HubTest, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, false, cfg.ConfigPaths.PatternDir)
HubTest, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, false)
if err != nil {
return fmt.Errorf("unable to load hubtest: %+v", err)
}

HubAppsecTests, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, true, cfg.ConfigPaths.PatternDir)
HubAppsecTests, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, true)
if err != nil {
return fmt.Errorf("unable to load appsec specific hubtest: %+v", 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

Check warning on line 46 in cmd/crowdsec-cli/clihubtest/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihubtest/run.go#L45-L46

Added lines #L45 - L46 were not covered by tests
for _, test := range hubPtr.Tests {
if cfg.Cscli.Output == "human" {
log.Infof("Running test '%s'", test.Name)
}

err := test.Run()
err := test.Run(patternDir)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L52 was not covered by tests
if err != nil {
log.Errorf("running test '%s' failed: %+v", test.Name, err)
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/hubtest/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type HubTest struct {

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

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

func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool, patternDir string) (HubTest, error) {
func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool) (HubTest, error) {
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 @@ -117,7 +116,6 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
NucleiTargetHost: DefaultNucleiTarget,
AppSecHost: DefaultAppsecHost,
HubIndex: hub,
PatternDir: patternDir,
}, nil
}

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

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

NucleiTargetHost string
AppSecHost string
PatternDir string
}

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

Expand Down Expand Up @@ -384,7 +382,7 @@ func createDirs(dirs []string) error {
return nil
}

func (t *HubTestItem) RunWithLogFile() error {
func (t *HubTestItem) RunWithLogFile(patternDir string) error {

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

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L385

Added line #L385 was not covered by tests
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 @@ -420,8 +418,8 @@ func (t *HubTestItem) RunWithLogFile() error {
}

// copy template patterns folder to runtime folder
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)
if err = CopyDir(patternDir, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", patternDir, t.RuntimePatternsPath, err)

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

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L421-L422

Added lines #L421 - L422 were not covered by tests
}

// 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 {

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

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L567

Added line #L567 was not covered by tests
var err error

t.Success = false
Expand Down Expand Up @@ -597,8 +595,8 @@ func (t *HubTestItem) Run() error {
}

// copy template patterns folder to runtime folder
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)
if err = CopyDir(patternDir, t.RuntimePatternsPath); err != nil {
return fmt.Errorf("unable to copy 'patterns' from '%s' to '%s': %w", patternDir, t.RuntimePatternsPath, err)

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

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L598-L599

Added lines #L598 - L599 were not covered by tests
}

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

if t.Config.LogFile != "" {
return t.RunWithLogFile()
return t.RunWithLogFile(patternDir)

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

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/hubtest_item.go#L633

Added line #L633 was not covered by tests
} else if t.Config.NucleiTemplate != "" {
return t.RunWithNucleiTemplate()
}
Expand Down

0 comments on commit 0946611

Please sign in to comment.