Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform: remove state from validate graph walk #26063

Merged
merged 3 commits into from
Aug 31, 2020

Conversation

mildwonkey
Copy link
Contributor

This pull reverts a recent change to backend/local which created two context, one with and one without state. Instead I have removed the state entirely from the validate graph (by explicitly passing a states.NewState()).

This changed caused a test failure, which (ty so much for the help) @jbardin discovered was inaccurate all along: the test's call to Validate() was actually what was removing the output from state, which should not have been happening during Validate anyway. The new expected test output matches terraform's actual behavior on the command line: if you use -target to destroy a resource, an output that references only that resource is not removed from state.

This includes two tests to cover the expected behavior:

Fixes #26035, #26027

A recent change to the backend had a side effect of causing all input
vars to be requested twice (due to two calls to `contextDirect`) and
provider input values to be omitted entirely (those were populated
during Validate, but Validate was called on the "extra" context and not
the returned context).

This PR adds a Context.StatelessCopy function which returns a nearly-identical copy of
a terraform.Context with the state "zeroed out", which can then be
Validate()d. A better, future refactor should remove the need for this
function and instead modify Validate() itself to ignore state.

Fixes #26027

I've added tests that confirm both behaviors; I am not sure what test
covers the behavior that lead to this change in the first place but I
did verify locally using the reproduction in the original PR.
@codecov
Copy link

codecov bot commented Aug 31, 2020

Codecov Report

Merging #26063 into master will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted Files Coverage Δ
backend/local/backend_local.go 41.23% <100.00%> (-0.90%) ⬇️
terraform/context.go 86.46% <100.00%> (+0.46%) ⬆️
terraform/eval_count_boundary.go 68.96% <0.00%> (-10.35%) ⬇️
command/ui_input.go 61.03% <0.00%> (-5.20%) ⬇️
terraform/evaluate.go 53.05% <0.00%> (-1.32%) ⬇️

@mildwonkey mildwonkey requested a review from a team August 31, 2020 16:02
Comment on lines +568 to +570
// This will (helpfully) panic if more than one variable is requested during plan:
// https://github.com/hashicorp/terraform/issues/26027
close := testInteractiveInput(t, []string{"bar"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment!

Copy link
Member

@jbardin jbardin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for finding a better spot to remove the state from Validate.

// from the targeted resource
if len(mod.OutputValues) != 0 {
t.Fatalf("expected 0 outputs, got: %#v", mod.OutputValues)
// the root output should not get removed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change this comment to indicate that we're verifying the current status-quo, but it's not necessarily incorrect to remove the output if we can manage it in the future.

@mildwonkey mildwonkey merged commit 196c183 into master Aug 31, 2020
@mildwonkey mildwonkey deleted the mildwonkey/b-input-vars branch August 31, 2020 19:49
@ghost
Copy link

ghost commented Oct 13, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

terraform not properly storing provider args from command line when there is no provider block
3 participants