Skip to content

integ-tests: awsApiCall assertion is not handling dates properly for GetMetricData with CloudWatch  #27962

@jose-clickup

Description

@jose-clickup

Describe the bug

When attempting to use the IntegTest(...).assertion.awsApiCall() method with CloudWatch and the GetMetricData function, it returns the error:

INFO	TypeError: input.StartTime.toISOString is not a function
    at serializeAws_queryGetMetricDataInput (/var/runtime/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:2506:48)
    at serializeAws_queryGetMetricDataCommand (/var/runtime/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:224:12)
    at serialize (/var/runtime/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/commands/GetMetricDataCommand.js:30:71)
    at /var/runtime/node_modules/@aws-sdk/middleware-serde/dist-cjs/serializerMiddleware.js:12:27
    at /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:28
    at CloudWatchClient.send (/var/runtime/node_modules/@aws-sdk/smithy-client/dist-cjs/client.js:20:20)
    at AwsApiCallHandler.processEvent (/var/task/index.js:32265:35)
    at AwsApiCallHandler.handle (/var/task/index.js:31915:37)
    at Runtime.handler (/var/task/index.js:32320:35)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1147:29)

The awsApiCall() method executes an API call from the Lambda custom resource generated by the assertion. Here, the lib parses the parameters it receives with the encode() function which converts all values to strings and pass them as string properties to the Lambda function.

The issue comes up when we use an API call that requires Date() types like in this case for the GetMetrciData request. For Date() types, it returns a JSON string representation of the timestamp (e.g. "2023-11-08T14:53:09.225Z"). As the Lambda function uses @aws-sdk to make the API call, it expects a Date() object that will be converted to ISO string, but in this scenario, it receives a string type, and because of this, it returns the error TypeError: input.StartTime.toISOString is not a function

Expected Behavior

Get a response from the AWS API after running the assertion integ.assertions.awsApiCall('CloudWatch', 'GetMetricData', params);****

Current Behavior

After running the integ.assertions.awsApiCall('CloudWatch', 'GetMetricData', params); the code fails because the Date() object is not parsed properly.
INFO TypeError: input.StartTime.toISOString is not a function

Reproduction Steps

  1. Create the following files:

test/integ.cloudwatch-call.ts

import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import { GetMetricDataCommandInput } from '@aws-sdk/client-cloudwatch' 
import { App } from 'aws-cdk-lib';

const app = new App();
const integ = new IntegTest(app, 'integ', {
    testCases: [],
});
const params: GetMetricDataCommandInput = {
    MetricDataQueries: [
        {
            Id: "id",
            Label: "label",
            MetricStat: {
                Metric: {
                    Namespace: "namespace",
                    MetricName: "metric",
                    Dimensions: [
                        {
                            Name: "key",
                            Value: "value",
                        },
                    ],
                },
                Period: 60,
                Stat: "MAX",
            },
        },
    ],
    StartTime: new Date(),
    EndTime: new Date(),
};
integ.assertions.awsApiCall('CloudWatch', 'GetMetricData', params);

package.json

{
  "name": "integ-tests",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-cdk/integ-runner": "^2.106.1-alpha.0",
    "@aws-cdk/integ-tests-alpha": "^2.106.1-alpha.0",
    "@aws-sdk/client-cloudwatch": "^3.449.0",
    "aws-cdk-lib": "^2.106.1",
    "constructs": "^10.3.0",
    "esbuild": "^0.19.5",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  }
}
  1. Run yarn install
  2. Run npx integ-runner --parallel-regions us-west-2 --update-on-failed

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.106.1

Framework Version

No response

Node.js Version

v18.17.1

OS

macOS Version 14.1 (23B74)

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions