Skip to content

Commit

Permalink
add sam cdk integration test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
moelasmar committed Feb 15, 2022
1 parent 692d23a commit c688f7c
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/aws-cdk/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
baseConfig.ignorePatterns.push('lib/init-templates/**/typescript/**/*.ts');
baseConfig.ignorePatterns.push('test/integ/cli/sam_cdk_integ_app/**/*.ts');
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
module.exports = baseConfig;
3 changes: 2 additions & 1 deletion packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"sinon": "^9.2.4",
"ts-jest": "^27.1.3",
"ts-mock-imports": "^1.3.8",
"xml-js": "^1.6.11"
"xml-js": "^1.6.11",
"axios": "^0.25.0"
},
"dependencies": {
"@aws-cdk/cloud-assembly-schema": "0.0.0",
Expand Down
170 changes: 170 additions & 0 deletions packages/aws-cdk/test/integ/cli/cli.integtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as os from 'os';
import * as path from 'path';
import { retry, sleep } from '../helpers/aws';
import { cloneDirectory, MAJOR_VERSION, shell, withDefaultFixture } from '../helpers/cdk';
import { randomInteger, withSamIntegrationFixture } from '../helpers/sam';
import { integTest } from '../helpers/test-helpers';

jest.setTimeout(600 * 1000);
Expand Down Expand Up @@ -757,6 +758,175 @@ integTest('templates on disk contain metadata resource, also in nested assemblie
expect(JSON.parse(nestedTemplateContents).Resources.CDKMetadata).toBeTruthy();
}));

integTest('CDK synth add the metadata properties expected by sam', withSamIntegrationFixture(async (fixture) => {
// Synth first
await fixture.cdkSynth();

const template = fixture.template('TestStack');

const expectedResources = [
{
// Python Layer Version
id: 'PythonLayerVersion39495CEF',
cdkId: 'PythonLayerVersion',
isBundled: true,
property: 'Content',
},
{
// Layer Version
id: 'LayerVersion3878DA3A',
cdkId: 'LayerVersion',
isBundled: false,
property: 'Content',
buildMethod: 'python3.7',
},
{
// Bundled layer version
id: 'BundledLayerVersionPythonRuntime6BADBD6E',
cdkId: 'BundledLayerVersionPythonRuntime',
isBundled: true,
property: 'Content',
},
{
// Python Function
id: 'PythonFunction0BCF77FD',
cdkId: 'PythonFunction',
isBundled: true,
property: 'Code',
},
{
// Log Retention Function
id: 'LogRetentionaae0aa3c5b4d4f87b02d85b201efdd8aFD4BFC8A',
cdkId: 'LogRetentionaae0aa3c5b4d4f87b02d85b201efdd8a',
isBundled: false,
property: 'Code',
},
{
// Function
id: 'FunctionPythonRuntime28CBDA05',
cdkId: 'FunctionPythonRuntime',
isBundled: false,
property: 'Code',
},
{
// Bundled Function
id: 'BundledFunctionPythonRuntime4D9A0918',
cdkId: 'BundledFunctionPythonRuntime',
isBundled: true,
property: 'Code',
},
{
// NodeJs Function
id: 'NodejsFunction09C1F20F',
cdkId: 'NodejsFunction',
isBundled: true,
property: 'Code',
},
{
// Go Function
id: 'GoFunctionCA95FBAA',
cdkId: 'GoFunction',
isBundled: true,
property: 'Code',
},
{
// Docker Image Function
id: 'DockerImageFunction28B773E6',
cdkId: 'DockerImageFunction',
dockerFilePath: 'Dockerfile',
property: 'Code.ImageUri',
},
{
// Function with docker image asset
id: 'FunctionImageAsset68AA1754',
cdkId: 'FunctionImageAsset',
dockerFilePath: 'Dockerfile',
property: 'Code.ImageUri',
},
{
// Spec Rest Api
id: 'SpecRestAPI7D4B3A34',
cdkId: 'SpecRestAPI',
property: 'BodyS3Location',
},
];

for (const resource of expectedResources) {
expect(resource.id in template.Resources).toBeTruthy();
expect(template.Resources[resource.id]).toEqual(expect.objectContaining({
Metadata: {
'aws:cdk:path': `${fixture.fullStackName('TestStack')}/${resource.cdkId}/Resource`,
'aws:asset:path': expect.stringMatching(/asset\.[0-9a-zA-Z]{64}/),
'aws:asset:is-bundled': resource.isBundled,
'aws:asset:dockerfile-path': resource.dockerFilePath,
'aws:asset:property': resource.property,
'BuildMethod': resource.buildMethod,
},
}));
}

// Nested Stack
expect('NestedStackNestedStackNestedStackNestedStackResourceB70834FD' in template.Resources).toBeTruthy();
expect(template.Resources.NestedStackNestedStackNestedStackNestedStackResourceB70834FD).toEqual(expect.objectContaining({
Metadata: {
'aws:cdk:path': `${fixture.fullStackName('TestStack')}/NestedStack.NestedStack/NestedStack.NestedStackResource`,
'aws:asset:path': expect.stringMatching(`${fixture.stackNamePrefix.replace(/-/, '')}TestStackNestedStack[0-9A-Z]{8}\.nested\.template\.json`),
'aws:asset:property': 'TemplateURL',
},
}));
}));

