Skip to content

Commit f17d4c3

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

File tree

5 files changed

+168
-73
lines changed

5 files changed

+168
-73
lines changed

.github/workflows/ci.yml

Lines changed: 135 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -115,65 +115,108 @@ jobs:
115115
SHELLCHECK_OPTS: --exclude=SC2086 --exclude=SC2046 --exclude=SC2004 --exclude=SC2129
116116

117117

118-
unit-tests:
119-
strategy:
120-
matrix:
121-
python-version:
122-
- "3.9"
123-
- "3.10"
124-
- "3.11"
125-
- "3.12"
126-
- "3.13"
127-
if: |
128-
always() && !cancelled() &&
129-
!contains(needs.*.result, 'failure') &&
130-
!contains(needs.*.result, 'cancelled') &&
131-
needs.files-changed.outputs.python == 'true'
132-
needs: ["files-changed", "yaml-lint", "python-lint"]
133-
runs-on: ubuntu-latest
134-
timeout-minutes: 30
135-
steps:
136-
- name: "Check out repository code"
137-
uses: "actions/checkout@v4"
138-
- name: Set up Python ${{ matrix.python-version }}
139-
uses: actions/setup-python@v5
140-
with:
141-
python-version: ${{ matrix.python-version }}
142-
- name: "Setup environment"
143-
run: |
144-
pipx install poetry==1.8.5
145-
poetry config virtualenvs.create true --local
146-
pip install invoke toml codecov
147-
- name: "Install Package"
148-
run: "poetry install --all-extras"
149-
- name: "Mypy Tests"
150-
run: "poetry run mypy --show-error-codes infrahub_sdk/"
151-
# - name: "Pylint Tests"
152-
# run: "poetry run pylint infrahub_sdk/"
153-
- name: "Unit Tests"
154-
run: "poetry run pytest --cov infrahub_sdk tests/unit/"
155-
- name: "Upload coverage to Codecov"
156-
run: |
157-
codecov --flags python-${{ matrix.python-version }}
158-
env:
159-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
160-
161-
# The pytest-cov plugin doesn't quite work with content that is
162-
# auto-imported by our own infrahub pytest plugin, this workaround
163-
# reports the missing lines
164-
- name: "Report coverage for pytest-plugin"
165-
if: matrix.python-version == '3.12'
166-
run: |
167-
source $(poetry env info --path)/bin/activate
168-
coverage run --source=infrahub_sdk -m pytest tests/unit/pytest_plugin
169-
coverage report -m
170-
coverage xml
171-
codecov --flags python-filler-${{ matrix.python-version }}
172-
env:
173-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
118+
# unit-tests:
119+
# strategy:
120+
# matrix:
121+
# python-version:
122+
# - "3.9"
123+
# - "3.10"
124+
# - "3.11"
125+
# - "3.12"
126+
# - "3.13"
127+
# if: |
128+
# always() && !cancelled() &&
129+
# !contains(needs.*.result, 'failure') &&
130+
# !contains(needs.*.result, 'cancelled') &&
131+
# needs.files-changed.outputs.python == 'true'
132+
# needs: ["files-changed", "yaml-lint", "python-lint"]
133+
# runs-on: ubuntu-latest
134+
# timeout-minutes: 30
135+
# steps:
136+
# - name: "Check out repository code"
137+
# uses: "actions/checkout@v4"
138+
# - name: Set up Python ${{ matrix.python-version }}
139+
# uses: actions/setup-python@v5
140+
# with:
141+
# python-version: ${{ matrix.python-version }}
142+
# - name: "Setup environment"
143+
# run: |
144+
# pipx install poetry==1.8.5
145+
# poetry config virtualenvs.create true --local
146+
# pip install invoke toml codecov
147+
# - name: "Install Package"
148+
# run: "poetry install --all-extras"
149+
# - name: "Mypy Tests"
150+
# run: "poetry run mypy --show-error-codes infrahub_sdk/"
151+
# # - name: "Pylint Tests"
152+
# # run: "poetry run pylint infrahub_sdk/"
153+
# - name: "Unit Tests"
154+
# run: "poetry run pytest --cov infrahub_sdk tests/unit/"
155+
# - name: "Upload coverage to Codecov"
156+
# run: |
157+
# codecov --flags python-${{ matrix.python-version }}
158+
# env:
159+
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
160+
#
161+
# # The pytest-cov plugin doesn't quite work with content that is
162+
# # auto-imported by our own infrahub pytest plugin, this workaround
163+
# # reports the missing lines
164+
# - name: "Report coverage for pytest-plugin"
165+
# if: matrix.python-version == '3.12'
166+
# run: |
167+
# source $(poetry env info --path)/bin/activate
168+
# coverage run --source=infrahub_sdk -m pytest tests/unit/pytest_plugin
169+
# coverage report -m
170+
# coverage xml
171+
# codecov --flags python-filler-${{ matrix.python-version }}
172+
# env:
173+
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
174174

