hashicorp / hashicorp/terraform-plugin-testing

Terraform working directory not set to `ConfigDirectory` when in use

Open
#277 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
68
Forks
22
Avg merge
2d 12h
Merged PRs (30d)
1

Description

### terraform-plugin-testing version

```
github.com/hashicorp/terraform-plugin-testing v1.6.0
```

The following code was from the `local` provider when attempting to refactor some of the tests to use `ConfigDirectory`: https://github.com/hashicorp/terraform-provider-local/blob/3b59b2e8a560c88b572fb46d3965bc9ae3a52ff3/internal/provider/data_source_local_file_test.go

### Problem

Implementing a test utilizing [`(TestStep).ConfigDirectory`](https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/configuration#teststep-configdirectory) with the following directory structure:

```bash
$ tree
.
├── data_source_local_file.go
├── data_source_local_file_test.go
├── .... other files
└── testdata
└── TestLocalFileDataSource
├── local_file
├── main.tf
```

#### `data_source_local_file_test.go`
```go
func TestLocalFileDataSource(t *testing.T) {
content := "This is some content"
checkSums := genFileChecksums([]byte(content))

resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
ConfigDirectory: config.TestNameDirectory(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.local_file.file", "content", content),
resource.TestCheckResourceAttr("data.local_file.file", "content_base64", base64.StdEncoding.EncodeToString([]byte(content))),
resource.TestCheckResourceAttr("data.local_file.file", "content_md5", checkSums.md5Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_sha1", checkSums.sha1Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_sha256", checkSums.sha256Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_base64sha256", checkSums.sha256Base64),
resource.TestCheckResourceAttr("data.local_file.file", "content_sha512", checkSums.sha512Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_base64sha512", checkSums.sha512Base64),
),
},
},
})
}

```

When attempting to reference the `local_file` file from the terraform config directory, you can't use a relative reference, as the working directory is not set to `testdata/TestLocalFileDataSource`.

#### `main.tf` (error)
```terraform
data "local_file" "file" {
# This doesn't work, throws an error saying the file can't be found
filename = "${path.module}/local_file"
}
```
#### `main.tf` (success)
```terraform
data "local_file" "file" {
# This works
filename = "${path.module}/testdata/TestLocalFileDataSource/local_file"
}
```

### Proposal

We should copy the contents of `ConfigDirectory` to the root of the working directory that terraform will run in. This may have compatibility concerns? Open to other suggestions.

At the very least, we should document that this behavior exists if we opt not to fix it.

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.