terraform-provider-dokploy/internal/provider/example_function_test.go
Brian Flad c4ac215bff
Bump Go version, dependency versions, add example function (#198)
Reference: https://developer.hashicorp.com/terraform/plugin/framework/functions
Reference: https://github.com/hashicorp/terraform-providers-devex-internal/issues/175

Terraform 1.8 and later introduce the concept of provider-defined functions and this adds an example function that accepts a string argument and returns the same value as a string return.

Dependencies updated via:

```
go get github.com/hashicorp/terraform-plugin-framework@v1.6.0
go get github.com/hashicorp/terraform-plugin-docs@v0.18.0
go mod tidy
```
2024-02-28 10:11:33 -05:00

77 lines
1.9 KiB
Go

package provider
import (
"regexp"
"testing"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)
func TestExampleFunction_Known(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0"))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::scaffolding::example("testvalue")
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "testvalue"),
),
},
},
})
}
func TestExampleFunction_Null(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.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(version.Must(version.NewVersion("1.8.0"))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
resource "terraform_data" "test" {
input = "testvalue"
}
output "test" {
value = provider::scaffolding::example(terraform_data.test.output)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "testvalue"),
),
},
},
})
}