Skip to content

feat: save failed test logs and truncate WAF log when using --log-failures-only#626

Open
Copilot wants to merge 5 commits intomainfrom
copilot/only-keep-logs-for-failing-tests
Open

feat: save failed test logs and truncate WAF log when using --log-failures-only#626
Copilot wants to merge 5 commits intomainfrom
copilot/only-keep-logs-for-failing-tests

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 19, 2026

When running tests, the WAF error log accumulates entries for every test—passing and failing—making CI artifact collection noisy. This adds a new --log-failures-only flag that enables per-stage log management so only failed test entries are retained.

Behavior

After each test stage, when --log-failures-only is active (non-cloud mode only):

  • Failed stage: WAF log lines between the start/end markers are appended to failed-tests.log (placed alongside the WAF error log)
  • Every stage: WAF error log is truncated to zero bytes

Result: WAF error log is empty at run end; failed-tests.log exists only if something failed and contains only those entries.

/var/log/modsec/error.log        → truncated to 0 bytes after every stage
/var/log/modsec/failed-tests.log → created/appended only for failed stages

Changes

waflog/waflog.go

  • openLogFile: switched from os.Open (read-only) to os.OpenFile(…, os.O_RDWR, …) to enable truncation. O_CREATE is intentionally omitted—the WAF must create the log before go-ftw starts.
  • TruncateLogFile(): truncates log to 0 bytes and seeks back to start.
  • GetMarkedLines(): public, safe wrapper around getMarkedLines()—returns nil when markers are unset instead of calling log.Fatal.

runner/run.go

  • failedTestsLogFilePath(logFilePath): derives <dir>/failed-tests.log from the WAF log path.
  • appendToFailedTestsLog(logFilePath, logLines): opens failed-tests.log in append/create mode and writes the marked lines.
  • RunStage: calls the above after displayResult when LogFailuresOnly && notRunningInCloudMode.

config/runner_config.go and runner/types.go

  • Added LogFailuresOnly bool field to RunnerConfig and TestRunContext.

cmd/run/run.go

  • Added --log-failures-only flag (independent of --show-failures-only).

[!WARNING]

Copilot AI linked an issue Apr 19, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add functionality to keep logs for failing tests feat: save failed test logs and truncate WAF log when using --show-failures-only Apr 19, 2026
Copilot AI requested a review from fzipi April 19, 2026 12:51
@fzipi
Copy link
Copy Markdown
Member

fzipi commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 21, 2026

@copilot fix the windows tests: https://github.com/coreruleset/go-ftw/actions/runs/24629480025/job/72128596838?pr=626

Fixed in a76dae6. The TestFailedTestsLogFilePath test was using hardcoded Unix-style paths (/tmp/failed-tests.log), but on Windows filepath.Join produces backslash separators. Changed the test to build expected paths with filepath.Join so it works cross-platform.

Copilot AI and others added 3 commits April 21, 2026 18:50
…nabled

When the --show-failures-only flag is enabled, after each test stage:
- If the test failed: append the WAF log lines (between markers) to a
  failed-tests.log file placed next to the WAF error log
- Always truncate the WAF error log to zero bytes

This leaves the WAF error log empty at the end of the run, and
failed-tests.log (if it exists) contains only entries from failed tests.

Changes:
- waflog/waflog.go: open log file with O_RDWR for truncation support;
  add TruncateLogFile() and GetMarkedLines() public methods
- runner/run.go: add appendToFailedTestsLog() and failedTestsLogFilePath()
  helpers; invoke log management after each stage in RunStage()
- runner/testdata/TestShowFailuresOnly.yaml: test data for new test
- runner/run_test.go: add TestShowFailuresOnly and TestFailedTestsLogFilePath
- waflog/waflog_test.go: add TestTruncateLogFile, TestGetMarkedLinesWithoutMarkers,
  TestGetMarkedLinesWithOnlyStartMarker

Agent-Logs-Url: https://github.com/coreruleset/go-ftw/sessions/8fe4ac6a-0744-4a39-a54f-ddf89c6fbf20

Co-authored-by: fzipi <3012076+fzipi@users.noreply.github.com>
- Use defer for test cleanup in TestShowFailuresOnly
- Add comment explaining why os.O_CREATE is intentionally omitted

Agent-Logs-Url: https://github.com/coreruleset/go-ftw/sessions/8fe4ac6a-0744-4a39-a54f-ddf89c6fbf20

Co-authored-by: fzipi <3012076+fzipi@users.noreply.github.com>
@fzipi fzipi force-pushed the copilot/only-keep-logs-for-failing-tests branch from a76dae6 to cf4861e Compare April 21, 2026 21:50
@fzipi fzipi marked this pull request as ready for review May 3, 2026 13:28
@fzipi
Copy link
Copy Markdown
Member

fzipi commented May 3, 2026

@copilot maybe adding a new flag instead, called --log-failures-only instead of reusing the --show-failures-only to trigger this feature.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 3, 2026

@copilot maybe adding a new flag instead, called --log-failures-only instead of reusing the --show-failures-only to trigger this feature.

Done in 28666e9. Added a new --log-failures-only flag that controls the WAF log management behavior (saving failed entries + truncating after each stage), keeping it fully independent from --show-failures-only.

Copilot AI changed the title feat: save failed test logs and truncate WAF log when using --show-failures-only feat: save failed test logs and truncate WAF log when using --log-failures-only May 3, 2026
Copilot finished work on behalf of fzipi May 3, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Only keep logs for failing tests

2 participants