Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasG0 committed Sep 18, 2024
1 parent b0d22fd commit e978a70
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 1,209 deletions.
832 changes: 2 additions & 830 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

89 changes: 87 additions & 2 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import pytest
import ujson

from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_for_logs

from infrahub_sdk.utils import str_to_bool

from infrahub import config
Expand Down Expand Up @@ -133,10 +137,88 @@ async def register_core_models_schema(default_branch: Branch, register_internal_
return schema_branch


# TODO env variable?
START_DOCKER_DEPENDENCIES = True

@pytest.fixture(scope="session")
def neo4j(request: pytest.FixtureRequest) -> DockerContainer:
#if not START_DOCKER_DEPENDENCIES:
return

container = (
DockerContainer(image="neo4j:5.19.0-enterprise")
.with_env("NEO4J_AUTH", "neo4j/admin")
.with_env("NEO4J_ACCEPT_LICENSE_AGREEMENT", "yes")
.with_env("NEO4J_dbms_security_procedures_unrestricted", "apoc.*")
.with_env("NEO4J_dbms_security_auth__minimum__password__length", "4")
.with_exposed_ports(7687, 7474, 7444)
)


def cleanup():
container.stop()

container.start()
wait_for_logs(container, "Started.") # wait_container_is_ready does not seem to be enough

request.addfinalizer(cleanup)
return container


@pytest.fixture(scope="session")
def rabbitmq(request: pytest.FixtureRequest) -> DockerContainer:
#if not START_DOCKER_DEPENDENCIES:
return

container = (
DockerContainer(image="rabbitmq:latest")
.with_env("RABBITMQ_DEFAULT_USER", "infrahub")
.with_env("RABBITMQ_DEFAULT_PASS", "infrahub")
.with_exposed_ports(5672, 15672)
)


def cleanup():
container.stop()

container.start()
wait_for_logs(container, "Server startup complete;") # wait_container_is_ready does not seem to be enough

request.addfinalizer(cleanup)
return container

@pytest.fixture(scope="session")
def redis(request: pytest.FixtureRequest) -> DockerContainer:
if not START_DOCKER_DEPENDENCIES:
return

container = DockerContainer(image="redis:latest").with_exposed_ports(6379)


def cleanup():
container.stop()

container.start()

wait_for_logs(container, "Ready to accept connections tcp") # wait_container_is_ready does not seem to be enough

request.addfinalizer(cleanup)
return container


@pytest.fixture(scope="module", autouse=True)
def execute_before_any_test(worker_id, tmpdir_factory):
def load_settings_before_any_test(worker_id, tmpdir_factory,
neo4j: DockerContainer,
rabbitmq: DockerContainer,
redis: DockerContainer):
config.load_and_exit()

if START_DOCKER_DEPENDENCIES:
# config.SETTINGS.database.port = neo4j.get_exposed_port(7687)
config.SETTINGS.broker.port = rabbitmq.get_exposed_port(5672)
config.SETTINGS.cache.port = redis.get_exposed_port(6379)


config.SETTINGS.storage.driver = config.StorageDriver.FileSystemStorage

if TEST_IN_DOCKER:
Expand All @@ -145,9 +227,12 @@ def execute_before_any_test(worker_id, tmpdir_factory):
except (ValueError, IndexError):
db_id = 1

config.SETTINGS.cache.address = f"{BUILD_NAME}-cache-{db_id}"
config.SETTINGS.cache.address = "localhost" if not START_DOCKER_DEPENDENCIES else "172.17.0.1"

if config.SETTINGS.cache.driver == config.CacheDriver.NATS:
raise ValueError("Just making sure this path is unused during testing phase.")
config.SETTINGS.cache.address = f"{BUILD_NAME}-message-queue-{db_id}"

config.SETTINGS.database.address = f"{BUILD_NAME}-database-{db_id}"
config.SETTINGS.broker.address = f"{BUILD_NAME}-message-queue-{db_id}"
config.SETTINGS.storage.local = config.FileSystemStorageSettings(path="/opt/infrahub/storage")
Expand Down
5 changes: 4 additions & 1 deletion backend/tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import pytest
import yaml
from testcontainers.core.container import DockerContainer

from infrahub import config
from infrahub_sdk import UUIDT

from infrahub.core import registry
Expand Down Expand Up @@ -33,7 +36,7 @@ def event_loop():


@pytest.fixture(scope="module")
async def db() -> AsyncGenerator[InfrahubDatabase, None]:
async def db(load_settings_before_any_test) -> AsyncGenerator[InfrahubDatabase, None]:
driver = InfrahubDatabase(driver=await get_db(retry=1))

yield driver
Expand Down
21 changes: 0 additions & 21 deletions development/docker-compose-deps.yml
Original file line number Diff line number Diff line change
@@ -1,21 +0,0 @@
---
services:
message-queue:
user: rabbitmq
image: "${MESSAGE_QUEUE_DOCKER_IMAGE:-rabbitmq:latest}"
environment:
- "RABBITMQ_DEFAULT_USER=infrahub"
- "RABBITMQ_DEFAULT_PASS=infrahub"
healthcheck:
test: rabbitmq-diagnostics -q check_port_connectivity
interval: 5s
timeout: 30s
retries: 10
start_period: 3s
cache:
image: "${CACHE_DOCKER_IMAGE:-redis:latest}"
healthcheck:
test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
interval: 5s
timeout: 5s
retries: 3
7 changes: 0 additions & 7 deletions development/docker-compose-test-cache.yml
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
---
# yamllint disable rule:line-length
services:
cache:
deploy:
mode: replicated
replicas: "${NBR_WORKERS}"
2 changes: 0 additions & 2 deletions development/docker-compose-test-database-neo4j.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@ services:
condition: service_healthy
message-queue:
condition: service_healthy
cache:
condition: service_healthy
6 changes: 0 additions & 6 deletions development/docker-compose-test-message-queue.yml
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
---
services:
message-queue:
deploy:
mode: replicated
replicas: "${NBR_WORKERS}"
2 changes: 0 additions & 2 deletions development/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ services:
condition: service_healthy
message-queue:
condition: service_healthy
cache:
condition: service_healthy
environment:
<<: *infrahub_config
INFRAHUB_CONFIG: /source/development/infrahub.toml
Expand Down
Loading

0 comments on commit e978a70

Please sign in to comment.