Skip to content

Commit c54a904

Browse files
authored
Merge pull request #246 from opsmill/lgu-ci-tests
Run CI tests against multiple versions
2 parents cce86a1 + e90acb4 commit c54a904

File tree

5 files changed

+111
-16
lines changed

5 files changed

+111
-16
lines changed

.github/workflows/ci.yml

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ jobs:
176176
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
177177

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

infrahub_sdk/testing/docker.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1+
from __future__ import annotations
2+
3+
import os
4+
15
import pytest
26
from infrahub_testcontainers.helpers import TestInfrahubDocker
7+
from packaging.version import InvalidVersion, Version
38

49
from .. import Config, InfrahubClient, InfrahubClientSync
510

11+
INFRAHUB_VERSION = os.getenv("INFRAHUB_TESTING_IMAGE_VER", "latest")
12+
13+
14+
def skip_version(min_infrahub_version: str | None = None, max_infrahub_version: str | None = None) -> bool:
15+
"""
16+
Check if a test should be skipped depending on infrahub version.
17+
"""
18+
try:
19+
version = Version(INFRAHUB_VERSION)
20+
except InvalidVersion:
21+
# We would typically end up here for development purpose while running a CI test against
22+
# unreleased versions of infrahub, like `stable` or `develop` branch.
23+
# For now, we consider this means we are testing against the most recent version of infrahub,
24+
# so we skip if the test should not be ran against a maximum version.
25+
return max_infrahub_version is None
26+
27+
if min_infrahub_version is not None and version < Version(min_infrahub_version):
28+
return True
29+
30+
return max_infrahub_version is not None and version > Version(max_infrahub_version)
31+
632

733
class TestInfrahubDockerClient(TestInfrahubDocker):
34+
@pytest.fixture(scope="class")
35+
def infrahub_version(self) -> str:
36+
return INFRAHUB_VERSION
37+
838
@pytest.fixture(scope="class")
939
def client(self, infrahub_port: int) -> InfrahubClient:
1040
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)