Skip to content

Commit 2035c93

Browse files
daschaadreamorosi
andauthored
chore(parameters): Use AppConfig L2 constructs for integration tests (#2524)
* chore(parameter): Use AppConfig L2 constructs for integration tests * Update packages/parameters/tests/helpers/resources.ts Co-authored-by: Andrea Amorosi <[email protected]> --------- Co-authored-by: Andrea Amorosi <[email protected]>
1 parent 30dfdc4 commit 2035c93

File tree

1 file changed

+54
-74
lines changed

1 file changed

+54
-74
lines changed

packages/parameters/tests/helpers/resources.ts

+54-74
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ import type {
44
} from '@aws-lambda-powertools/testing-utils/types';
55
import {
66
concatenateResourceName,
7-
getRuntimeKey,
87
getArchitectureKey,
8+
getRuntimeKey,
99
type TestStack,
1010
} from '@aws-lambda-powertools/testing-utils';
1111
import { TestNodejsFunction } from '@aws-lambda-powertools/testing-utils/resources/lambda';
1212
import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resources/dynamodb';
1313
import { marshall } from '@aws-sdk/util-dynamodb';
14-
import { CfnOutput, Stack } from 'aws-cdk-lib';
14+
import { CfnOutput, Duration, RemovalPolicy, Stack } from 'aws-cdk-lib';
1515
import {
16-
CfnApplication,
17-
CfnConfigurationProfile,
18-
CfnDeployment,
19-
CfnDeploymentStrategy,
20-
CfnEnvironment,
16+
Application,
17+
ConfigurationContent,
18+
ConfigurationType,
19+
DeploymentStrategy,
20+
HostedConfiguration,
21+
IEnvironment,
22+
RolloutStrategy,
2123
CfnHostedConfigurationVersion,
2224
} from 'aws-cdk-lib/aws-appconfig';
2325
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
@@ -212,10 +214,10 @@ class TestDynamodbTableWithItems extends TestDynamodbTable {
212214
* A set of AppConfig resources that can be used in tests.
213215
*/
214216
class TestAppConfigWithProfiles extends Construct {
215-
private readonly application: CfnApplication;
216-
private readonly deploymentStrategy: CfnDeploymentStrategy;
217-
private readonly environment: CfnEnvironment;
218-
private readonly profiles: CfnDeployment[] = [];
217+
private readonly application: Application;
218+
private readonly deploymentStrategy: DeploymentStrategy;
219+
private readonly environment: IEnvironment;
220+
private readonly profiles: HostedConfiguration[] = [];
219221

220222
public constructor(
221223
testStack: TestStack,
@@ -240,77 +242,55 @@ class TestAppConfigWithProfiles extends Construct {
240242

241243
const { profiles } = props;
242244

243-
this.application = new CfnApplication(
244-
testStack.stack,
245-
`app-${randomUUID()}`,
246-
{
247-
name: randomUUID(),
248-
}
249-
);
245+
this.application = new Application(testStack.stack, `app-${randomUUID()}`, {
246+
applicationName: randomUUID(),
247+
description: 'Test application for Powertools Parameters',
248+
});
250249

251-
this.deploymentStrategy = new CfnDeploymentStrategy(
252-
testStack.stack,
253-
`de-${randomUUID()}`,
254-
{
255-
name: randomUUID(),
256-
deploymentDurationInMinutes: 0,
257-
growthFactor: 100,
258-
replicateTo: 'NONE',
259-
finalBakeTimeInMinutes: 0,
260-
}
261-
);
250+
this.environment = this.application.addEnvironment(`ce-${randomUUID()}`, {
251+
environmentName: randomUUID(),
252+
description: 'Test environment for Powertools Parameters',
253+
});
262254

263-
this.environment = new CfnEnvironment(
255+
this.deploymentStrategy = new DeploymentStrategy(
264256
testStack.stack,
265-
`ce-${randomUUID()}`,
257+
`de-${randomUUID()}`,
266258
{
267-
name: randomUUID(),
268-
applicationId: this.application.ref,
259+
deploymentStrategyName: randomUUID(),
260+
description: 'Test deployment strategy for Powertools Parameter',
261+
rolloutStrategy: RolloutStrategy.linear({
262+
deploymentDuration: Duration.minutes(0),
263+
growthFactor: 100,
264+
finalBakeTime: Duration.minutes(0),
265+
}),
269266
}
270267
);
271268

272-
profiles.forEach((profile, index) => {
273-
const configProfile = new CfnConfigurationProfile(
269+
profiles.forEach((profile) => {
270+
const config = new HostedConfiguration(
274271
testStack.stack,
275-
`cp-${randomUUID()}`,
272+
`hc-${randomUUID()}`,
276273
{
277-
name: randomUUID(),
278-
applicationId: this.application.ref,
279-
locationUri: 'hosted',
280-
type: profile.type,
274+
name: `${randomUUID()}-${profile.nameSuffix}`,
275+
description: 'Test hosted configuration for Powertools Parameter',
276+
deploymentStrategy: this.deploymentStrategy,
277+
deployTo: [this.environment],
278+
application: this.application,
279+
type:
280+
profile.type === 'AWS.Freeform'
281+
? ConfigurationType.FREEFORM
282+
: ConfigurationType.FEATURE_FLAGS,
283+
content: ConfigurationContent.fromInlineJson(
284+
profile.content.content,
285+
profile.content.contentType
286+
),
281287
}
282288
);
283-
284-
const configVersion = new CfnHostedConfigurationVersion(
285-
testStack.stack,
286-
`cv-${randomUUID()}`,
287-
{
288-
applicationId: this.application.ref,
289-
configurationProfileId: configProfile.ref,
290-
...profile.content,
291-
}
292-
);
293-
294-
const deployment = new CfnDeployment(
295-
testStack.stack,
296-
concatenateResourceName({
297-
testName: testStack.testName,
298-
resourceName: profile.nameSuffix,
299-
}),
300-
{
301-
applicationId: this.application.ref,
302-
configurationProfileId: configProfile.ref,
303-
configurationVersion: configVersion.ref,
304-
deploymentStrategyId: this.deploymentStrategy.ref,
305-
environmentId: this.environment.ref,
306-
}
307-
);
308-
309-
if (index > 0 && this.profiles) {
310-
deployment.node.addDependency(this.profiles[index - 1]);
311-
}
312-
313-
this.profiles.push(deployment);
289+
// The default is RETAIN so this escape hatch is needed to override it
290+
(
291+
config.node.defaultChild as CfnHostedConfigurationVersion
292+
).applyRemovalPolicy(RemovalPolicy.DESTROY);
293+
this.profiles.push(config);
314294
});
315295
}
316296

@@ -320,8 +300,8 @@ class TestAppConfigWithProfiles extends Construct {
320300
* @param fn The function to add the environment variables to
321301
*/
322302
public addEnvVariablesToFunction(fn: TestNodejsFunction): void {
323-
fn.addEnvironment('APPLICATION_NAME', this.application.name);
324-
fn.addEnvironment('ENVIRONMENT_NAME', this.environment.name);
303+
fn.addEnvironment('APPLICATION_NAME', this.application.name!);
304+
fn.addEnvironment('ENVIRONMENT_NAME', this.environment.name!);
325305
fn.addEnvironment(
326306
'FREEFORM_JSON_NAME',
327307
this.profiles[0].configurationProfileId
@@ -349,7 +329,7 @@ class TestAppConfigWithProfiles extends Construct {
349329
this.profiles.forEach((profile) => {
350330
const appConfigConfigurationArn = Stack.of(fn).formatArn({
351331
service: 'appconfig',
352-
resource: `application/${profile.applicationId}/environment/${profile.environmentId}/configuration/${profile.configurationProfileId}`,
332+
resource: `application/${profile.application.applicationId}/environment/${profile.deployTo![0].environmentId}/configuration/${profile.configurationProfileId}`,
353333
});
354334

355335
fn.addToRolePolicy(

0 commit comments

Comments
 (0)