github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/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/internal/addrs" 10 "github.com/hashicorp/terraform/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.AbsProviderConfig{ 47 Provider: addrs.NewDefaultProvider("aws"), 48 Module: addrs.RootModule, 49 }, 50 ChangeSrc: plans.ChangeSrc{ 51 Action: plans.Update, 52 Before: beforeVal, 53 After: afterVal, 54 }, 55 }, 56 }, 57 }, 58 } 59 if err := tf.Transform(&g); err != nil { 60 t.Fatalf("err: %s", err) 61 } 62 63 actual := strings.TrimSpace(g.String()) 64 expected := strings.TrimSpace(testTransformDiffBasicStr) 65 if actual != expected { 66 t.Fatalf("bad:\n\n%s", actual) 67 } 68 } 69 70 func TestDiffTransformer_noOpChange(t *testing.T) { 71 // "No-op" changes are how we record explicitly in a plan that we did 72 // indeed visit a particular resource instance during the planning phase 73 // and concluded that no changes were needed, as opposed to the resource 74 // instance not existing at all or having been excluded from planning 75 // entirely. 76 // 77 // We must include nodes for resource instances with no-op changes in the 78 // apply graph, even though they won't take any external actions, because 79 // there are some secondary effects such as precondition/postcondition 80 // checks that can refer to objects elsewhere and so might have their 81 // results changed even if the resource instance they are attached to 82 // didn't actually change directly itself. 83 84 g := Graph{Path: addrs.RootModuleInstance} 85 86 beforeVal, err := plans.NewDynamicValue(cty.StringVal(""), cty.String) 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 tf := &DiffTransformer{ 92 Changes: &plans.Changes{ 93 Resources: []*plans.ResourceInstanceChangeSrc{ 94 { 95 Addr: addrs.Resource{ 96 Mode: addrs.ManagedResourceMode, 97 Type: "aws_instance", 98 Name: "foo", 99 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance), 100 ProviderAddr: addrs.AbsProviderConfig{ 101 Provider: addrs.NewDefaultProvider("aws"), 102 Module: addrs.RootModule, 103 }, 104 ChangeSrc: plans.ChangeSrc{ 105 // A "no-op" change has the no-op action and has the 106 // same object as both Before and After. 107 Action: plans.NoOp, 108 Before: beforeVal, 109 After: beforeVal, 110 }, 111 }, 112 }, 113 }, 114 } 115 if err := tf.Transform(&g); err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 actual := strings.TrimSpace(g.String()) 120 expected := strings.TrimSpace(testTransformDiffBasicStr) 121 if actual != expected { 122 t.Fatalf("bad:\n\n%s", actual) 123 } 124 } 125 126 const testTransformDiffBasicStr = ` 127 aws_instance.foo 128 `