Skip to content

Commit

Permalink
revise schedule integ test to have assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
samson-keung committed Oct 24, 2024
1 parent 9025add commit 01b387a
Showing 1 changed file with 56 additions and 54 deletions.
110 changes: 56 additions & 54 deletions packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/// !cdk-integ aws-cdk-scheduler-schedule
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import { ExpectedResult, IntegTest, Match } from '@aws-cdk/integ-tests-alpha';
import * as cdk from 'aws-cdk-lib';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as kms from 'aws-cdk-lib/aws-kms';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as scheduler from '../lib';

class SomeLambdaTarget implements scheduler.IScheduleTarget {
public constructor(private readonly fn: lambda.IFunction, private readonly role: iam.IRole) {
class SomeSqsTarget implements scheduler.IScheduleTarget {
public constructor(
private readonly queue: sqs.IQueue,
private readonly role: iam.IRole,
private readonly input?: scheduler.ScheduleTargetInput) {
}

public bind(): scheduler.ScheduleTargetConfig {
return {
arn: this.fn.functionArn,
arn: this.queue.queueArn,
role: this.role,
input: scheduler.ScheduleTargetInput.fromText('Input Text'),
input: this.input,
retryPolicy: {
maximumEventAgeInSeconds: 180,
maximumRetryAttempts: 3,
Expand All @@ -26,18 +29,28 @@ class SomeLambdaTarget implements scheduler.IScheduleTarget {

const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-cdk-scheduler-schedule');

const expression = scheduler.ScheduleExpression.rate(cdk.Duration.hours(12));
const func = new lambda.Function(stack, 'Function', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_LATEST,
});
const queue = new sqs.Queue(stack, 'ScheduleTargetQueue');
const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('scheduler.amazonaws.com'),
});

const target = new SomeLambdaTarget(func, role);
new scheduler.Schedule(stack, 'ScheduleToSendMessageToQueue', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
description: 'test description from the ScheduleToSendMessageToQueue',
target: new SomeSqsTarget(queue, role,
scheduler.ScheduleTargetInput.fromObject({
keyA: `valueA-${stack.region}`,
})),
});
const key = new kms.Key(stack, 'ScheduleKey');
new scheduler.Schedule(stack, 'ScheduleWithCMK', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: new SomeSqsTarget(queue, role,
scheduler.ScheduleTargetInput.fromObject({
keyB: `valueB-${stack.region}`,
})),
key,
});

const namedGroup = new scheduler.Group(stack, 'NamedGroup', {
groupName: 'TestGroup',
Expand All @@ -47,64 +60,53 @@ namedGroup.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
const unnamedGroup = new scheduler.Group(stack, 'UnnamedGroup', {});
unnamedGroup.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);

new scheduler.Schedule(stack, 'DefaultSchedule', {
schedule: expression,
target: target,
});

new scheduler.Schedule(stack, 'NamedGroupSchedule', {
schedule: expression,
target: target,
const randomTargetThatWontGetInvoked = new SomeSqsTarget(queue, role);
new scheduler.Schedule(stack, 'ScheduleToTestWithNamedGroup', {
schedule: scheduler.ScheduleExpression.at(new Date('2000-01-01T00:00:00Z')),
target: randomTargetThatWontGetInvoked,
group: namedGroup,
});

new scheduler.Schedule(stack, 'UnnamedGroupSchedule', {
schedule: expression,
target: target,
new scheduler.Schedule(stack, 'ScheduleToTestWithUnnamedGroup', {
schedule: scheduler.ScheduleExpression.at(new Date('2000-01-01T00:00:00Z')),
target: randomTargetThatWontGetInvoked,
group: unnamedGroup,
});

new scheduler.Schedule(stack, 'DisabledSchedule', {
schedule: expression,
target: target,
new scheduler.Schedule(stack, 'TestDisabledSchedule', {
schedule: scheduler.ScheduleExpression.at(new Date('2000-01-01T00:00:00Z')),
target: randomTargetThatWontGetInvoked,
enabled: false,
});

new cloudwatch.Alarm(stack, 'AllSchedulerErrorsAlarm', {
metric: scheduler.Schedule.metricAllErrors(),
threshold: 1,
evaluationPeriods: 1,
});

const key = new kms.Key(stack, 'ScheduleKey');
new scheduler.Schedule(stack, 'CustomerKmsSchedule', {
schedule: expression,
target: target,
key,
});

new scheduler.Schedule(stack, 'UseFlexibleTimeWindow', {
schedule: expression,
target: target,
schedule: scheduler.ScheduleExpression.at(new Date('2000-01-01T00:00:00Z')),
target: randomTargetThatWontGetInvoked,
timeWindow: scheduler.TimeWindow.flexible(cdk.Duration.minutes(10)),
});

const currentYear = new Date().getFullYear();
new scheduler.Schedule(stack, 'ScheduleWithTimeFrame', {
schedule: expression,
target: target,
start: new Date(`${currentYear + 1}-04-15T06:30:00.000Z`),
end: new Date(`${currentYear + 2}-10-01T00:00:00.000Z`),
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: randomTargetThatWontGetInvoked,
start: new Date('2060-04-15T06:30:00.000Z'),
end: new Date('2060-10-01T00:00:00.000Z'),
});

new scheduler.Schedule(stack, 'UseDescription', {
schedule: expression,
target: target,
description: 'test description',
new cloudwatch.Alarm(stack, 'AllSchedulerErrorsAlarm', {
metric: scheduler.Schedule.metricAllErrors(),
threshold: 1,
evaluationPeriods: 1,
});

new IntegTest(app, 'integtest-schedule', {
const integ = new IntegTest(app, 'integtest-schedule', {
testCases: [stack],
});

app.synth();
integ.assertions.awsApiCall('SQS', 'receiveMessage', {
QueueUrl: queue.queueUrl,
}).expect(ExpectedResult.objectLike({
Messages: Match.arrayWith([{ keyA: `valueA-${stack.region}` }, { keyB: `valueB-${stack.region}` }]),
})).waitForAssertions({
totalTimeout: cdk.Duration.minutes(10),
interval: cdk.Duration.seconds(20),
});

0 comments on commit 01b387a

Please sign in to comment.