From 93ea01ca8d4b927b2b244fe2d5e555f6a345126d Mon Sep 17 00:00:00 2001 From: Novak Zaballa <41410593+novakzaballa@users.noreply.github.com> Date: Fri, 22 Dec 2023 11:51:53 -0400 Subject: [PATCH] ci: API tests optimization. Enable merge queue checks and run only changed tests (#3216) --- .github/workflows/api-pull-request.yml | 28 +++++++++++++++++-- .gitignore | 1 + api/.env-ci-testmon | 3 ++ api/.gitignore | 5 +++- api/poetry.lock | 15 ++++++++++ api/pyproject.toml | 1 + .../test_unit_environments_views.py | 2 +- 7 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 api/.env-ci-testmon diff --git a/.github/workflows/api-pull-request.yml b/.github/workflows/api-pull-request.yml index eaf2cb72c26b..dcf2622a499b 100644 --- a/.github/workflows/api-pull-request.yml +++ b/.github/workflows/api-pull-request.yml @@ -12,6 +12,8 @@ on: - .github/** branches: - main + merge_group: + types: [checks_requested] defaults: run: @@ -19,7 +21,7 @@ defaults: jobs: test: - runs-on: ubuntu-latest + runs-on: General-Purpose-8c-Runner name: API Unit Tests services: @@ -33,7 +35,7 @@ jobs: options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 strategy: - max-parallel: 4 + max-parallel: 2 matrix: python-version: ['3.10', '3.11'] @@ -67,11 +69,31 @@ jobs: opts: --no-input --dry-run --check run: make django-make-migrations + - name: Restore cached testmon data + if: ${{ github.event_name == 'pull_request' }} + id: cache-testmon-restore + uses: actions/cache/restore@v3 + with: + enableCrossOsArchive: true + path: | + /home/runner/work/flagsmith/flagsmith/api/.testmondata* + key: testmon-data-python${{ matrix.python-version }}-${{ github.event.pull_request.base.sha }} + restore-keys: testmon-data-python${{ matrix.python-version }}- + - name: Run Tests env: - DOTENV_OVERRIDE_FILE: .env-ci + DOTENV_OVERRIDE_FILE: "${{ github.event_name == 'merge_group' && '.env-ci' || '.env-ci-testmon' }}" run: make test + - name: Save testmon data cache + id: cache-testmon-save + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} + uses: actions/cache/save@v3 + with: + path: | + /home/runner/work/flagsmith/flagsmith/api/.testmondata* + key: testmon-data-python${{ matrix.python-version }}-${{github.sha}} + - name: Upload Coverage uses: codecov/codecov-action@v3 env: diff --git a/.gitignore b/.gitignore index c639e95f4aa8..84c608d8a6fc 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ checkstyle.txt .env* !.env-local !.env-ci +!.env-ci-testmon .direnv .envrc .tool-versions diff --git a/api/.env-ci-testmon b/api/.env-ci-testmon new file mode 100644 index 000000000000..227ff75502ca --- /dev/null +++ b/api/.env-ci-testmon @@ -0,0 +1,3 @@ +DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres +ANALYTICS_DATABASE_URL=postgres://postgres:postgres@localhost:5432/analytics +PYTEST_ADDOPTS=--cov . --cov-report xml --dist worksteal --testmon diff --git a/api/.gitignore b/api/.gitignore index 3607aef19237..d3ce421641db 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -6,4 +6,7 @@ saml/ features/workflows/logic/ # Unit test coverage -.coverage \ No newline at end of file +.coverage + +# pytest-testmon files +.testmondata* diff --git a/api/poetry.lock b/api/poetry.lock index 06cd33e5314f..cf327e8d65c2 100644 --- a/api/poetry.lock +++ b/api/poetry.lock @@ -3282,6 +3282,21 @@ pytest = ">=5.0" [package.extras] dev = ["pre-commit", "pytest-asyncio", "tox"] +[[package]] +name = "pytest-testmon" +version = "2.0.13" +description = "selects tests affected by changed files and methods" +optional = false +python-versions = ">=3.7" +files = [ + {file = "pytest-testmon-2.0.13.tar.gz", hash = "sha256:9cf56021ccaa6c8c6bcaef07589fb6c872db1797f0f7ff1f104e93c96078d642"}, + {file = "pytest_testmon-2.0.13-py3-none-any.whl", hash = "sha256:170872f2407e1eab431a266108229dbcde73513771a71ab77ed1e84ec94c816c"}, +] + +[package.dependencies] +coverage = ">=6,<8" +pytest = ">=5,<8" + [[package]] name = "pytest-xdist" version = "3.2.1" diff --git a/api/pyproject.toml b/api/pyproject.toml index b9a8fae41d0e..d7efb428f080 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -145,6 +145,7 @@ requests-mock = "^1.11.0" django-extensions = "^3.2.3" pdbpp = "^0.10.3" django-capture-on-commit-callbacks = "^1.11.0" +pytest-testmon = "^2.0.13" mypy-boto3-dynamodb = "^1.33.0" [build-system] diff --git a/api/tests/unit/environments/test_unit_environments_views.py b/api/tests/unit/environments/test_unit_environments_views.py index fa9b4b3ccb9b..6cf0083d598c 100644 --- a/api/tests/unit/environments/test_unit_environments_views.py +++ b/api/tests/unit/environments/test_unit_environments_views.py @@ -722,7 +722,7 @@ def test_create_environment_with_required_metadata_returns_201( assert response.status_code == status.HTTP_201_CREATED assert ( response.json()["metadata"][0]["model_field"] - == required_a_environment_metadata_field.field.id + == required_a_environment_metadata_field.id ) assert response.json()["metadata"][0]["field_value"] == str(field_value)