Terraform
List Known Value Checks
The known value checks that are available for list values are:
ListExact
Check
The ListExact check tests that a resource attribute, or output value has an order-dependent, matching collection of element values.
Example usage of ListExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_List(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed list 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.ListExact([]knownvalue.Check{
knownvalue.StringExact("value1"),
knownvalue.StringExact("value2"),
}),
),
},
},
},
},
})
}
ListPartial
Check
The ListPartial check tests that a resource attribute, or output value has matching element values for the specified collection indices.
Example usage of ListPartial in an ExpectKnownValue plan check. In this example, only the first element within the list, the element defined at index 0
, is checked.
func TestExpectKnownValue_CheckPlan_ListPartial(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed list 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.ListPartial(map[int]knownvalue.Check{
0: knownvalue.StringExact("value1"),
}),
),
},
},
},
},
})
}
ListSizeExact
Check
The ListSizeExact check tests that a resource attribute, or output value contains the specified number of elements.
Example usage of ListSizeExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_ListElements(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed list 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.ListSizeExact(2),
),
},
},
},
},
})
}