Skip to content

Terraform working directory not set to ConfigDirectory when in use #277

@austinvalle

Description

@austinvalle

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 with the following directory structure:

 $ 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

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)

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)

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions