Terraform
Set Known Value Checks
The known value checks that are available for set values are:
SetExact
Check
The SetExact check tests that a resource attribute, or output value has an order-independent, matching collection of element values.
Example usage of SetExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_Set(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed set 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.SetExact([]knownvalue.Check{
knownvalue.StringExact("value2"),
knownvalue.StringExact("value1"),
}),
),
},
},
},
},
})
}
SetPartial
Check
The SetPartial check tests that a resource attribute, or output value contains matching element values.
Example usage of SetPartial in an ExpectKnownValue plan check. In this example, only the one element within the set is checked.
func TestExpectKnownValue_CheckPlan_SetPartial(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed set 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.SetPartial([]knownvalue.Check{
knownvalue.StringExact("value2"),
}),
),
},
},
},
},
})
}
SetSizeExact
Check
The SetSizeExact check tests that a resource attribute, or output value contains the specified number of elements.
Example usage of SetSizeExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_SetElements(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed set 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.SetSizeExact(2),
),
},
},
},
},
})
}