hashicorp / hashicorp/terraform-plugin-sdk

Acceptance tests fail randomly with default TMPDIR

Open
#598 2 comments 5 reactions 0 assignees View on GitHub
bug subsystem/tests
Dominant language
Go
Stars
485
Forks
244
Avg merge
19h 57m
Merged PRs (30d)
4

Description

### SDK version

```
{
"Path": "github.com/hashicorp/terraform-plugin-sdk/v2",
"Version": "v2.0.3"
}
```

### Relevant provider source code

I implemented a multi-step acceptance test using the hashicups example project. [Here](https://github.com/gnarlex/terraform-provider-hashicups/tree/bug/multi-acctest) is my fork with updated SDK dependency and [the implemented acceptance test](https://github.com/gnarlex/terraform-provider-hashicups/blob/bug/multi-acctest/hashicups/resource_order_test.go#L80-L93).

```go
func TestAccResourceOrderCreateMultiStep(t *testing.T) {
steps := []resource.TestStep{}
for i := 0; i < 10; i++ {
steps = append(steps, resource.TestStep{
Config: testAccCheckHashicupsOrderConfigBasic("1", strconv.Itoa(i+1)),
})
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: steps,
})
}
```

### Terraform Configuration Files

```hcl
resource "hashicups_order" "new" {
items {
coffee {
id = 1
}
quantity = 1 # this increments every test step
}
}
```

### Debug Output

### Expected Behavior

Tests run successfully.

### Actual Behavior

An error similar to following is thrown:

```
resource_order_test.go:88: Step 3/100 error: Error running second post-apply plan:
Error: Failed to read directory for module

The source directory
../../../../../../../../var/folders/fz/fcd3jr3114xbcjz16cq3ld5m0000gp/T/tftest839129906/work291258601/config225439103
could not be read
```

Or this one:

```
resource_order_test.go:88: Step 3/100 error: Error running pre-apply plan:
Error: No configuration files

Plan requires configuration to be present. Planning without a configuration
would mark everything for destruction, which is normally not what is desired.
If you would like to destroy everything, run plan with the -destroy option.
Otherwise, create a Terraform configuration file (.tf file) and try again.
```

This error seems to occur randomly at different steps (e.g. post-apply refresh).

**After further investigation I found that this bug seems to be connected to the default tmp folder (TMPDIR) provided by the OS.**

### Steps to Reproduce

```sh
# 1. clone repo and cd into it
$ git clone https://github.com/gnarlex/terraform-provider-hashicups.git
$ cd terraform-provider-hashicups

# 2. update vendor folder (wanted to keep the commits small)
$ go mod vendor

# 3. start hashicups api in seperate terminal window in current working directory
$ cd docker-compose && docker_compose up

# 4. create example user and export login credentials
$ curl -X POST localhost:19090/signup -d '{"username":"education", "password":"test123"}'
$ export HASHICUPS_USERNAME=education
$ export HASHICUPS_PASSWORD=test123

# 5. run acceptance tests
$ make testacc
# this should fail

# 6. now create a temporary folder for the acc. tests
$ mkdir tmp

# 7. run acc. tests again with TMPDIR env variable set to created folder
$ TMPDIR=$(pwd)/tmp make testacc
# this should run
```

### Additional Information

I don't know if this bug is OS-, machine- or version-specific and I don't have the resources to investigate this further unfortunately.

- OS: MacOS Catalina
- Architecture: amd64
- Terraform Version: v0.13.3

### References

Contributor guide

Open the contributing guide

Research direction

Start with the multi-step acceptance test in resource_order_test.go and the make testacc entry point. Reproduce it with the default TMPDIR and with TMPDIR set to a repository-local tmp directory, then trace the acceptance-test workspace handling; done means repeated multi-step runs no longer lose configuration or module directories.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.