@@ -5,13 +5,16 @@ import (
55 "fmt"
66 "strings"
77 "testing"
8+ "time"
89
910 "github.com/google/go-cmp/cmp"
1011 "github.com/hashicorp/go-cty/cty"
1112 "github.com/hashicorp/terraform-plugin-go/tfprotov5"
1213 "github.com/hashicorp/terraform-plugin-go/tfprotov6"
14+
1315 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1416 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17+ "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1518)
1619
1720func TestStepProviderConfig (t * testing.T ) {
@@ -342,6 +345,75 @@ func TestTest_TestStep_ExternalProviders_To_ProviderFactories_StateUpgraders(t *
342345 })
343346}
344347
348+ func TestTest_TestStep_Taint (t * testing.T ) {
349+ t .Parallel ()
350+
351+ var idOne , idTwo string
352+
353+ Test (t , TestCase {
354+ ProviderFactories : map [string ]func () (* schema.Provider , error ){
355+ "random" : func () (* schema.Provider , error ) { //nolint:unparam // required signature
356+ return & schema.Provider {
357+ ResourcesMap : map [string ]* schema.Resource {
358+ "random_id" : {
359+ CreateContext : func (_ context.Context , d * schema.ResourceData , _ interface {}) diag.Diagnostics {
360+ d .SetId (time .Now ().String ())
361+ return nil
362+ },
363+ DeleteContext : func (_ context.Context , _ * schema.ResourceData , _ interface {}) diag.Diagnostics {
364+ return nil
365+ },
366+ ReadContext : func (_ context.Context , _ * schema.ResourceData , _ interface {}) diag.Diagnostics {
367+ return nil
368+ },
369+ Schema : map [string ]* schema.Schema {},
370+ },
371+ },
372+ }, nil
373+ },
374+ },
375+ Steps : []TestStep {
376+ {
377+ Config : `resource "random_id" "test" {}` ,
378+ Check : ComposeAggregateTestCheckFunc (
379+ extractResourceAttr ("random_id.test" , "id" , & idOne ),
380+ ),
381+ },
382+ {
383+ Taint : []string {"random_id.test" },
384+ Config : `resource "random_id" "test" {}` ,
385+ Check : ComposeAggregateTestCheckFunc (
386+ extractResourceAttr ("random_id.test" , "id" , & idTwo ),
387+ ),
388+ },
389+ },
390+ })
391+
392+ if idOne == idTwo {
393+ t .Errorf ("taint is not causing destroy-create cycle, idOne == idTwo: %s == %s" , idOne , idTwo )
394+ }
395+ }
396+
397+ func extractResourceAttr (resourceName string , attributeName string , attributeValue * string ) TestCheckFunc {
398+ return func (s * terraform.State ) error {
399+ rs , ok := s .RootModule ().Resources [resourceName ]
400+
401+ if ! ok {
402+ return fmt .Errorf ("resource name %s not found in state" , resourceName )
403+ }
404+
405+ attrValue , ok := rs .Primary .Attributes [attributeName ]
406+
407+ if ! ok {
408+ return fmt .Errorf ("attribute %s not found in resource %s state" , attributeName , resourceName )
409+ }
410+
411+ * attributeValue = attrValue
412+
413+ return nil
414+ }
415+ }
416+
345417func TestTest_TestStep_ProtoV5ProviderFactories (t * testing.T ) {
346418 t .Parallel ()
347419
0 commit comments