diff --git a/docker-entrypoint.py b/docker-entrypoint.py index 095c266d7..ce54882ec 100644 --- a/docker-entrypoint.py +++ b/docker-entrypoint.py @@ -12,6 +12,14 @@ # Import the Flask app from rustchain_dashboard from rustchain_dashboard import app + +def _safe_int_env(name, default): + try: + return int(os.environ.get(name, str(default))) + except (TypeError, ValueError): + return default + + # Add health check endpoint @app.route('/health') def health_check(): @@ -43,5 +51,5 @@ def health_check(): if __name__ == '__main__': # Run the app - port = int(os.environ.get('PORT', 8099)) + port = _safe_int_env('PORT', 8099) app.run(host='0.0.0.0', port=port, debug=False) diff --git a/tests/test_docker_entrypoint_env.py b/tests/test_docker_entrypoint_env.py new file mode 100644 index 000000000..ee31e6f83 --- /dev/null +++ b/tests/test_docker_entrypoint_env.py @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: MIT +import importlib.util +import sys +import types +from pathlib import Path + +from flask import Flask + + +MODULE_PATH = Path(__file__).resolve().parents[1] / "docker-entrypoint.py" + + +def load_entrypoint(monkeypatch): + dashboard = types.ModuleType("rustchain_dashboard") + dashboard.app = Flask("rustchain_dashboard_test") + monkeypatch.setitem(sys.modules, "rustchain_dashboard", dashboard) + + module_name = "docker_entrypoint_under_test" + sys.modules.pop(module_name, None) + spec = importlib.util.spec_from_file_location(module_name, MODULE_PATH) + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + sys.modules[module_name] = module + spec.loader.exec_module(module) + return module + + +def test_malformed_port_env_uses_default(monkeypatch): + monkeypatch.setenv("PORT", "not-a-port") + module = load_entrypoint(monkeypatch) + + assert module._safe_int_env("PORT", 8099) == 8099 + + +def test_numeric_port_env_is_preserved(monkeypatch): + monkeypatch.setenv("PORT", "8100") + module = load_entrypoint(monkeypatch) + + assert module._safe_int_env("PORT", 8099) == 8100 + + +def test_health_route_registers_with_stub_dashboard(monkeypatch): + monkeypatch.delenv("PORT", raising=False) + module = load_entrypoint(monkeypatch) + + response = module.app.test_client().get("/health") + + assert response.status_code == 200 + assert response.get_json()["status"] == "healthy"