175175
# ------------------------------------------ Integration Tests ------------------------------------------
176-
integration-tests:
176+
# integration-tests:
177+
# if: |
178+
# always() && !cancelled() &&
179+
# !contains(needs.*.result, 'failure') &&
180+
# !contains(needs.*.result, 'cancelled') &&
181+
# needs.files-changed.outputs.python == 'true'
182+
# needs: ["files-changed", "yaml-lint", "python-lint"]
183+
# runs-on:
184+
# group: "huge-runners"
185+
# timeout-minutes: 30
186+
# strategy:
187+
# matrix:
188+
# version: ["1.0.0", "1.1.0"]
189+
# steps:
190+
# - name: "Check out repository code"
191+
# uses: "actions/checkout@v4"
192+
# - name: Set up Python
193+
# uses: actions/setup-python@v5
194+
# with:
195+
# python-version: "3.12"
196+
# - name: "Set environment variables"
197+
# run: |
198+
# RUNNER_NAME=$(echo "${{ runner.name }}" | grep -o 'ghrunner[0-9]\+' | sed 's/ghrunner\([0-9]\+\)/ghrunner_\1/')
199+
# echo "PYTEST_DEBUG_TEMPROOT=/var/lib/github/${RUNNER_NAME}/_temp" >> $GITHUB_ENV
200+
# - name: "Setup environment"
201+
# run: |
202+
# pipx install poetry==1.8.5
203+
# poetry config virtualenvs.create true --local
204+
# pip install invoke toml codecov
205+
# - name: "Install Package"
206+
# run: "poetry install --all-extras"
207+
# - name: "Set environment variables for python_testcontainers"
208+
# run: |
209+
# echo INFRAHUB_TESTING_IMAGE_VER=${{ matrix.version }} >> $GITHUB_ENV
210+
# - name: "Integration Tests"
211+
# run: |
212+
# echo "Running tests for version: ${{ matrix.version }}"
213+
# poetry run pytest --cov infrahub_sdk tests/integration/
214+
# - name: "Upload coverage to Codecov"
215+
# run: |
216+
# codecov --flags integration-tests
217+
# env:
218+
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
219+
integration-tests-against-local-infrahub-build:
177220
if: |
178221
always() && !cancelled() &&
179222
!contains(needs.*.result, 'failure') &&
@@ -186,23 +229,56 @@ jobs:
186229
steps:
187230
- name: "Check out repository code"
188231
uses: "actions/checkout@v4"
232+
233+
- name: "Checkout infrahub repository"
234+
uses: "actions/checkout@v4"
235+
with:
236+
repository: "opsmill/infrahub"
237+
path: "infrahub-server"
238+
submodules: true
239+
189240
- name: Set up Python
190241
uses: actions/setup-python@v5
191242
with:
192243
python-version: "3.12"
193-
- name: "Set environment variables"
244+
245+
- name: "Setup git credentials prior dev.build"
246+
run: |
247+
cd infrahub-server
248+
git config --global user.name 'Infrahub'
249+
git config --global user.email '[email protected]'
250+
git config --global --add safe.directory '*'
251+
git config --global credential.usehttppath true
252+
git config --global credential.helper /usr/local/bin/infrahub-git-credential
253+
254+
- name: "Set environment variables prior dev.build"
194255
run: |
256+
echo "INFRAHUB_BUILD_NAME=infrahub-${{ runner.name }}" >> $GITHUB_ENV
195257
RUNNER_NAME=$(echo "${{ runner.name }}" | grep -o 'ghrunner[0-9]\+' | sed 's/ghrunner\([0-9]\+\)/ghrunner_\1/')
196258
echo "PYTEST_DEBUG_TEMPROOT=/var/lib/github/${RUNNER_NAME}/_temp" >> $GITHUB_ENV
259+
echo "INFRAHUB_IMAGE_VER=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
260+
echo "INFRAHUB_TESTING_IMAGE_TAG=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
261+
echo "INFRAHUB_TESTING_DOCKER_IMAGE=opsmill/infrahub" >> $GITHUB_ENV
262+
263+
- name: "Build container"
264+
run: |
265+
cd infrahub-server
266+
inv dev.build
267+
197268
- name: "Setup environment"
198269
run: |
199270
pipx install poetry==1.8.5
200271
poetry config virtualenvs.create true --local
201272
pip install invoke toml codecov
273+
202274
- name: "Install Package"
203275
run: "poetry install --all-extras"
276+
204277
- name: "Integration Tests"
205-
run: "poetry run pytest --cov infrahub_sdk tests/integration/"
278+
run: |
279+
echo "Running tests for version: $INFRAHUB_TESTING_IMAGE_TAG"
280+
poetry run pytest --cov infrahub_sdk tests/integration/
281+
206282
- name: "Upload coverage to Codecov"
207283
run: |
208284
codecov --flags integration-tests

infrahub_sdk/testing/docker.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
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+
DEFAULT_INFRAHUB_VERSION = "todo"
12+
INFRAHUB_VERSION = os.getenv("INFRAHUB_TESTING_IMAGE_TAG", DEFAULT_INFRAHUB_VERSION)
13+
14+
15+
# TODO test it works
16+
def skip_version(min_infrahub_version: str | None = None, max_infrahub_version: str | None = None) -> bool:
17+
"""
18+
Check if a test should be skipped depending on infrahub version.
19+
"""
20+
try:
21+
version = Version(INFRAHUB_VERSION)
22+
except InvalidVersion:
23+
# We would typically end up here for development purpose while running a CI test against
24+
# unreleased versions of infrahub, like `stable` or `develop` branch.
25+
# For now, we consider this means we are testing against the most recent version of infrahub,
26+
# so we skip if the test should not be ran against a maximum version.
27+
return max_infrahub_version is None
28+
29+
if min_infrahub_version is not None and version <= Version(min_infrahub_version):
30+
return True
31+
32+
return max_infrahub_version is not None and version <= Version(max_infrahub_version)
33+
634

735
class TestInfrahubDockerClient(TestInfrahubDocker):
36+
@pytest.fixture(scope="class")
37+
def infrahub_version(self) -> str:
38+
print(f"{INFRAHUB_VERSION=}")
39+
return INFRAHUB_VERSION
40+
841
@pytest.fixture(scope="class")
942
def client(self, infrahub_port: int) -> InfrahubClient:
1043
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)