Skip to content

Commit

Permalink
test(AWS Lambda): refactor EFS test to rely on infra (#8289)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Sep 25, 2020
1 parent 2eebf51 commit 3ed9779
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 89 deletions.
34 changes: 34 additions & 0 deletions scripts/test/integration-setup/cloudformation.yml
Expand Up @@ -137,6 +137,36 @@ Resources:
Arn: !Ref ClusterConfigurationArn
Revision: !Ref ClusterConfigurationRevision

FileSystem:
Type: AWS::EFS::FileSystem
Properties:
PerformanceMode: generalPurpose
FileSystemTags:
- Key: Name
Value: ServerlessFrameworkTestsVolume

MountTarget:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystem
SubnetId: !Ref PrivateSubnetA
SecurityGroups:
- !GetAtt VPC.DefaultSecurityGroup

AccessPointResource:
Type: AWS::EFS::AccessPoint
Properties:
FileSystemId: !Ref FileSystem
PosixUser:
Uid: 1001
Gid: 1001
RootDirectory:
CreationInfo:
OwnerGid: 1001
OwnerUid: 1001
Permissions: 770
Path: /efs

Outputs:
VPC:
Description: VPC ID
Expand All @@ -153,3 +183,7 @@ Outputs:
MSKCluster:
Description: Created MSK Cluster
Value: !Ref MSKCluster

EFSAccessPointARN:
Description: EFS Access Point ARN
Value: !GetAtt AccessPointResource.Arn
2 changes: 1 addition & 1 deletion test/fixtures/functionEfs/core.js
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs');

const filename = '/mnt/testing/file.txt';
const filename = process.env.FILENAME;

function writer(event, context, callback) {
fs.writeFileSync(filename, 'fromlambda', 'utf8');
Expand Down
60 changes: 0 additions & 60 deletions test/integration/fileSystemConfig/cloudformation.yml

This file was deleted.

@@ -1,14 +1,16 @@
'use strict';

const path = require('path');
const { expect } = require('chai');
const log = require('log').get('serverless:test');
const fixtures = require('../../fixtures');

const awsRequest = require('@serverless/test/aws-request');
const fs = require('fs');
const crypto = require('crypto');
const { deployService, removeService } = require('../../utils/integration');
const {
isDependencyStackAvailable,
getDependencyStackOutputMap,
} = require('../../utils/cludformation');

const EFS_MAX_PROPAGATION_TIME = 1000 * 60 * 5;

Expand All @@ -24,37 +26,30 @@ describe('AWS - FileSystemConfig Integration Test', function() {
let startTime;
let servicePath;
const stage = 'dev';
const resourcesStackName = `efs-integration-tests-deps-stack-${crypto
.randomBytes(8)
.toString('hex')}`;
const filename = `/mnt/testing/${crypto.randomBytes(8).toString('hex')}.txt`;

before(async () => {
const cfnTemplate = fs.readFileSync(path.join(__dirname, 'cloudformation.yml'), 'utf8');

log.notice('Deploying CloudFormation stack with required resources...');
await awsRequest('CloudFormation', 'createStack', {
StackName: resourcesStackName,
TemplateBody: cfnTemplate,
});
const waitForResult = await awsRequest('CloudFormation', 'waitFor', 'stackCreateComplete', {
StackName: resourcesStackName,
});
const isDepsStackAvailable = await isDependencyStackAvailable();
if (!isDepsStackAvailable) {
throw new Error('CloudFormation stack with integration test dependencies not found.');
}

const outputMap = waitForResult.Stacks[0].Outputs.reduce((map, output) => {
map[output.OutputKey] = output.OutputValue;
return map;
}, {});
const outputMap = await getDependencyStackOutputMap();

const fileSystemConfig = {
localMountPath: '/mnt/testing',
arn: outputMap.AccessPointARN,
arn: outputMap.get('EFSAccessPointARN'),
};

const serviceData = await fixtures.setup('functionEfs', {
configExt: {
provider: {
vpc: {
subnetIds: [outputMap.Subnet],
securityGroupIds: [outputMap.SecurityGroup],
subnetIds: [outputMap.get('PrivateSubnetA')],
securityGroupIds: [outputMap.get('SecurityGroup')],
},
environment: {
FILENAME: filename,
},
},
functions: { writer: { fileSystemConfig }, reader: { fileSystemConfig } },
Expand All @@ -69,12 +64,9 @@ describe('AWS - FileSystemConfig Integration Test', function() {
});

after(async () => {
await removeService(servicePath);
log.notice('Removing CloudFormation stack with required resources...');
await awsRequest('CloudFormation', 'deleteStack', { StackName: resourcesStackName });
return awsRequest('CloudFormation', 'waitFor', 'stackDeleteComplete', {
StackName: resourcesStackName,
});
if (servicePath) {
await removeService(servicePath);
}
});

it('should be able to write to efs and read from it in a separate function', async function self() {
Expand Down

0 comments on commit 3ed9779

Please sign in to comment.