Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
api_changes: ${{ steps.changes.outputs.api_changes }}
devservices_changes: ${{ steps.changes.outputs.devservices_changes }}
sentry_sha: ${{ steps.sentry.outputs.sha }}
# Matrix for the `sentry` job. Every shard collects sentry's whole suite
# to find its `-m` share, so each extra shard costs a full collection.
# Without API changes only the few `snuba_ci` tests run, and 2 shards
# finish about as fast as more.
sentry_shards: ${{ steps.changes.outputs.api_changes == 'true' && '[0, 1, 2, 3]' || '[0, 1]' }}
# Matrix for the `sentry` job. API changes also run the `snuba`-marked
# tests, which take most of the time, so they get 8 shards. Without API
# changes only the few `snuba_ci` tests run, and 2 shards finish about
# as fast as more.
sentry_shards: ${{ steps.changes.outputs.api_changes == 'true' && '[0, 1, 2, 3, 4, 5, 6, 7]' || '[0, 1]' }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

Expand Down Expand Up @@ -452,6 +452,9 @@ jobs:
TEST_GROUP_STRATEGY: ROUND_ROBIN
XDIST_WORKERS: 2
XDIST_PER_WORKER_SNUBA: '1'
# Each shard collects only its planned Sentry test files instead of the
# whole suite. See sentry's .github/workflows/scripts/pytest-selected.py.
PYTEST_SELECTED_FILTERED_PLAN: '1'

steps:
- name: Checkout code
Expand Down Expand Up @@ -520,12 +523,45 @@ jobs:
working-directory: sentry
env:
# API changes also run the rest of the `snuba`-marked tests. One
# pytest run instead of two saves a full collection + DB setup.
# pytest run instead of two saves a collection + DB setup.
MARKERS: ${{ needs.files-changed.outputs.api_changes == 'true' && 'snuba_ci or snuba' || 'snuba_ci' }}
run: |
export PYTEST_ADDOPTS="$PYTEST_ADDOPTS -k 'not __In' -m '$MARKERS' -n $XDIST_WORKERS --dist=loadfile -vv"
make test-python-ci

- name: Upload the tests this shard ran
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: sentry-tests-${{ matrix.instance }}
path: sentry/.artifacts/pytest.json
if-no-files-found: error
overwrite: true
retention-days: 7

sentry-selected-tests:
name: Sentry shards selected tests
needs: [files-changed, sentry]
runs-on: ubuntu-26.04-arm
timeout-minutes: 3
permissions: {}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8
with:
pattern: sentry-tests-*
path: sentry-tests

- name: Test the check
run: python3 -m unittest discover -s .github/workflows/scripts -p 'test_*.py'

- name: Check that the shards together selected tests
env:
SENTRY_SHARDS: ${{ needs.files-changed.outputs.sentry_shards }}
run: python3 .github/workflows/scripts/check_sentry_selected_tests.py --reports-dir sentry-tests

clickhouse-versions:
needs: [linting, snuba-image, docker-deps]
name: Tests on multiple clickhouse versions
Expand Down Expand Up @@ -684,7 +720,7 @@ jobs:
# too: when one of them fails, the jobs after it are skipped, not failed.
required-checks:
name: Required checks
needs: [files-changed, snuba-image, docker-deps, linting, rust-linting, tests, sentry]
needs: [files-changed, snuba-image, docker-deps, linting, rust-linting, tests, sentry, sentry-selected-tests]
# Run even when a dependency fails, which would otherwise skip this job.
if: always()
runs-on: ubuntu-26.04-arm
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/scripts/check_sentry_selected_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""Check the tests that the `sentry` job's shards selected, across all shards.

A shard may select no tests, but every shard in SENTRY_SHARDS must report, the
shards together must select at least one test, and no test may run in two
shards. Each report is the shard's pytest-json-report file, one directory per
shard under --reports-dir.
"""

from __future__ import annotations

import argparse
import json
import os
import sys
from collections import Counter
from pathlib import Path


def check(reports_dir: Path, shards: list[int]) -> list[str]:
reports = sorted(reports_dir.glob("*/pytest.json"))
errors = []
if len(reports) != len(shards):
errors.append(f"expected {len(shards)} shard reports, found {len(reports)}")

shards_per_test: Counter[str] = Counter()
for report in reports:
nodeids = {test["nodeid"] for test in json.loads(report.read_text())["tests"]}
print(f"{report.parent.name}: {len(nodeids)} tests")
shards_per_test.update(nodeids)

if not shards_per_test:
errors.append("the shards selected no tests")
repeated = sorted(nodeid for nodeid, count in shards_per_test.items() if count > 1)
if repeated:
errors.append(f"{len(repeated)} tests ran in more than one shard, such as {repeated[0]}")
return errors


def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--reports-dir", type=Path, required=True)
args = parser.parse_args()
shards = json.loads(os.environ["SENTRY_SHARDS"])
errors = check(args.reports_dir, shards)
for error in errors:
print(f"error: {error}", file=sys.stderr)
return 1 if errors else 0


if __name__ == "__main__":
raise SystemExit(main())
44 changes: 44 additions & 0 deletions .github/workflows/scripts/test_check_sentry_selected_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

import json
import tempfile
import unittest
from pathlib import Path

from check_sentry_selected_tests import check


class CheckTest(unittest.TestCase):
def setUp(self) -> None:
self.dir = Path(self.enterContext(tempfile.TemporaryDirectory()))

def report(self, shard: int, *nodeids: str) -> None:
path = self.dir / f"sentry-tests-{shard}" / "pytest.json"
path.parent.mkdir()
path.write_text(json.dumps({"tests": [{"nodeid": n} for n in nodeids]}))

def test_empty_shard_passes_when_another_selected_tests(self) -> None:
self.report(0, "tests/test_a.py::test_one")
self.report(1)
self.assertEqual(check(self.dir, [0, 1]), [])

def test_missing_shard_report_fails(self) -> None:
self.report(0, "tests/test_a.py::test_one")
self.assertEqual(check(self.dir, [0, 1]), ["expected 2 shard reports, found 1"])

def test_no_selected_tests_fails(self) -> None:
self.report(0)
self.report(1)
self.assertEqual(check(self.dir, [0, 1]), ["the shards selected no tests"])

def test_test_in_two_shards_fails(self) -> None:
self.report(0, "tests/test_a.py::test_one")
self.report(1, "tests/test_a.py::test_one", "tests/test_b.py::test_two")
self.assertEqual(
check(self.dir, [0, 1]),
["1 tests ran in more than one shard, such as tests/test_a.py::test_one"],
)


if __name__ == "__main__":
unittest.main()
Loading