crowdsecurity/crowdsec · error

fail to run '%s' for test '%s': %w

Error message

fail to run '%s' for test '%s': %w

What it means

HubTestItem.RunWithNucleiTemplate runs `cscli machines register` as a subprocess to register a test machine before running an AppSec/nuclei test. If the command exits non-zero and its output does not contain the tolerated 'user already exist' message, the test is aborted with this wrapped error. It signals that the test-environment bootstrap failed, not that the security test itself failed.

Source

Thrown at pkg/hubtest/hubtest_item.go:347

func (t *HubTestItem) RunWithNucleiTemplate(ctx context.Context) 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)
	}

	crowdsecLogFile := filepath.Join(t.RuntimePath, "log", "crowdsec.log")

	// machine add
	cmdArgs := []string{"-c", t.RuntimeConfigFilePath, "machines", "add", "testMachine", "--force", "--auto"}
	cscliRegisterCmd := exec.CommandContext(ctx, t.CscliPath, cmdArgs...)
	cscliRegisterCmd.Dir = testPath
	cscliRegisterCmd.Env = []string{"TESTDIR=" + testPath, "DATADIR=" + t.RuntimeHubConfig.InstallDataDir, "TZ=UTC"}

	output, err := cscliRegisterCmd.CombinedOutput()
	if err != nil {
		if !strings.Contains(string(output), "unable to create machine: user 'testMachine': user already exist") {
			fmt.Fprintln(os.Stdout, string(output))
			return fmt.Errorf("fail to run '%s' for test '%s': %w", cscliRegisterCmd.String(), t.Name, err)
		}
	}

	// hardcode bouncer key
	cmdArgs = []string{"-c", t.RuntimeConfigFilePath, "bouncers", "add", "appsectests", "-k", TestBouncerAPIKey}
	cscliBouncerCmd := exec.CommandContext(ctx, t.CscliPath, cmdArgs...)
	cscliBouncerCmd.Dir = testPath
	cscliBouncerCmd.Env = []string{"TESTDIR=" + testPath, "DATADIR=" + t.RuntimeHubConfig.InstallDataDir, "TZ=UTC"}

	output, err = cscliBouncerCmd.CombinedOutput()
	if err != nil {
		if !strings.Contains(string(output), "unable to create bouncer: bouncer appsectests already exists") {
			fmt.Fprintln(os.Stdout, string(output))
			return fmt.Errorf("fail to run '%s' for test '%s': %w", cscliRegisterCmd.String(), t.Name, err)
		}
	}

	// start crowdsec service

View on GitHub (pinned to 909b515798)

Solutions

  1. Ensure the LAPI is reachable and running at the address configured in the test's RuntimeConfigFilePath before running the test
  2. Inspect the printed subprocess output above the error for the real cscli failure cause
  3. Delete stale test data (machines) from the install DATADIR so registration succeeds cleanly
  4. Verify TZ/TESTDIR/DATADIR env and cscli path are correct for the test harness

Example fix

// before
output, err := cscliRegisterCmd.CombinedOutput()
if err != nil { ... return fmt.Errorf("fail to run '%s' for test '%s': %w", ...) }
// after
// start LAPI first, e.g. in test harness: crowdsec -c dev.yaml & (or docker localapi) before running hubtest
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := net.Dial("tcp", lapiAddr); err != nil { t.Skipf("LAPI not reachable: %v", err) }

Try / catch

if err := test.Run(ctx); err != nil {
	if strings.Contains(err.Error(), "fail to run") {
		log.Printf("setup command failed, check cscli/LAPI: %v", err)
	}
}

Prevention

When it happens

Trigger: Calling HubTestItem.Run on an AppSec test whose setup invokes cscli register against a LAPI that refuses or errors on machine registration (other than the already-exist case).

Common situations: LAPI not running or wrong API URL in the test config; an existing machine 'testMachine' with different credentials causing a different error string; TLS/auth misconfiguration; cscli binary version mismatch; DATADIR pointing at a broken or read-only data dir.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/6d237e3422c75172. Report an issue: GitHub.