core: Re-instate the ignore_changes processing tests

This commit is contained in:
Martin Atkins 2018-09-27 17:47:06 -07:00
parent 33d01a2ba2
commit 3b2834b8fc
2 changed files with 112 additions and 98 deletions

View File

@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/plans" "github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/plans/objchange" "github.com/hashicorp/terraform/plans/objchange"
"github.com/hashicorp/terraform/providers" "github.com/hashicorp/terraform/providers"
@ -219,7 +218,7 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
{ {
var moreDiags tfdiags.Diagnostics var moreDiags tfdiags.Diagnostics
plannedNewVal, moreDiags = n.processIgnoreChanges(schema, priorVal, plannedNewVal) plannedNewVal, moreDiags = n.processIgnoreChanges(priorVal, plannedNewVal)
diags = diags.Append(moreDiags) diags = diags.Append(moreDiags)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
return nil, diags.Err() return nil, diags.Err()
@ -417,7 +416,7 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
return nil, nil return nil, nil
} }
func (n *EvalDiff) processIgnoreChanges(schema *configschema.Block, prior, proposed cty.Value) (cty.Value, tfdiags.Diagnostics) { func (n *EvalDiff) processIgnoreChanges(prior, proposed cty.Value) (cty.Value, tfdiags.Diagnostics) {
// ignore_changes only applies when an object already exists, since we // ignore_changes only applies when an object already exists, since we
// can't ignore changes to a thing we've not created yet. // can't ignore changes to a thing we've not created yet.
if prior.IsNull() { if prior.IsNull() {
@ -439,6 +438,10 @@ func (n *EvalDiff) processIgnoreChanges(schema *configschema.Block, prior, propo
return proposed, nil return proposed, nil
} }
return processIgnoreChangesIndividual(prior, proposed, ignoreChanges)
}
func processIgnoreChangesIndividual(prior, proposed cty.Value, ignoreChanges []hcl.Traversal) (cty.Value, tfdiags.Diagnostics) {
// When we walk below we will be using cty.Path values for comparison, so // When we walk below we will be using cty.Path values for comparison, so
// we'll convert our traversals here so we can compare more easily. // we'll convert our traversals here so we can compare more easily.
ignoreChangesPath := make([]cty.Path, len(ignoreChanges)) ignoreChangesPath := make([]cty.Path, len(ignoreChanges))

View File

@ -2,106 +2,117 @@ package terraform
import ( import (
"testing" "testing"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
) )
func TestProcessIgnoreChanges(t *testing.T) { func TestProcessIgnoreChangesIndividual(t *testing.T) {
t.Fatalf("TestProcessIgnoreChanges not yet updated for new processIgnoreChanges signature") tests := map[string]struct {
/*var evalDiff *EvalDiff Old, New cty.Value
var instanceDiff *InstanceDiff Ignore []string
Want cty.Value
var testDiffs = func(t *testing.T, ignoreChanges []string, newAttribute string) (*EvalDiff, *InstanceDiff) { }{
ignoreChangesTravs := make([]hcl.Traversal, len(ignoreChanges)) "string": {
for i, s := range ignoreChanges { cty.ObjectVal(map[string]cty.Value{
traversal, travDiags := hclsyntax.ParseTraversalAbs([]byte(s), "", hcl.Pos{Line: 1, Column: 1}) "a": cty.StringVal("a value"),
if travDiags.HasErrors() { "b": cty.StringVal("b value"),
t.Fatal(travDiags.Error()) }),
} cty.ObjectVal(map[string]cty.Value{
ignoreChangesTravs[i] = traversal "a": cty.StringVal("new a value"),
} "b": cty.StringVal("new b value"),
}),
return &EvalDiff{ []string{"a"},
Config: &configs.Resource{ cty.ObjectVal(map[string]cty.Value{
Managed: &configs.ManagedResource{ "a": cty.StringVal("a value"),
IgnoreChanges: ignoreChangesTravs, "b": cty.StringVal("new b value"),
}, }),
}, },
}, "changed type": {
&InstanceDiff{ cty.ObjectVal(map[string]cty.Value{
Destroy: true, "a": cty.StringVal("a value"),
Attributes: map[string]*ResourceAttrDiff{ "b": cty.StringVal("b value"),
"resource.%": { }),
Old: "3", cty.ObjectVal(map[string]cty.Value{
New: "3", "a": cty.NumberIntVal(1),
}, "b": cty.StringVal("new b value"),
"resource.changed": { }),
RequiresNew: true, []string{"a"},
Type: DiffAttrInput, cty.ObjectVal(map[string]cty.Value{
Old: "old", "a": cty.StringVal("a value"),
New: "new", "b": cty.StringVal("new b value"),
}, }),
"resource.maybe": { },
Old: "", "list": {
New: newAttribute, cty.ObjectVal(map[string]cty.Value{
}, "a": cty.ListVal([]cty.Value{
"resource.same": { cty.StringVal("a0 value"),
Old: "same", cty.StringVal("a1 value"),
New: "same", }),
}, "b": cty.StringVal("b value"),
}, }),
} cty.ObjectVal(map[string]cty.Value{
"a": cty.ListVal([]cty.Value{
cty.StringVal("new a0 value"),
cty.StringVal("new a1 value"),
}),
"b": cty.StringVal("new b value"),
}),
[]string{"a"},
cty.ObjectVal(map[string]cty.Value{
"a": cty.ListVal([]cty.Value{
cty.StringVal("a0 value"),
cty.StringVal("a1 value"),
}),
"b": cty.StringVal("new b value"),
}),
},
"object attribute": {
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("a.foo value"),
"bar": cty.StringVal("a.bar value"),
}),
"b": cty.StringVal("b value"),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("new a.foo value"),
"bar": cty.StringVal("new a.bar value"),
}),
"b": cty.StringVal("new b value"),
}),
[]string{"a.bar"},
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("new a.foo value"),
"bar": cty.StringVal("a.bar value"),
}),
"b": cty.StringVal("new b value"),
}),
},
} }
for i, tc := range []struct { for name, test := range tests {
ignore []string t.Run(name, func(t *testing.T) {
newAttr string ignore := make([]hcl.Traversal, len(test.Ignore))
attrDiffs int for i, ignoreStr := range test.Ignore {
}{ trav, diags := hclsyntax.ParseTraversalAbs([]byte(ignoreStr), "", hcl.Pos{Line: 1, Column: 1})
// attr diffs should be all (4), or nothing if diags.HasErrors() {
{ t.Fatalf("failed to parse %q: %s", ignoreStr, diags.Error())
ignore: []string{"resource.changed"},
attrDiffs: 0,
},
{
ignore: []string{"resource.changed"},
newAttr: "new",
attrDiffs: 4,
},
{
attrDiffs: 4,
},
{
ignore: []string{"resource.maybe"},
newAttr: "new",
attrDiffs: 4,
},
{
newAttr: "new",
attrDiffs: 4,
},
{
ignore: []string{"resource"},
newAttr: "new",
attrDiffs: 0,
},
{
// extra ignored values shouldn't affect the diff
ignore: []string{"resource.missing", "resource.maybe"},
newAttr: "new",
attrDiffs: 4,
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
evalDiff, instanceDiff = testDiffs(t, tc.ignore, tc.newAttr)
err := evalDiff.processIgnoreChanges(instanceDiff)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(instanceDiff.Attributes) != tc.attrDiffs {
t.Errorf("expected %d diffs, found %d", tc.attrDiffs, len(instanceDiff.Attributes))
for k, attr := range instanceDiff.Attributes {
fmt.Printf(" %s:%#v\n", k, attr)
} }
ignore[i] = trav
}
ret, diags := processIgnoreChangesIndividual(test.Old, test.New, ignore)
if diags.HasErrors() {
t.Fatal(diags.Err())
}
if got, want := ret, test.Want; !want.RawEquals(got) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
} }
}) })
}*/ }
} }