Skip to content

Commit 53b624f

Browse files
committed
set up contract tests for sfn describe state machine
1 parent a7dcb45 commit 53b624f

File tree

5 files changed

+176
-170
lines changed

5 files changed

+176
-170
lines changed

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/awssdk/base/AwsSdkBaseTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,8 +2311,8 @@ protected void doTestSecretsManagerCreateSecret() throws Exception {
23112311
0.0);
23122312
}
23132313

2314-
protected void doTestStepFunctionsCreateStateMachine() throws Exception {
2315-
appClient.get("/sfn/createstatemachine/test-state-machine").aggregate().join();
2314+
protected void doTestStepFunctionsDescribeStateMachine() throws Exception {
2315+
appClient.get("/sfn/describestatemachine/test-state-machine").aggregate().join();
23162316
var traces = mockCollectorClient.getTraces();
23172317
System.out.println("TRACES: " + traces);
23182318
var metrics =
@@ -2323,20 +2323,20 @@ protected void doTestStepFunctionsCreateStateMachine() throws Exception {
23232323
AppSignalsConstants.LATENCY_METRIC));
23242324

23252325
var localService = getApplicationOtelServiceName();
2326-
var localOperation = "GET /sfn/createstatemachine/:name";
2326+
var localOperation = "GET /sfn/describestatemachine/:name";
23272327
var type = "AWS::StepFunctions::StateMachine";
23282328
var identifier = "test-state-machine";
23292329
var cloudformationIdentifier =
23302330
"arn:aws:states:us-west-2:000000000000:stateMachine:test-state-machine";
23312331

23322332
assertSpanClientAttributes(
23332333
traces,
2334-
stepFunctionsSpanName("CreateStateMachine"),
2334+
stepFunctionsSpanName("DescribeStateMachine"),
23352335
getStepFunctionsRpcServiceName(),
23362336
localService,
23372337
localOperation,
23382338
getStepFunctionsServiceName(),
2339-
"CreateStateMachine",
2339+
"DescribeStateMachine",
23402340
type,
23412341
identifier,
23422342
cloudformationIdentifier,

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/awssdk/v1/AwsSdkV1Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ void testSecretsManagerCreateSecret() throws Exception {
262262
}
263263

264264
@Test
265-
void testStepFunctionsCreateStateMachine() throws Exception {
266-
doTestStepFunctionsCreateStateMachine();
265+
void testStepFunctionsDescribeStateMachine() throws Exception {
266+
doTestStepFunctionsDescribeStateMachine();
267267
}
268268
}

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/awssdk/v2/AwsSdkV2Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void testSecretsManagerCreateSecret() throws Exception {
267267
}
268268

269269
@Test
270-
void testStepFunctionsCreateStateMachine() throws Exception {
271-
doTestStepFunctionsCreateStateMachine();
270+
void testStepFunctionsDescribeStateMachine() throws Exception {
271+
doTestStepFunctionsDescribeStateMachine();
272272
}
273273
}

appsignals-tests/images/aws-sdk/aws-sdk-v1/src/main/java/com/amazon/sampleapp/App.java

Lines changed: 82 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
6363
import com.amazonaws.services.sqs.model.SendMessageRequest;
6464
import com.amazonaws.services.stepfunctions.AWSStepFunctionsClient;
65-
import com.amazonaws.services.stepfunctions.model.CreateStateMachineRequest;
66-
import com.amazonaws.services.stepfunctions.model.StateMachineType;
65+
import com.amazonaws.services.stepfunctions.model.*;
66+
6767
import java.io.File;
6868
import java.io.IOException;
6969
import java.net.http.HttpClient;
@@ -671,94 +671,89 @@ private static void setupStepFunctions() {
671671
.withEndpointConfiguration(endpointConfiguration)
672672
.build();
673673

674+
var sfnName = "test-state-machine";
675+
String existingStateMachineArn = null;
676+
var listRequest = new ListStateMachinesRequest();
677+
var listResponse = stepFunctionsClient.listStateMachines(listRequest);
678+
existingStateMachineArn = listResponse.getStateMachines().stream()
679+
.filter(machine -> machine.getName().equals(sfnName))
680+
.findFirst()
681+
.map(StateMachineListItem::getStateMachineArn)
682+
.orElse(null);
683+
684+
if (existingStateMachineArn != null) {
685+
logger.info("State machine already exists, skipping creation");
686+
} else {
687+
logger.info("State machine not found, creating a new one");
688+
String trustPolicy =
689+
"{"
690+
+ "\"Version\": \"2012-10-17\","
691+
+ "\"Statement\": ["
692+
+ " {"
693+
+ " \"Effect\": \"Allow\","
694+
+ " \"Principal\": {"
695+
+ " \"Service\": \"states.amazonaws.com\""
696+
+ " },"
697+
+ " \"Action\": \"sts:AssumeRole\""
698+
+ " }"
699+
+ "]}";
700+
var roleRequest =
701+
new CreateRoleRequest()
702+
.withRoleName(sfnName + "-role")
703+
.withAssumeRolePolicyDocument(trustPolicy);
704+
var roleArn = iamClient.createRole(roleRequest).getRole().getArn();
705+
String policyDocument =
706+
"{"
707+
+ "\"Version\": \"2012-10-17\","
708+
+ "\"Statement\": ["
709+
+ " {"
710+
+ " \"Effect\": \"Allow\","
711+
+ " \"Action\": ["
712+
+ " \"lambda:InvokeFunction\""
713+
+ " ],"
714+
+ " \"Resource\": ["
715+
+ " \"*\""
716+
+ " ]"
717+
+ " }"
718+
+ "]}";
719+
var policyRequest =
720+
new PutRolePolicyRequest()
721+
.withRoleName(sfnName + "-role")
722+
.withPolicyName(sfnName + "-policy")
723+
.withPolicyDocument(policyDocument);
724+
iamClient.putRolePolicy(policyRequest);
725+
// Simple state machine definition - a basic Hello World
726+
String stateMachineDefinition =
727+
"{"
728+
+ " \"Comment\": \"A Hello World example of the Amazon States Language using a Pass state\","
729+
+ " \"StartAt\": \"HelloWorld\","
730+
+ " \"States\": {"
731+
+ " \"HelloWorld\": {"
732+
+ " \"Type\": \"Pass\","
733+
+ " \"Result\": \"Hello World!\","
734+
+ " \"End\": true"
735+
+ " }"
736+
+ " }"
737+
+ "}";
738+
// Create state machine using the role
739+
var sfnRequest =
740+
new CreateStateMachineRequest()
741+
.withName(sfnName)
742+
.withRoleArn(roleArn)
743+
.withDefinition(stateMachineDefinition)
744+
.withType(StateMachineType.STANDARD);
745+
var createResponse = stepFunctionsClient.createStateMachine(sfnRequest);
746+
existingStateMachineArn = createResponse.getStateMachineArn();
747+
}
748+
749+
String finalExistingStateMachineArn = existingStateMachineArn;
674750
get(
675-
"/sfn/createstatemachine/:name",
751+
"/sfn/describestatemachine/:name",
676752
(req, res) -> {
677753
var name = req.params(":name");
678-
String trustPolicy =
679-
"{"
680-
+ "\"Version\": \"2012-10-17\","
681-
+ "\"Statement\": ["
682-
+ " {"
683-
+ " \"Effect\": \"Allow\","
684-
+ " \"Principal\": {"
685-
+ " \"Service\": \"states.amazonaws.com\""
686-
+ " },"
687-
+ " \"Action\": \"sts:AssumeRole\""
688-
+ " }"
689-
+ "]}";
690-
var roleRequest =
691-
new CreateRoleRequest()
692-
.withRoleName(name + "-role")
693-
.withAssumeRolePolicyDocument(trustPolicy);
694-
var roleArn = iamClient.createRole(roleRequest).getRole().getArn();
695-
String policyDocument =
696-
"{"
697-
+ "\"Version\": \"2012-10-17\","
698-
+ "\"Statement\": ["
699-
+ " {"
700-
+ " \"Effect\": \"Allow\","
701-
+ " \"Action\": ["
702-
+ " \"lambda:InvokeFunction\""
703-
+ " ],"
704-
+ " \"Resource\": ["
705-
+ " \"*\""
706-
+ " ]"
707-
+ " }"
708-
+ "]}";
709-
var policyRequest =
710-
new PutRolePolicyRequest()
711-
.withRoleName(name + "-role")
712-
.withPolicyName(name + "-policy")
713-
.withPolicyDocument(policyDocument);
714-
iamClient.putRolePolicy(policyRequest);
715-
// Simple state machine definition - a basic Hello World
716-
String stateMachineDefinition =
717-
"{"
718-
+ " \"Comment\": \"A Hello World example of the Amazon States Language using a Pass state\","
719-
+ " \"StartAt\": \"HelloWorld\","
720-
+ " \"States\": {"
721-
+ " \"HelloWorld\": {"
722-
+ " \"Type\": \"Pass\","
723-
+ " \"Result\": \"Hello World!\","
724-
+ " \"End\": true"
725-
+ " }"
726-
+ " }"
727-
+ "}";
728-
// Create state machine using the role
729-
var sfnRequest =
730-
new CreateStateMachineRequest()
731-
.withName(name)
732-
.withRoleArn(roleArn)
733-
.withDefinition(stateMachineDefinition)
734-
.withType(StateMachineType.STANDARD);
735-
stepFunctionsClient.createStateMachine(sfnRequest);
736-
return "";
737-
});
738-
739-
get(
740-
"/sfn/error",
741-
(req, res) -> {
742-
setMainStatus(400);
743-
var errorClient =
744-
AWSStepFunctionsClient.builder()
745-
.withCredentials(CREDENTIALS_PROVIDER)
746-
.withEndpointConfiguration(
747-
new EndpointConfiguration(
748-
"http://error.test:8080", Regions.US_WEST_2.getName()))
749-
.build();
750-
751-
try {
752-
String invalidDefinition = "{\"Invalid\": \"Definition\"}";
753-
errorClient.createStateMachine(
754-
new CreateStateMachineRequest()
755-
.withName("error-state-machine")
756-
.withDefinition(invalidDefinition)
757-
.withRoleArn("arn:aws:iam::invalid:role/invalid"));
758-
} catch (Exception ex) {
759-
logger.info("Exception Caught in Sample App");
760-
ex.printStackTrace();
761-
}
754+
var describeRequest = new DescribeStateMachineRequest()
755+
.withStateMachineArn(finalExistingStateMachineArn);
756+
stepFunctionsClient.describeStateMachine(describeRequest);
762757
return "";
763758
});
764759
}

0 commit comments

Comments
 (0)