core: Update working state for resource instances in refresh-only mode

Previously in refresh-only mode we were skipping making any updates to the
working state at all. That's not correct, though: if the state upgrade or
refresh steps detected changes then we need to at least commit _those_ to
the working state, because those can then be detected by downstream
objects like output values.
This commit is contained in:
Martin Atkins 2021-05-07 15:50:11 -07:00
parent 0f936b9d80
commit 0ee76b92b8
2 changed files with 31 additions and 0 deletions

View File

@ -740,6 +740,10 @@ func TestContext2Plan_refreshOnlyMode(t *testing.T) {
resource "test_object" "a" {
arg = "after"
}
output "out" {
value = test_object.a.arg
}
`,
})
state := states.BuildState(func(s *states.SyncState) {
@ -838,6 +842,23 @@ func TestContext2Plan_refreshOnlyMode(t *testing.T) {
t.Errorf("%s has wrong previous run state after plan\ngot:\n%s\n\nwant substring: %s", addr, got, want)
}
}
// The output value should also have updated. If not, it's likely that we
// skipped updating the working state to match the refreshed state when we
// were evaluating the resource.
if outChangeSrc := plan.Changes.OutputValue(addrs.RootModuleInstance.OutputValue("out")); outChangeSrc == nil {
t.Errorf("no change planned for output value 'out'")
} else {
outChange, err := outChangeSrc.Decode()
if err != nil {
t.Fatalf("failed to decode output value 'out': %s", err)
}
got := outChange.After
want := cty.StringVal("current")
if !want.RawEquals(got) {
t.Errorf("wrong value for output value 'out'\ngot: %#v\nwant: %#v", got, want)
}
}
}
func TestContext2Plan_invalidSensitiveModuleOutput(t *testing.T) {

View File

@ -226,6 +226,16 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
}
diags = diags.Append(n.writeChange(ctx, change, ""))
} else {
// Even if we don't plan changes, we do still need to at least update
// the working state to reflect the refresh result. If not, then e.g.
// any output values refering to this will not react to the drift.
// (Even if we didn't actually refresh above, this will still save
// the result of any schema upgrading we did in readResourceInstanceState.)
diags = diags.Append(n.writeResourceInstanceState(ctx, instanceRefreshState, workingState))
if diags.HasErrors() {
return diags
}
}
return diags