crowdsecurity/crowdsec · error

failed to open

Error message

failed to open

What it means

ScenarioAssert.AssertFile opens the scenario dump file at s.File and returns the bare error "failed to open" if os.Open fails, discarding the OS cause. It means the scenario assertion harness cannot read its dump file, so scenario-level assertions cannot run.

Source

Thrown at pkg/hubtest/scenario_assert.go:83

	s.TestData = bucketDump

	if bucketpour != "" {
		pourDump, err := dumps.LoadBucketPourDump(bucketpour)
		if err != nil {
			return fmt.Errorf("loading bucket pour dump file '%s': %w", filename, err)
		}

		s.PourData = pourDump
	}

	return nil
}

func (s *ScenarioAssert) AssertFile(testFile string) error {
	file, err := os.Open(s.File)
	if err != nil {
		return errors.New("failed to open")
	}

	if err := s.LoadTest(testFile, ""); err != nil {
		return fmt.Errorf("unable to load parser dump file '%s': %w", testFile, err)
	}

	scanner := bufio.NewScanner(file)
	scanner.Split(bufio.ScanLines)

	nbLine := 0

	for scanner.Scan() {
		nbLine++

		if scanner.Text() == "" {
			continue
		}

View on GitHub (pinned to 909b515798)

Solutions

  1. Run the scenario test stage that generates the dump file before the assert stage
  2. Verify s.File exists and fix the path in the scenario test config
  3. Fix permissions on the dump file and its parent directory
  4. Wrap the os.Open error with %w (as in the suggested fix) to surface the true cause
  5. Re-run hubtest with a clean output_dir to regenerate the dump at the expected location

Example fix

// before
file, err := os.Open(s.File)
if err != nil {
    return errors.New("failed to open")
}
// after
file, err := os.Open(s.File)
if err != nil {
    return fmt.Errorf("failed to open scenario dump '%s': %w", s.File, err)
}
Defensive patterns

Strategy: validation

Validate before calling

func assertDumpReadable(path string) error {
    fi, err := os.Stat(path)
    if err != nil { return fmt.Errorf("scenario dump missing: %w", err) }
    if fi.IsDir() { return fmt.Errorf("%s is a directory", path) }
    f, err := os.Open(path); if err != nil { return err }; return f.Close()
}

Try / catch

if err := s.AssertFile(testFile); err != nil {
    if strings.Contains(err.Error(), "failed to open") {
        log.Printf("check scenario dump path %q exists and is readable", s.File)
    }
    return err
}

Prevention

When it happens

Trigger: Calling ScenarioAssert.AssertFile (via RunWithLogFile) when s.File is missing, unreadable, or not a regular file — any os.Open failure.

Common situations: Scenario dump never produced because the earlier test stage failed or was skipped; wrong output_dir configured for the scenario test; stale absolute paths after moving the test tree; permission issues on generated artifacts.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


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