如何在 AWS::StepFunctions::StateMachine 中针对 DefinitionString 使用 CDK 细粒度断言?

Dav*_*vid 6 amazon-web-services aws-cdk

按照细粒度断言的方法,特别是https://docs.aws.amazon.com/cdk/v2/guide/testing.html#testing_fine_grainedAWS::StepFunctions::StateMachine中的 示例,我无法使用 来为自己工作状态机定义。Match.serializedJson()

这是我生成的 CloudFormation 模板的片段 -

  "StateMachine985JK630": {
   "Type": "AWS::StepFunctions::StateMachine",
   "Properties": {
    "RoleArn": {
     "Fn::GetAtt": [
      "StateMachineRoleJ4IK852F",
      "Arn"
     ]
    },
    "DefinitionString": {
     "Fn::Join": [
      "",
      [
       "{\"StartAt\":\"My Task One\",\"States\":{\"My Task One \":{\"Next\":\"My Task Two\",\"Retry\":[{\"ErrorEquals\":[\"Lambda.ServiceException\",\"Lambda.AWSLambdaException\",\"Lambda.SdkClientException\"],\"IntervalSeconds\":2,\"MaxAttempts\":6,\"BackoffRate\":2}],\"Type\":\"Task\",\"ResultPath\":\"$.Auth\",\"ResultSelector\":{\"authString.$\":\"$.Payload\"},\"Resource\":\"arn:",
       {
        "Ref": "AWS::Partition"
       },
       ":states:::lambda:invoke\",\"Parameters\":{\"FunctionName\":\"",
       {
        "Fn::GetAtt": [
         "MyTaskOne",
         "Arn"
        ]
       },
       "\",\"Payload.$\":\"$\"}},\"My Task Two\":{\"Next\":\"My Task Three\",\"Retry\"
       ...
      ]
...
Run Code Online (Sandbox Code Playgroud)

DefinitionString文档示例中的测试不处理Fn::Join上述 CloudFormation 模板中看到的数组或属性,该模板的粗略格式为DefinitionString: { 'Fn::Join': [ '', [Array] ] }. 我的下面的测试按原样工作,但是没有检查属性,例如StartAt它显然是无用的。

我的测试 -

// PASSES
it('should create My Task 1', () => {
      template.hasResourceProperties('AWS::StepFunctions::StateMachine', {
        DefinitionString: {
          'Fn::Join': [
            '',
            Match.arrayWith([
              // Match.serializedJson(Match.objectLike({ StartAt: 'My Task One' })), // FAILS
              // Match.serializedJson({ StartAt: 'My Task One' }) // FAILS
              // Match.objectLike(Match.serializedJson({ StartAt: 'My Task One' })) // FAILS
              {
                Ref: 'AWS::Partition',
              },
            ]),
          ],
        },
      });
    });
Run Code Online (Sandbox Code Playgroud)

我努力了

  • 完全按照文档中的方式提升示例(并将预期属性更改为我自己的),但失败了
Expected JSON as a string but found object at /Properties/DefinitionString
Run Code Online (Sandbox Code Playgroud)

考虑到 DefinitionString 的构成,这是有道理的。

  • 将任何注释掉的行添加到Match.arrayWith([])失败中
Missing element at pattern index 0 at /Properties/DefinitionString/Fn::Join[1] (using arrayWith matcher)
Run Code Online (Sandbox Code Playgroud)

但我不太明白这一点,因为我使用的函数是“类似”匹配。

  • 将整个字符串"{\"StartAt\":\"My Task One\"...直接粘贴到Match.arrayWith([])传递中,但远非理想。

我的问题是如何解析字符串和对象数组,然后仅匹配我想要的内容?任何指导将不胜感激。