core: testDiffFn must populate old value for "type"

Previously testDiffFn was just assuming that the prior value for "type"
was always the empty string, but that doesn't hold if a mocked object is
updated in-place with a previously-populated value for type.

This wasn't a problem before because the old values in the diff were
largely just for presentation to the user, but we do now verify that the
old values match what we're applying to as an extra safety check and so
we must populate the old value properly.

This fix is verified by TestContext2Apply_Provisioner_Diff.
This commit is contained in:
Martin Atkins 2018-09-17 17:17:42 -07:00
parent e0cbdd0c4a
commit cf903b9bec
3 changed files with 14 additions and 7 deletions

View File

@ -5762,12 +5762,14 @@ func TestContext2Apply_Provisioner_Diff(t *testing.T) {
})
if _, diags := ctx.Plan(); diags.HasErrors() {
t.Fatalf("plan errors: %s", diags.Err())
logDiagnostics(t, diags)
t.Fatal("plan failed")
}
state, diags := ctx.Apply()
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
logDiagnostics(t, diags)
t.Fatal("apply failed")
}
actual := strings.TrimSpace(state.String())
@ -5778,7 +5780,7 @@ func TestContext2Apply_Provisioner_Diff(t *testing.T) {
// Verify apply was invoked
if !pr.ProvisionResourceCalled {
t.Fatalf("provisioner not invoked")
t.Fatalf("provisioner was not called on first apply")
}
pr.ProvisionResourceCalled = false
@ -5811,12 +5813,14 @@ func TestContext2Apply_Provisioner_Diff(t *testing.T) {
})
if _, diags := ctx.Plan(); diags.HasErrors() {
t.Fatalf("plan errors: %s", diags.Err())
logDiagnostics(t, diags)
t.Fatal("plan failed")
}
state2, diags := ctx.Apply()
if diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
logDiagnostics(t, diags)
t.Fatal("apply failed")
}
actual = strings.TrimSpace(state2.String())
@ -5826,7 +5830,7 @@ func TestContext2Apply_Provisioner_Diff(t *testing.T) {
// Verify apply was NOT invoked
if pr.ProvisionResourceCalled {
t.Fatalf("provisioner invoked")
t.Fatalf("provisioner was called on second apply; should not have been")
}
}

View File

@ -325,6 +325,9 @@ func testDiffFn(
Old: "",
New: info.Type,
}
if s != nil && s.Attributes != nil {
diff.Attributes["type"].Old = s.Attributes["type"]
}
}
return diff, nil

View File

@ -444,7 +444,7 @@ func (d *InstanceDiff) ApplyToValue(base cty.Value, schema *configschema.Block)
// if new or old is unknown, then there's no mismatch
old != config.UnknownVariableValue &&
diff.Old != config.UnknownVariableValue {
return base, fmt.Errorf("mismatched diff: %q != %q", old, diff.Old)
return base, fmt.Errorf("diff apply conflict for %s: diff expects %q, but prior value has %q", attr, diff.Old, old)
}
if diff.NewComputed {