github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/transform_diff_test.go (about) 1 package terraform 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/zclconf/go-cty/cty" 8 9 "github.com/hashicorp/terraform-plugin-sdk/internal/addrs" 10 "github.com/hashicorp/terraform-plugin-sdk/internal/plans" 11 ) 12 13 func TestDiffTransformer_nilDiff(t *testing.T) { 14 g := Graph{Path: addrs.RootModuleInstance} 15 tf := &DiffTransformer{} 16 if err := tf.Transform(&g); err != nil { 17 t.Fatalf("err: %s", err) 18 } 19 20 if len(g.Vertices()) > 0 { 21 t.Fatal("graph should be empty") 22 } 23 } 24 25 func TestDiffTransformer(t *testing.T) { 26 g := Graph{Path: addrs.RootModuleInstance} 27 28 beforeVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String) 29 if err != nil { 30 t.Fatal(err) 31 } 32 afterVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 tf := &DiffTransformer{ 38 Changes: &plans.Changes{ 39 Resources: []*plans.ResourceInstanceChangeSrc{ 40 { 41 Addr: addrs.Resource{ 42 Mode: addrs.ManagedResourceMode, 43 Type: "aws_instance", 44 Name: "foo", 45 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance), 46 ProviderAddr: addrs.ProviderConfig{ 47 Type: "aws", 48 }.Absolute(addrs.RootModuleInstance), 49 ChangeSrc: plans.ChangeSrc{ 50 Action: plans.Update, 51 Before: beforeVal, 52 After: afterVal, 53 }, 54 }, 55 }, 56 }, 57 } 58 if err := tf.Transform(&g); err != nil { 59 t.Fatalf("err: %s", err) 60 } 61 62 actual := strings.TrimSpace(g.String()) 63 expected := strings.TrimSpace(testTransformDiffBasicStr) 64 if actual != expected { 65 t.Fatalf("bad:\n\n%s", actual) 66 } 67 } 68 69 const testTransformDiffBasicStr = ` 70 aws_instance.foo 71 `