Skip to content

Terraform Test: cleanup command #36847

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

Open
wants to merge 13 commits into
base: f-controlling-destroys
Choose a base branch
from

Conversation

dsa0x
Copy link
Member

@dsa0x dsa0x commented Apr 4, 2025

Fixes #

Target Release

1.13.x

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

@dsa0x dsa0x added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Apr 4, 2025
@dsa0x dsa0x force-pushed the sams/cmd-test-cleanup branch from daac5b0 to b0b4ac2 Compare April 14, 2025 14:16
@dsa0x dsa0x marked this pull request as ready for review April 14, 2025 14:18
@dsa0x dsa0x requested a review from a team as a code owner April 14, 2025 14:18
Copy link
Member

@liamcervante liamcervante left a comment

Choose a reason for hiding this comment

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

Nice! I like how you managed to reuse the same nodes from the main test command!

run "test_two" {
skip_cleanup = true # This will leave behind the state
variables {
id = "test_two"
Copy link
Member

Choose a reason for hiding this comment

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

Could you add another new test case, similar to this one, where a variable depends on a prior run block. I think this implementation might not work for that case as we won't be able to retrieve the required input variable in that case.

We might need to preserve the input variables for each run block in the manifest file as well so we can retrieve them, or even have to execute the earlier run blocks to get the input variables again in the case of ephemeral or sensitive vars.

We could also look into this in a future PR before launch.

Copy link
Member Author

@dsa0x dsa0x Apr 16, 2025

Choose a reason for hiding this comment

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

I see, that raises an important point worth documenting. However, It might feel a bit unintuitive for users that we execute runs when trying to clean up the state.

Do we even need the variables for a destroy plan? Besides provider config, The required inputs for destruction are probably already computed. Maybe we could provide just the state and an empty config to the plan. That should be enough to destroy everything defined in the state, right?
The striken-out text just feels like it will lead to other complication

Copy link
Member

Choose a reason for hiding this comment

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

Terraform requires values for all non-optional input variables in all plan operations (normal, refresh, and destroy). It only actually uses the ones that end up in provider configurations but it doesn't know up front which ones are needed for what so it just requires all of them.

The same problem would be true for provider configs in the test file as well. If we need to initialise any providers in order to run the destroy, they might refer to outputs of previous test runs which will need to be recomputed.

Copy link
Member Author

@dsa0x dsa0x Apr 16, 2025

Choose a reason for hiding this comment

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

You're right that the non-optional values are needed regardless.
I still have some doubts about the new command executing runs. Is there a clear distinction between what it does and the normal test command then?

In the new command, we will have to also execute the runs, and then cleanup after them. This is something that we do in the normal mode anyway.

Perhaps one way to see it is that the new command is just a way to execute runs without reporting their failure. If that is the case, then should it be a flag?

Also, the new command cleanup might fail and leave new state behind, but I guess you can simply rerun it

Copy link
Member Author

@dsa0x dsa0x Apr 16, 2025

Choose a reason for hiding this comment

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

We might need to preserve the input variables for each run block in the manifest file as well so we can retrieve them

I doubt that this is sufficient, because the references in subsequent blocks may be a computed output. For that reason, prior run execution makes better sense.

Nevertheless, that is not without problems. For example, if a prior run continue to fail, then we can never clean up the state that depends on it. Perhaps that is acceptable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe the problem is that we are trying to shoehorn the cleanup workflow along with the main test workflow. Should there be clearer separation, and a different way to provide input variables for the destroy plan of the left-over state?

Copy link
Member

Choose a reason for hiding this comment

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

This is probably complicated enough that we could discuss it offline? We can then resolve the variable question in a follow up PR prior to launch?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good

@dsa0x dsa0x force-pushed the sams/docs-and-test branch from 576420a to c760feb Compare April 16, 2025 10:22
@dsa0x dsa0x requested a review from a team as a code owner April 16, 2025 10:22
Base automatically changed from sams/docs-and-test to f-controlling-destroys April 16, 2025 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog-needed Add this to your PR if the change does not require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants