Terraform
Map Known Value Checks
The known value checks that are available for map values are:
MapExact
Check
The MapExact check tests that a resource attribute, or output value has a key-specified, matching collection of element values.
Example usage of MapExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_Map(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed map attribute named "computed_attribute"
Config: `resource "test_resource" "one" {}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("computed_attribute"),
knownvalue.MapExact(map[string]knownvalue.Check{
"key1": knownvalue.StringExact("value1"),
"key2": knownvalue.StringExact("value2"),
}),
),
},
},
},
},
})
}
MapPartial
Check
The MapPartial check tests that a resource attribute, or output value has matching element values for the specified keys.
Example usage of MapPartial in an ExpectKnownValue plan check.
In this example, only the element associated with key1
within the map is checked.
func TestExpectKnownValue_CheckPlan_MapPartial(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed map attribute named "computed_attribute"
Config: `resource "test_resource" "one" {}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("computed_attribute"),
knownvalue.MapPartial(map[string]knownvalue.Check{
"key1": knownvalue.StringExact("value1"),
}),
),
},
},
},
},
})
}
MapSizeExact
Check
The MapSizeExact check tests that a resource attribute, or output value contains the specified number of elements.
Example usage of MapSizeExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_MapElements(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed map attribute named "computed_attribute"
Config: `resource "test_resource" "one" {}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("computed_attribute"),
knownvalue.MapSizeExact(2),
),
},
},
},
},
})
}