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