Skip to content

chore(parameters): Use AppConfig L2 constructs for integration tests #2524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 54 additions & 74 deletions packages/parameters/tests/helpers/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import type {
} from '@aws-lambda-powertools/testing-utils/types';
import {
concatenateResourceName,
getRuntimeKey,
getArchitectureKey,
getRuntimeKey,
type TestStack,
} from '@aws-lambda-powertools/testing-utils';
import { TestNodejsFunction } from '@aws-lambda-powertools/testing-utils/resources/lambda';
import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resources/dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
import { CfnOutput, Stack } from 'aws-cdk-lib';
import { CfnOutput, Duration, RemovalPolicy, Stack } from 'aws-cdk-lib';
import {
CfnApplication,
CfnConfigurationProfile,
CfnDeployment,
CfnDeploymentStrategy,
CfnEnvironment,
Application,
ConfigurationContent,
ConfigurationType,
DeploymentStrategy,
HostedConfiguration,
IEnvironment,
RolloutStrategy,
CfnHostedConfigurationVersion,
} from 'aws-cdk-lib/aws-appconfig';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
Expand Down Expand Up @@ -212,10 +214,10 @@ class TestDynamodbTableWithItems extends TestDynamodbTable {
* A set of AppConfig resources that can be used in tests.
*/
class TestAppConfigWithProfiles extends Construct {
private readonly application: CfnApplication;
private readonly deploymentStrategy: CfnDeploymentStrategy;
private readonly environment: CfnEnvironment;
private readonly profiles: CfnDeployment[] = [];
private readonly application: Application;
private readonly deploymentStrategy: DeploymentStrategy;
private readonly environment: IEnvironment;
private readonly profiles: HostedConfiguration[] = [];

public constructor(
testStack: TestStack,
Expand All @@ -240,77 +242,55 @@ class TestAppConfigWithProfiles extends Construct {

const { profiles } = props;

this.application = new CfnApplication(
testStack.stack,
`app-${randomUUID()}`,
{
name: randomUUID(),
}
);
this.application = new Application(testStack.stack, `app-${randomUUID()}`, {
applicationName: randomUUID(),
description: 'Test application for Powertools Parameters',
});

this.deploymentStrategy = new CfnDeploymentStrategy(
testStack.stack,
`de-${randomUUID()}`,
{
name: randomUUID(),
deploymentDurationInMinutes: 0,
growthFactor: 100,
replicateTo: 'NONE',
finalBakeTimeInMinutes: 0,
}
);
this.environment = this.application.addEnvironment(`ce-${randomUUID()}`, {
environmentName: randomUUID(),
description: 'Test environment for Powertools Parameters',
});

this.environment = new CfnEnvironment(
this.deploymentStrategy = new DeploymentStrategy(
testStack.stack,
`ce-${randomUUID()}`,
`de-${randomUUID()}`,
{
name: randomUUID(),
applicationId: this.application.ref,
deploymentStrategyName: randomUUID(),
description: 'Test deployment strategy for Powertools Parameter',
rolloutStrategy: RolloutStrategy.linear({
deploymentDuration: Duration.minutes(0),
growthFactor: 100,
finalBakeTime: Duration.minutes(0),
}),
}
);

profiles.forEach((profile, index) => {
const configProfile = new CfnConfigurationProfile(
profiles.forEach((profile) => {
const config = new HostedConfiguration(
testStack.stack,
`cp-${randomUUID()}`,
`hc-${randomUUID()}`,
{
name: randomUUID(),
applicationId: this.application.ref,
locationUri: 'hosted',
type: profile.type,
name: `${randomUUID()}-${profile.nameSuffix}`,
description: 'Test hosted configuration for Powertools Parameter',
deploymentStrategy: this.deploymentStrategy,
deployTo: [this.environment],
application: this.application,
type:
profile.type === 'AWS.Freeform'
? ConfigurationType.FREEFORM
: ConfigurationType.FEATURE_FLAGS,
content: ConfigurationContent.fromInlineJson(
profile.content.content,
profile.content.contentType
),
}
);

const configVersion = new CfnHostedConfigurationVersion(
testStack.stack,
`cv-${randomUUID()}`,
{
applicationId: this.application.ref,
configurationProfileId: configProfile.ref,
...profile.content,
}
);

const deployment = new CfnDeployment(
testStack.stack,
concatenateResourceName({
testName: testStack.testName,
resourceName: profile.nameSuffix,
}),
{
applicationId: this.application.ref,
configurationProfileId: configProfile.ref,
configurationVersion: configVersion.ref,
deploymentStrategyId: this.deploymentStrategy.ref,
environmentId: this.environment.ref,
}
);

if (index > 0 && this.profiles) {
deployment.node.addDependency(this.profiles[index - 1]);
}

this.profiles.push(deployment);
// The default is RETAIN so this escape hatch is needed to override it
(
config.node.defaultChild as CfnHostedConfigurationVersion
).applyRemovalPolicy(RemovalPolicy.DESTROY);
this.profiles.push(config);
});
}

Expand All @@ -320,8 +300,8 @@ class TestAppConfigWithProfiles extends Construct {
* @param fn The function to add the environment variables to
*/
public addEnvVariablesToFunction(fn: TestNodejsFunction): void {
fn.addEnvironment('APPLICATION_NAME', this.application.name);
fn.addEnvironment('ENVIRONMENT_NAME', this.environment.name);
fn.addEnvironment('APPLICATION_NAME', this.application.name!);
fn.addEnvironment('ENVIRONMENT_NAME', this.environment.name!);
fn.addEnvironment(
'FREEFORM_JSON_NAME',
this.profiles[0].configurationProfileId
Expand Down Expand Up @@ -349,7 +329,7 @@ class TestAppConfigWithProfiles extends Construct {
this.profiles.forEach((profile) => {
const appConfigConfigurationArn = Stack.of(fn).formatArn({
service: 'appconfig',
resource: `application/${profile.applicationId}/environment/${profile.environmentId}/configuration/${profile.configurationProfileId}`,
resource: `application/${profile.application.applicationId}/environment/${profile.deployTo![0].environmentId}/configuration/${profile.configurationProfileId}`,
});

fn.addToRolePolicy(
Expand Down