crowdsecurity/crowdsec · error

unable to stat log file '%s': %w

Error message

unable to stat log file '%s': %w

What it means

RunWithLogFile stats the test's input log file (the log sample that will be fed to crowdsec via a file:// datasource); a stat failure aborts the test with this error. It is distinct from the missing-test-directory error: the test dir exists but the configured log file inside it does not.

Source

Thrown at pkg/hubtest/hubtest_item.go:472

		}
	}

	return nil
}

func (t *HubTestItem) RunWithLogFile(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)
	}

	logFile := filepath.Join(testPath, t.Config.LogFile)
	logType := t.Config.LogType
	dsn := "file://" + logFile

	logFileStat, err := os.Stat(logFile)
	if err != nil {
		return fmt.Errorf("unable to stat log file '%s': %w", logFile, err)
	}

	if logFileStat.Size() == 0 {
		return fmt.Errorf("log file '%s' is empty, please fill it with log", logFile)
	}

	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"}

	log.Debugf("%s", cscliRegisterCmd.String())

	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)

View on GitHub (pinned to 909b515798)

Solutions

  1. Verify the file exists: ls <testdir>/<log_file from .config.yaml>
  2. Fix the LogFile value in the test's .config.yaml to match the actual sample filename
  3. Restore/checkout the missing sample log file (git checkout -- <path>)
  4. Check read permissions on the file and its parent directory

Example fix

// before (.config.yaml)
log_file: logs/app.log   # file actually named sample.log
// after
log_file: sample.log
Defensive patterns

Strategy: validation

Validate before calling

logFile := filepath.Join(testPath, cfg.LogFile)
if fi, err := os.Stat(logFile); err != nil || fi.Size() == 0 {
	return fmt.Errorf("log sample %s missing or empty", logFile)
}

Prevention

When it happens

Trigger: os.Stat(logFile) fails where logFile = <testPath>/<t.Config.LogFile>: the file named in the test's .config.yaml (LogType/LogFile) is absent, the config path is wrong, or permission denies access.

Common situations: Test config 'log_file' key renamed/renamed file not committed; empty repo checkout missing the sample log; running tests from a different working directory so relative paths resolve elsewhere; symlink to a file the user cannot read.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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