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" "testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource" "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) { func TestAccExampleDataSource(t *testing.T) {
@ -17,9 +20,13 @@ func TestAccExampleDataSource(t *testing.T) {
// Read testing // Read testing
{ {
Config: testAccExampleDataSourceConfig, Config: testAccExampleDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc( ConfigStateChecks: []statecheck.StateCheck{
resource.TestCheckResourceAttr("data.scaffolding_example.test", "id", "example-id"), 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"), Config: testAccExampleEphemeralResourceConfig("example"),
ConfigStateChecks: []statecheck.StateCheck{ 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" "testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource" "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" "github.com/hashicorp/terraform-plugin-testing/tfversion"
) )
@ -24,9 +26,12 @@ func TestExampleFunction_Known(t *testing.T) {
value = provider::scaffolding::example("testvalue") value = provider::scaffolding::example("testvalue")
} }
`, `,
Check: resource.ComposeAggregateTestCheckFunc( ConfigStateChecks: []statecheck.StateCheck{
resource.TestCheckOutput("test", "testvalue"), statecheck.ExpectKnownOutputValue(
), "test",
knownvalue.StringExact("testvalue"),
),
},
}, },
}, },
}) })
@ -69,9 +74,12 @@ func TestExampleFunction_Unknown(t *testing.T) {
value = provider::scaffolding::example(terraform_data.test.output) value = provider::scaffolding::example(terraform_data.test.output)
} }
`, `,
Check: resource.ComposeAggregateTestCheckFunc( ConfigStateChecks: []statecheck.StateCheck{
resource.TestCheckOutput("test", "testvalue"), statecheck.ExpectKnownOutputValue(
), "test",
knownvalue.StringExact("testvalue"),
),
},
}, },
}, },
}) })

View File

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