Skip to content

Commit

Permalink
fix: handle exceptions in AbstractTestBase
Browse files Browse the repository at this point in the history
The CFN handler framework automatically handles certain exceptions and
returns a failed response. Changing test_handleRequest_base to do the
same.
  • Loading branch information
zrfr committed Nov 19, 2024
1 parent ef0c3d0 commit 2cfe91a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import software.amazon.awssdk.awscore.AwsResponse;
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.cloudformation.exceptions.BaseHandlerException;
import software.amazon.cloudformation.proxy.HandlerErrorCode;
import software.amazon.cloudformation.proxy.OperationStatus;
import software.amazon.cloudformation.proxy.ProgressEvent;
Expand Down Expand Up @@ -109,7 +110,12 @@ protected ProgressEvent<ModelT, ContextT> test_handleRequest_base(
builder.clientRequestToken(newClientRequestToken());
builder.stackId(newStackId());

final ProgressEvent<ModelT, ContextT> response = invokeHandleRequest(builder.build(), context);
ProgressEvent<ModelT, ContextT> response;
try {
response = invokeHandleRequest(builder.build(), context);
} catch (final BaseHandlerException e) {
response = ProgressEvent.defaultFailureHandler(e, e.getErrorCode());
}
expect.accept(response);

return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,16 +720,14 @@ public void handleRequest_CreateDBCluster_HandleException(
public void handleRequest_CreateDBCluster_DBClusterInTerminalState() {
final CallbackContext context = new CallbackContext();

Assertions.assertThatThrownBy(() -> {
test_handleRequest_base(
context,
() -> DBCLUSTER_ACTIVE.toBuilder()
.status(DBClusterStatus.InaccessibleEncryptionCredentials.toString())
.build(),
() -> RESOURCE_MODEL,
expectFailed(HandlerErrorCode.NotStabilized)
);
}).isInstanceOf(CfnNotStabilizedException.class);
test_handleRequest_base(
context,
() -> DBCLUSTER_ACTIVE.toBuilder()
.status(DBClusterStatus.InaccessibleEncryptionCredentials.toString())
.build(),
() -> RESOURCE_MODEL,
expectFailed(HandlerErrorCode.NotStabilized)
);

verify(rdsProxy.client(), times(1)).createDBCluster(any(CreateDbClusterRequest.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,18 +1344,16 @@ public void handleRequest_CreateDBInstance_OptionGroupInTerminalState() {
final CallbackContext context = new CallbackContext();
context.setCreated(false);

Assertions.assertThatThrownBy(() -> {
test_handleRequest_base(
context,
() -> DB_INSTANCE_ACTIVE.toBuilder()
.optionGroupMemberships(OptionGroupMembership.builder()
.status(OptionGroupStatus.Failed.toString())
.optionGroupName(OPTION_GROUP_NAME_MYSQL_DEFAULT)
.build()).build(),
() -> RESOURCE_MODEL_BLDR().build(),
expectFailed(HandlerErrorCode.NotStabilized)
);
}).isInstanceOf(CfnNotStabilizedException.class);
test_handleRequest_base(
context,
() -> DB_INSTANCE_ACTIVE.toBuilder()
.optionGroupMemberships(OptionGroupMembership.builder()
.status(OptionGroupStatus.Failed.toString())
.optionGroupName(OPTION_GROUP_NAME_MYSQL_DEFAULT)
.build()).build(),
() -> RESOURCE_MODEL_BLDR().build(),
expectFailed(HandlerErrorCode.NotStabilized)
);

verify(rdsProxy.client(), times(1)).createDBInstance(any(CreateDbInstanceRequest.class));
}
Expand All @@ -1365,18 +1363,16 @@ public void handleRequest_CreateDBInstance_DomainMembershipInTerminalState() {
final CallbackContext context = new CallbackContext();
context.setCreated(false);

Assertions.assertThatThrownBy(() -> {
test_handleRequest_base(
context,
() -> DB_INSTANCE_ACTIVE.toBuilder()
.domainMemberships(DomainMembership.builder()
.status(DomainMembershipStatus.Failed.toString())
.domain("domain")
.build()).build(),
() -> RESOURCE_MODEL_BLDR().build(),
expectFailed(HandlerErrorCode.NotStabilized)
);
}).isInstanceOf(CfnNotStabilizedException.class);
test_handleRequest_base(
context,
() -> DB_INSTANCE_ACTIVE.toBuilder()
.domainMemberships(DomainMembership.builder()
.status(DomainMembershipStatus.Failed.toString())
.domain("domain")
.build()).build(),
() -> RESOURCE_MODEL_BLDR().build(),
expectFailed(HandlerErrorCode.NotStabilized)
);

verify(rdsProxy.client(), times(1)).createDBInstance(any(CreateDbInstanceRequest.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,17 @@ public void handleRequest_CreateIntegration_withTerminalFailureState_returnFailu
// Integration goes from CREATING -> ACTIVE, when everything is normal
Queue<Integration> transitions = new ConcurrentLinkedQueue<>();
transitions.add(INTEGRATION_CREATING);
Assertions.assertThatThrownBy(() ->
test_handleRequest_base(
new CallbackContext(),
() -> {
if (transitions.size() > 0) {
return transitions.remove();
}
return INTEGRATION_FAILED;
},
() -> INTEGRATION_ACTIVE_MODEL, // unused
expectFailed(HandlerErrorCode.NotStabilized) // unused
)
).isInstanceOf(CfnNotStabilizedException.class);
test_handleRequest_base(
new CallbackContext(),
() -> {
if (transitions.size() > 0) {
return transitions.remove();
}
return INTEGRATION_FAILED;
},
() -> INTEGRATION_ACTIVE_MODEL, // unused
expectFailed(HandlerErrorCode.NotStabilized)
);

verify(rdsProxy.client(), times(1)).createIntegration(
ArgumentMatchers. <CreateIntegrationRequest>argThat(req -> Objects.equals(req.integrationName(), INTEGRATION_NAME) &&
Expand Down

0 comments on commit 2cfe91a

Please sign in to comment.