Skip to content

Commit c60c3df

Browse files
committed
feat: StepFunctions test state command 🎉
1 parent 0850d09 commit c60c3df

File tree

7 files changed

+8083
-18179
lines changed

7 files changed

+8083
-18179
lines changed

package-lock.json

Lines changed: 7873 additions & 18166 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "samp-cli",
3-
"version": "1.0.64",
3+
"version": "1.0.65",
44
"description": "CLI tool for extended productivity with AWS Serverless Application Model (SAM)",
55
"main": "index.js",
66
"scripts": {
@@ -34,19 +34,19 @@
3434
"@aws-sdk/client-iot": "^3.345.0",
3535
"@aws-sdk/client-lambda": "^3.358.0",
3636
"@aws-sdk/client-schemas": "^3.358.0",
37-
"@aws-sdk/client-sfn": "^3.359.0",
37+
"@aws-sdk/client-sfn": "^3.465.0",
3838
"@aws-sdk/client-sts": "^3.379.1",
3939
"@aws-sdk/credential-provider-sso": "^3.319.0",
4040
"@aws-sdk/shared-ini-file-loader": "^3.374.0",
41-
"@mhlabs/iam-policies-cli": "^1.0.5",
42-
"@mhlabs/sfn-cli": "^1.0.2",
41+
"@mhlabs/iam-policies-cli": "^1.0.7",
42+
"@mhlabs/sfn-cli": "^1.0.3",
4343
"@mhlabs/xray-cli": "^1.0.8",
4444
"@octokit/rest": "^18.5.2",
4545
"archiver": "^6.0.0",
4646
"ascii-table3": "^0.9.0",
47-
"aws-cdk-lib": "^2.87.0",
4847
"axios": "^1.4.0",
4948
"chokidar": "^3.5.3",
49+
"cli-color": "^2.0.3",
5050
"cli-spinner": "^0.2.10",
5151
"clipboardy": "^2.2.0",
5252
"commander": "^7.2.0",
@@ -75,9 +75,6 @@
7575
"yaml": "^2.3.1",
7676
"yaml-cfn": "^0.3.2"
7777
},
78-
"devDependencies": {
79-
"jest": "^26.6.3"
80-
},
8178
"engines": {
8279
"node": ">=15.0.0"
8380
}

