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

Integrate use of backend blocks in tests with skip_cleanup feature #36848

Draft
wants to merge 11 commits into
base: sarah/backend-skip-cleanup-validation
Choose a base branch
from

Conversation

SarahFrench
Copy link
Member

@SarahFrench SarahFrench commented Apr 4, 2025

This PR:

  • Adds validation to stop users setting skip_cleanup=false in a run block with a backend block
  • Adds tests for that validation, and also to show it's ok to set skip_cleanup=true in a run block with a backend block
  • Makes presence of a backend block enable skip cleanup behaviour, updates existing tests to reflect this (i.e. expecting resources to still exist at the end).
  • Stops a state artefact being made if a backend is in use (unless there's an error during cleanup)
  • Fixes a bug where the state wasn't loaded in properly from the backend
  • Improvements to tests - show that prior state is actually used and resources aren't deleted and recreated.

@SarahFrench SarahFrench changed the title Make inclusion of a backend block imply skip_cleanup=true, disallow setting skip_cleanup=false alongside a backend block. Integrate use of backend blocks in tests with skip_cleanup feature Apr 4, 2025
@SarahFrench SarahFrench added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Apr 4, 2025
Copy link
Member Author

@SarahFrench SarahFrench left a comment

Choose a reason for hiding this comment

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

Self review

Comment on lines +119 to 122
// We don't return destroyDiags here because the calling code sets the return code for the test operation
// based on whether the tests passed or not; cleanup is not a factor.
// Users will be aware of issues with cleanup due to destroyDiags being rendered to the View.
return nil
Copy link
Member Author

Choose a reason for hiding this comment

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

Is this accurate? Or would we update this to return destroyDiags?

Comment on lines +112 to +114
// We don't create a state artefact when the node state's corresponding run block has a backend,
// UNLESS an error occurs when returning the state to match that run block's config during cleanup.
return nil
Copy link
Member Author

Choose a reason for hiding this comment

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

This prevents the state artefact being made for the state key if there's a backend in play but only if there are no errors from the cleanup

Copy link
Member Author

Choose a reason for hiding this comment

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

The manifest file still exists and references an uncreated file. However this seems to be the expected behaviour for when no state artefacts are created during a test operation.

@@ -94,9 +94,11 @@ func (t *TestStateTransformer) Transform(g *terraform.Graph) error {
if err != nil {
return fmt.Errorf("error retrieving state for state key %q from backend: error retrieving state manager: %w", key, err)
}
stmgr.RefreshState()
Copy link
Member Author

Choose a reason for hiding this comment

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

Realising that I never read in the state from the state file (lol) made me realise that my tests weren't very effective previously. I've updated some tests in ways that detect that state is used or not (see non deterministic ID of test_resource being deterministic due to state in TestTest_UseOfBackends_priorStateUsedByBackend)

@SarahFrench
Copy link
Member Author

I'm happy to receive feedback on this PR, but work will be paused for about a week and I've returned it to draft. Instead please see the config validation work I split out of this PR.

@SarahFrench SarahFrench force-pushed the sarah/skip-cleanup-with-backend branch from 86e5c1f to 371a391 Compare April 6, 2025 08:02
@SarahFrench SarahFrench changed the base branch from f-controlling-destroys to sarah/backend-skip-cleanup-validation April 6, 2025 08:06
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.

1 participant