example_resource_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) HashiCorp, Inc.
  2. // SPDX-License-Identifier: MPL-2.0
  3. package provider
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/hashicorp/terraform-plugin-testing/helper/resource"
  8. )
  9. func TestAccExampleResource(t *testing.T) {
  10. resource.Test(t, resource.TestCase{
  11. PreCheck: func() { testAccPreCheck(t) },
  12. ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
  13. Steps: []resource.TestStep{
  14. // Create and Read testing
  15. {
  16. Config: testAccExampleResourceConfig("one"),
  17. Check: resource.ComposeAggregateTestCheckFunc(
  18. resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "one"),
  19. resource.TestCheckResourceAttr("scaffolding_example.test", "defaulted", "example value when not configured"),
  20. resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"),
  21. ),
  22. },
  23. // ImportState testing
  24. {
  25. ResourceName: "scaffolding_example.test",
  26. ImportState: true,
  27. ImportStateVerify: true,
  28. // This is not normally necessary, but is here because this
  29. // example code does not have an actual upstream service.
  30. // Once the Read method is able to refresh information from
  31. // the upstream service, this can be removed.
  32. ImportStateVerifyIgnore: []string{"configurable_attribute", "defaulted"},
  33. },
  34. // Update and Read testing
  35. {
  36. Config: testAccExampleResourceConfig("two"),
  37. Check: resource.ComposeAggregateTestCheckFunc(
  38. resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "two"),
  39. ),
  40. },
  41. // Delete testing automatically occurs in TestCase
  42. },
  43. })
  44. }
  45. func testAccExampleResourceConfig(configurableAttribute string) string {
  46. return fmt.Sprintf(`
  47. resource "scaffolding_example" "test" {
  48. configurable_attribute = %[1]q
  49. }
  50. `, configurableAttribute)
  51. }