Class: AWSCDK::Assertions::Template

Inherits:
Jsii::Object
  • Object
show all
Defined in:
assertions/template.rb

Overview

Suite of assertions that can be run on a CDK stack.

Typically used, as part of unit tests, to validate that the rendered CloudFormation template has expected resources and properties.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Template

Returns a new instance of Template.

Raises:

  • (NoMethodError)


11
12
13
# File 'assertions/template.rb', line 11

def initialize(*args)
  raise NoMethodError, "aws-cdk-lib.assertions.Template does not have a visible constructor; use the provided factory methods"
end

Class Method Details

.from_json(template, template_parsing_options = nil) ⇒ AWSCDK::Assertions::Template

Base your assertions from an existing CloudFormation template formatted as an in-memory JSON object.

Parameters:

  • template (Hash{String => Object})

    the CloudFormation template formatted as a nested set of records.

  • template_parsing_options (AWSCDK::Assertions::TemplateParsingOptions, nil) (defaults to: nil)

    Optional param to configure template parsing behavior, such as disregarding circular dependencies.

Returns:

  • (AWSCDK::Assertions::Template)


43
44
45
46
47
48
# File 'assertions/template.rb', line 43

def self.from_json(template, template_parsing_options = nil)
  Jsii::Type.check_type(template, JSON.parse(Base64.strict_decode64("eyJjb2xsZWN0aW9uIjp7ImVsZW1lbnR0eXBlIjp7InByaW1pdGl2ZSI6ImFueSJ9LCJraW5kIjoibWFwIn19")), "template")
  template_parsing_options = template_parsing_options.is_a?(Hash) ? ::AWSCDK::Assertions::TemplateParsingOptions.new(**template_parsing_options.transform_keys(&:to_sym)) : template_parsing_options
  Jsii::Type.check_type(template_parsing_options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hc3NlcnRpb25zLlRlbXBsYXRlUGFyc2luZ09wdGlvbnMifQ==")), "templateParsingOptions") unless template_parsing_options.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.assertions.Template", "fromJSON", [template, template_parsing_options])
end

.from_stack(stack, template_parsing_options = nil) ⇒ AWSCDK::Assertions::Template

Base your assertions on the CloudFormation template synthesized by a CDK Stack.

Parameters:

Returns:

  • (AWSCDK::Assertions::Template)


55
56
57
58
59
60
# File 'assertions/template.rb', line 55

