Terraform
Number Known Value Checks
The known value checks that are available for number values are:
NumberExact
Check
The NumberExact check tests that a resource attribute, or output value has an exactly matching number value.
Example usage of NumberExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_Number(t *testing.T) {
t.Parallel()
num, _, err := big.ParseFloat("1.797693134862315797693134862315797693134862315", 10, 512, big.ToNearestEven)
if err != nil {
t.Errorf("%s", err)
}
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed number 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.NumberExact(num),
),
},
},
},
},
})
}
NumberFunc
Check
The NumberFunc check allows defining a custom function to validate whether the number value of a resource attribute or output satisfies specific conditions.
Example usage of NumberFunc in an ExpectKnownValue state check.
func TestExpectKnownValue_CheckState_NumberFunc(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a number attribute named "configurable_attribute"
Config: `resource "test_resource" "one" {}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("configurable_attribute"),
knownvalue.NumberFunc(func(v *big.Float) error {
if err := testConfigurableAttribute(v); err != nil {
return fmt.Errorf("attribute validation failed: %w", err)
}
return nil
}),
),
},
},
},
})
}