-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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 Test: Allow skipping cleanup of entire test file or individual run blocks #36729
base: f-controlling-destroys
Are you sure you want to change the base?
Conversation
f121f50
to
88896d6
Compare
88896d6
to
7f4cb14
Compare
stateGetter := func(stateKey string) (*states.State, error) { | ||
backend := local.New() | ||
dir := filepath.Dir(stateKey) | ||
backendConfig := cty.ObjectVal(map[string]cty.Value{ | ||
"path": cty.StringVal(stateKey), | ||
"workspace_dir": cty.StringVal(dir), | ||
}) | ||
diags := backend.Configure(backendConfig) | ||
if diags.HasErrors() { | ||
return nil, fmt.Errorf("failed to configure backend: %s", diags) | ||
} | ||
|
||
mgr, err := backend.StateMgr("default") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create state manager: %s", err) | ||
} | ||
if err := mgr.RefreshState(); err != nil { | ||
return nil, fmt.Errorf("failed to refresh state: %s", err) | ||
} | ||
return mgr.State(), nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been learning about the state packages recently, and an easier way to implement code that does this is using a statemgr.Filesystem
:
// stateKey is value like ".terraform/test/TpQOqtev.tfstate"
sm := statemgr.NewFilesystem(stateKey)
err := sm.RefreshState()
if err != nil {
return nil, fmt.Errorf("error when reading state file: %w", err)
}
return sm.State(), err
Alternatively, I think your helper can be replaced with the pre-existing testStateRead function! I've just been refactoring some code to remove my own implementation of this helper also, and it looks like there are a number of helpers like this in the codebase 😂
Implements
skip_cleanup
, which allows skipping the cleanup of individual run blocks or the entire test file. This simply ensures that the cleanup stage for the state is an apply of the run requesting the skip, instead of the normal destroyTarget Release
1.12.x
CHANGELOG entry