example_resource_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package provider
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/hashicorp/terraform-plugin-testing/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", "defaulted", "example value when not configured"),
  18. resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"),
  19. ),
  20. },
  21. // ImportState testing
  22. {
  23. ResourceName: "scaffolding_example.test",
  24. ImportState: true,
  25. ImportStateVerify: true,
  26. // This is not normally necessary, but is here because this
  27. // example code does not have an actual upstream service.
  28. // Once the Read method is able to refresh information from
  29. // the upstream service, this can be removed.
  30. ImportStateVerifyIgnore: []string{"configurable_attribute", "defaulted"},
  31. },
  32. // Update and Read testing
  33. {
  34. Config: testAccExampleResourceConfig("two"),
  35. Check: resource.ComposeAggregateTestCheckFunc(
  36. resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "two"),
  37. ),
  38. },
  39. // Delete testing automatically occurs in TestCase
  40. },
  41. })
  42. }
  43. func testAccExampleResourceConfig(configurableAttribute string) string {
  44. return fmt.Sprintf(`
  45. resource "scaffolding_example" "test" {
  46. configurable_attribute = %[1]q
  47. }
  48. `, configurableAttribute)
  49. }