← Back to Pull requests

junit upload: pass CI when every failing test is quarantined #2650

Open
afontan opened this pull request with 650 lines diff across 8 files
View on GitHub
afontan afontan commented on Nov 24, 2026 09:53 UTC
Author

Flaky Test Management docs

Why the change

Repositories that only upload JUnit XML can quarantine or disable tests in Flaky Test Management, but their CI jobs still fail on those tests, so this adds an opt-in --test-exit-code to datadog-ci junit upload that passes a job whose only failures are quarantined or disabled tests.

Special things to note

  • Opt-in, and red when in doubt. Without --test-exit-code nothing changes. Only the runner's "tests failed" exit code can be excused (--test-failure-exit-code, default 1; Bazel 3, nextest 100, ctest 8). Anything the reports can't explain keeps the job red: an unreadable or dropped report, no failing testcase, an unknown commit, Flaky Test Management turned off in the settings the test libraries read (test_management.enabled), a failed lookup, a Go panic or timeout.

  • The reports are tagged before the upload, in memory. The runs of quarantined and disabled tests get a dd_tags[test.test_management.is_quarantined] or dd_tags[test.test_management.is_test_disabled] property, so Datadog shows them like the test libraries' runs. A testcase is found by its position and checked by its name; if anything doesn't line up, the report is uploaded as it is. The files on disk don't change.

  • Matching follows how Datadog keys JUnit tests: suite + name, no module, with --tags / DD_TAGS overrides of git. applied. When testcases with different classnames share a suite and name (pytest's default suite, for example), Datadog merges them into one test, so none of them is excused. Go reports are judged at the leaves, and the last rerun within a report wins; another report is another execution. A quarantined Go parent does not cover its subtests: in JUnit reports a parent fails whenever a subtest does, so failure-rate policies quarantine parents of flaky subtests, and covering the subtests would hide their real failures. Known limitation: a parent that fails on its own assertion while a quarantined subtest fails is excused.
  • Change outline

    The new flags, and how a pipeline uses them:

     datadog-ci junit upload --service <service> [options] <paths>
    +  --test-exit-code <code>          exit code of the test command; turns the verdict on
    +  --test-failure-exit-code <code>  the runner's "tests failed" code (default 1)
    

    code=0; go test ./... || code=$?          # don't let the test step end the job
    datadog-ci junit upload --service my-service --test-exit-code "$code" reports/
    

    The order of the command with the flag:

    junit upload --test-exit-code $code
      find and validate the reports
      verdict                                   (only when $code is the "tests failed" code and a testcase failed)
        POST /api/v2/libraries/tests/services/setting        → test_management.enabled
        POST /api/v2/test/libraries/test-management/tests    → quarantined and disabled tests
        judge the failing testcases                          → the exit code
        tag the runs of quarantined and disabled tests       (in memory)
      upload the reports                        (the tagged copy when there is one)
      print the verdict, and exit with its code
    

    The two lookups use the API-key endpoints the test libraries use:

    POST /api/v2/libraries/tests/services/setting
      {"data": {"type": "ci_app_test_service_libraries_settings",
                "attributes": {"service", "env", "repository_url", "sha", "branch", "configurations": {}, "test_level": "test"}}}
    → data.attributes.test_management.enabled
    
    POST /api/v2/test/libraries/test-management/tests
      {"data": {"type": "ci_app_libraries_tests_request", "attributes": {"repository_url", "sha"}}}
    → data.attributes.modules[""].suites[<suite>].tests[<name>].properties.{quarantined, disabled}
    

    The verdict, judge() in quarantine.ts:

    judge(exitCode)
      exitCode is 0                              → 0
      exitCode isn't the "tests failed" code     → keep exitCode
      a report is invalid or would be dropped    → keep exitCode
      failing = failing testcases                  (Go: the last rerun wins)
      no failing testcase                        → keep exitCode
      repository URL or SHA unknown              → keep exitCode
      test_management.enabled is false           → keep exitCode    (error → keep exitCode)
      excused = fetch the states                   (error → keep exitCode)
      for each failing test                        (Go: leaves only, plus parents that panicked)
        excused when its own suite + name is quarantined or disabled, it isn't a panic or timeout,
        and no testcase of another classname shares its suite + name
      every failing test excused                 → 0, else keep exitCode   (kept as 1 when outside 1..255)
      toTag = every run of a quarantined or disabled test
    

    What a tagged testcase looks like in the uploaded report:

     <testcase classname="pkg" name="TestFlaky">
    +  <properties><property name="dd_tags[test.test_management.is_quarantined]" value="true"></property></properties>
       <failure message="…">…</failure>
     </testcase>
    

    Where it hooks into the upload command:

     PluginCommand.execute
       getMatchingJUnitXMLFiles
    +    remember the reports that failed validation
    +  testVerdict                      # only with --test-exit-code
    +    judge → fetchQuarantineEnabled, fetchExcusedTests, decide
    +    tagReport → payload.xmlContent
       upload the reports               # api.ts sends xmlContent when set
       uploadToGitDB
    +  print the verdict, return its exit code
    

     packages/
     ├── base/src/
     │   ├── commands/junit/upload.ts           # the two flags and usage examples
     │   └── helpers/request/datadog-route.ts    # allow the two library routes
     └── plugin-junit/
         ├── README.md                           # setup, runner exit codes, GitLab, tags, limitations
         └── src/
    +        ├── quarantine.ts                   # parse reports, look up states, decide, tag
    +        ├── __tests__/quarantine.test.ts    # 50 tests, one per rule and exit path
    +        ├── __tests__/api.test.ts           # the tagged copy is sent, on every attempt
             ├── api.ts                          # upload the tagged copy when there is one
             ├── interfaces.ts                   # Payload.xmlContent
             └── commands/upload.ts              # the verdict before the upload
    

    Validation

  • Claim: a job that fails only because of quarantined or disabled tests exits 0, and the runs of those tests are tagged in Datadog. Every other job keeps its exit code, apart from the Go parent limitation above. Without the flag, nothing changes.

  • Unit tests: 50 tests in quarantine.test.ts and 1 in api.test.ts, covering every rule and exit path. Removing any one rule makes at least one test fail: Go leaves, last rerun, JUnit-only modules, panic and timeout, invalid reports, the exit-code check, entities, value trimming, unnamed suites, attribute case, the .xml modules, --test-failure-exit-code, the quarantine setting, the --tags override order, "disabled wins", tagging every run, the name check, document order, skipping comments and the DOCTYPE, the testcase name boundary, the settings branch fallback, no parent coverage, reruns and parents per report, panicking parents, shared suite + name, the exit-code range, and sending the tagged copy. yarn tsc:build, eslint, prettier and lint:readme-usage pass.

  • Replay on real CI data: two weeks of a large Go repository's JUnit uploads, 2026-11-09 → 2026-11-23. Of 29 failed jobs where a quarantined or disabled test failed, 26 would have passed and 3 stay red, each with real failures; none wrongly green. The three quarantined tests in that window are subtests, so the parent-rule change below doesn't affect the result. Caveats: today's states rather than each job's, and stored events rather than the XML files.

  • End to end on GitHub Actions and GitLab CI, at 3f01cf92: sandbox pipelines build this branch, run a Go package (gotestsum) and a pytest suite where one test fails on demand, and upload to a Datadog test environment. Each job asserts the exit code and the verdict's own line. The "real failure" is named after the run, so no policy can quarantine it.
  • | Provider | Job | Tests exit | datadog-ci exit |
    |---|---|---|---|
    | GitHub Actions (the test and its Go parent quarantined) | quarantined-only | 1 | 0 |
    | | real-failure | 1 | 1, only the real failure listed |
    | | flaky-passes | 0 | 0 |
    | GitLab CI (the test and its Go parent disabled) | quarantined-only | 1 | 0 |
    | | real-failure | 1 | 1 |
    | | all-pass | 0 | 0 |
    | | customer-setup, no assertion: the job's result is datadog-ci's exit | 1 | 0, job green |
    | | upload in after_script | 1 | 0, but the job stays red: GitLab ignores after_script, as the README says |
    | GitLab CI, pytest, default <testsuite name="pytest"> (the test quarantined) | quarantined-only | 1 | 0 |
    | | real-failure | 1 | 1, only the real failure listed |
    | | all-pass | 0 | 0 |
    | | same-name: a test of another module with the same name fails too | 1 | 1: "another testcase has the same suite and name" |
    | GitLab CI, pytest with --xpath-tag test.suite=/testcase/@classname (the test quarantined) | quarantined-only, the other module's same-name test passing | 1 | 0 |
    | | all-pass | 0 | 0 |

  • Tags in Datadog: the failing runs of the quarantined test and its parent carry @test.test_management.is_quarantined:true (GitHub, and pytest on GitLab), and of the disabled ones @test.test_management.is_test_disabled:true (Go on GitLab). The real failures carry neither, and neither do the same-name runs. With --xpath-tag test.suite=/testcase/@classname, Datadog stored the classname as the suite, as the CLI matched it. Runs in jobs whose tests passed carry no tag, as the README says. A separate probe confirmed the property is stored as a boolean and that two <properties> blocks in one testcase are fine.

  • Found by the end-to-end runs and fixed here:

  • - failure-rate policies quarantined and disabled the Go parents between runs; with "a quarantined parent covers its subtests", a real-failure job turned green;
    - the tests lookup failed with a 500 when a branch was sent for a repository without Flaky Test Management settings, so it now sends only the repository and commit, which is all quarantine and disable need.
  • Found by two independent reviews and fixed here, each with a unit test that failed before the fix:

  • - with several reports in one upload, a Go test that failed in one report and passed in another counted as passing, and a Go parent's own failure in one report could be skipped because of a subtest in another;
    - a Go parent whose failure shows a panic was skipped when a quarantined subtest also failed;
    - testcases of different classnames sharing a suite and name (pytest's default suite) could excuse each other;
    - an exit code above 255 could wrap to 0;
    - elements named like <testcase.step> and a DOCTYPE could misplace a tag; the settings call needs a branch.
  • --tags git.repository_url override: without it the lookup used the CI's repository and kept exit 1; with it, exit 0.

  • Not reproducible end to end: test_management.enabled false. It's on for every repository today, so that path is covered by unit tests only; the CLI reads the same setting as the test libraries, so it applies quarantine exactly when they do.

  • Do-no-harm:

  • - no change without the flag;
    - at most two extra API calls per command (10 s timeout, 2 retries), only when the code is the "tests failed" code and a testcase failed;
    - the report is uploaded unchanged if tagging can't line up, and any lookup error keeps the exit code;
    - upload or auth errors exit 1 as today, and the API key is never logged.
  • Rollout: opt-in per pipeline; dropping the flag reverts it.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: None requested Review duration: 14597m
    All Vouch review confidence checks passed
    Residual risk 0.36 = Change Risk (0.64) × (1 − Review Confidence (0.44)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.64 · 650 lines diff across 8 files
    0.64
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 10%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    No Reviewer Assigned no_reviewer_assigned
    0.120
    Real PR Conversation Timeline
    1 comments
    "Review active on #2650"
    general weight: 0.35
    Reviewers
    None requested
    Assignees
    afontan afontan
    Labels
    risk: medium confidence: 44.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 64% (MEDIUM) · Higher = larger blast radius
    64%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.2% · Lower = rushed or shallow review
    44.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 35.7% · Moderate risk
    35.7%