aws-cloudformation / cloudformation-cli

The CloudFormation Provider Development Toolkit allows you to author your own resource providers and modules that can be used by CloudFormation.
Apache License 2.0
316 stars 157 forks source link

Contract test compare_model assertion error when integer property is defined as integer literal in input file #1063

Open AgustinBettati opened 3 months ago

AgustinBettati commented 3 months ago

Command being run

cfn test --function-name TestEntrypoint --verbose

Version

cfn 0.2.35

Resource type schema

{
  ...
  "definitions": {
    "ApiSearchDeploymentSpec": {
      "type": "object",
      "properties": {
        "InstanceSize": {
          "type": "string"
        },
        "NodeCount": {
          "type": "integer"
        }
      },
      "required": [
        "InstanceSize",
        "NodeCount"
      ],
      "additionalProperties": false
    }
  },
  "properties": { 
    "Specs": {
      "insertionOrder": false,
      "type": "array",
      "items": {
        "$ref": "#/definitions/ApiSearchDeploymentSpec",
        "type": "object"
      },
    }
  },
  ...
}

Contract test input file

{
  ...
  "Specs": [
    {
      "InstanceSize": "S30_HIGHCPU_NVME",
      "NodeCount": 3
    }
  ]
}

Unexpected error output

/opt/homebrew/Cellar/cloudformation-cli/0.2.35_1/libexec/lib/python3.12/site-packages/rpdk/core/contract/resource_client.py:498: in compare_model
raise AssertionError(assertion_error_message) from exception
E   AssertionError: All properties specified in the request MUST be present in the model returned, and they MUST match exactly, with the exception of properties defined as writeOnlyProperties in the resource schema
E    Request Model : {'Profile': 'default', 'ProjectId': '65251446ae5f3f6ec7968b13', 'ClusterName': 'Cluster0', 'Specs': [{'InstanceSize': 'S30_HIGHCPU_NVME', 'NodeCount': 3}]}
E    Returned Model : {'Profile': 'default', 'ClusterName': 'Cluster0', 'ProjectId': '65251446ae5f3f6ec7968b13', 'Specs': [{'InstanceSize': 'S30_HIGHCPU_NVME', 'NodeCount': '3'}]}
E   , inputs: {"Profile": "default", "ProjectId": "65251446ae5f3f6ec7968b13", "ClusterName": "Cluster0", "Specs": [{"InstanceSize": "S30_HIGHCPU_NVME", "NodeCount": 3}]}, outputs: {"Profile": "default", "ClusterName": "Cluster0", "ProjectId": "65251446ae5f3f6ec7968b13", "Specs": [{"InstanceSize": "S30_HIGHCPU_NVME", "NodeCount": "3"}]}

As a workaround, if the input file defines the integer property as a string the compare logic does not fail and contract tests are successful:

{
  ...
  "Specs": [
    {
      "InstanceSize": "S30_HIGHCPU_NVME",
      "NodeCount": "3"
    }
  ]
}