Terraform
Bool Known Value Checks
The known value checks that are available for bool values are:
Bool
Check
The Bool check tests that a resource attribute, or output value has an exactly matching bool value.
Example usage of Bool in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_Bool(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed boolean 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.Bool(true),
),
},
},
},
},
})
}
BoolFunc
Check
The BoolFunc check allows defining a custom function to validate whether the bool value of a resource attribute or output satisfies specific conditions.
Example usage of BoolFunc in an ExpectKnownValue state check.
func TestExpectKnownValue_CheckState_BoolFunc(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a boolean attribute named "configurable_attribute"
Config: `resource "test_resource" "one" {}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("configurable_attribute"),
knownvalue.BoolFunc(func(v bool) error {
if !v {
return fmt.Errorf("expected true, got %t", v)
}
return nil
}),
),
},
},
},
})
}