-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstateMachineConstruct.test.ts
More file actions
165 lines (142 loc) · 5.13 KB
/
stateMachineConstruct.test.ts
File metadata and controls
165 lines (142 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {App, Stack} from "aws-cdk-lib"
import {Template, Match} from "aws-cdk-lib/assertions"
import {
describe,
test,
beforeAll,
expect
} from "vitest"
import {Pass} from "aws-cdk-lib/aws-stepfunctions"
import {ExpressStateMachine} from "../../src/constructs/StateMachine.js"
import {CatchAllErrorPass} from "../../src/constructs/StateMachine/CatchAllErrorPass.js"
describe("ExpressStateMachine construct", () => {
let stack: Stack
let app: App
let template: Template
let construct: ExpressStateMachine
beforeAll(() => {
app = new App()
stack = new Stack(app, "StateMachineStack")
const dummyState = new Pass(stack, "DummyState")
construct = new ExpressStateMachine(stack, "TestStateMachine", {
stackName: "test-stack",
stateMachineName: "test-state-machine",
definition: dummyState,
logRetentionInDays: 30
})
template = Template.fromStack(stack)
})
test("creates CloudWatch log group with correct name and KMS key", () => {
template.hasResourceProperties("AWS::Logs::LogGroup", {
LogGroupName: "/aws/stepfunctions/test-state-machine",
KmsKeyId: {"Fn::ImportValue": "account-resources-cdk-uk:KMS:CloudwatchLogsKmsKey:Arn"},
RetentionInDays: 30
})
})
test("creates Splunk subscription filter by default", () => {
template.hasResourceProperties("AWS::Logs::SubscriptionFilter", {
FilterPattern: "",
RoleArn: {"Fn::ImportValue": "account-resources-cdk-uk:IAM:SplunkSubscriptionFilterRole:Arn"}
})
})
test("creates IAM role for state machine with correct service principal", () => {
template.hasResourceProperties("AWS::IAM::Role", {
AssumeRolePolicyDocument: {
Statement: [{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: {Service: "states.amazonaws.com"}
}]
}
})
})
test("creates Express state machine with tracing and logging", () => {
template.hasResourceProperties("AWS::StepFunctions::StateMachine", {
StateMachineName: "test-state-machine",
StateMachineType: "EXPRESS",
TracingConfiguration: {Enabled: true},
LoggingConfiguration: {
IncludeExecutionData: true,
Level: "ALL"
}
})
})
test("creates execution managed policy with StartSyncExecution permission", () => {
template.hasResourceProperties("AWS::IAM::ManagedPolicy", {
Description: "execute state machine test-state-machine",
PolicyDocument: {
Statement: [Match.objectLike({
Action: Match.arrayWith(["states:StartSyncExecution", "states:StartExecution"]),
Effect: "Allow"
})]
}
})
})
test("creates put-logs managed policy allowing wildcard on log delivery actions", () => {
template.hasResourceProperties("AWS::IAM::ManagedPolicy", {
Description: "write to test-state-machine logs",
PolicyDocument: {
Statement: [
Match.objectLike({
Action: Match.arrayWith(["logs:CreateLogStream", "logs:PutLogEvents"]),
Effect: "Allow"
}),
Match.objectLike({
Action: Match.arrayWith(["logs:DescribeLogGroups", "logs:CreateLogDelivery"]),
Effect: "Allow",
Resource: "*"
})
]
}
})
})
test("exposes executionPolicy and stateMachine as public properties", () => {
expect(construct.executionPolicy).toBeDefined()
expect(construct.stateMachine).toBeDefined()
})
})
describe("ExpressStateMachine with Splunk disabled", () => {
let template: Template
let construct: ExpressStateMachine
beforeAll(() => {
const app = new App()
const stack = new Stack(app, "StateMachineNoSplunkStack")
const dummyState = new Pass(stack, "DummyState")
construct = new ExpressStateMachine(stack, "TestStateMachine", {
stackName: "test-stack",
stateMachineName: "test-state-machine",
definition: dummyState,
logRetentionInDays: 30,
addSplunkSubscriptionFilter: false
})
template = Template.fromStack(stack)
})
test("does not create a subscription filter when addSplunkSubscriptionFilter is false", () => {
const filters = template.findResources("AWS::Logs::SubscriptionFilter")
expect(Object.keys(filters).length).toBe(0)
})
test("exposes executionPolicy and stateMachine as public properties", () => {
expect(construct.executionPolicy).toBeDefined()
expect(construct.stateMachine).toBeDefined()
})
})
describe("CatchAllErrorPass construct", () => {
let stack: Stack
let template: Template
let construct: CatchAllErrorPass
beforeAll(() => {
const app = new App()
stack = new Stack(app, "CatchAllErrorStack")
construct = new CatchAllErrorPass(stack, "TestCatchAllError")
template = Template.fromStack(stack)
})
test("exposes a state property", () => {
expect(construct.state).toBeDefined()
})
test("creates a Pass state in the stack", () => {
template.resourceCountIs("AWS::StepFunctions::StateMachine", 0)
// CatchAllErrorPass creates a Pass state; verify it is present as a construct child
const passState = construct.node.tryFindChild("Catch All Error")
expect(passState).toBeDefined()
})
})