example_resource_test.go 1.6 KB

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