Skip to content

Commit 0459a9a

Browse files
authored
update appsec test runner (#3518)
1 parent 663dad0 commit 0459a9a

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

cmd/crowdsec-cli/clihubtest/run.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func (cli *cliHubTest) run(ctx context.Context, all bool, nucleiTargetHost strin
4545

4646
var eg errgroup.Group
4747

48+
if isAppsecTest {
49+
log.Info("Appsec tests can not run in parallel: setting max_jobs=1")
50+
51+
maxJobs = 1
52+
}
53+
4854
eg.SetLimit(int(maxJobs))
4955

5056
for _, test := range hubPtr.Tests {
@@ -215,7 +221,7 @@ func (cli *cliHubTest) newRunCmd() *cobra.Command {
215221
cmd.Flags().StringVar(&appSecHost, "host", hubtest.DefaultAppsecHost, "Address to expose AppSec for hubtest")
216222
cmd.Flags().BoolVar(&all, "all", false, "Run all tests")
217223
cmd.Flags().BoolVar(&reportSuccess, "report-success", false, "Report successful tests too (implied with json output)")
218-
cmd.Flags().UintVar(&maxJobs, "max-jobs", maxJobs, "Run <num> batch")
224+
cmd.Flags().UintVar(&maxJobs, "max-jobs", maxJobs, "Max number of concurrent tests (does not apply to appsec)")
219225

220226
return cmd
221227
}

pkg/hubtest/hubtest.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
6464
return HubTest{}, fmt.Errorf("can't get absolute path of hub: %w", err)
6565
}
6666

67+
sharedDataDir := filepath.Join(hubPath, ".cache", "data")
68+
if err = os.MkdirAll(sharedDataDir, 0o700); err != nil {
69+
return HubTest{}, fmt.Errorf("while creating data dir: %w", err)
70+
}
71+
6772
// we can't use hubtest without the hub
6873
if _, err = os.Stat(hubPath); os.IsNotExist(err) {
6974
return HubTest{}, fmt.Errorf("path to hub '%s' doesn't exist, can't run", hubPath)
@@ -90,7 +95,7 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
9095
HubDir: hubPath,
9196
HubIndexFile: hubIndexFile,
9297
InstallDir: HubTestPath,
93-
InstallDataDir: HubTestPath,
98+
InstallDataDir: sharedDataDir,
9499
}
95100

96101
hub, err := cwhub.NewHub(local, nil)
@@ -105,6 +110,7 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
105110
return HubTest{
106111
CrowdSecPath: crowdsecPath,
107112
CscliPath: cscliPath,
113+
DataDir: sharedDataDir,
108114
HubPath: hubPath,
109115
HubTestPath: HubTestPath,
110116
HubIndexFile: hubIndexFile,
@@ -127,7 +133,7 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
127133
HubDir: hubPath,
128134
HubIndexFile: hubIndexFile,
129135
InstallDir: HubTestPath,
130-
InstallDataDir: HubTestPath,
136+
InstallDataDir: sharedDataDir,
131137
}
132138

133139
hub, err := cwhub.NewHub(local, nil)
@@ -139,15 +145,10 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
139145
return HubTest{}, err
140146
}
141147

142-
dataDir := filepath.Join(hubPath, ".cache", "data")
143-
if err = os.MkdirAll(dataDir, 0o700); err != nil {
144-
return HubTest{}, fmt.Errorf("while creating data dir: %w", err)
145-
}
146-
147148
return HubTest{
148149
CrowdSecPath: crowdsecPath,
149150
CscliPath: cscliPath,
150-
DataDir: dataDir,
151+
DataDir: sharedDataDir,
151152
HubPath: hubPath,
152153
HubTestPath: HubTestPath,
153154
HubIndexFile: hubIndexFile,

pkg/hubtest/hubtest_item.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (t *HubTestItem) RunWithNucleiTemplate() error {
290290
return fmt.Errorf("test '%s' doesn't exist in '%s', exiting", t.Name, t.HubTestPath)
291291
}
292292

293-
crowdsecLogFile := fmt.Sprintf("%s/log/crowdsec.log", t.RuntimePath)
293+
crowdsecLogFile := filepath.Join(t.RuntimePath, "log", "crowdsec.log")
294294

295295
// machine add
296296
cmdArgs := []string{"-c", t.RuntimeConfigFilePath, "machines", "add", "testMachine", "--force", "--auto"}
@@ -363,7 +363,10 @@ func (t *HubTestItem) RunWithNucleiTemplate() error {
363363
},
364364
}
365365

366-
err = nucleiConfig.RunNucleiTemplate(t.Name, t.Config.NucleiTemplate, t.NucleiTargetHost)
366+
// the value in config is relative
367+
nucleiTemplate := filepath.Join(t.Path, t.Config.NucleiTemplate)
368+
369+
err = nucleiConfig.RunNucleiTemplate(t.Name, nucleiTemplate, t.NucleiTargetHost)
367370
if t.Config.ExpectedNucleiFailure {
368371
if err != nil && errors.Is(err, ErrNucleiTemplateFail) {
369372
log.Infof("Appsec test %s failed as expected", t.Name)
@@ -381,7 +384,6 @@ func (t *HubTestItem) RunWithNucleiTemplate() error {
381384
}
382385
} else {
383386
if err == nil {
384-
log.Infof("Appsec test %s succeeded", t.Name)
385387
t.Success = true
386388
} else {
387389
log.Errorf("Appsec test %s failed: %s", t.Name, err)
@@ -419,7 +421,7 @@ func (t *HubTestItem) RunWithLogFile() error {
419421

420422
logFile := filepath.Join(testPath, t.Config.LogFile)
421423
logType := t.Config.LogType
422-
dsn := fmt.Sprintf("file://%s", logFile)
424+
dsn := "file://" + logFile
423425

424426
logFileStat, err := os.Stat(logFile)
425427
if err != nil {
@@ -441,7 +443,7 @@ func (t *HubTestItem) RunWithLogFile() error {
441443
if err != nil {
442444
if !strings.Contains(string(output), "unable to create machine: user 'testMachine': user already exist") {
443445
fmt.Println(string(output))
444-
return fmt.Errorf("fail to run '%s' for test '%s': %v", cscliRegisterCmd.String(), t.Name, err)
446+
return fmt.Errorf("fail to run '%s' for test '%s': %w", cscliRegisterCmd.String(), t.Name, err)
445447
}
446448
}
447449

@@ -464,7 +466,7 @@ func (t *HubTestItem) RunWithLogFile() error {
464466
}
465467

466468
if err != nil {
467-
return fmt.Errorf("fail to run '%s' for test '%s': %v", crowdsecCmd.String(), t.Name, err)
469+
return fmt.Errorf("fail to run '%s' for test '%s': %w", crowdsecCmd.String(), t.Name, err)
468470
}
469471

470472
// assert parsers
@@ -570,17 +572,17 @@ func (t *HubTestItem) Run(ctx context.Context, patternDir string) error {
570572

571573
// copy template config file to runtime folder
572574
if err = Copy(t.TemplateConfigPath, t.RuntimeConfigFilePath); err != nil {
573-
return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateConfigPath, t.RuntimeConfigFilePath, err)
575+
return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateConfigPath, t.RuntimeConfigFilePath, err)
574576
}
575577

576578
// copy template profile file to runtime folder
577579
if err = Copy(t.TemplateProfilePath, t.RuntimeProfileFilePath); err != nil {
578-
return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateProfilePath, t.RuntimeProfileFilePath, err)
580+
return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateProfilePath, t.RuntimeProfileFilePath, err)
579581
}
580582

581583
// copy template simulation file to runtime folder
582584
if err = Copy(t.TemplateSimulationPath, t.RuntimeSimulationFilePath); err != nil {
583-
return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateSimulationPath, t.RuntimeSimulationFilePath, err)
585+
return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateSimulationPath, t.RuntimeSimulationFilePath, err)
584586
}
585587

586588
// copy template patterns folder to runtime folder
@@ -590,7 +592,7 @@ func (t *HubTestItem) Run(ctx context.Context, patternDir string) error {
590592

591593
// create the appsec-configs dir
592594
if err = os.MkdirAll(filepath.Join(t.RuntimePath, "appsec-configs"), os.ModePerm); err != nil {
593-
return fmt.Errorf("unable to create folder '%s': %+v", t.RuntimePath, err)
595+
return fmt.Errorf("unable to create folder '%s': %w", t.RuntimePath, err)
594596
}
595597

596598
// if it's an appsec rule test, we need acquis and appsec profile
@@ -599,13 +601,13 @@ func (t *HubTestItem) Run(ctx context.Context, patternDir string) error {
599601
log.Debugf("copying %s to %s", t.TemplateAcquisPath, t.RuntimeAcquisFilePath)
600602

601603
if err = Copy(t.TemplateAcquisPath, t.RuntimeAcquisFilePath); err != nil {
602-
return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateAcquisPath, t.RuntimeAcquisFilePath, err)
604+
return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateAcquisPath, t.RuntimeAcquisFilePath, err)
603605
}
604606

605607
log.Debugf("copying %s to %s", t.TemplateAppsecProfilePath, filepath.Join(t.RuntimePath, "appsec-configs", "config.yaml"))
606608
// copy template appsec-config file to runtime folder
607609
if err = Copy(t.TemplateAppsecProfilePath, filepath.Join(t.RuntimePath, "appsec-configs", "config.yaml")); err != nil {
608-
return fmt.Errorf("unable to copy '%s' to '%s': %v", t.TemplateAppsecProfilePath, filepath.Join(t.RuntimePath, "appsec-configs", "config.yaml"), err)
610+
return fmt.Errorf("unable to copy '%s' to '%s': %w", t.TemplateAppsecProfilePath, filepath.Join(t.RuntimePath, "appsec-configs", "config.yaml"), err)
609611
}
610612
} else { // otherwise we drop a blank acquis file
611613
if err = os.WriteFile(t.RuntimeAcquisFilePath, []byte(""), os.ModePerm); err != nil {

0 commit comments

Comments
 (0)