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.
This commit is contained in:
Bruno Schaatsbergen 2025-01-22 17:03:57 +01:00 committed by GitHub
parent 2eaa836f99
commit 8934cc1ded
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 18 deletions

View File

@ -7,6 +7,9 @@ import (
"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/tfjsonpath"
)
func TestAccExampleDataSource(t *testing.T) {
@ -17,9 +20,13 @@ func TestAccExampleDataSource(t *testing.T) {
// Read testing
{
Config: testAccExampleDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.scaffolding_example.test", "id", "example-id"),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"data.scaffolding_example.test",
tfjsonpath.New("id"),
knownvalue.StringExact("example-id"),
),
},
},
},
})

View File

@ -26,7 +26,11 @@ func TestAccExampleEphemeralResource(t *testing.T) {
{
Config: testAccExampleEphemeralResourceConfig("example"),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue("echo.test", tfjsonpath.New("data").AtMapKey("value"), knownvalue.StringExact("token-123")),
statecheck.ExpectKnownValue(
"echo.test",
tfjsonpath.New("data").AtMapKey("value"),
knownvalue.StringExact("token-123"),
),
},
},
},

View File

@ -8,6 +8,8 @@ import (
"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"
)
@ -24,9 +26,12 @@ func TestExampleFunction_Known(t *testing.T) {
value = provider::scaffolding::example("testvalue")
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "testvalue"),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue(
"test",
knownvalue.StringExact("testvalue"),
),
},
},
},
})
@ -69,9 +74,12 @@ func TestExampleFunction_Unknown(t *testing.T) {
value = provider::scaffolding::example(terraform_data.test.output)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "testvalue"),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue(
"test",
knownvalue.StringExact("testvalue"),
),
},
},
},
})

View File

@ -8,6 +8,9 @@ import (
"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/tfjsonpath"
)
func TestAccExampleResource(t *testing.T) {
@ -18,11 +21,23 @@ func TestAccExampleResource(t *testing.T) {
// Create and Read testing
{
Config: testAccExampleResourceConfig("one"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "one"),
resource.TestCheckResourceAttr("scaffolding_example.test", "defaulted", "example value when not configured"),
resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"scaffolding_example.test",
tfjsonpath.New("id"),
knownvalue.StringExact("example-id"),
),
statecheck.ExpectKnownValue(
"scaffolding_example.test",
tfjsonpath.New("defaulted"),
knownvalue.StringExact("example value when not configured"),
),
statecheck.ExpectKnownValue(
"scaffolding_example.test",
tfjsonpath.New("configurable_attribute"),
knownvalue.StringExact("one"),
),
},
},
// ImportState testing
{
@ -38,9 +53,23 @@ func TestAccExampleResource(t *testing.T) {
// Update and Read testing
{
Config: testAccExampleResourceConfig("two"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "two"),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"scaffolding_example.test",
tfjsonpath.New("id"),
knownvalue.StringExact("example-id"),
),
statecheck.ExpectKnownValue(
"scaffolding_example.test",
tfjsonpath.New("defaulted"),
knownvalue.StringExact("example value when not configured"),
),
statecheck.ExpectKnownValue(
"scaffolding_example.test",
tfjsonpath.New("configurable_attribute"),
knownvalue.StringExact("two"),
),
},
},
// Delete testing automatically occurs in TestCase
},