Skip to content

Commit

Permalink
test: add cdk resource invoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalenic committed Feb 18, 2024
1 parent 1756960 commit 9e675b2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
1 change: 0 additions & 1 deletion config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const orcaBusStatefulConfig = {
monitoring: {
cloudwatchLogsExports: ['orcabus-postgresql'],
},
databaseSecurityGroupName: 'database-security-group',
inboundSecurityGroupName: 'inbound-database-security-group',
},
securityGroupProps: {
Expand Down
74 changes: 74 additions & 0 deletions test/stateless/cdkResourceInvoke.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as cdk from 'aws-cdk-lib';
import { Match, Template } from 'aws-cdk-lib/assertions';
import { CdkResourceInvoke } from '../../lib/workload/stateless/functions/cdk_resource_invoke';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as lambda from 'aws-cdk-lib/aws-lambda';

let stack: cdk.Stack;
let vpc: ec2.Vpc;

beforeEach(() => {
stack = new cdk.Stack();
vpc = new ec2.Vpc(stack, 'MockExistingVPC', {
subnetConfiguration: [
{
cidrMask: 24,
name: 'publicSubnet',
subnetType: ec2.SubnetType.PUBLIC,
},
{
cidrMask: 24,
name: 'privateWithEgressSubnet',
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
],
});
});

test('Test CdkResourceInvoke', () => {
new CdkResourceInvoke(stack, 'TestCdkResourceInvoke', {
functionProps: {
code: new lambda.InlineCode('exports.handler = async (event) => console.log(event)'),
runtime: lambda.Runtime.NODEJS_20_X,
handler: 'index.handler',
},
id: 'TestFunction',
vpc,
createFunction: (scope, id, props) => {
return new lambda.Function(scope, id, props);
},
});
const template = Template.fromStack(stack);

const expectedHash = '7a1920d61156abc05a60135a'; // pragma: allowlist secret

// Contains the Lambda function.
template.hasResourceProperties('AWS::Lambda::Function', {
Code: {
ZipFile: 'exports.handler = async (event) => console.log(event)',
},
FunctionName: `${expectedHash}-ResourceInvokeFunction-TestFunction`,
Handler: 'index.handler',
Runtime: 'nodejs20.x',
});

// Has policy to invoke it.
template.hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'lambda:InvokeFunction',
Effect: 'Allow',
Resource: {
'Fn::Join': [
'',
Match.arrayWith([`:function:${expectedHash}-ResourceInvokeFunction-*`]),
],
},
},
],
},
});

console.log(JSON.stringify(template, undefined, 2));
});

0 comments on commit 9e675b2

Please sign in to comment.