Bruno Schaatsbergen 8934cc1ded
provider: migrate to the new plugin testing API (#266)
Migrating from the legacy plugin testing API to the new plan and state check API, which provides improved testing capabilities.
2025-01-22 11:03:57 -05:00

87 lines
2.1 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)
func TestExampleFunction_Known(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::scaffolding::example("testvalue")
}
`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue(
"test",
knownvalue.StringExact("testvalue"),
),
},
},
},
})
}
func TestExampleFunction_Null(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::scaffolding::example(null)
}
`,
// The parameter does not enable AllowNullValue
ExpectError: regexp.MustCompile(`argument must not be null`),
},
},
})
}
func TestExampleFunction_Unknown(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
resource "terraform_data" "test" {
input = "testvalue"
}
output "test" {
value = provider::scaffolding::example(terraform_data.test.output)
}
`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue(
"test",
knownvalue.StringExact("testvalue"),
),
},
},
},
})
}