|
| 1 | +# Bazel Build Event Protocol (BEP) Parser |
| 2 | + |
| 3 | +This module parses Bazel Build Event Protocol (BEP) files to extract test execution information for identifying test states. The BEP is a stream of events that Bazel emits during a build, providing detailed information about targets, test execution, and build artifacts. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The BEP parser extracts information from two main event types: |
| 8 | + |
| 9 | +- **TestResult events**: Individual test execution results |
| 10 | +- **TestSummary events**: Aggregated test execution summaries |
| 11 | + |
| 12 | +The parser supports both JSON and binary proto formats, with automatic fallback from JSON to binary parsing if JSON parsing fails. |
| 13 | + |
| 14 | +## Information Extracted from BEP |
| 15 | + |
| 16 | +### 1. Target Information (Labels) |
| 17 | + |
| 18 | +**Source**: `TestResult` and `TestSummary` event IDs |
| 19 | + |
| 20 | +Each test target in Bazel has a unique label (e.g., `//trunk/hello_world/cc:hello_test`). The parser extracts these labels to: |
| 21 | + |
| 22 | +- Group test results by target |
| 23 | +- Associate JUnit XML files with their corresponding targets |
| 24 | +- Track which targets were executed |
| 25 | +- Use for Codeowners associations |
| 26 | + |
| 27 | +### 2. JUnit XML File Paths |
| 28 | + |
| 29 | +**Source**: `TestResult.test_action_output` fields |
| 30 | + |
| 31 | +The parser extracts paths to JUnit XML test result files. These paths can be: |
| 32 | + |
| 33 | +- **Local file paths**: Absolute paths to XML files on the filesystem |
| 34 | + |
| 35 | + - Example: `/tmp/hello_test/test.xml` |
| 36 | + - Example: `/tmp/hello_test/test_attempts/attempt_1.xml` (for retries) |
| 37 | + |
| 38 | +- **Remote bytestream URIs**: References to files stored in remote caches |
| 39 | + - Example: `bytestream://build.example.io/blobs/1234/567` |
| 40 | + |
| 41 | +**Multiple files per target**: A single target may produce multiple XML files in cases of: |
| 42 | + |
| 43 | +- Test retries (each attempt generates its own XML) |
| 44 | +- Flaky test detection (multiple attempts with different outcomes) |
| 45 | + |
| 46 | +**File filtering**: Only files with `.xml` extension are extracted from the `test_action_output` list. |
| 47 | + |
| 48 | +### 3. Cache State |
| 49 | + |
| 50 | +**Source**: `TestResult.execution_info` and `TestResult.cached_locally` |
| 51 | + |
| 52 | +The parser determines whether a test result was cached: |
| 53 | + |
| 54 | +- **`cached_locally`**: Test result was retrieved from local cache |
| 55 | +- **`cached_remotely`**: Test result was retrieved from remote cache (via `execution_info.cached_remotely`) |
| 56 | + |
| 57 | +**Usage**: |
| 58 | + |
| 59 | +- Cached test results are typically excluded from analytics (via `uncached_xml_files()` and `uncached_labels()`) |
| 60 | +- Cache state helps distinguish between actual test executions and cached results |
| 61 | +- XML file counts distinguish between total files and cached files |
| 62 | + |
| 63 | +### 4. Test Status (Build Status) |
| 64 | + |
| 65 | +**Latest status**: For tests with multiple attempts (retries), the parser tracks the status from the latest attempt. |
| 66 | + |
| 67 | +### 5. Test Runner Report |
| 68 | + |
| 69 | +**Source**: `TestSummary` events |
| 70 | + |
| 71 | +The parser extracts aggregated test execution information from TestSummary events: |
| 72 | + |
| 73 | +- **Overall status**: `overall_status` field (Passed, Failed, or Flaky) |
| 74 | +- **Start time**: `first_start_time` - when the first test attempt started |
| 75 | +- **End time**: `last_stop_time` - when the last test attempt completed |
| 76 | +- **Label**: The target label associated with the summary |
| 77 | + |
| 78 | +**Usage**: Test runner reports provide high-level timing and status information that complements the detailed JUnit XML parsing. They are used for validation and to provide context when parsing JUnit files. |
| 79 | + |
| 80 | +### 6. Attempt Numbers |
| 81 | + |
| 82 | +**Source**: `TestResult` event ID (`TestResult.id.attempt`) |
| 83 | + |
| 84 | +For tests that are retried (e.g., flaky tests), each attempt is tracked with an attempt number: |
| 85 | + |
| 86 | +- Attempt numbers start at 1 |
| 87 | +- Each retry increments the attempt number |
| 88 | +- Multiple XML files from the same target can have different attempt numbers |
| 89 | + |
| 90 | +**Usage**: |
| 91 | + |
| 92 | +- Attempt numbers are attached to individual test case runs to track which attempt they came from |
| 93 | +- Helps distinguish between different execution attempts of the same test |
| 94 | +- Used to merge results from multiple attempts into a single test result, especially when [`--flaky_test_attempts`](https://bazel.build/reference/command-line-reference#flag--flaky_test_attempts) results in an eventual success |
0 commit comments