Skip to content

Commit

Permalink
add more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual committed Jul 16, 2024
1 parent 79dfa9f commit 2d11214
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
16 changes: 16 additions & 0 deletions packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ void describe('AmplifyFunctionFactory', () => {

template.hasResourceProperties('AWS::Events::Rule', {
ScheduleExpression: 'cron(*/5 * * * ? *)',
Targets: [
{
Arn: {
'Fn::GetAtt': ['handlerlambdaE29D1580', 'Arn'],

Check warning on line 350 in packages/backend-function/src/factory.test.ts

View workflow job for this annotation

GitHub Actions / lint

You have a misspelled word: handlerlambda on String
},
Id: 'Target0',
},
],
});
});

Expand All @@ -356,6 +364,14 @@ void describe('AmplifyFunctionFactory', () => {

template.hasResourceProperties('AWS::Events::Rule', {
ScheduleExpression: 'cron(0 1 * * ? *)',
Targets: [
{
Arn: {
'Fn::GetAtt': ['handlerlambdaE29D1580', 'Arn'],

Check warning on line 370 in packages/backend-function/src/factory.test.ts

View workflow job for this annotation

GitHub Actions / lint

You have a misspelled word: handlerlambda on String
},
Id: 'Target0',
},
],
});
});

Expand Down
5 changes: 5 additions & 0 deletions packages/integration-tests/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
* Used to run `create amplify` at a specific tagged release
*/
export const amplifyAtTag = 'amplify@latest';

/**
* Used as environment key for function to increment to keep track of number of invocations
*/
export const numInvocationsKey = 'NUM_INVOCATIONS';
13 changes: 6 additions & 7 deletions packages/integration-tests/src/test-e2e/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
amplifySharedSecretNameKey,
createAmplifySharedSecretName,
} from '../shared_secret.js';
import { numInvocationsKey } from '../constants.js';

const testProjectCreators = getTestProjectCreators();
const testCdkProjectCreators = getTestCdkProjectCreators();
Expand Down Expand Up @@ -130,14 +131,12 @@ void describe('deployment tests', { concurrency: testConcurrencyLevel }, () => {
});

void describe('in sequence', { concurrency: false }, () => {
const sharedSecretsEnv = {
const env = {
[amplifySharedSecretNameKey]: createAmplifySharedSecretName(),
[numInvocationsKey]: '0',
};
void it(`[${testProjectCreator.name}] deploys fully`, async () => {
await testProject.deploy(
sandboxBackendIdentifier,
sharedSecretsEnv
);
await testProject.deploy(sandboxBackendIdentifier, env);
await testProject.assertPostDeployment(sandboxBackendIdentifier);
});

Expand All @@ -146,7 +145,7 @@ void describe('deployment tests', { concurrency: testConcurrencyLevel }, () => {
['sandbox', '--once'],
testProject.projectDirPath,
{
env: sharedSecretsEnv,
env,
}
);
await processController
Expand All @@ -161,7 +160,7 @@ void describe('deployment tests', { concurrency: testConcurrencyLevel }, () => {
['sandbox', '--dirToWatch', 'amplify'],
testProject.projectDirPath,
{
env: sharedSecretsEnv,
env,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { HeadBucketCommand, S3Client } from '@aws-sdk/client-s3';
import { GetRoleCommand, IAMClient } from '@aws-sdk/client-iam';
import { AmplifyClient } from '@aws-sdk/client-amplify';
import { numInvocationsKey } from '../constants.js';

/**
* Creates test projects with data, storage, and auth categories.
Expand Down Expand Up @@ -140,11 +141,12 @@ class DataStorageAuthWithTriggerTestProject extends TestProjectBase {
amplifySharedSecretNameKey in environment
? environment[amplifySharedSecretNameKey]
: createAmplifySharedSecretName();
const sharedSecretEnvObject = {
const env = {
[amplifySharedSecretNameKey]: this.amplifySharedSecret,
[numInvocationsKey]: '0',
};
await this.setUpDeployEnvironment(backendIdentifier);
await super.deploy(backendIdentifier, sharedSecretEnvObject);
await super.deploy(backendIdentifier, env);
}

/**
Expand Down Expand Up @@ -235,7 +237,12 @@ class DataStorageAuthWithTriggerTestProject extends TestProjectBase {
await this.checkLambdaResponse(node16Lambda[0], expectedResponse);
await this.checkLambdaResponse(funcWithSsm[0], 'It is working');
await this.checkLambdaResponse(funcWithAwsSdk[0], 'It is working');
await this.checkLambdaResponse(funcWithSchedule[0], 'It is working');
await this.checkLambdaResponse(funcWithSchedule[0], 1);

// wait 1 minute and 10 seconds for schedule to invoke lambda again
await new Promise((resolve) => setTimeout(resolve, 1000 * 70));

await this.checkLambdaResponse(funcWithSchedule[0], 3);

const bucketName = await this.resourceFinder.findByBackendIdentifier(
backendId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* This function asserts that schedule functions are working properly
*/
export const handler = async () => {
let numInvocations = process.env.NUM_INVOCATIONS
? Number(process.env.NUM_INVOCATIONS)
: 100;
numInvocations++;
process.env.NUM_INVOCATIONS = numInvocations.toString();

return numInvocations;
};
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export const funcWithAwsSdk = defineFunction({

export const funcWithSchedule = defineFunction({
name: 'funcWithSchedule',
entry: './func-src/handler_with_ssm.ts',
schedule: 'every day',
entry: './func-src/handler_with_counter.ts',
schedule: '* * * * ?',
});

0 comments on commit 2d11214

Please sign in to comment.