From 9d08774f56a3618da98722ac006e768ec11e828d Mon Sep 17 00:00:00 2001 From: maz Date: Tue, 18 Jun 2024 10:11:19 +0900 Subject: [PATCH] fix: add validation --- .../aws-cdk-lib/aws-synthetics/lib/canary.ts | 5 +++++ .../aws-synthetics/test/canary.test.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/aws-cdk-lib/aws-synthetics/lib/canary.ts b/packages/aws-cdk-lib/aws-synthetics/lib/canary.ts index b48cdb82649e6..deadfd1cdcd4d 100644 --- a/packages/aws-cdk-lib/aws-synthetics/lib/canary.ts +++ b/packages/aws-cdk-lib/aws-synthetics/lib/canary.ts @@ -571,6 +571,11 @@ export class Canary extends cdk.Resource implements ec2.IConnectable { let timeoutInSeconds: number | undefined; if (!cdk.Token.isUnresolved(props.timeout) && props.timeout !== undefined) { + const timeoutInMillis = props.timeout?.toMilliseconds(); + if (timeoutInMillis % 1000 !== 0) { + throw new Error(`\`timeout\` must be set as an integer representing seconds, got ${timeoutInMillis} milliseconds.`); + } + timeoutInSeconds = props.timeout.toSeconds(); if (timeoutInSeconds < 3 || timeoutInSeconds > 840) { throw new Error(`\`timeout\` must be between 3 seconds and 840 seconds, got ${timeoutInSeconds} seconds.`); diff --git a/packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts b/packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts index 5d70ab0c3a8d4..7c29bcf5e6026 100644 --- a/packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts +++ b/packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts @@ -396,6 +396,22 @@ test('timeout can be set', () => { }); }); +test.each([100, 3100])('throws when timeout is not set as an integer representing seconds , %d milliseconds', (milliseconds: number) => { + // GIVEN + const stack = new Stack(); + + // WHEN + expect(() => new synthetics.Canary(stack, 'Canary', { + runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_7_0, + test: synthetics.Test.custom({ + handler: 'index.handler', + code: synthetics.Code.fromInline('/* Synthetics handler code */'), + }), + timeout: Duration.millis(milliseconds), + })) + .toThrow(`\`timeout\` must be set as an integer representing seconds, got ${milliseconds} milliseconds.`); +}); + test.each([2, 900])('throws when timeout is out of range, %d seconds', (seconds: number) => { // GIVEN const stack = new Stack();