crowdsecurity/crowdsec · error

couldn't generate assertion: %w

Error message

couldn't generate assertion: %w

What it means

If an assert file contains zero assertions (NbAssert == 0 after reading all lines), hubtest tries to auto-generate assertions from the test file via AutoGenFromFile. This error wraps a failure of that auto-generation step, meaning the test file could not be parsed into generated assertion data.

Source

Thrown at pkg/hubtest/parser_assert.go:141

			if err != nil {
				log.Errorf("unable to evaluate variable '%s': %s", variable, err)
				continue
			}

			failedAssert.Debug[variable] = result
			p.Fails = append(p.Fails, *failedAssert)

			continue
		}
		// fmt.Printf(" %s '%s'\n", emoji.GreenSquare, scanner.Text())
	}

	file.Close()

	if p.NbAssert == 0 {
		assertData, err := p.AutoGenFromFile(testFile)
		if err != nil {
			return fmt.Errorf("couldn't generate assertion: %w", err)
		}

		p.AutoGenAssertData = assertData
		p.AutoGenAssert = true
	}

	if len(p.Fails) == 0 {
		p.Success = true
	}

	return nil
}

func basenameShim(expression string) string {
	if strings.Contains(expression, "datasource_path") && !strings.Contains(expression, "basename(") {
		// match everything before == and wrap it with basename()
		match := strings.Split(expression, "==")
		return fmt.Sprintf("basename(%s) == %s", match[0], match[1])

View on GitHub (pinned to 909b515798)

Solutions

  1. Check that the test file paired with the assert file exists and is readable at the expected path
  2. Write at least one real assertion in the .assert file so auto-generation is skipped
  3. Inspect the wrapped AutoGenFromFile error for the root cause (I/O vs parse)
  4. Regenerate the test bundle with cscli hubtest commands

Example fix

// before: empty mytest.assert causing autogen
// after: add a valid assertion
Alert.GetRemediationComponent() == 'crowdsec'
Defensive patterns

Strategy: validation

Validate before calling

info, err := os.Stat(testFile)
if err != nil || info.IsDir() {
    return fmt.Errorf("test file %q missing for autogen", testFile)
}

Try / catch

if err := p.AssertFile(...); err != nil {
    if strings.Contains(err.Error(), "couldn't generate assertion") {
        // fall back to writing manual assertions
    }
    return err
}

Prevention

When it happens

Trigger: Calling AssertFile with an empty or all-comment assert file whose paired test file cannot be auto-asserted, e.g. AutoGenFromFile fails to read/parse the test file or produce assertion data.

Common situations: Empty .assert files where the sibling .txt/.log test file is missing or unreadable; test file path wrong in the test definition; malformed test data preventing auto-generation.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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