github.com/leighwaller/terraform@v0.11.12-beta1/terraform/eval_diff_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/config" 9 ) 10 11 func TestEvalFilterDiff(t *testing.T) { 12 ctx := new(MockEvalContext) 13 14 cases := []struct { 15 Node *EvalFilterDiff 16 Input *InstanceDiff 17 Output *InstanceDiff 18 }{ 19 // With no settings, it returns an empty diff 20 { 21 &EvalFilterDiff{}, 22 &InstanceDiff{Destroy: true}, 23 &InstanceDiff{}, 24 }, 25 26 // Destroy 27 { 28 &EvalFilterDiff{Destroy: true}, 29 &InstanceDiff{Destroy: false}, 30 &InstanceDiff{Destroy: false}, 31 }, 32 { 33 &EvalFilterDiff{Destroy: true}, 34 &InstanceDiff{Destroy: true}, 35 &InstanceDiff{Destroy: true}, 36 }, 37 { 38 &EvalFilterDiff{Destroy: true}, 39 &InstanceDiff{ 40 Destroy: true, 41 Attributes: map[string]*ResourceAttrDiff{ 42 "foo": &ResourceAttrDiff{}, 43 }, 44 }, 45 &InstanceDiff{Destroy: true}, 46 }, 47 { 48 &EvalFilterDiff{Destroy: true}, 49 &InstanceDiff{ 50 Attributes: map[string]*ResourceAttrDiff{ 51 "foo": &ResourceAttrDiff{ 52 RequiresNew: true, 53 }, 54 }, 55 }, 56 &InstanceDiff{Destroy: true}, 57 }, 58 { 59 &EvalFilterDiff{Destroy: true}, 60 &InstanceDiff{ 61 Attributes: map[string]*ResourceAttrDiff{ 62 "foo": &ResourceAttrDiff{}, 63 }, 64 }, 65 &InstanceDiff{Destroy: false}, 66 }, 67 } 68 69 for i, tc := range cases { 70 var output *InstanceDiff 71 tc.Node.Diff = &tc.Input 72 tc.Node.Output = &output 73 if _, err := tc.Node.Eval(ctx); err != nil { 74 t.Fatalf("err: %s", err) 75 } 76 77 if !reflect.DeepEqual(output, tc.Output) { 78 t.Fatalf("bad: %d\n\n%#v", i, output) 79 } 80 } 81 } 82 83 func TestProcessIgnoreChanges(t *testing.T) { 84 var evalDiff *EvalDiff 85 var instanceDiff *InstanceDiff 86 87 var testDiffs = func(ignoreChanges []string, newAttribute string) (*EvalDiff, *InstanceDiff) { 88 return &EvalDiff{ 89 Resource: &config.Resource{ 90 Lifecycle: config.ResourceLifecycle{ 91 IgnoreChanges: ignoreChanges, 92 }, 93 }, 94 }, 95 &InstanceDiff{ 96 Destroy: true, 97 Attributes: map[string]*ResourceAttrDiff{ 98 "resource.%": { 99 Old: "3", 100 New: "3", 101 }, 102 "resource.changed": { 103 RequiresNew: true, 104 Type: DiffAttrInput, 105 Old: "old", 106 New: "new", 107 }, 108 "resource.maybe": { 109 Old: "", 110 New: newAttribute, 111 }, 112 "resource.same": { 113 Old: "same", 114 New: "same", 115 }, 116 }, 117 } 118 } 119 120 for i, tc := range []struct { 121 ignore []string 122 newAttr string 123 attrDiffs int 124 }{ 125 // attr diffs should be all (4), or nothing 126 { 127 ignore: []string{"resource.changed"}, 128 attrDiffs: 0, 129 }, 130 { 131 ignore: []string{"resource.changed"}, 132 newAttr: "new", 133 attrDiffs: 4, 134 }, 135 { 136 attrDiffs: 4, 137 }, 138 { 139 ignore: []string{"resource.maybe"}, 140 newAttr: "new", 141 attrDiffs: 4, 142 }, 143 { 144 newAttr: "new", 145 attrDiffs: 4, 146 }, 147 { 148 ignore: []string{"resource"}, 149 newAttr: "new", 150 attrDiffs: 0, 151 }, 152 // extra ignored values shouldn't effect the diff 153 { 154 ignore: []string{"resource.missing", "resource.maybe"}, 155 newAttr: "new", 156 attrDiffs: 4, 157 }, 158 // this isn't useful, but make sure it doesn't break 159 { 160 ignore: []string{"resource.%"}, 161 attrDiffs: 4, 162 }, 163 } { 164 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 165 evalDiff, instanceDiff = testDiffs(tc.ignore, tc.newAttr) 166 err := evalDiff.processIgnoreChanges(instanceDiff) 167 if err != nil { 168 t.Fatalf("err: %s", err) 169 } 170 if len(instanceDiff.Attributes) != tc.attrDiffs { 171 t.Errorf("expected %d diffs, found %d", tc.attrDiffs, len(instanceDiff.Attributes)) 172 for k, attr := range instanceDiff.Attributes { 173 fmt.Printf(" %s:%#v\n", k, attr) 174 } 175 } 176 }) 177 } 178 }