Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .vscode/launch.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@
"justMyCode": false
},
{
// This test adds --external-envfile and expects a file named ".secrets" in the workspace root.
"name": "Python: Test w/ repo.config",
"type": "debugpy",
"request": "launch",
"module": "pytest",
"args": [
"--ff",
"--log-cli-level=INFO",
"--external-envfile=${workspaceFolder}/.secrets",
"--pdb",
"--setup-show",
"--durations=5",
"-s",
"-vv",
"${file}"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"justMyCode": false
},
{
// This tests enables the httpx spy and dumps captures in a json. Mainly for api-server
"name": "Python: Test-Httpx-Spy",
"type": "debugpy",
"request": "launch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# pylint: disable=unused-variable


import logging
import re
from pathlib import Path
from typing import Any
Expand All @@ -12,6 +13,8 @@
from .helpers.monkeypatch_envs import load_dotenv, setenvs_from_dict
from .helpers.typing_env import EnvVarsDict

_logger = logging.getLogger(__name__)


def pytest_addoption(parser: pytest.Parser):
simcore_group = parser.getgroup("simcore")
Expand All @@ -20,12 +23,17 @@ def pytest_addoption(parser: pytest.Parser):
action="store",
type=Path,
default=None,
help="Path to an env file. Consider passing a link to repo configs, i.e. `ln -s /path/to/osparc-ops-config/repo.config`",
help="Path to an env file. Replaces .env-devel in the tests by an external envfile."
"e.g. consider "
" `ln -s /path/to/osparc-ops-config/repo.config .secrets` and then "
" `pytest --external-envfile=.secrets --pdb tests/unit/test_core_settings.py`",
)


@pytest.fixture(scope="session")
def external_envfile_dict(request: pytest.FixtureRequest) -> EnvVarsDict:
def external_envfile_dict(
request: pytest.FixtureRequest, osparc_simcore_root_dir: Path
) -> EnvVarsDict:
"""
If a file under test folder prefixed with `.env-secret` is present,
then this fixture captures it.
Expand All @@ -35,19 +43,43 @@ def external_envfile_dict(request: pytest.FixtureRequest) -> EnvVarsDict:
"""
envs = {}
if envfile := request.config.getoption("--external-envfile"):
print("🚨 EXTERNAL `envfile` option detected. Loading", envfile, "...")
_logger.warning(
"🚨 EXTERNAL `envfile` option detected. Loading '%s' ...", envfile
)

assert isinstance(envfile, Path)
assert envfile.exists()
assert envfile.is_file()

envfile = envfile.resolve()
osparc_simcore_root_dir = osparc_simcore_root_dir.resolve()

if osparc_simcore_root_dir in envfile.parents and not any(
term in envfile.name.lower() for term in ("ignore", "secret")
):
_logger.warning(
"🚨 CAUTION: The external envfile '%s' may contain sensitive data and could be accidentally versioned. "
"To prevent this, include the words 'secret' or 'ignore' in the filename.",
envfile.name,
)

envs = load_dotenv(envfile)

if envs:
response = input(
f"🚨 CAUTION: You are about to run tests using environment variables loaded from '{envfile}'.\n"
"This may cause tests to interact with or modify real external systems (e.g., production or staging environments).\n"
"Proceeding could result in data loss or unintended side effects.\n"
"Are you sure you want to continue? [y/N]: "
)
if response.strip().lower() not in ("y", "yes"):
pytest.exit("Aborted by user due to external envfile usage.")

return envs


@pytest.fixture(scope="session")
def skip_if_external_envfile_dict(external_envfile_dict: EnvVarsDict) -> None:
def skip_if_no_external_envfile(external_envfile_dict: EnvVarsDict) -> None:
if not external_envfile_dict:
pytest.skip(reason="Skipping test since external-envfile is not set")

Expand Down
18 changes: 18 additions & 0 deletions services/agent/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# pylint: disable=unused-variable
# pylint: disable=unused-argument
# pylint: disable=redefined-outer-name


from pytest_simcore.helpers.monkeypatch_envs import (
EnvVarsDict,
)
from simcore_service_agent.core.settings import ApplicationSettings


def test_valid_application_settings(mock_environment: EnvVarsDict):
assert mock_environment

settings = ApplicationSettings() # type: ignore
assert settings

assert settings == ApplicationSettings.create_from_envs()
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def get_job_metadata_filter(
*Format*: `key:pattern` where pattern can contain glob wildcards
"""
),
example=["key1:val*", "key2:exactval"],
examples=[
["key1:val*", "key2:exactval"],
],
),
] = None,
) -> JobMetadataFilter | None:
Expand Down
32 changes: 5 additions & 27 deletions services/api-server/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,16 @@
# pylint: disable=redefined-outer-name


import pytest
from pytest_simcore.helpers.monkeypatch_envs import (
EnvVarsDict,
delenvs_from_dict,
setenvs_from_dict,
)
from simcore_service_api_server.core.settings import ApplicationSettings


@pytest.fixture
def app_environment(
monkeypatch: pytest.MonkeyPatch,
app_environment: EnvVarsDict,
external_envfile_dict: EnvVarsDict,
) -> EnvVarsDict:
"""
NOTE: To run against repo.config in osparc-config repo

ln -s /path/to/osparc-config/deployments/mydeploy.com/repo.config .secrets
pytest --external-envfile=.secrets tests/unit/test_core_settings.py

"""
if external_envfile_dict:
delenvs_from_dict(monkeypatch, app_environment, raising=False)
return setenvs_from_dict(
monkeypatch,
{**external_envfile_dict},
)
return app_environment
def test_valid_application_settings(app_environment: EnvVarsDict):
assert app_environment

settings = ApplicationSettings() # type: ignore
assert settings

def test_unit_app_environment(app_environment: EnvVarsDict):
assert app_environment
settings = ApplicationSettings.create_from_envs()
print("captured settings: \n", settings.model_dump_json(indent=2))
assert settings == ApplicationSettings.create_from_envs()
6 changes: 6 additions & 0 deletions services/autoscaling/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
# pylint: disable=unused-variable
"""
We can validate actual .env files (also refered as `repo.config` files) by passing them via the CLI

$ ln -s /path/to/osparc-config/deployments/mydeploy.com/repo.config .secrets
$ pytest --external-envfile=.secrets --pdb tests/unit/test_core_settings.py

"""
import datetime
import json
import logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ async def test_cluster_scaling_up_and_down(
],
)
async def test_cluster_scaling_up_and_down_against_aws(
skip_if_external_envfile_dict: None,
skip_if_no_external_envfile: None,
external_ec2_instances_allowed_types: None | dict[str, EC2InstanceBootSpecific],
with_labelize_drain_nodes: EnvVarsDict,
app_with_docker_join_drained: EnvVarsDict,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def pre_pull_images(


async def test_monitor_buffer_machines_against_aws(
skip_if_external_envfile_dict: None,
skip_if_no_external_envfile: None,
disable_buffers_pool_background_task: None,
disable_autoscaling_background_task: None,
disabled_rabbitmq: None,
Expand Down
3 changes: 0 additions & 3 deletions services/catalog/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import pytest
import respx
import simcore_service_catalog
import simcore_service_catalog.core.application
import simcore_service_catalog.core.events
import simcore_service_catalog.repository
import simcore_service_catalog.repository.events
import yaml
from asgi_lifespan import LifespanManager
from faker import Faker
Expand Down
10 changes: 2 additions & 8 deletions services/catalog/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
# pylint: disable=unused-variable
# pylint: disable=too-many-arguments


from pytest_simcore.helpers.typing_env import EnvVarsDict
from simcore_service_catalog.core.settings import ApplicationSettings


def test_valid_web_application_settings(app_environment: EnvVarsDict):
"""
We can validate actual .env files (also refered as `repo.config` files) by passing them via the CLI

$ ln -s /path/to/osparc-config/deployments/mydeploy.com/repo.config .secrets
$ pytest --external-envfile=.secrets --pdb tests/unit/test_core_settings.py
def test_valid_application_settings(app_environment: EnvVarsDict):
assert app_environment

"""
settings = ApplicationSettings() # type: ignore
assert settings

Expand Down
19 changes: 18 additions & 1 deletion services/clusters-keeper/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import pytest
from aws_library.ec2 import EC2InstanceBootSpecific
from pydantic import ValidationError
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict, setenvs_from_dict
from pytest_simcore.helpers.monkeypatch_envs import (
EnvVarsDict,
setenvs_from_dict,
)
from simcore_service_clusters_keeper.core.settings import ApplicationSettings
from types_aiobotocore_ec2.literals import InstanceTypeType

Expand Down Expand Up @@ -110,3 +113,17 @@ def test_valid_primary_custom_tags(
{"PRIMARY_EC2_INSTANCES_CUSTOM_TAGS": json.dumps(valid_tag)},
)
ApplicationSettings.create_from_envs()


def test_valid_application_settings(
monkeypatch: pytest.MonkeyPatch,
app_environment: EnvVarsDict,
):
# Mock
assert app_environment

# Test
settings = ApplicationSettings() # type: ignore
assert settings

assert settings == ApplicationSettings.create_from_envs()
34 changes: 5 additions & 29 deletions services/datcore-adapter/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,17 @@
# pylint: disable=unused-argument
# pylint: disable=redefined-outer-name


import pytest
from pytest_simcore.helpers.monkeypatch_envs import (
EnvVarsDict,
delenvs_from_dict,
setenvs_from_dict,
)
from simcore_service_datcore_adapter.core.settings import ApplicationSettings


@pytest.fixture
def app_environment(
monkeypatch: pytest.MonkeyPatch,
app_environment: EnvVarsDict,
external_envfile_dict: EnvVarsDict,
) -> EnvVarsDict:
"""
NOTE: To run against repo.config in osparc-config repo

ln -s /path/to/osparc-config/deployments/mydeploy.com/repo.config .secrets
pytest --external-envfile=.secrets tests/unit/test_core_settings.py

"""
if external_envfile_dict:
delenvs_from_dict(monkeypatch, app_environment, raising=False)
return setenvs_from_dict(
monkeypatch,
{**external_envfile_dict},
)
return app_environment


def test_unit_app_environment(app_environment: EnvVarsDict):
def test_valid_application_settings(app_environment: EnvVarsDict):
assert app_environment
settings = ApplicationSettings.create_from_envs()
print("captured settings: \n", settings.model_dump_json(indent=2))

settings = ApplicationSettings() # type: ignore
assert settings
assert settings == ApplicationSettings.create_from_envs()

assert settings.PENNSIEVE
2 changes: 1 addition & 1 deletion services/director-v2/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _get_backend_type_options() -> set[str]:

def test_supported_backends_did_not_change() -> None:
_EXPECTED = {"AWS", "CEPH", "MINIO"}
assert _EXPECTED == _get_backend_type_options(), (
assert _get_backend_type_options() == _EXPECTED, (
"Backend configuration change, please code support for "
"it in volumes_resolver -> _get_s3_volume_driver_config. "
"When done, adjust above list."
Expand Down
10 changes: 1 addition & 9 deletions services/director/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# pylint: disable=unused-variable
# pylint: disable=too-many-arguments


import datetime

import pytest
Expand All @@ -16,14 +15,7 @@
from simcore_service_director.core.settings import ApplicationSettings


def test_valid_web_application_settings(app_environment: EnvVarsDict):
"""
We validate actual envfiles (e.g. repo.config files) by passing them via the CLI

$ ln -s /path/to/osparc-config/deployments/mydeploy.com/repo.config .secrets
$ pytest --external-envfile=.secrets --pdb tests/unit/test_core_settings.py

"""
def test_valid_application_settings(app_environment: EnvVarsDict):
settings = ApplicationSettings() # type: ignore
assert settings

Expand Down
2 changes: 1 addition & 1 deletion services/director/tests/unit/test_registry_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def configure_number_concurrency_calls(


def test_list_services_performance(
skip_if_external_envfile_dict: None,
skip_if_no_external_envfile: None,
configure_external_registry_access: EnvVarsDict,
configure_number_concurrency_calls: EnvVarsDict,
registry_settings: RegistrySettings,
Expand Down
17 changes: 17 additions & 0 deletions services/dynamic-scheduler/tests/unit/test_core_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# pylint: disable=unused-variable
# pylint: disable=unused-argument
# pylint: disable=redefined-outer-name


from pytest_simcore.helpers.monkeypatch_envs import (
EnvVarsDict,
)
from simcore_service_dynamic_scheduler.core.settings import ApplicationSettings


def test_valid_application_settings(app_environment: EnvVarsDict):
assert app_environment

settings = ApplicationSettings() # type: ignore
assert settings
assert settings == ApplicationSettings.create_from_envs()
Loading
Loading