def self.from_stack(stack, template_parsing_options = nil)
  Jsii::Type.check_type(stack, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5TdGFjayJ9")), "stack")
  template_parsing_options = template_parsing_options.is_a?(Hash) ? ::AWSCDK::Assertions::TemplateParsingOptions.new(**template_parsing_options.transform_keys(&:to_sym)) : template_parsing_options
  Jsii::Type.check_type(template_parsing_options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hc3NlcnRpb25zLlRlbXBsYXRlUGFyc2luZ09wdGlvbnMifQ==")), "templateParsingOptions") unless template_parsing_options.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.assertions.Template", "fromStack", [stack, template_parsing_options])
end

.from_string(template, template_parsing_options = nil) ⇒ AWSCDK::Assertions::Template

Base your assertions from an existing CloudFormation template formatted as a JSON string.

Parameters:

  • template (String)

    the CloudFormation template in.

  • template_parsing_options (AWSCDK::Assertions::TemplateParsingOptions, nil) (defaults to: nil)

    Optional param to configure template parsing behavior, such as disregarding circular dependencies.

Returns:

  • (AWSCDK::Assertions::Template)


67
68
69
70
71
72
# File 'assertions/template.rb', line 67

def self.from_string(template, template_parsing_options = nil)
  Jsii::Type.check_type(template, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "template")
  template_parsing_options = template_parsing_options.is_a?(Hash) ? ::AWSCDK::Assertions::TemplateParsingOptions.new(**template_parsing_options.transform_keys(&:to_sym)) : template_parsing_options
  Jsii::Type.check_type(template_parsing_options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hc3NlcnRpb25zLlRlbXBsYXRlUGFyc2luZ09wdGlvbnMifQ==")), "templateParsingOptions") unless template_parsing_options.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.assertions.Template", "fromString", [template, template_parsing_options])
end

.jsii_overridable_methodsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'assertions/template.rb', line 15

def self.jsii_overridable_methods
  {
    :all_resources => { kind: :method, name: "allResources", is_optional: false },
    :all_resources_properties => { kind: :method, name: "allResourcesProperties", is_optional: false },
    :find_conditions => { kind: :method, name: "findConditions", is_optional: false },
    :find_mappings => { kind: :method, name: "findMappings", is_optional: false },
    :find_outputs => { kind: :method, name: "findOutputs", is_optional: false },
    :find_parameters => { kind: :method, name: "findParameters", is_optional: false },
    :find_resources => { kind: :method, name: "findResources", is_optional: false },
    :get_resource_id => { kind: :method, name: "getResourceId", is_optional: false },
    :has_condition => { kind: :method, name: "hasCondition", is_optional: false },
    :has_mapping => { kind: :method, name: "hasMapping", is_optional: false },
    :has_output => { kind: :method, name: "hasOutput", is_optional: false },
    :has_parameter => { kind: :method, name: "hasParameter", is_optional: false },
    :has_resource => { kind: :method, name: "hasResource", is_optional: false },
    :has_resource_properties => { kind: :method, name: "hasResourceProperties", is_optional: false },
    :resource_count_is => { kind: :method, name: "resourceCountIs", is_optional: false },
    :resource_properties_count_is => { kind: :method, name: "resourcePropertiesCountIs", is_optional: false },
    :template_matches => { kind: :method, name: "templateMatches", is_optional: false },
    :to_json => { kind: :method, name: "toJSON", is_optional: false },
  }
end

Instance Method Details

#all_resources(type, props) ⇒ void

This method returns an undefined value.

Assert that all resources of the given type contain the given definition in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • type (String)

    the resource type;

  • props (Object)

    the entire definition of the resources as they should be expected in the template.



82
83
84
85
86
# File 'assertions/template.rb', line 82

def all_resources(type, props)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("allResources", [type, props])
end

#all_resources_properties(type, props) ⇒ void

This method returns an undefined value.

Assert that all resources of the given type contain the given properties CloudFormation template.

By default, performs partial matching on the Properties key of the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • type (String)

    the resource type;

  • props (Object)

    the 'Properties' section of the resource as should be expected in the template.



96
97
98
99
100
# File 'assertions/template.rb', line 96

def all_resources_properties(type, props)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("allResourcesProperties", [type, props])
end

#find_conditions(logical_id, props = nil) ⇒ Hash{String => Hash{String => Object}}

Get the set of matching Conditions that match the given properties in the CloudFormation template.

Parameters:

  • logical_id (String)

    the name of the condition, provide '*' to match all conditions in the template.

  • props (Object, nil) (defaults to: nil)

    by default, matches all Conditions in the template.

Returns:

  • (Hash{String => Hash{String => Object}})


107
108
109
110
111
# File 'assertions/template.rb', line 107

def find_conditions(logical_id, props = nil)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props") unless props.nil?
  jsii_call_method("findConditions", [logical_id, props])
end

#find_mappings(logical_id, props = nil) ⇒ Hash{String => Hash{String => Object}}

Get the set of matching Mappings that match the given properties in the CloudFormation template.

Parameters:

  • logical_id (String)

    the name of the mapping, provide '*' to match all mappings in the template.

  • props (Object, nil) (defaults to: nil)

    by default, matches all Mappings in the template.

Returns:

  • (Hash{String => Hash{String => Object}})


118
119
120
121
122
# File 'assertions/template.rb', line 118

def find_mappings(logical_id, props = nil)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props") unless props.nil?
  jsii_call_method("findMappings", [logical_id, props])
end

#find_outputs(logical_id, props = nil) ⇒ Hash{String => Hash{String => Object}}

Get the set of matching Outputs that match the given properties in the CloudFormation template.

Parameters:

  • logical_id (String)

    the name of the output, provide '*' to match all outputs in the template.

  • props (Object, nil) (defaults to: nil)

    by default, matches all Outputs in the template.

Returns:

  • (Hash{String => Hash{String => Object}})


129
130
131
132
133
# File 'assertions/template.rb', line 129

def find_outputs(logical_id, props = nil)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props") unless props.nil?
  jsii_call_method("findOutputs", [logical_id, props])
end

#find_parameters(logical_id, props = nil) ⇒ Hash{String => Hash{String => Object}}

Get the set of matching Parameters that match the given properties in the CloudFormation template.

Parameters:

  • logical_id (String)

    the name of the parameter, provide '*' to match all parameters in the template.

  • props (Object, nil) (defaults to: nil)

    by default, matches all Parameters in the template.

Returns:

  • (Hash{String => Hash{String => Object}})


140
141
142
143
144
# File 'assertions/template.rb', line 140

def find_parameters(logical_id, props = nil)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props") unless props.nil?
  jsii_call_method("findParameters", [logical_id, props])
end

#find_resources(type, props = nil) ⇒ Hash{String => Hash{String => Object}}

Get the set of matching resources of a given type and properties in the CloudFormation template.

Parameters:

  • type (String)

    the type to match in the CloudFormation template.

  • props (Object, nil) (defaults to: nil)

    by default, matches all resources with the given type.

Returns:

  • (Hash{String => Hash{String => Object}})


151
152
153
154
155
# File 'assertions/template.rb', line 151

def find_resources(type, props = nil)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props") unless props.nil?
  jsii_call_method("findResources", [type, props])
end

#get_resource_id(type, props = nil) ⇒ String

Get the Resource ID of a matching resource, expects only to find one match.

Throws AssertionError if none or multiple resources were found.

Parameters:

  • type (String)

    the resource type;

  • props (Object, nil) (defaults to: nil)

    by default, matches all resources with the given type.

Returns:

  • (String)

    The resource id of the matched resource. Performs a partial match via Match.objectLike().



164
165
166
167
168
# File 'assertions/template.rb', line 164

def get_resource_id(type, props = nil)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props") unless props.nil?
  jsii_call_method("getResourceId", [type, props])
end

#has_condition(logical_id, props) ⇒ void

This method returns an undefined value.

Assert that a Condition with the given properties exists in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • logical_id (String)

    the name of the mapping, provide '*' to match all conditions in the template.

  • props (Object)

    the output as should be expected in the template.



178
179
180
181
182
# File 'assertions/template.rb', line 178

def has_condition(logical_id, props)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("hasCondition", [logical_id, props])
end

#has_mapping(logical_id, props) ⇒ void

This method returns an undefined value.

Assert that a Mapping with the given properties exists in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • logical_id (String)

    the name of the mapping, provide '*' to match all mappings in the template.

  • props (Object)

    the output as should be expected in the template.



192
193
194
195
196
# File 'assertions/template.rb', line 192

def has_mapping(logical_id, props)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("hasMapping", [logical_id, props])
end

#has_output(logical_id, props) ⇒ void

This method returns an undefined value.

Assert that an Output with the given properties exists in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • logical_id (String)

    the name of the output, provide '*' to match all outputs in the template.

  • props (Object)

    the output as should be expected in the template.



206
207
208
209
210
# File 'assertions/template.rb', line 206

def has_output(logical_id, props)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("hasOutput", [logical_id, props])
end

#has_parameter(logical_id, props) ⇒ void

This method returns an undefined value.

Assert that a Parameter with the given properties exists in the CloudFormation template.

By default, performs partial matching on the parameter, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • logical_id (String)

    the name of the parameter, provide '*' to match all parameters in the template.

  • props (Object)

    the parameter as should be expected in the template.



220
221
222
223
224
# File 'assertions/template.rb', line 220

def has_parameter(logical_id, props)
  Jsii::Type.check_type(logical_id, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "logicalId")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("hasParameter", [logical_id, props])
end

#has_resource(type, props) ⇒ void

This method returns an undefined value.

Assert that a resource of the given type and given definition exists in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • type (String)

    the resource type;

  • props (Object)

    the entire definition of the resource as should be expected in the template.



234
235
236
237
238
# File 'assertions/template.rb', line 234

def has_resource(type, props)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("hasResource", [type, props])
end

#has_resource_properties(type, props) ⇒ void

This method returns an undefined value.

Assert that a resource of the given type and properties exists in the CloudFormation template.

By default, performs partial matching on the Properties key of the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.

Parameters:

  • type (String)

    the resource type;

  • props (Object)

    the 'Properties' section of the resource as should be expected in the template.



248
249
250
251
252
# File 'assertions/template.rb', line 248

def has_resource_properties(type, props)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  jsii_call_method("hasResourceProperties", [type, props])
end

#resource_count_is(type, count) ⇒ void

This method returns an undefined value.

Assert that the given number of resources of the given type exist in the template.

Parameters:

  • type (String)

    the resource type;

  • count (Numeric)

    number of expected instances.



259
260
261
262
263
# File 'assertions/template.rb', line 259

def resource_count_is(type, count)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(count, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJudW1iZXIifQ==")), "count")
  jsii_call_method("resourceCountIs", [type, count])
end

#resource_properties_count_is(type, props, count) ⇒ void

This method returns an undefined value.

Assert that the given number of resources of the given type and properties exists in the CloudFormation template.

Parameters:

  • type (String)

    the resource type;

  • props (Object)

    the 'Properties' section of the resource as should be expected in the template.

  • count (Numeric)

    number of expected instances.



271
272
273
274
275
276
# File 'assertions/template.rb', line 271

def resource_properties_count_is(type, props, count)
  Jsii::Type.check_type(type, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "type")
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "props")
  Jsii::Type.check_type(count, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJudW1iZXIifQ==")), "count")
  jsii_call_method("resourcePropertiesCountIs", [type, props, count])
end

#template_matches(expected) ⇒ void

This method returns an undefined value.

Assert that the CloudFormation template matches the given value.

Parameters:

  • expected (Object)

    the expected CloudFormation template as key-value pairs.



282
283
284
285
# File 'assertions/template.rb', line 282

def template_matches(expected)
  Jsii::Type.check_type(expected, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJhbnkifQ==")), "expected")
  jsii_call_method("templateMatches", [expected])
end

#to_jsonHash{String => Object}

The CloudFormation template deserialized into an object.

Returns:

  • (Hash{String => Object})


290
291
292
# File 'assertions/template.rb', line 290

def to_json()
  jsii_call_method("toJSON", [])
end