From dd9539940eac93b8f2070fc1c5cb40928a32e48f Mon Sep 17 00:00:00 2001 From: AksharaAK Date: Wed, 26 Jan 2022 17:47:28 +0000 Subject: [PATCH 1/3] added step function helper, merging latest main --- .../index.ts | 15 ++++------- src/helpers/stepFunctions.ts | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/assertions/toHaveCompletedExecutionWithStatus/index.ts b/src/assertions/toHaveCompletedExecutionWithStatus/index.ts index a9c5075..4b683b7 100644 --- a/src/assertions/toHaveCompletedExecutionWithStatus/index.ts +++ b/src/assertions/toHaveCompletedExecutionWithStatus/index.ts @@ -1,4 +1,5 @@ import { testResult, TestResultOutput } from "../../utils/testResult"; +import StepFunctions from "../../helpers/stepFunctions"; import * as AWS from "aws-sdk"; @@ -8,16 +9,10 @@ export default { expectedStatus: string ): Promise { const stepFunctions = new AWS.StepFunctions(); - const listStateMachineParams = {}; - // Get all state machines - const allStateMachines = await stepFunctions - .listStateMachines(listStateMachineParams) - .promise(); - // Find state machine with specified name and get its arn - const smList = allStateMachines.stateMachines.filter( - (stateMachine: any) => stateMachine.name === stateMachineName - ); - const smArn = smList[0].stateMachineArn; + + const stepFunctionsObject = await StepFunctions.build(); + const smArn = await stepFunctionsObject.obtainStateMachineArn(stateMachineName); + const listExecutionsParams = { stateMachineArn: smArn }; // Get all executions of specified state machine const smExecutions = await stepFunctions diff --git a/src/helpers/stepFunctions.ts b/src/helpers/stepFunctions.ts index 5e31d35..548bd28 100644 --- a/src/helpers/stepFunctions.ts +++ b/src/helpers/stepFunctions.ts @@ -1,4 +1,5 @@ import { StepFunctions as AWSStepFunctions } from "aws-sdk"; +import { String } from "aws-sdk/clients/apigateway"; export default class StepFunctions { stepFunctions: AWSStepFunctions | undefined; @@ -61,4 +62,28 @@ export default class StepFunctions { .describeExecution({ executionArn: execution.executionArn }) .promise(); } + + async obtainStateMachineArn(stateMachineName: string): Promise { + + const listStateMachineParams = {}; + // Get all state machines + if (this.stepFunctions === undefined) { + throw new Error( + "The Step Functions client is undefined. You might have forgotten to run build()." + ); + } + const allStateMachines = await this.stepFunctions + .listStateMachines(listStateMachineParams) + .promise(); + // Find state machine with specified name and get its arn + // changed filter to find- returns the first object that matches condition + const smList = allStateMachines.stateMachines.find( + (stateMachine: any) => stateMachine.name === stateMachineName + ); + if (smList == null) + throw new Error( + "No matching state machine. " + ); + return smList.stateMachineArn; + } } From 22cf3644c175d6a590fb31bfa05572ad16129fcc Mon Sep 17 00:00:00 2001 From: AksharaAK Date: Thu, 27 Jan 2022 12:07:33 +0000 Subject: [PATCH 2/3] Merging in latest main --- .../toMatchStateMachineOutput/index.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/assertions/toMatchStateMachineOutput/index.ts diff --git a/src/assertions/toMatchStateMachineOutput/index.ts b/src/assertions/toMatchStateMachineOutput/index.ts new file mode 100644 index 0000000..d445040 --- /dev/null +++ b/src/assertions/toMatchStateMachineOutput/index.ts @@ -0,0 +1,51 @@ +import { testResult, TestResultOutput } from "../../utils/testResult"; +import { StepFunctions as AWSStepFunctions } from "aws-sdk"; + +export default { + async toMatchStateMachineOutput( + stateMachineName: string, + expectedOutput: any + ): Promise { + const stepFunctions = new AWSStepFunctions(); + const allStateMachines = await stepFunctions.listStateMachines().promise(); + + const smList = allStateMachines.stateMachines.filter( + (stateMachine: any) => stateMachine.name === stateMachineName + ); + + const stateMachineArn = smList[0].stateMachineArn; + const listExecParams = { stateMachineArn: stateMachineArn }; + const executionList = await stepFunctions + .listExecutions(listExecParams) + .promise(); + + const executionResult = await stepFunctions + .describeExecution({ + executionArn: executionList.executions[0].executionArn, + }) + .promise(); + + if (executionResult.status === "SUCCEEDED") { + if (executionResult.output === expectedOutput) { + return testResult( + `Output is ${JSON.stringify(executionResult.output)} as expected`, + true + ); + } else { + return testResult( + `Expected output was ${JSON.stringify( + expectedOutput + )}, but output received was ${JSON.stringify( + executionResult.output + )}`, + false + ); + } + } + + return testResult( + "Step Function execution failed. Cannot verify output for failed executions.", + false + ); + }, +}; From 319438a4786884521cfd32ebb4132c7635261476 Mon Sep 17 00:00:00 2001 From: AksharaAK Date: Thu, 27 Jan 2022 12:26:57 +0000 Subject: [PATCH 3/3] changed toMatchStateMachineOutput assertion to use obtainStateMachineArn helper --- src/assertions/toMatchStateMachineOutput/index.ts | 12 ++++-------- src/helpers/stepFunctions.ts | 4 +--- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/assertions/toMatchStateMachineOutput/index.ts b/src/assertions/toMatchStateMachineOutput/index.ts index d445040..3b0e20f 100644 --- a/src/assertions/toMatchStateMachineOutput/index.ts +++ b/src/assertions/toMatchStateMachineOutput/index.ts @@ -1,5 +1,6 @@ import { testResult, TestResultOutput } from "../../utils/testResult"; import { StepFunctions as AWSStepFunctions } from "aws-sdk"; +import StepFunctions from "../../helpers/stepFunctions"; export default { async toMatchStateMachineOutput( @@ -7,14 +8,9 @@ export default { expectedOutput: any ): Promise { const stepFunctions = new AWSStepFunctions(); - const allStateMachines = await stepFunctions.listStateMachines().promise(); - - const smList = allStateMachines.stateMachines.filter( - (stateMachine: any) => stateMachine.name === stateMachineName - ); - - const stateMachineArn = smList[0].stateMachineArn; - const listExecParams = { stateMachineArn: stateMachineArn }; + const stepFunctionsObject = await StepFunctions.build(); + const smArn = await stepFunctionsObject.obtainStateMachineArn(stateMachineName); + const listExecParams = { stateMachineArn: smArn }; const executionList = await stepFunctions .listExecutions(listExecParams) .promise(); diff --git a/src/helpers/stepFunctions.ts b/src/helpers/stepFunctions.ts index 548bd28..118bf8d 100644 --- a/src/helpers/stepFunctions.ts +++ b/src/helpers/stepFunctions.ts @@ -1,5 +1,4 @@ import { StepFunctions as AWSStepFunctions } from "aws-sdk"; -import { String } from "aws-sdk/clients/apigateway"; export default class StepFunctions { stepFunctions: AWSStepFunctions | undefined; @@ -63,7 +62,7 @@ export default class StepFunctions { .promise(); } - async obtainStateMachineArn(stateMachineName: string): Promise { + async obtainStateMachineArn(stateMachineName: string): Promise { const listStateMachineParams = {}; // Get all state machines @@ -76,7 +75,6 @@ export default class StepFunctions { .listStateMachines(listStateMachineParams) .promise(); // Find state machine with specified name and get its arn - // changed filter to find- returns the first object that matches condition const smList = allStateMachines.stateMachines.find( (stateMachine: any) => stateMachine.name === stateMachineName );