Skip to content

Commit

Permalink
feat: added integration tests [CLK-268673] (#8)
Browse files Browse the repository at this point in the history
* chore: added integration tests

* chore: update dependencies

* chore: run projen build

* feat: added integration test with integ-runner

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* test: update integration test

* docs: added documentation

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
jose-clickup and github-actions authored Nov 14, 2023
1 parent 9a4f787 commit 76e1452
Show file tree
Hide file tree
Showing 30 changed files with 64,067 additions and 888 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions .projenrc.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { clickupCdk } from '@time-loop/clickup-projen';
import { JsonPatch } from 'projen';

const repoName = 'cdk-lambda-eni-usage-metric-publisher';
const project = new clickupCdk.ClickUpCdkConstructLibrary({
author: 'jose-clickup',
authorAddress: '[email protected]',
cdkVersion: '2.99.0',
cdkVersion: '2.105.0',
defaultReleaseBranch: 'main',
devDeps: ['@time-loop/clickup-projen', '@aws-cdk/integ-tests-alpha', 'aws-sdk-mock'],
devDeps: ['@time-loop/clickup-projen', '@aws-cdk/integ-tests-alpha', 'aws-sdk-mock', '@aws-sdk/client-cloudwatch'],
jsiiVersion: '~5.0.0',
name: 'cdk-lambda-eni-usage-metric-publisher',
projenrcTs: true,
repositoryUrl: 'https://github.com/time-loop/cdk-lambda-eni-usage-metric-publisher.git',
gitignore: ['.vscode/**'],
bundledDeps: ['aws-sdk'],
peerDeps: ['multi-convention-namer'],
experimentalIntegRunner: true,
// deps: [] /* Runtime dependencies of this module. */,
// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
// packageName: undefined, /* The "name" in package.json. */
});
// Assume the usInfraDev role
const build = project.tryFindObjectFile('.github/workflows/build.yml');
build?.addOverride('jobs.build.permissions', { 'id-token': 'write' });
build?.patch(
JsonPatch.add('/jobs/build/steps/0', {

Check failure on line 27 in .projenrc.ts

View workflow job for this annotation

GitHub Actions / Upgrade

Argument of type 'import("/home/runner/work/cdk-lambda-eni-usage-metric-publisher/cdk-lambda-eni-usage-metric-publisher/node_modules/projen/lib/json-patch").JsonPatch' is not assignable to parameter of type 'import("/home/runner/work/cdk-lambda-eni-usage-metric-publisher/cdk-lambda-eni-usage-metric-publisher/node_modules/@time-loop/clickup-projen/node_modules/projen/lib/json-patch").JsonPatch'.

Check failure on line 27 in .projenrc.ts

View workflow job for this annotation

GitHub Actions / Upgrade

Argument of type 'import("/home/runner/work/cdk-lambda-eni-usage-metric-publisher/cdk-lambda-eni-usage-metric-publisher/node_modules/projen/lib/json-patch").JsonPatch' is not assignable to parameter of type 'import("/home/runner/work/cdk-lambda-eni-usage-metric-publisher/cdk-lambda-eni-usage-metric-publisher/node_modules/@time-loop/clickup-projen/node_modules/projen/lib/json-patch").JsonPatch'.
name: 'Configure AWS Credentials',
uses: 'aws-actions/configure-aws-credentials@v2',
with: {
'aws-region': 'us-west-2',
'role-to-assume': `arn:aws:iam::425845004253:role/${repoName}-github-actions-role`,
'role-duration-seconds': 900,
},
}),
);
project.synth();
34 changes: 34 additions & 0 deletions cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"vpc-provider:account=425845004253:filter.vpc-id=vpc-00f8af8d4967b2a3b:region=us-west-2:returnAsymmetricSubnets=true": {
"vpcId": "vpc-00f8af8d4967b2a3b",
"vpcCidrBlock": "10.60.0.0/16",
"ownerAccountId": "425845004253",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Isolated",
"type": "Isolated",
"subnets": [
{
"subnetId": "subnet-02127dec34cbcdf7e",
"cidr": "10.60.1.0/24",
"availabilityZone": "us-west-2a",
"routeTableId": "rtb-0c2aa60a7630b0f63"
},
{
"subnetId": "subnet-0c9e0da263eaaf57f",
"cidr": "10.60.2.0/24",
"availabilityZone": "us-west-2b",
"routeTableId": "rtb-0c2aa60a7630b0f63"
},
{
"subnetId": "subnet-013ad4436277f8fb8",
"cidr": "10.60.3.0/24",
"availabilityZone": "us-west-2c",
"routeTableId": "rtb-0c2aa60a7630b0f63"
}
]
}
]
}
}
12 changes: 8 additions & 4 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions renovate.json5

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/lambda-eni-usage-metric-publisher.monitor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/**
* The Lambda function resource is managed from lambda-eni-usage-metric-publisher.ts
*/
import * as AWS from 'aws-sdk';

export interface Result {
region: string;
vpcId: string;
count: number;
}

