-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |