Skip to content

Commit 132c4fc

Browse files
committed
WIP: Run CI tests against multiple versions
1 parent dbbe385 commit 132c4fc

File tree

6 files changed

+126
-15
lines changed

6 files changed

+126
-15
lines changed

.github/workflows/ci.yml

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,87 @@ jobs:
201201
pip install invoke toml codecov
202202
- name: "Install Package"
203203
run: "poetry install --all-extras"
204+
- name: "Set environment variables for python_testcontainers"
205+
run: |
206+
echo INFRAHUB_TESTING_IMAGE_TAG=latest >> $GITHUB_ENV
204207
- name: "Integration Tests"
205-
run: "poetry run pytest --cov infrahub_sdk tests/integration/"
208+
run: |
209+
poetry run pytest --cov infrahub_sdk tests/integration/
210+
- name: "Upload coverage to Codecov"
211+
run: |
212+
codecov --flags integration-tests
213+
env:
214+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
215+
216+
integration-tests-against-local-infrahub-build:
217+
if: |
218+
always() && !cancelled() &&
219+
!contains(needs.*.result, 'failure') &&
220+
!contains(needs.*.result, 'cancelled') &&
221+
needs.files-changed.outputs.python == 'true' &&
222+
(github.base_ref == 'stable' || github.base_ref == 'develop')
223+
needs: ["files-changed", "yaml-lint", "python-lint"]
224+
runs-on:
225+
group: "huge-runners"
226+
timeout-minutes: 30
227+
steps:
228+
- name: "Check out repository code"
229+
uses: "actions/checkout@v4"
230+
231+
- name: "Extract target branch name"
232+
id: extract_branch
233+
run: echo "TARGET_BRANCH=${{ github.base_ref }}" >> $GITHUB_ENV
234+
235+
- name: "Checkout infrahub repository"
236+
uses: "actions/checkout@v4"
237+
with:
238+
repository: "opsmill/infrahub"
239+
path: "infrahub-server"
240+
ref: ${{ github.base_ref }}
241+
submodules: true
242+
243+
- name: Set up Python
244+
uses: actions/setup-python@v5
245+
with:
246+
python-version: "3.12"
247+
248+
- name: "Setup git credentials prior dev.build"
249+
run: |
250+
cd infrahub-server
251+
git config --global user.name 'Infrahub'
252+
git config --global user.email '[email protected]'
253+
git config --global --add safe.directory '*'
254+
git config --global credential.usehttppath true
255+
git config --global credential.helper /usr/local/bin/infrahub-git-credential
256+
257+
- name: "Set environment variables prior dev.build"
258+
run: |
259+
echo "INFRAHUB_BUILD_NAME=infrahub-${{ runner.name }}" >> $GITHUB_ENV
260+
RUNNER_NAME=$(echo "${{ runner.name }}" | grep -o 'ghrunner[0-9]\+' | sed 's/ghrunner\([0-9]\+\)/ghrunner_\1/')
261+
echo "PYTEST_DEBUG_TEMPROOT=/var/lib/github/${RUNNER_NAME}/_temp" >> $GITHUB_ENV
262+
echo "INFRAHUB_IMAGE_VER=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
263+
echo "INFRAHUB_TESTING_IMAGE_TAG=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
264+
echo "INFRAHUB_TESTING_DOCKER_IMAGE=opsmill/infrahub" >> $GITHUB_ENV
265+
266+
- name: "Build container"
267+
run: |
268+
cd infrahub-server
269+
inv dev.build
270+
271+
- name: "Setup environment"
272+
run: |
273+
pipx install poetry==1.8.5
274+
poetry config virtualenvs.create true --local
275+
pip install invoke toml codecov
276+
277+
- name: "Install Package"
278+
run: "poetry install --all-extras"
279+
280+
- name: "Integration Tests"
281+
run: |
282+
echo "Running tests for version: $INFRAHUB_TESTING_IMAGE_TAG"
283+
poetry run pytest --cov infrahub_sdk tests/integration/
284+
206285
- name: "Upload coverage to Codecov"
207286
run: |
208287
codecov --flags integration-tests

