mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-06 17:15:38 -08:00
Add APP_CONF_OVERRIDE support
This commit is contained in:
@@ -56,6 +56,10 @@ set -u
|
||||
FAILED_STATUS=""
|
||||
echo "Startup pre-checks"
|
||||
for script in ${SYSTEM_SERVICES_SCRIPTS}/check-*.sh; do
|
||||
if [ -n "${DISABLE_STARTUP_CHECKS:-}" ]; then
|
||||
echo "Skipping startup checks as DISABLE_STARTUP_CHECKS is set."
|
||||
break
|
||||
fi
|
||||
script_name=$(basename "$script" | sed 's/^check-//;s/\.sh$//;s/-/ /g')
|
||||
echo " --> ${script_name}"
|
||||
|
||||
@@ -76,6 +80,13 @@ if [ -n "${FAILED_STATUS}" ]; then
|
||||
exit ${FAILED_STATUS}
|
||||
fi
|
||||
|
||||
# Set APP_CONF_OVERRIDE based on GRAPHQL_PORT if not already set
|
||||
if [ -n "${GRAPHQL_PORT:-}" ] && [ -z "${APP_CONF_OVERRIDE:-}" ]; then
|
||||
export APP_CONF_OVERRIDE='{"GRAPHQL_PORT":"'"${GRAPHQL_PORT}"'"}'
|
||||
echo "Setting APP_CONF_OVERRIDE to $APP_CONF_OVERRIDE"
|
||||
fi
|
||||
|
||||
|
||||
# Exit after checks if in check-only mode (for testing)
|
||||
if [ "${NETALERTX_CHECK_ONLY:-0}" -eq 1 ]; then
|
||||
exit 0
|
||||
|
||||
82
test/docker_tests/test_entrypoint.py
Normal file
82
test/docker_tests/test_entrypoint.py
Normal file
@@ -0,0 +1,82 @@
|
||||
'''
|
||||
Tests for the NetAlertX entrypoint.sh script.
|
||||
|
||||
These tests verify the behavior of the entrypoint script under various conditions,
|
||||
such as environment variable settings and check skipping.
|
||||
'''
|
||||
|
||||
import subprocess
|
||||
import uuid
|
||||
import pytest
|
||||
|
||||
IMAGE = "netalertx-test"
|
||||
|
||||
|
||||
def _run_entrypoint(env: dict[str, str] | None = None, check_only: bool = True) -> subprocess.CompletedProcess[str]:
|
||||
"""Run the entrypoint script in the test container with given environment."""
|
||||
name = f"netalertx-test-entrypoint-{uuid.uuid4().hex[:8]}".lower()
|
||||
cmd = [
|
||||
"docker", "run", "--rm", "--name", name,
|
||||
"--network", "host", "--userns", "host",
|
||||
"--tmpfs", "/tmp:mode=777",
|
||||
"--cap-add", "NET_RAW", "--cap-add", "NET_ADMIN", "--cap-add", "NET_BIND_SERVICE",
|
||||
]
|
||||
if env:
|
||||
for key, value in env.items():
|
||||
cmd.extend(["-e", f"{key}={value}"])
|
||||
if check_only:
|
||||
cmd.extend(["-e", "NETALERTX_CHECK_ONLY=1"])
|
||||
cmd.extend([
|
||||
"--entrypoint", "/bin/sh", IMAGE, "-c",
|
||||
"sh /entrypoint.sh"
|
||||
])
|
||||
return subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
||||
|
||||
|
||||
@pytest.mark.docker
|
||||
@pytest.mark.feature_complete
|
||||
def test_skip_tests_env_var():
|
||||
# If SKIP_TESTS=1 is set, the entrypoint should skip all startup checks and print a
|
||||
# message indicating checks are skipped.
|
||||
# There should be no check output, and the script should exit successfully.
|
||||
result = _run_entrypoint(env={"SKIP_TESTS": "1"}, check_only=True)
|
||||
assert "Skipping startup checks as SKIP_TESTS is set." in result.stdout
|
||||
assert " --> " not in result.stdout # No check outputs
|
||||
assert result.returncode == 0
|
||||
|
||||
|
||||
@pytest.mark.docker
|
||||
@pytest.mark.feature_complete
|
||||
def test_app_conf_override_from_graphql_port():
|
||||
# If GRAPHQL_PORT is set and APP_CONF_OVERRIDE is not set, the entrypoint should set
|
||||
# APP_CONF_OVERRIDE to a JSON string containing the GRAPHQL_PORT value and print a message
|
||||
# about it.
|
||||
# The script should exit successfully.
|
||||
result = _run_entrypoint(env={"GRAPHQL_PORT": "20212", "SKIP_TESTS": "1"}, check_only=True)
|
||||
assert 'Setting APP_CONF_OVERRIDE to {"GRAPHQL_PORT":"20212"}' in result.stdout
|
||||
assert result.returncode == 0
|
||||
|
||||
|
||||
@pytest.mark.docker
|
||||
@pytest.mark.feature_complete
|
||||
def test_app_conf_override_not_overridden():
|
||||
# If both GRAPHQL_PORT and APP_CONF_OVERRIDE are set, the entrypoint should NOT override
|
||||
# APP_CONF_OVERRIDE or print a message about it.
|
||||
# The script should exit successfully.
|
||||
result = _run_entrypoint(env={
|
||||
"GRAPHQL_PORT": "20212",
|
||||
"APP_CONF_OVERRIDE": '{"OTHER":"value"}',
|
||||
"SKIP_TESTS": "1"
|
||||
}, check_only=True)
|
||||
assert 'Setting APP_CONF_OVERRIDE to' not in result.stdout
|
||||
assert result.returncode == 0
|
||||
|
||||
|
||||
@pytest.mark.docker
|
||||
@pytest.mark.feature_complete
|
||||
def test_no_app_conf_override_when_no_graphql_port():
|
||||
# If GRAPHQL_PORT is not set, the entrypoint should NOT set or print APP_CONF_OVERRIDE.
|
||||
# The script should exit successfully.
|
||||
result = _run_entrypoint(env={"SKIP_TESTS": "1"}, check_only=True)
|
||||
assert 'Setting APP_CONF_OVERRIDE to' not in result.stdout
|
||||
assert result.returncode == 0
|
||||
Reference in New Issue
Block a user