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

86 lines
2.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"fmt"
"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) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccExampleResourceConfig("one"),
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
{
ResourceName: "scaffolding_example.test",
ImportState: true,
ImportStateVerify: true,
// This is not normally necessary, but is here because this
// example code does not have an actual upstream service.
// Once the Read method is able to refresh information from
// the upstream service, this can be removed.
ImportStateVerifyIgnore: []string{"configurable_attribute", "defaulted"},
},
// Update and Read testing
{
Config: testAccExampleResourceConfig("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
},
})
}
func testAccExampleResourceConfig(configurableAttribute string) string {
return fmt.Sprintf(`
resource "scaffolding_example" "test" {
configurable_attribute = %[1]q
}
`, configurableAttribute)
}