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
29 changes: 21 additions & 8 deletions src/aws_durable_execution_sdk_python_testing/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CliConfig:
# Server configuration
host: str = "0.0.0.0" # noqa:S104
port: int = 5000
log_level: int = 20 # INFO level
log_level: int = logging.INFO
lambda_endpoint: str = "http://127.0.0.1:3001"
local_runner_endpoint: str = "http://0.0.0.0:5000"
local_runner_region: str = "us-west-2"
Expand All @@ -53,10 +53,14 @@ class CliConfig:
@classmethod
def from_environment(cls) -> CliConfig:
"""Create configuration from environment variables with defaults."""
# Convert log level string to integer if provided
log_level_str = os.getenv("AWS_DEX_LOG_LEVEL", "INFO")
log_level = logging.getLevelNamesMapping().get(log_level_str, logging.INFO)

return cls(
host=os.getenv("AWS_DEX_HOST", "0.0.0.0"), # noqa:S104
port=int(os.getenv("AWS_DEX_PORT", "5000")),
log_level=int(os.getenv("AWS_DEX_LOG_LEVEL", "20")),
log_level=log_level,
lambda_endpoint=os.getenv(
"AWS_DEX_LAMBDA_ENDPOINT", "http://127.0.0.1:3001"
),
Expand Down Expand Up @@ -89,10 +93,18 @@ def run(self, args: list[str] | None = None) -> int:
parsed_args = parser.parse_args(args)

# Configure logging based on log level
if hasattr(parsed_args, "log_level") and isinstance(
parsed_args.log_level, str
):
level = logging.getLevelNamesMapping().get(
parsed_args.log_level, logging.INFO
)
else:
# config.log_level is always an integer
level = self.config.log_level

logging.basicConfig(
level=parsed_args.log_level
if hasattr(parsed_args, "log_level")
else self.config.log_level,
level=level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

Expand Down Expand Up @@ -151,9 +163,10 @@ def _create_start_server_parser(self, subparsers) -> None:
)
start_server_parser.add_argument(
"--log-level",
type=int,
default=self.config.log_level,
help=f"Logging level as integer (default: {self.config.log_level}, env: AWS_DEX_LOG_LEVEL)",
type=str,
choices=list(logging.getLevelNamesMapping().keys()),
default=logging.getLevelName(self.config.log_level),
help=f"Logging level (default: {logging.getLevelName(self.config.log_level)}, env: AWS_DEX_LOG_LEVEL)",
)
start_server_parser.add_argument(
"--lambda-endpoint",
Expand Down
23 changes: 12 additions & 11 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import argparse
import json
import logging
import os
import sys
from io import StringIO
Expand All @@ -24,7 +25,7 @@ def test_cli_config_has_correct_default_values() -> None:

assert config.host == "0.0.0.0" # noqa: S104
assert config.port == 5000
assert config.log_level == 20
assert config.log_level == logging.INFO
assert config.lambda_endpoint == "http://127.0.0.1:3001"
assert config.local_runner_endpoint == "http://0.0.0.0:5000"
assert config.local_runner_region == "us-west-2"
Expand All @@ -38,7 +39,7 @@ def test_cli_config_from_environment_uses_defaults_when_no_env_vars() -> None:

assert config.host == "0.0.0.0" # noqa: S104
assert config.port == 5000
assert config.log_level == 20
assert config.log_level == logging.INFO
assert config.lambda_endpoint == "http://127.0.0.1:3001"
assert config.local_runner_endpoint == "http://0.0.0.0:5000"
assert config.local_runner_region == "us-west-2"
Expand All @@ -50,7 +51,7 @@ def test_cli_config_from_environment_uses_all_env_vars_when_set() -> None:
env_vars = {
"AWS_DEX_HOST": "127.0.0.1",
"AWS_DEX_PORT": "8080",
"AWS_DEX_LOG_LEVEL": "10",
"AWS_DEX_LOG_LEVEL": "DEBUG",
"AWS_DEX_LAMBDA_ENDPOINT": "http://localhost:4000",
"AWS_DEX_LOCAL_RUNNER_ENDPOINT": "http://localhost:8080",
"AWS_DEX_LOCAL_RUNNER_REGION": "us-east-1",
Expand All @@ -62,7 +63,7 @@ def test_cli_config_from_environment_uses_all_env_vars_when_set() -> None:

assert config.host == "127.0.0.1"
assert config.port == 8080
assert config.log_level == 10
assert config.log_level == logging.DEBUG
assert config.lambda_endpoint == "http://localhost:4000"
assert config.local_runner_endpoint == "http://localhost:8080"
assert config.local_runner_region == "us-east-1"
Expand All @@ -82,7 +83,7 @@ def test_cli_config_from_environment_uses_partial_env_vars_with_defaults() -> No
assert config.host == "192.168.1.1"
assert config.port == 9000
# Other values should be defaults
assert config.log_level == 20
assert config.log_level == logging.INFO
assert config.lambda_endpoint == "http://127.0.0.1:3001"


Expand Down Expand Up @@ -173,7 +174,7 @@ def test_start_server_command_parses_arguments_correctly() -> None:
"--port",
"8080",
"--log-level",
"10",
"DEBUG",
"--lambda-endpoint",
"http://localhost:4000",
"--local-runner-endpoint",
Expand Down Expand Up @@ -303,7 +304,7 @@ def test_logging_configuration_uses_specified_log_level() -> None:
with patch("logging.basicConfig") as mock_basic_config:
with patch("sys.stdout", new_callable=StringIO):
with patch.object(app, "start_server_command", return_value=0):
app.run(["start-server", "--log-level", "10"])
app.run(["start-server", "--log-level", "DEBUG"])

mock_basic_config.assert_called_once()
call_args = mock_basic_config.call_args
Expand Down Expand Up @@ -348,7 +349,7 @@ def test_start_server_command_works_with_mocked_dependencies() -> None:
"--port",
"8080",
"--log-level",
"10",
"DEBUG",
]
)

Expand All @@ -359,7 +360,7 @@ def test_start_server_command_works_with_mocked_dependencies() -> None:
call_args = mock_web_runner.call_args[0][0] # First positional argument
assert call_args.web_service.host == "127.0.0.1"
assert call_args.web_service.port == 8080
assert call_args.web_service.log_level == 10
assert call_args.web_service.log_level == "DEBUG"


def test_start_server_command_handles_server_startup_errors() -> None:
Expand Down Expand Up @@ -398,7 +399,7 @@ def test_start_server_command_creates_correct_web_runner_config() -> None:
"--port",
"9000",
"--log-level",
"30",
"WARNING",
"--lambda-endpoint",
"http://custom-lambda:4000",
"--local-runner-endpoint",
Expand All @@ -419,7 +420,7 @@ def test_start_server_command_creates_correct_web_runner_config() -> None:
# Verify web service configuration
assert config.web_service.host == "192.168.1.100"
assert config.web_service.port == 9000
assert config.web_service.log_level == 30
assert config.web_service.log_level == "WARNING"

# Verify Lambda service configuration
assert config.lambda_endpoint == "http://custom-lambda:4000"
Expand Down
16 changes: 8 additions & 8 deletions tests/runner_web_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def test_should_pass_correct_configuration_to_web_server():
"""Test that WebServer receives correct configuration from WebRunnerConfig."""
# Arrange
web_config = WebServiceConfig(
host="custom-host", port=9999, log_level=30, max_request_size=2048
host="custom-host", port=9999, log_level="WARNING", max_request_size=2048
)
runner_config = WebRunnerConfig(web_service=web_config)
runner = WebRunner(runner_config)
Expand Down Expand Up @@ -734,7 +734,7 @@ def test_should_pass_correct_configuration_to_web_server():
assert passed_config == web_config
assert passed_config.host == "custom-host"
assert passed_config.port == 9999
assert passed_config.log_level == 30
assert passed_config.log_level == "WARNING"
assert passed_config.max_request_size == 2048

# Cleanup
Expand Down Expand Up @@ -1284,7 +1284,7 @@ def test_should_integrate_with_cli_start_server_command():
"--port",
"7777",
"--log-level",
"30",
"WARNING",
"--lambda-endpoint",
"http://integration-lambda:4000",
"--local-runner-endpoint",
Expand All @@ -1306,7 +1306,7 @@ def test_should_integrate_with_cli_start_server_command():
# Verify web service configuration
assert config.web_service.host == "integration-host"
assert config.web_service.port == 7777
assert config.web_service.log_level == 30
assert config.web_service.log_level == "WARNING"

# Verify Lambda service configuration
assert config.lambda_endpoint == "http://integration-lambda:4000"
Expand Down Expand Up @@ -1430,7 +1430,7 @@ def test_should_preserve_cli_configuration_through_web_runner():
"--port",
"9999",
"--log-level",
"40", # ERROR level
"ERROR", # ERROR level
"--lambda-endpoint",
"http://config-lambda:5000",
"--local-runner-endpoint",
Expand All @@ -1452,7 +1452,7 @@ def test_should_preserve_cli_configuration_through_web_runner():
# Verify web service configuration
assert config.web_service.host == "config-test-host"
assert config.web_service.port == 9999
assert config.web_service.log_level == 40
assert config.web_service.log_level == "ERROR"

# Verify Lambda service configuration
assert config.lambda_endpoint == "http://config-lambda:5000"
Expand All @@ -1472,7 +1472,7 @@ def test_should_handle_environment_variable_integration():
env_vars = {
"AWS_DEX_HOST": "env-host",
"AWS_DEX_PORT": "8888",
"AWS_DEX_LOG_LEVEL": "50", # CRITICAL level
"AWS_DEX_LOG_LEVEL": "CRITICAL", # CRITICAL level
"AWS_DEX_LAMBDA_ENDPOINT": "http://env-lambda:6000",
"AWS_DEX_LOCAL_RUNNER_ENDPOINT": "http://env-runner:7000",
"AWS_DEX_LOCAL_RUNNER_REGION": "sa-east-1",
Expand Down Expand Up @@ -1505,7 +1505,7 @@ def test_should_handle_environment_variable_integration():
# Verify environment variables were used
assert config.web_service.host == "env-host"
assert config.web_service.port == 8888
assert config.web_service.log_level == 50
assert config.web_service.log_level == "CRITICAL"
assert config.lambda_endpoint == "http://env-lambda:6000"
assert config.local_runner_endpoint == "http://env-runner:7000"
assert config.local_runner_region == "sa-east-1"
Expand Down