Skip to content

Commit dbd02e4

Browse files
committed
chore: update CLI to accept log level as string
1 parent 85bae2a commit dbd02e4

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

src/aws_durable_execution_sdk_python_testing/cli.py

Lines changed: 22 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,16 @@ 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(
59+
log_level_str.upper(), logging.INFO
60+
)
61+
5662
return cls(
5763
host=os.getenv("AWS_DEX_HOST", "0.0.0.0"), # noqa:S104
5864
port=int(os.getenv("AWS_DEX_PORT", "5000")),
59-
log_level=int(os.getenv("AWS_DEX_LOG_LEVEL", "20")),
65+
log_level=log_level,
6066
lambda_endpoint=os.getenv(
6167
"AWS_DEX_LAMBDA_ENDPOINT", "http://127.0.0.1:3001"
6268
),
@@ -89,10 +95,17 @@ def run(self, args: list[str] | None = None) -> int:
8995
parsed_args = parser.parse_args(args)
9096

9197
# Configure logging based on log level
98+
if hasattr(parsed_args, "log_level") and isinstance(
99+
parsed_args.log_level, str
100+
):
101+
level_name = parsed_args.log_level.upper()
102+
level = logging.getLevelNamesMapping().get(level_name, logging.INFO)
103+
else:
104+
# config.log_level is always an integer
105+
level = self.config.log_level
106+
92107
logging.basicConfig(
93-
level=parsed_args.log_level
94-
if hasattr(parsed_args, "log_level")
95-
else self.config.log_level,
108+
level=level,
96109
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
97110
)
98111

@@ -151,9 +164,10 @@ def _create_start_server_parser(self, subparsers) -> None:
151164
)
152165
start_server_parser.add_argument(
153166
"--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)",
167+
type=str,
168+
choices=[level.lower() for level in logging.getLevelNamesMapping()],
169+
default=logging.getLevelName(self.config.log_level).lower(),
170+
help=f"Logging level (default: {logging.getLevelName(self.config.log_level).lower()}, env: AWS_DEX_LOG_LEVEL)",
157171
)
158172
start_server_parser.add_argument(
159173
"--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)