src/commands/invoke/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ program
88
.option("-r, --resource [resource]", "The resource (function name or state machine ARN) to invoke. If not specified, you will be prompted to select one")
99
.option("-pl, --payload [payload]", "The payload to send to the function. Could be stringified JSON, a file path to a JSON file or the name of a shared test event")
1010
.option("-l, --latest", "Invokes the latest request that was sent to the function", false)
11-
.option("-p, --profile [profile]", "AWS profile to use", "default")
11+
.option("-p, --profile [profile]", "AWS profile to use")
1212
.option("-sync", "--synchronous", "StepFuncitons only - wait for the state machine to finish and print the output", false)
1313
.option("--region [region]", "The AWS region to use. Falls back on AWS_REGION environment variable if not specified")
1414
.action(async (cmd) => {

src/commands/invoke/stepFunctionsInvoker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const { SFNClient, DescribeStateMachineForExecutionCommand, StartExecutionCommand, ListExecutionsCommand, DescribeExecutionCommand } = require('@aws-sdk/client-sfn');
22
const { SchemasClient, DescribeSchemaCommand, UpdateSchemaCommand, CreateSchemaCommand, CreateRegistryCommand } = require('@aws-sdk/client-schemas');
33
const { fromSSO } = require("@aws-sdk/credential-provider-sso");
4+
const samConfigParser = require('../../shared/samConfigParser');
45
const link2aws = require('link2aws');
56
const fs = require('fs');
67
const inputUtil = require('../../shared/inputUtil');
78
const registryName = "sfn-testevent-schemas";
89
async function invoke(cmd, sfnArn) {
9-
const sfnClient = new SFNClient({ credentials: await fromSSO({ profile: cmd.profile }) });
10+
const config = await samConfigParser.parse();
11+
const sfnClient = new SFNClient({ credentials: await fromSSO({ profile: cmd.profile || config.profile }), region: cmd.region || config.region });
1012
const schemasClient = new SchemasClient({ credentials: await fromSSO({ profile: cmd.profile }) });
1113
const stateMachineName = sfnArn.split(":").pop();
1214
if (!cmd.payload) {

src/commands/stepfunctions/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const program = require("commander");
22
const sync = require("@mhlabs/sfn-cli/src/commands/sync/sync");
33
const init = require("@mhlabs/sfn-cli/src/commands/init/init");
44
const inputUtil = require("../../shared/inputUtil");
5+
const testState = require("./test-state");
56
program
67
.command("stepfunctions")
78
.alias("sfn")
@@ -10,7 +11,7 @@ program
1011
.description("Initiates a state machine or sets up a live sync between your local ASL and the cloud")
1112
.option("-t, --template-file [templateFile]", "Path to SAM template file", "template.yaml")
1213
.option("-s, --stack-name [stackName]", "[Only applicable when syncing] The name of the deployed stack")
13-
.option("-p, --profile [profile]", "[Only applicable when syncing] AWS profile to use", "default")
14+
.option("-p, --profile [profile]", "[Only applicable when syncing] AWS profile to use")
1415
.option("--region [region]", "The AWS region to use. Falls back on AWS_REGION environment variable if not specified")
1516

1617
.action(async (cmd, opts) => {
@@ -21,8 +22,10 @@ program
2122
await init.run(opts);
2223
} else if (cmd === "sync") {
2324
await sync.run(opts);
25+
} else if (cmd === "test-state") {
26+
await testState.run(opts);
2427
} else {
25-
console.log("Unknown command. Valid commands are: init, sync");
28+
console.log("Unknown command. Valid commands are: init, sync, test-state");
2629
}
2730
return;
2831
});
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
const { SFNClient, TestStateCommand, DescribeStateMachineCommand, ListExecutionsCommand, GetExecutionHistoryCommand } = require('@aws-sdk/client-sfn');
2+
const { CloudFormationClient, ListStackResourcesCommand } = require('@aws-sdk/client-cloudformation');
3+
const { STSClient, GetCallerIdentityCommand } = require('@aws-sdk/client-sts');
4+
const { fromSSO } = require("@aws-sdk/credential-provider-sso");
5+
const samConfigParser = require('../../shared/samConfigParser');
6+
const parser = require('../../shared/parser');
7+
const fs = require('fs');
8+
const inputUtil = require('../../shared/inputUtil');
9+
const clc = require("cli-color");
10+
const path = require('path');
11+
const { Spinner } = require('cli-spinner');
12+
13+
const os = require('os');
14+
let clientParams;
15+
async function run(cmd) {
16+
const config = await samConfigParser.parse();
17+
const credentials = await fromSSO({ profile: cmd.profile || config.profile || 'default' });
18+
clientParams = { credentials, region: cmd.region || config.region }
19+
const sfnClient = new SFNClient(clientParams);
20+
const cloudFormation = new CloudFormationClient(clientParams);
21+
const sts = new STSClient(clientParams);
22+
const template = await parser.findSAMTemplateFile(process.cwd());
23+
const templateContent = fs.readFileSync(template, 'utf8');
24+
const templateObj = parser.parse("template", templateContent);
25+
const stateMachines = findAllStateMachines(templateObj);
26+
const stateMachine = stateMachines.length === 1 ? stateMachines[0] : await inputUtil.list("Select state machine", stateMachines);
27+
28+
const spinner = new Spinner(`Fetching state machine ${stateMachine}... %s`);
29+
spinner.setSpinnerString(30);
30+
spinner.start();
31+
32+
const stackResources = await listAllStackResourcesWithPagination(cloudFormation, cmd.stackName || config.stack_name);
33+
34+
const stateMachineArn = stackResources.find(r => r.LogicalResourceId === stateMachine).PhysicalResourceId;
35+
const stateMachineRoleName = stackResources.find(r => r.LogicalResourceId === `${stateMachine}Role`).PhysicalResourceId;
36+
37+
const describedStateMachine = await sfnClient.send(new DescribeStateMachineCommand({ stateMachineArn }));
38+
const definition = JSON.parse(describedStateMachine.definition);
39+
40+
spinner.stop(true);
41+
const states = findStates(definition);
42+
const state = await inputUtil.autocomplete("Select state", states.map(s => { return { name: s.key, value: { name: s.key, state: s.state } } }));
43+
44+
const input = await getInput(stateMachineArn, state.name, describedStateMachine.type);
45+
46+
const accountId = (await sts.send(new GetCallerIdentityCommand({}))).Account;
47+
console.log(`Invoking state ${clc.green(state.name)} with input:\n${clc.green(input)}\n`);
48+
const testResult = await sfnClient.send(new TestStateCommand(
49+
{
50+
definition: JSON.stringify(state.state),
51+
roleArn: `arn:aws:iam::${accountId}:role/${stateMachineRoleName}`,
52+
input: input
53+
}
54+
));
55+
delete testResult.$metadata;
56+
let color = "green";
57+
if (testResult.error) {
58+
color = "red";
59+
}
60+
for (const key in testResult) {
61+
console.log(`${clc[color](key.charAt(0).toUpperCase() + key.slice(1))}: ${testResult[key]}`);
62+
}
63+
}
64+
65+
async function getInput(stateMachineArn, state, stateMachineType) {
66+
let types = [
67+
"Empty JSON",
68+
"Manual input",
69+
"From file"];
70+
71+
if (stateMachineType === "STANDARD") {
72+
types.push("From recent execution");
73+
}
74+
75+
const configDirExists = fs.existsSync(path.join(os.homedir(), '.samp-cli', 'state-tests'));
76+
if (!configDirExists) {
77+
fs.mkdirSync(path.join(os.homedir(), '.samp-cli', 'state-tests'), { recursive: true });
78+
}
79+
80+
const stateMachineStateFileExists = fs.existsSync(path.join(os.homedir(), '.samp-cli', 'state-tests', stateMachineArn));
81+
82+
if (!stateMachineStateFileExists) {
83+
fs.writeFileSync(path.join(os.homedir(), '.samp-cli', 'state-tests', stateMachineArn), "{}");
84+
}
85+
86+
const storedState = JSON.parse(fs.readFileSync(path.join(os.homedir(), '.samp-cli', 'state-tests', stateMachineArn), "utf8"));
87+
if (Object.keys(storedState).length > 0) {
88+
types = ["Latest input", ...types];
89+
}
90+
91+
const type = await inputUtil.list("Select input type", types);
92+
93+
if (type === "Empty JSON") {
94+
return "{}";
95+
}
96+
97+
if (type === "Manual input") {
98+
return inputUtil.text("Enter input JSON", "{}");
99+
}
100+
101+
if (type === "From file") {
102+
const file = await inputUtil.file("Select input file", "json");
103+
return fs.readFileSync(file, "utf8");
104+
}
105+
106+
if (type === "Latest input") {
107+
return JSON.stringify(storedState[state]);
108+
}
109+
110+
if (type === "From recent execution") {
111+
const sfnClient = new SFNClient(clientParams);
112+
113+
const executions = await sfnClient.send(new ListExecutionsCommand({ stateMachineArn }));
114+
const execution = await inputUtil.autocomplete("Select execution", executions.executions.map(e => { return { name: `[${e.startDate.toLocaleTimeString()}] ${e.name}`, value: e.executionArn } }));
115+
const executionHistory = await sfnClient.send(new GetExecutionHistoryCommand({ executionArn: execution }));
116+
const input = findFirstTaskEnteredEvent(executionHistory, state);
117+
if (!input) {
118+
console.log("No input found for state. Did it execute in the chosen execution?");
119+
process.exit(1);
120+
}
121+
return input.stateEnteredEventDetails.input;
122+
}
123+
}
124+
125+
function findFirstTaskEnteredEvent(jsonData, state) {
126+
console.log("state", state);
127+
for (const event of jsonData.events) {
128+
if (event.type.endsWith("StateEntered") && event.stateEnteredEventDetails.name === state) {
129+
return event;
130+
}
131+
}
132+
return null; // or any appropriate default value
133+
}
134+
135+
136+
function findStates(aslDefinition) {
137+
const result = [];
138+
139+
function traverseStates(states) {
140+
Object.keys(states).forEach(key => {
141+
const state = states[key];
142+
if (state.Type === 'Task' || state.Type === 'Pass' || state.Type === 'Choice') {
143+
result.push({ key, state });
144+
}
145+
// Recursively search in Parallel and Map structures
146+
if (state.Type === 'Parallel' && state.Branches) {
147+
state.Branches.forEach(branch => {
148+
traverseStates(branch.States);
149+
});
150+
}
151+
if (state.Type === 'Map' && state.ItemProcessor && state.ItemProcessor.States) {
152+
traverseStates(state.ItemProcessor.States);
153+
}
154+
});
155+
}
156+
157+
traverseStates(aslDefinition.States);
158+
return result;
159+
}
160+
161+
function listAllStackResourcesWithPagination(cloudFormation, stackName) {
162+
const params = {
163+
StackName: stackName
164+
};
165+
const resources = [];
166+
const listStackResources = async (params) => {
167+
const response = await cloudFormation.send(new ListStackResourcesCommand(params));
168+
resources.push(...response.StackResourceSummaries);
169+
if (response.NextToken) {
170+
params.NextToken = response.NextToken;
171+
await listStackResources(params);
172+
}
173+
};
174+
175+
return listStackResources(params).then(() => resources);
176+
}
177+
178+
function findAllStateMachines(templateObj) {
179+
const stateMachines = Object.keys(templateObj.Resources).filter(r => templateObj.Resources[r].Type === "AWS::Serverless::StateMachine");
180+
if (stateMachines.length === 0) {
181+
console.log("No state machines found in template");
182+
process.exit(0);
183+
}
184+
185+
return stateMachines;
186+
}
187+
188+
module.exports = {
189+
run
190+
}

src/commands/traces/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const program = require("commander");
22
const traces = require("@mhlabs/xray-cli/src/commands/traces/traces");
3+
const samConfigParser = require("../../shared/samConfigParser");
34
program
45
.command("traces")
56
.alias("t")
@@ -9,8 +10,12 @@ program
910
.option("-as, --absolute-start <start>", "Start time (ISO 8601)")
1011
.option("-ae, --absolute-end <end>", "End time (ISO 8601)")
1112
.option("-f, --filter-expression <filter>", "Filter expression. Must be inside double or single quotes (\"/')")
12-
.option("-p, --profile <profile>", "AWS profile to use", "default")
13+
.option("-p, --profile <profile>", "AWS profile to use")
1314
.option("-r, --region <region>", "AWS region to use")
1415
.action(async (cmd) => {
16+
const config = await samConfigParser.parse();
17+
cmd.region = cmd.region || config.region;
18+
cmd.profile = cmd.profile || config.profile || 'default';
19+
1520
await traces.run(cmd);
1621
});

0 commit comments

Comments
 (0)