Skip to content

Commit 128c297

Browse files
authored
chore: update CLI to accept log level as string (#21)
1 parent 85bae2a commit 128c297

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

src/aws_durable_execution_sdk_python_testing/cli.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CliConfig:
4444
# Server configuration
4545
host: str = "0.0.0.0" # noqa:S104
4646
port: int = 5000
47-
log_level: int = 20 # INFO level
47+
log_level: int = logging.INFO
4848
lambda_endpoint: str = "http://127.0.0.1:3001"
4949
local_runner_endpoint: str = "http://0.0.0.0:5000"
5050
local_runner_region: str = "us-west-2"
@@ -53,10 +53,14 @@ class CliConfig:
5353
@classmethod
5454
def from_environment(cls) -> CliConfig:
5555
"""Create configuration from environment variables with defaults."""
56+
# Convert log level string to integer if provided
57+
log_level_str = os.getenv("AWS_DEX_LOG_LEVEL", "INFO")
58+
log_level = logging.getLevelNamesMapping().get(log_level_str, logging.INFO)
59+
5660
return cls(
5761
host=os.getenv("AWS_DEX_HOST", "0.0.0.0"), # noqa:S104
5862
port=int(os.getenv("AWS_DEX_PORT", "5000")),
59-
log_level=int(os.getenv("AWS_DEX_LOG_LEVEL", "20")),
63+
log_level=log_level,
6064
lambda_endpoint=os.getenv(
6165
"AWS_DEX_LAMBDA_ENDPOINT", "http://127.0.0.1:3001"
6266
),
@@ -89,10 +93,18 @@ def run(self, args: list[str] | None = None) -> int:
8993
parsed_args = parser.parse_args(args)
9094

9195
# Configure logging based on log level
96+
if hasattr(parsed_args, "log_level") and isinstance(
97+
parsed_args.log_level, str
98+
):
99+
level = logging.getLevelNamesMapping().get(
100+
parsed_args.log_level, logging.INFO
101+
)
102+
else:
103+
# config.log_level is always an integer
104+
level = self.config.log_level
105+
92106
logging.basicConfig(
93-
level=parsed_args.log_level
94-
if hasattr(parsed_args, "log_level")
95-
else self.config.log_level,
107+
level=level,
96108
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
97109
)
98110

@@ -151,9 +163,10 @@ def _create_start_server_parser(self, subparsers) -> None:
151163
)
152164
start_server_parser.add_argument(
153165
"--log-level",
154-
type=int,
155-
default=self.config.log_level,
156-
help=f"Logging level as integer (default: {self.config.log_level}, env: AWS_DEX_LOG_LEVEL)",
166+
type=str,
167+
choices=list(logging.getLevelNamesMapping().keys()),
168+
default=logging.getLevelName(self.config.log_level),
169+
help=f"Logging level (default: {logging.getLevelName(self.config.log_level)}, env: AWS_DEX_LOG_LEVEL)",
157170
)
158171
start_server_parser.add_argument(
159172
"--lambda-endpoint",

tests/cli_test.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import argparse
66
import json
7+
import logging
78
import os
89
import sys
910
from io import StringIO
@@ -24,7 +25,7 @@ def test_cli_config_has_correct_default_values() -> None:
2425

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

3940
assert config.host == "0.0.0.0" # noqa: S104
4041
assert config.port == 5000
41-
assert config.log_level == 20
42+
assert config.log_level == logging.INFO
4243
assert config.lambda_endpoint == "http://127.0.0.1:3001"
4344
assert config.local_runner_endpoint == "http://0.0.0.0:5000"
4445
assert config.local_runner_region == "us-west-2"
@@ -50,7 +51,7 @@ def test_cli_config_from_environment_uses_all_env_vars_when_set() -> None:
5051
env_vars = {
5152
"AWS_DEX_HOST": "127.0.0.1",
5253
"AWS_DEX_PORT": "8080",
53-
"AWS_DEX_LOG_LEVEL": "10",
54+
"AWS_DEX_LOG_LEVEL": "DEBUG",
5455
"AWS_DEX_LAMBDA_ENDPOINT": "http://localhost:4000",
5556
"AWS_DEX_LOCAL_RUNNER_ENDPOINT": "http://localhost:8080",
5657
"AWS_DEX_LOCAL_RUNNER_REGION": "us-east-1",
@@ -62,7 +63,7 @@ def test_cli_config_from_environment_uses_all_env_vars_when_set() -> None:
6263

6364
assert config.host == "127.0.0.1"
6465
assert config.port == 8080
65-
assert config.log_level == 10
66+
assert config.log_level == logging.DEBUG
6667
assert config.lambda_endpoint == "http://localhost:4000"
6768
assert config.local_runner_endpoint == "http://localhost:8080"
6869
assert config.local_runner_region == "us-east-1"
@@ -82,7 +83,7 @@ def test_cli_config_from_environment_uses_partial_env_vars_with_defaults() -> No
8283
assert config.host == "192.168.1.1"
8384
assert config.port == 9000
8485
# Other values should be defaults
85-
assert config.log_level == 20
86+
assert config.log_level == logging.INFO
8687
assert config.lambda_endpoint == "http://127.0.0.1:3001"
8788

8889

@@ -173,7 +174,7 @@ def test_start_server_command_parses_arguments_correctly() -> None:
173174
"--port",
174175
"8080",
175176
"--log-level",
176-
"10",
177+
"DEBUG",
177178
"--lambda-endpoint",
178179
"http://localhost:4000",
179180
"--local-runner-endpoint",
@@ -303,7 +304,7 @@ def test_logging_configuration_uses_specified_log_level() -> None:
303304
with patch("logging.basicConfig") as mock_basic_config:
304305
with patch("sys.stdout", new_callable=StringIO):
305306
with patch.object(app, "start_server_command", return_value=0):
306-
app.run(["start-server", "--log-level", "10"])
307+
app.run(["start-server", "--log-level", "DEBUG"])
307308

308309
mock_basic_config.assert_called_once()
309310
call_args = mock_basic_config.call_args
@@ -348,7 +349,7 @@ def test_start_server_command_works_with_mocked_dependencies() -> None:
348349
"--port",
349350
"8080",
350351
"--log-level",
351-
"10",
352+
"DEBUG",
352353
]
353354
)
354355

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

364365

365366
def test_start_server_command_handles_server_startup_errors() -> None:
@@ -398,7 +399,7 @@ def test_start_server_command_creates_correct_web_runner_config() -> None:
398399
"--port",
399400
"9000",
400401
"--log-level",
401-
"30",
402+
"WARNING",
402403
"--lambda-endpoint",
403404
"http://custom-lambda:4000",
404405
"--local-runner-endpoint",
@@ -419,7 +420,7 @@ def test_start_server_command_creates_correct_web_runner_config() -> None:
419420
# Verify web service configuration
420421
assert config.web_service.host == "192.168.1.100"
421422
assert config.web_service.port == 9000
422-
assert config.web_service.log_level == 30
423+
assert config.web_service.log_level == "WARNING"
423424

424425
# Verify Lambda service configuration
425426
assert config.lambda_endpoint == "http://custom-lambda:4000"

tests/runner_web_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ def test_should_pass_correct_configuration_to_web_server():
705705
"""Test that WebServer receives correct configuration from WebRunnerConfig."""
706706
# Arrange
707707
web_config = WebServiceConfig(
708-
host="custom-host", port=9999, log_level=30, max_request_size=2048
708+
host="custom-host", port=9999, log_level="WARNING", max_request_size=2048
709709
)
710710
runner_config = WebRunnerConfig(web_service=web_config)
711711
runner = WebRunner(runner_config)
@@ -734,7 +734,7 @@ def test_should_pass_correct_configuration_to_web_server():
734734
assert passed_config == web_config
735735
assert passed_config.host == "custom-host"
736736
assert passed_config.port == 9999
737-
assert passed_config.log_level == 30
737+
assert passed_config.log_level == "WARNING"
738738
assert passed_config.max_request_size == 2048
739739

740740
# Cleanup
@@ -1284,7 +1284,7 @@ def test_should_integrate_with_cli_start_server_command():
12841284
"--port",
12851285
"7777",
12861286
"--log-level",
1287-
"30",
1287+
"WARNING",
12881288
"--lambda-endpoint",
12891289
"http://integration-lambda:4000",
12901290
"--local-runner-endpoint",
@@ -1306,7 +1306,7 @@ def test_should_integrate_with_cli_start_server_command():
13061306
# Verify web service configuration
13071307
assert config.web_service.host == "integration-host"
13081308
assert config.web_service.port == 7777
1309-
assert config.web_service.log_level == 30
1309+
assert config.web_service.log_level == "WARNING"
13101310

13111311
# Verify Lambda service configuration
13121312
assert config.lambda_endpoint == "http://integration-lambda:4000"
@@ -1430,7 +1430,7 @@ def test_should_preserve_cli_configuration_through_web_runner():
14301430
"--port",
14311431
"9999",
14321432
"--log-level",
1433-
"40", # ERROR level
1433+
"ERROR", # ERROR level
14341434
"--lambda-endpoint",
14351435
"http://config-lambda:5000",
14361436
"--local-runner-endpoint",
@@ -1452,7 +1452,7 @@ def test_should_preserve_cli_configuration_through_web_runner():
14521452
# Verify web service configuration
14531453
assert config.web_service.host == "config-test-host"
14541454
assert config.web_service.port == 9999
1455-
assert config.web_service.log_level == 40
1455+
assert config.web_service.log_level == "ERROR"
14561456

14571457
# Verify Lambda service configuration
14581458
assert config.lambda_endpoint == "http://config-lambda:5000"
@@ -1472,7 +1472,7 @@ def test_should_handle_environment_variable_integration():
14721472
env_vars = {
14731473
"AWS_DEX_HOST": "env-host",
14741474
"AWS_DEX_PORT": "8888",
1475-
"AWS_DEX_LOG_LEVEL": "50", # CRITICAL level
1475+
"AWS_DEX_LOG_LEVEL": "CRITICAL", # CRITICAL level
14761476
"AWS_DEX_LAMBDA_ENDPOINT": "http://env-lambda:6000",
14771477
"AWS_DEX_LOCAL_RUNNER_ENDPOINT": "http://env-runner:7000",
14781478
"AWS_DEX_LOCAL_RUNNER_REGION": "sa-east-1",
@@ -1505,7 +1505,7 @@ def test_should_handle_environment_variable_integration():
15051505
# Verify environment variables were used
15061506
assert config.web_service.host == "env-host"
15071507
assert config.web_service.port == 8888
1508-
assert config.web_service.log_level == 50
1508+
assert config.web_service.log_level == "CRITICAL"
15091509
assert config.lambda_endpoint == "http://env-lambda:6000"
15101510
assert config.local_runner_endpoint == "http://env-runner:7000"
15111511
assert config.local_runner_region == "sa-east-1"

0 commit comments

Comments
 (0)