integTest('sam can locally test the synthesized cdk application', withSamIntegrationFixture(async (fixture) => {
// Synth first
await fixture.cdkSynth();

await fixture.samBuild('TestStack');

const apisResponses = [
{
apiPath: '/httpapis/nestedPythonFunction',
expectedMessage: 'Hello World from Nested Python Function Construct 7',
},
{
apiPath: '/restapis/spec/pythonFunction',
expectedMessage: 'Hello World from python function construct 7',
},
{
apiPath: '/restapis/normal/functionPythonRuntime',
expectedMessage: 'Hello World from function construct with python runtime 7',
},
{
apiPath: '/restapis/normal/bundledFunctionPythonRuntime',
expectedMessage: 'Hello World from bundled function construct with python runtime 7',
},
{
apiPath: '/restapis/normal/nodejsFunction',
expectedMessage: 'Hello World from nodejs function construct 7',
},
{
apiPath: '/restapis/normal/goFunction',
expectedMessage: 'Hello World from go function construct',
},
{
apiPath: '/restapis/normal/dockerImageFunction',
expectedMessage: 'Hello World from docker image function construct',
},
{
apiPath: '/restapis/normal/functionImageAsset',
expectedMessage: 'Hello World from function construct with image asset',
},
];

for (const api of apisResponses) {
const result = await fixture.samLocalStartApi('TestStack', true, randomInteger(30000, 40000), api.apiPath);
expect(result.actionSucceeded).toBeTruthy();
expect(result.actionOutput).toEqual(expect.objectContaining({
message: api.expectedMessage,
}));
}

}));

async function listChildren(parent: string, pred: (x: string) => Promise<boolean>) {
const ret = new Array<string>();
for (const child of await fs.readdir(parent, { encoding: 'utf-8' })) {
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/test/integ/helpers/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const REGIONS = process.env.AWS_REGIONS
? process.env.AWS_REGIONS.split(',')
: [process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION ?? 'us-east-1'];

const FRAMEWORK_VERSION = process.env.FRAMEWORK_VERSION ?? '*';
export const FRAMEWORK_VERSION = process.env.FRAMEWORK_VERSION ?? '*';

export let MAJOR_VERSION = FRAMEWORK_VERSION.split('.')[0];
if (MAJOR_VERSION === '*') {
Expand Down Expand Up @@ -659,7 +659,7 @@ export function randomString() {
* symlinked from the TARGET directory's `node_modules` directory (which is sufficient
* for Node's dependency lookup mechanism).
*/
async function installNpmPackages(fixture: TestFixture, packages: Record<string, string>) {
export async function installNpmPackages(fixture: TestFixture, packages: Record<string, string>) {
if (process.env.REPO_ROOT) {
const monoRepo = await findYarnPackages(process.env.REPO_ROOT);

Expand Down
Loading

0 comments on commit c688f7c

Please sign in to comment.