/**
* Monitor the usage of ENIs by Lambda functions in multiple regions and publish the metric data to CloudWatch.
* @returns An array of objects containing the region, VPC ID, and count of ENIs used by Lambda functions in that VPC.
* @throws An error if the REGION_LIST or CW_NAMESPACE environment variables are not set, or if there is an error publishing the metric data to CloudWatch.
*/
export const monitor = async () => {
try {
const regionList = process.env.REGION_LIST;
Expand Down
34 changes: 34 additions & 0 deletions test/cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"vpc-provider:account=425845004253:filter.vpc-id=vpc-00f8af8d4967b2a3b:region=us-west-2:returnAsymmetricSubnets=true": {
"vpcId": "vpc-00f8af8d4967b2a3b",
"vpcCidrBlock": "10.60.0.0/16",
"ownerAccountId": "425845004253",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Isolated",
"type": "Isolated",
"subnets": [
{
"subnetId": "subnet-02127dec34cbcdf7e",
"cidr": "10.60.1.0/24",
"availabilityZone": "us-west-2a",
"routeTableId": "rtb-0c2aa60a7630b0f63"
},
{
"subnetId": "subnet-0c9e0da263eaaf57f",
"cidr": "10.60.2.0/24",
"availabilityZone": "us-west-2b",
"routeTableId": "rtb-0c2aa60a7630b0f63"
},
{
"subnetId": "subnet-013ad4436277f8fb8",
"cidr": "10.60.3.0/24",
"availabilityZone": "us-west-2c",
"routeTableId": "rtb-0c2aa60a7630b0f63"
}
]
}
]
}
}
83 changes: 83 additions & 0 deletions test/integ.lambda-eni-usage-metric-publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { join } from 'path';
import { ExpectedResult, IntegTest } from '@aws-cdk/integ-tests-alpha';
import { App, Stack, StackProps, aws_lambda_nodejs, aws_ec2, Duration, aws_iam } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Namer } from 'multi-convention-namer';
import { IntegTestResources } from './utils/integ-tests-types';
import { LambdaEniUsageMetricPublisher } from '../src';
// The BaselineStack creates an instance of LambdaEniUsageMetricPublisher
export class BaselineStack extends Stack {
readonly lambdaFunction: LambdaEniUsageMetricPublisher;

constructor(scope: Construct, props: StackProps) {
const id = new Namer(['monitor', 'baseline']);
super(scope, id.pascal, props);
this.lambdaFunction = new LambdaEniUsageMetricPublisher(this, id, {
publishFrequency: 1,
regions: [IntegTestResources.AWS_REGION],
cwNamespace: 'LambdaHyperplaneEniUsage',
});
}
}

// The AssertionStack creates AWS Lambda helpers to run the assertions
export class AssertionStack extends Stack {
constructor(scope: Construct, props: StackProps) {
const id = new Namer(['helper', 'monitor', 'baseline']);
super(scope, id.pascal, props);
// The Lambda DummyFunction cover the scenarios where no Lambda functions are deployed in a VPC.
new aws_lambda_nodejs.NodejsFunction(this, 'DummyFunction', {
vpc: aws_ec2.Vpc.fromLookup(this, 'ImportVPC', {
vpcId: IntegTestResources.VPC_ID,
}),
entry: join(__dirname, 'utils/hello-world.ts'),
allowPublicSubnet: true,
});
// The Lambda GetEniIntegRunnerFunction is used to run the assertions as the current awsApiCall presents an issue with the AWS SDK and timestamps parsing.
new aws_lambda_nodejs.NodejsFunction(this, 'GetEniIntegRunnerFunction', {
functionName: 'GetEniIntegRunnerFunction',
entry: join(__dirname, 'utils/get-eni-usage-metrics.ts'),
timeout: Duration.seconds(15),
}).addToRolePolicy(
new aws_iam.PolicyStatement({
actions: ['cloudwatch:GetMetricData'],
resources: ['*'],
}),
);
}
}

const app = new App();
const stack = new BaselineStack(app, {
env: {
account: IntegTestResources.AWS_ACCOUNT,
region: IntegTestResources.AWS_REGION,
},
});
const assertionStack = new AssertionStack(app, {
env: {
account: IntegTestResources.AWS_ACCOUNT,
region: IntegTestResources.AWS_REGION,
},
});

const integ = new IntegTest(app, 'integ', {
testCases: [stack],
enableLookups: true,
cdkCommandOptions: {
destroy: {
args: {
force: true,
},
},
},
});

integ.node.addDependency(assertionStack, stack);
const lambdaFunctionTest = integ.assertions.invokeFunction({
functionName: 'GetEniIntegRunnerFunction',
});
lambdaFunctionTest.expect(ExpectedResult.objectLike({ StatusCode: 200 }));
lambdaFunctionTest.assertAtPath('Payload.body.message', ExpectedResult.stringLikeRegexp('Metrics found'));

app.synth();
Loading

0 comments on commit 76e1452

Please sign in to comment.