hashicorp / terraform-plugin-testing

Module for testing Terraform providers
Mozilla Public License 2.0
45 stars 11 forks source link

helper/resource: run Check even when apply fails in TestStep #76

Open mingzhaodotname opened 4 years ago

mingzhaodotname commented 4 years ago

SDK version

1.6.0

Use-cases

We want to test the behavior when an Update fails.

Currently, we can only use ExpectError to check the error message. However, if we want to check more things, e.g. what's the value of a given attributes in tfstate, it is not possible because "Check" function is not run when there are errors, see: https://github.com/hashicorp/terraform-plugin-sdk/blob/7f53d13be6bdb4b24d447c26d66bcf8888c5be90/helper/resource/testing_config.go#L99

Proposal

Run the Check function before returning the Update failures, e.g.

        state, err = shimNewState(newState, step.providers)
        if err != nil {
            return nil, err
        }

        // Run any configured checks
        if step.Check != nil {
            if step.Destroy {
                if err := step.Check(stateBeforeApplication); err != nil {
                    return state, fmt.Errorf("Check failed: %s", err)
                }
            } else {
                if err := step.Check(state); err != nil {
                    return state, fmt.Errorf("Check failed: %s", err)
                }
            }
        }

        if stepDiags.HasErrors() {
            return state, newOperationError("apply", stepDiags)
        }
bflad commented 1 year ago

Hi folks 👋

With the introduction of the terraform-plugin-testing module (migration guide), we are transferring feature requests relating to the helper/resource "testing framework" in the terraform-plugin-sdk repository over to the new terraform-plugin-testing repository.

Thanks again for this feature request.