infrahub_sdk/testing/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
import pytest
4+
5+
infrahub_version = os.getenv("INFRAHUB_TESTING_IMAGE_TAG", "latest")
6+
7+
8+
def pytest_addoption(parser): # type: ignore # noqa: ANN001
9+
parser.addoption(
10+
"--infrahub-version", action="store", default=None, help="Specify the Infrahub version to use for tests"
11+
)
12+
13+
14+
@pytest.fixture(autouse=True, scope="session")
15+
def set_infrahub_version(request): # type: ignore # noqa: ANN001
16+
# We want to provide both environment variable and pytest CLI to set infrahub testing version.
17+
global infrahub_version
18+
infrahub_version = request.config.getoption("--infrahub-version")

infrahub_sdk/testing/docker.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
1+
from __future__ import annotations
2+
13
import pytest
24
from infrahub_testcontainers.helpers import TestInfrahubDocker
5+
from packaging.version import InvalidVersion, Version
36

47
from .. import Config, InfrahubClient, InfrahubClientSync
8+
from .conftest import infrahub_version
9+
10+
11+
# TODO test it works
12+
def skip_version(min_infrahub_version: str | None = None, max_infrahub_version: str | None = None) -> bool:
13+
"""
14+
Check if a test should be skipped depending on infrahub version.
15+
"""
16+
try:
17+
version = Version(infrahub_version)
18+
except InvalidVersion:
19+
# We would typically end up here for development purpose while running a CI test against
20+
# unreleased versions of infrahub, like `stable` or `develop` branch.
21+
# For now, we consider this means we are testing against the most recent version of infrahub,
22+
# so we skip if the test should not be ran against a maximum version.
23+
return max_infrahub_version is None
24+
25+
if min_infrahub_version is not None and version <= Version(min_infrahub_version):
26+
return True
27+
28+
return max_infrahub_version is not None and version <= Version(max_infrahub_version)
529

630

731
class TestInfrahubDockerClient(TestInfrahubDocker):
32+
@pytest.fixture(scope="class")
33+
def infrahub_version(self) -> str:
34+
return infrahub_version
35+
836
@pytest.fixture(scope="class")
937
def client(self, infrahub_port: int) -> InfrahubClient:
1038
return InfrahubClient(

tests/integration/test_infrahub_client.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717

1818
class TestInfrahubNode(TestInfrahubDockerClient, SchemaAnimal):
19-
@pytest.fixture(scope="class")
20-
def infrahub_version(self) -> str:
21-
return "1.1.0"
22-
2319
@pytest.fixture(scope="class")
2420
async def base_dataset(
2521
self,

tests/integration/test_node.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212

1313
class TestInfrahubNode(TestInfrahubDockerClient, SchemaCarPerson):
14-
@pytest.fixture(scope="class")
15-
def infrahub_version(self) -> str:
16-
return "1.1.0"
17-
1814
@pytest.fixture(scope="class")
1915
async def initial_schema(self, default_branch: str, client: InfrahubClient, schema_base: SchemaRoot) -> None:
2016
await client.schema.wait_until_converged(branch=default_branch)

tests/integration/test_repository.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
import pytest
6-
75
from infrahub_sdk.testing.docker import TestInfrahubDockerClient
86
from infrahub_sdk.testing.repository import GitRepo
97
from infrahub_sdk.utils import get_fixtures_dir
@@ -13,10 +11,6 @@
1311

1412

1513
class TestInfrahubRepository(TestInfrahubDockerClient):
16-
@pytest.fixture(scope="class")
17-
def infrahub_version(self) -> str:
18-
return "1.1.0"
19-
2014
async def test_add_repository(self, client: InfrahubClient, remote_repos_dir):
2115
src_directory = get_fixtures_dir() / "integration/mock_repo"
2216
repo = GitRepo(name="mock_repo", src_directory=src_directory, dst_directory=remote_repos_dir)

0 commit comments

Comments
 (0)