-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssmParametersConstruct.test.ts
More file actions
190 lines (172 loc) · 6.07 KB
/
ssmParametersConstruct.test.ts
File metadata and controls
190 lines (172 loc) · 6.07 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import {App, Stack} from "aws-cdk-lib"
import {Template} from "aws-cdk-lib/assertions"
import {
beforeAll,
describe,
expect,
test
} from "vitest"
import {SsmParametersConstruct} from "../../src/constructs/SsmParametersConstruct"
import {assert} from "node:console"
describe("SsmParametersConstruct", () => {
let template: Template
beforeAll(() => {
const app = new App()
const stack = new Stack(app, "parameterStack")
const params = new SsmParametersConstruct(stack, "TestingParameters", {
namePrefix: "mock-stack",
parameters: [
{
id: "MockParam1",
nameSuffix: "MockParam1",
description: "Description for mock parameter 1",
value: "mock-value-1",
outputExportSuffix: "MockParam1Parameter",
outputDescription: "Name of the SSM parameter holding MockParam1"
},
{
id: "MockParam2",
nameSuffix: "MockParam2",
description: "Description for mock parameter 2",
value: "mock-value-2",
outputExportSuffix: "MockParam2Parameter",
outputDescription: "Name of the SSM parameter holding MockParam2"
},
{
id: "MockParam3",
nameSuffix: "MockParam3",
description: "Description for mock parameter 3",
value: "mock-value-3",
outputExportSuffix: "MockParam3Parameter",
outputDescription: "Name of the SSM parameter holding MockParam3"
}
],
readPolicyDescription: "Mock policy description",
readPolicyOutputDescription: "Mock read policy output description",
readPolicyExportSuffix: "MockGetParametersPolicy"
})
// Sonarcloud complains that the construct is not used, so we add an assertion to sidestep that.
assert(params, "SsmParametersConstruct should be created successfully")
template = Template.fromStack(stack)
})
test("creates all SSM parameters", () => {
template.hasResourceProperties("AWS::SSM::Parameter", {
Name: "mock-stack-MockParam1",
Type: "String",
Value: "mock-value-1"
})
template.hasResourceProperties("AWS::SSM::Parameter", {
Name: "mock-stack-MockParam2",
Type: "String",
Value: "mock-value-2"
})
template.hasResourceProperties("AWS::SSM::Parameter", {
Name: "mock-stack-MockParam3",
Type: "String",
Value: "mock-value-3"
})
})
test("creates read policy with GetParameter actions for all parameters", () => {
const policies = template.findResources("AWS::IAM::ManagedPolicy", {
Properties: {
Description: "Mock policy description"
}
})
expect(Object.keys(policies)).toHaveLength(1)
const policy = Object.values(policies)[0] as {
Properties: {
PolicyDocument: {
Statement: Array<{
Action: Array<string>
Resource: Array<unknown>
}>
}
}
}
const statement = policy.Properties.PolicyDocument.Statement[0]
expect(statement.Action).toEqual(["ssm:GetParameter", "ssm:GetParameters"])
expect(statement.Resource).toHaveLength(3)
})
})
describe("SsmParametersConstruct uses defaults when optional fields are omitted", () => {
test("creates parameter and policy with default readPolicyDescription when optional fields are omitted", () => {
const app = new App()
const stack = new Stack(app, "defaultsStack")
const params = new SsmParametersConstruct(stack, "DefaultsParameters", {
namePrefix: "mock-stack",
parameters: [
{
id: "MockParam1",
nameSuffix: "MockParam1Suffix",
description: "Mock SSM parameter description",
value: "mock-value-1"
}
],
readPolicyExportSuffix: "MockGetParametersPolicy"
})
// Get sonar to shup up about the construct not being used
assert(params, "SsmParametersConstruct should be created successfully")
const template = Template.fromStack(stack)
template.hasResourceProperties("AWS::SSM::Parameter", {
Name: "mock-stack-MockParam1Suffix",
Type: "String",
Value: "mock-value-1",
Description: "Mock SSM parameter description"
})
template.hasResourceProperties("AWS::IAM::ManagedPolicy", {
Description: "Allows reading SSM parameters"
})
})
})
describe("SsmParametersConstruct validation", () => {
test("throws when parameters array is empty", () => {
const app = new App()
const stack = new Stack(app, "emptyParamStack")
expect(() => new SsmParametersConstruct(stack, "EmptyParameters", {
namePrefix: "mock-stack",
parameters: []
})).toThrow("SsmParametersConstruct requires at least one parameter definition")
})
test("throws when duplicate parameter ids are detected", () => {
const app = new App()
const stack = new Stack(app, "duplicateIdStack")
expect(() => new SsmParametersConstruct(stack, "DuplicateIdParameters", {
namePrefix: "mock-stack",
parameters: [
{
id: "MockParam1",
nameSuffix: "MockParam1",
description: "Description for mock parameter 1",
value: "mock-value-1"
},
{
id: "MockParam1",
nameSuffix: "MockParam1Different",
description: "Description for duplicate id parameter",
value: "mock-value-2"
}
]
})).toThrow("Duplicate parameter id detected: MockParam1.")
})
test("throws when duplicate parameter names are detected", () => {
const app = new App()
const stack = new Stack(app, "duplicateNameStack")
expect(() => new SsmParametersConstruct(stack, "DuplicateNameParameters", {
namePrefix: "mock-stack",
parameters: [
{
id: "MockParam1",
nameSuffix: "SharedSuffix",
description: "Description for mock parameter 1",
value: "mock-value-1"
},
{
id: "MockParam2",
nameSuffix: "SharedSuffix",
description: "Description for duplicate name parameter",
value: "mock-value-2"
}
]
})).toThrow("Duplicate parameter name detected: mock-stack-SharedSuffix.")
})
})