Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenstad committed Dec 12, 2024
1 parent 8dacdd4 commit 74e0e16
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,43 @@ jobs:
if: always()
run: docker compose -p $INFRAHUB_BUILD_NAME down -v --remove-orphans --rmi local

backend-docker-integration:
if: |
always() && !cancelled() &&
!contains(needs.*.result, 'failure') &&
!contains(needs.*.result, 'cancelled') &&
needs.files-changed.outputs.backend == 'true'
needs: ["files-changed", "yaml-lint", "python-lint"]
runs-on:
group: huge-runners
timeout-minutes: 45
env:
INFRAHUB_DB_TYPE: neo4j
steps:
- name: "Check out repository code"
uses: "actions/checkout@v4"
with:
submodules: true
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: "Setup git credentials"
run: "git config --global user.name 'Infrahub' && \
git config --global user.email '[email protected]' && \
git config --global --add safe.directory '*' && \
git config --global credential.usehttppath true && \
git config --global credential.helper /usr/local/bin/infrahub-git-credential"
- name: "Setup Python environment"
run: |
poetry config virtualenvs.create true --local
poetry env use 3.12
- name: "Install dependencies"
run: "poetry install --no-interaction --no-ansi"
- name: "Install dependencies"
run: "poetry run pytest tests/integration_docker/"


# ------------------------------------------ Benchmarks ------------------------------------------------
backend-benchmark:
needs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
version: "1.0"
nodes:
- name: Device
namespace: Network
human_friendly_id: ['hostname__value']
attributes:
- name: hostname
kind: Text
unique: true
- name: model
kind: Text
- name: Interface
namespace: Network
state: absent
attributes:
- name: name
kind: Text
- name: description
kind: Text
optional: true

Check failure on line 21 in backend/tests/integration_docker/test_files/delete_interface_schema.yml

View workflow job for this annotation

GitHub Actions / yaml-lint

21:23 [new-line-at-end-of-file] no new line character at the end of file
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
version: "1.0"
nodes:
- name: Device
namespace: Network
human_friendly_id: ['hostname__value']
attributes:
- name: hostname
kind: Text
unique: true
- name: model
kind: Text
- name: Interface
namespace: Network
attributes:
- name: name
kind: Text
- name: description
kind: Text
optional: true

Check failure on line 20 in backend/tests/integration_docker/test_files/device_and_interface_schema.yml

View workflow job for this annotation

GitHub Actions / yaml-lint

20:23 [new-line-at-end-of-file] no new line character at the end of file
44 changes: 44 additions & 0 deletions backend/tests/integration_docker/test_schema_migration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import copy
from pathlib import Path
from typing import Any

import pytest
import yaml
from infrahub_sdk import InfrahubClient

from infrahub.testing.helpers import TestInfrahubDev
Expand All @@ -11,6 +13,8 @@
SchemaCarPerson,
)

CURRENT_DIRECTORY = Path(__file__).parent.resolve()


class TestSchemaMigrations(TestInfrahubDev, SchemaCarPerson):
@pytest.fixture(scope="class")
Expand All @@ -34,6 +38,9 @@ def schema_person_with_age(
async def test_setup_initial_schema(
self, default_branch: str, infrahub_client: InfrahubClient, schema_base: dict[str, Any]
) -> None:
await infrahub_client.schema.wait_until_converged(branch=default_branch)
# Validate that the schema is in sync after initial startup
assert await self.schema_in_sync(client=infrahub_client, branch=default_branch)
resp = await infrahub_client.schema.load(
schemas=[schema_base], branch=default_branch, wait_until_converged=True
)
Expand All @@ -53,3 +60,40 @@ async def test_update_schema(self, infrahub_client: InfrahubClient, schema_perso
resp = await infrahub_client.schema.load(schemas=[schema_person_with_age], branch=branch.name)

assert resp.errors == {}

async def test_schema_load_and_delete(self, infrahub_client: InfrahubClient) -> None:
with Path(CURRENT_DIRECTORY / "test_files/device_and_interface_schema.yml").open(encoding="utf-8") as file:
device_and_interface_schema = yaml.safe_load(file.read())

with Path(CURRENT_DIRECTORY / "test_files/delete_interface_schema.yml").open(encoding="utf-8") as file:
delete_interface_schema = yaml.safe_load(file.read())

device_branch = await infrahub_client.branch.create(branch_name="device_branch")

device_interface = await infrahub_client.schema.load(
schemas=[device_and_interface_schema], branch=device_branch.name, wait_until_converged=True
)
assert device_interface.schema_updated
# Validate that the schema is in sync after loading the device and interface schema
assert await self.schema_in_sync(client=infrahub_client, branch=device_branch.name)

delete_interface = await infrahub_client.schema.load(
schemas=[delete_interface_schema], branch=device_branch.name, wait_until_converged=True
)
assert delete_interface.schema_updated
# Validate that the schema is in sync after removing the interface
assert await self.schema_in_sync(client=infrahub_client, branch=device_branch.name)

@staticmethod
async def schema_in_sync(client: InfrahubClient, branch: str | None) -> bool:
SCHEMA_HASH_SYNC_STATUS = """
query {
InfrahubStatus {
summary {
schema_hash_synced
}
}
}
"""
response = await client.execute_graphql(query=SCHEMA_HASH_SYNC_STATUS, branch_name=branch)
return response["InfrahubStatus"]["summary"]["schema_hash_synced"]

0 comments on commit 74e0e16

Please sign in to comment.