hashicorp / hashicorp/terraform-plugin-sdk

ExpectError is ignored for test case where destroy action expected to have error

Open
#609 14 comments 7 reactions 1 assignee Claimed by @bflad View on GitHub
bug subsystem/tests
Dominant language
Go
Stars
485
Forks
244
Avg merge
19h 57m
Merged PRs (30d)
4

Description

Maybe I'm doing this wrong?

### SDK version
```
{
"Path": "github.com/hashicorp/terraform-plugin-sdk/v2",
"Version": "v2.0.2"
}
```

### Relevant provider source code

```go
// In provider resource DELETE function:
_, err := client.Remove(ctx, req)
if err != nil {
logger.WithError(err).Error("could not remove")
return diag.FromErr(err) // <-- Code path under test
}
```

```go
// In test case:
client.On("Remove", mock.Anything, req).Return(nil, &err) // Set up mock client to return error

resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{{
Config: loadFixtureString("testdata/%s.tf", t.Name()),
ExpectError: regexp.MustCompile(`.*`), // <-- should match any error at all
Check: resource.ComposeAggregateTestCheckFunc(
// irrelevant
),
}},
})

```
### Debug Output
Test output:

```
testing_new.go:22: WARNING: destroy failed, so remote objects may still exist and be subject to billing
testing_new.go:22: failed to destroy:
Error: could not remove
```

### Expected Behavior

Test should pass because the delete operation is expected to fail

### Actual Behavior

Test fails because the resource is not destroyed.

### Steps to Reproduce

Create an apply/destroy test case where the destroy is guaranteed to fail.

### References

https://github.com/hashicorp/terraform-plugin-sdk/issues/347 - Except their problem was during resource creation.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.