Skip to content

Commit 6954160

Browse files
Alex Wangwangyb-A
authored andcommitted
feat: Implement get_dex in cli module
- Add error handle for boto client request - Update unit tests
1 parent f90b3d4 commit 6954160

File tree

2 files changed

+143
-22
lines changed

2 files changed

+143
-22
lines changed

src/aws_durable_execution_sdk_python_testing/cli.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import aws_durable_execution_sdk_python
2323
import boto3 # type: ignore
2424
import requests
25+
from botocore.exceptions import ConnectionError # type: ignore
2526

2627
from aws_durable_execution_sdk_python_testing.exceptions import (
2728
DurableFunctionsLocalRunnerError,
@@ -384,8 +385,6 @@ def invoke_command(self, args: argparse.Namespace) -> int:
384385
def get_durable_execution_command(self, args: argparse.Namespace) -> int:
385386
"""Execute the get-durable-execution command.
386387
387-
TODO: implement - this is incomplete
388-
389388
Args:
390389
args: Parsed command line arguments
391390
@@ -405,8 +404,25 @@ def get_durable_execution_command(self, args: argparse.Namespace) -> int:
405404
print(json.dumps(response, indent=2, default=str)) # noqa: T201
406405
return 0 # noqa: TRY300
407406

407+
except client.exceptions.InvalidParameterValueException as e:
408+
print(f"Error: Invalid parameter - {e}", file=sys.stderr) # noqa: T201
409+
return 1
410+
except client.exceptions.ResourceNotFoundException as e:
411+
print(f"Error: Execution not found - {e}", file=sys.stderr) # noqa: T201
412+
return 1
413+
except client.exceptions.TooManyRequestsException as e:
414+
print(f"Error: Too many requests - {e}", file=sys.stderr) # noqa: T201
415+
return 1
416+
except client.exceptions.ServiceException as e:
417+
print(f"Error: Service error - {e}", file=sys.stderr) # noqa: T201
418+
return 1
419+
except ConnectionError:
420+
logger.exception(
421+
"Error: Could not connect to the local runner server. Is it running?"
422+
)
423+
return 1
408424
except Exception:
409-
logger.exception("General error")
425+
logger.exception("Unexpected error in get-durable-execution command")
410426
return 1
411427

412428
def get_durable_execution_history_command(self, args: argparse.Namespace) -> int:

tests/cli_test.py

Lines changed: 124 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212

1313
import pytest
1414
import requests
15+
from botocore.exceptions import ConnectionError # type: ignore
1516

1617
from aws_durable_execution_sdk_python_testing.cli import CliApp, CliConfig, main
1718
from aws_durable_execution_sdk_python_testing.exceptions import (
1819
DurableFunctionsLocalRunnerError,
20+
InvalidParameterValueException,
21+
ResourceNotFoundException,
22+
ServiceException,
23+
TooManyRequestsException,
1924
)
2025

2126

@@ -777,15 +782,25 @@ def test_get_durable_execution_command_handles_resource_not_found() -> None:
777782

778783
with patch.object(app, "_create_boto3_client") as mock_create_client:
779784
mock_client = mock_create_client.return_value
780-
mock_client.get_durable_execution.side_effect = Exception(
781-
"ResourceNotFoundException: Execution not found"
785+
786+
mock_client.exceptions.ResourceNotFoundException = ResourceNotFoundException
787+
mock_client.exceptions.InvalidParameterValueException = (
788+
InvalidParameterValueException
782789
)
790+
mock_client.exceptions.TooManyRequestsException = TooManyRequestsException
791+
mock_client.exceptions.ServiceException = ServiceException
783792

784-
exit_code = app.get_durable_execution_command(
785-
argparse.Namespace(durable_execution_arn="nonexistent-arn")
793+
mock_client.get_durable_execution.side_effect = ResourceNotFoundException(
794+
"Resource not found"
786795
)
787796

788-
assert exit_code == 1
797+
with patch("sys.stderr", new_callable=StringIO) as mock_stderr:
798+
exit_code = app.get_durable_execution_command(
799+
argparse.Namespace(durable_execution_arn="nonexistent-arn")
800+
)
801+
802+
assert exit_code == 1
803+
assert "Error: Execution not found" in mock_stderr.getvalue()
789804

790805

791806
def test_get_durable_execution_command_handles_invalid_parameter() -> None:
@@ -794,15 +809,79 @@ def test_get_durable_execution_command_handles_invalid_parameter() -> None:
794809

795810
with patch.object(app, "_create_boto3_client") as mock_create_client:
796811
mock_client = mock_create_client.return_value
797-
mock_client.get_durable_execution.side_effect = Exception(
798-
"InvalidParameterValueException: Invalid ARN format"
812+
813+
mock_client.exceptions.ResourceNotFoundException = ResourceNotFoundException
814+
mock_client.exceptions.InvalidParameterValueException = (
815+
InvalidParameterValueException
799816
)
817+
mock_client.exceptions.TooManyRequestsException = TooManyRequestsException
818+
mock_client.exceptions.ServiceException = ServiceException
800819

801-
exit_code = app.get_durable_execution_command(
802-
argparse.Namespace(durable_execution_arn="invalid-arn")
820+
mock_client.get_durable_execution.side_effect = InvalidParameterValueException(
821+
"Invalid parameters"
803822
)
804823

805-
assert exit_code == 1
824+
with patch("sys.stderr", new_callable=StringIO) as mock_stderr:
825+
exit_code = app.get_durable_execution_command(
826+
argparse.Namespace(durable_execution_arn="invalid-arn")
827+
)
828+
829+
assert exit_code == 1
830+
assert "Error: Invalid parameter" in mock_stderr.getvalue()
831+
832+
833+
def test_get_durable_execution_command_handles_too_many_requests() -> None:
834+
"""Test that get-durable-execution command handles InvalidParameterValueException."""
835+
app = CliApp()
836+
837+
with patch.object(app, "_create_boto3_client") as mock_create_client:
838+
mock_client = mock_create_client.return_value
839+
840+
mock_client.exceptions.ResourceNotFoundException = ResourceNotFoundException
841+
mock_client.exceptions.InvalidParameterValueException = (
842+
InvalidParameterValueException
843+
)
844+
mock_client.exceptions.TooManyRequestsException = TooManyRequestsException
845+
mock_client.exceptions.ServiceException = ServiceException
846+
847+
mock_client.get_durable_execution.side_effect = TooManyRequestsException(
848+
"Too many requests"
849+
)
850+
851+
with patch("sys.stderr", new_callable=StringIO) as mock_stderr:
852+
exit_code = app.get_durable_execution_command(
853+
argparse.Namespace(durable_execution_arn="my-arn")
854+
)
855+
856+
assert exit_code == 1
857+
assert "Error: Too many requests" in mock_stderr.getvalue()
858+
859+
860+
def test_get_durable_execution_command_handles_service_exception() -> None:
861+
"""Test that get-durable-execution command handles InvalidParameterValueException."""
862+
app = CliApp()
863+
864+
with patch.object(app, "_create_boto3_client") as mock_create_client:
865+
mock_client = mock_create_client.return_value
866+
867+
mock_client.exceptions.ResourceNotFoundException = ResourceNotFoundException
868+
mock_client.exceptions.InvalidParameterValueException = (
869+
InvalidParameterValueException
870+
)
871+
mock_client.exceptions.TooManyRequestsException = TooManyRequestsException
872+
mock_client.exceptions.ServiceException = ServiceException
873+
874+
mock_client.get_durable_execution.side_effect = ServiceException(
875+
"Service exception"
876+
)
877+
878+
with patch("sys.stderr", new_callable=StringIO) as mock_stderr:
879+
exit_code = app.get_durable_execution_command(
880+
argparse.Namespace(durable_execution_arn="my-arn")
881+
)
882+
883+
assert exit_code == 1
884+
assert "Error: Service error" in mock_stderr.getvalue()
806885

807886

808887
def test_get_durable_execution_command_handles_connection_error() -> None:
@@ -811,15 +890,29 @@ def test_get_durable_execution_command_handles_connection_error() -> None:
811890

812891
with patch.object(app, "_create_boto3_client") as mock_create_client:
813892
mock_client = mock_create_client.return_value
814-
mock_client.get_durable_execution.side_effect = Exception(
815-
"Could not connect to endpoint"
893+
894+
mock_client.exceptions.ResourceNotFoundException = ResourceNotFoundException
895+
mock_client.exceptions.InvalidParameterValueException = (
896+
InvalidParameterValueException
816897
)
898+
mock_client.exceptions.TooManyRequestsException = TooManyRequestsException
899+
mock_client.exceptions.ServiceException = ServiceException
817900

818-
exit_code = app.get_durable_execution_command(
819-
argparse.Namespace(durable_execution_arn="test-arn")
901+
mock_client.get_durable_execution.side_effect = ConnectionError(
902+
error="Mocked connection error"
820903
)
821904

822-
assert exit_code == 1
905+
with patch(
906+
"aws_durable_execution_sdk_python_testing.cli.logger"
907+
) as mock_logger:
908+
exit_code = app.get_durable_execution_command(
909+
argparse.Namespace(durable_execution_arn="my-arn")
910+
)
911+
912+
assert exit_code == 1
913+
mock_logger.exception.assert_called_once_with(
914+
"Error: Could not connect to the local runner server. Is it running?"
915+
)
823916

824917

825918
def test_get_durable_execution_history_command_uses_boto3_client() -> None:
@@ -987,15 +1080,27 @@ def test_get_durable_execution_command_handles_general_exception() -> None:
9871080

9881081
with patch.object(app, "_create_boto3_client") as mock_create_client:
9891082
mock_client = mock_create_client.return_value
1083+
mock_client.exceptions.ResourceNotFoundException = ResourceNotFoundException
1084+
mock_client.exceptions.InvalidParameterValueException = (
1085+
InvalidParameterValueException
1086+
)
1087+
mock_client.exceptions.TooManyRequestsException = TooManyRequestsException
1088+
mock_client.exceptions.ServiceException = ServiceException
9901089
mock_client.get_durable_execution.side_effect = ValueError(
9911090
"Some unexpected error"
9921091
)
9931092

994-
exit_code = app.get_durable_execution_command(
995-
argparse.Namespace(durable_execution_arn="test-arn")
996-
)
1093+
with patch(
1094+
"aws_durable_execution_sdk_python_testing.cli.logger"
1095+
) as mock_logger:
1096+
exit_code = app.get_durable_execution_command(
1097+
argparse.Namespace(durable_execution_arn="my-arn")
1098+
)
9971099

998-
assert exit_code == 1
1100+
assert exit_code == 1
1101+
mock_logger.exception.assert_called_once_with(
1102+
"Unexpected error in get-durable-execution command"
1103+
)
9991104

10001105

10011106
def test_get_durable_execution_history_command_handles_general_exception() -> None:

0 commit comments

Comments
 (0)