Skip to content

Commit

Permalink
move env to conftest
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <[email protected]>
  • Loading branch information
AndreKurait committed Nov 7, 2024
1 parent 77813d1 commit be19bc3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from tests.utils import target_cluster_refresh, get_target_index_info
from src.tools.create_index import main as create_index
from src.cluster_tools.utils import console_curl
from .utils import env as env # noqa: F401 - used via pytest fixtures
import argparse
import logging
import pytest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
import tempfile
import yaml
import pytest
from testcontainers.opensearch import OpenSearchContainer
from testcontainers.core.waiting_utils import wait_for_logs
from console_link.environment import Environment


@pytest.fixture(scope="function")
def env():
# Spin up the OpenSearch container and wait until it's healthy
container = OpenSearchContainer()
container.start()
wait_for_logs(container, ".*recovered .* indices into cluster_state.*")

base_url = f"http://{container.get_container_host_ip()}:{container.get_exposed_port(9200)}"
# Create a temporary services.yaml file based on the services.yaml spec
services_config = {
'target_cluster': {
'endpoint': base_url,
'allow_insecure': True,
'no_auth': {}
}
}

with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_config:
yaml.dump(services_config, temp_config)
temp_config_path = temp_config.name

yield Environment(temp_config_path)

# Stop the container and clean up the temporary services.yaml file after tests complete
container.stop()
os.remove(temp_config_path)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from src.tools.create_index import main as create_index
from tests.utils import get_target_index_info
import argparse
from .utils import env as env # noqa: F401 - used via pytest fixtures
import logging

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from src.tools.disable_compatibility_mode import main as disable_compatibility_mode
from src.cluster_tools.utils import console_curl
from .utils import env as env # noqa: F401 - used via pytest fixtures
import logging

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from src.tools.enable_compatibility_mode import main as enable_compatibility_mode
from src.cluster_tools.utils import console_curl
from .utils import env as env # noqa: F401 - used via pytest fixtures
import logging

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import argparse
from tests.utils import get_target_index_info
import src.tools.create_index as create_index
from .utils import env as env # noqa: F401 - used via pytest fixtures
import logging

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
import pytest
from testcontainers.opensearch import OpenSearchContainer
from testcontainers.core.waiting_utils import wait_for_logs
import tempfile
import os
import yaml
from console_link.environment import Environment
from src.cluster_tools.utils import console_curl
import logging

logger = logging.getLogger(__name__)


@pytest.fixture(scope="function")
def env():
# Spin up the OpenSearch container and wait until it's healthy
container = OpenSearchContainer()
container.start()
wait_for_logs(container, ".*recovered .* indices into cluster_state.*")

base_url = f"http://{container.get_container_host_ip()}:{container.get_exposed_port(9200)}"
# Create a temporary services.yaml file based on the services.yaml spec
services_config = {
'target_cluster': {
'endpoint': base_url,
'allow_insecure': True,
'no_auth': {}
}
}

with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_config:
yaml.dump(services_config, temp_config)
temp_config_path = temp_config.name

yield Environment(temp_config_path)

# Stop the container and clean up the temporary services.yaml file after tests complete
container.stop()
os.remove(temp_config_path)


def target_cluster_refresh(env: Environment) -> None:
"""Refreshes the target cluster's indices."""
console_curl(
Expand Down

0 comments on commit be19bc3

Please sign in to comment.