Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to read variables file. No .tfvars file copied when using test_structure.CopyTerraformFoldertoTemp #972

Open
ent-lhirschi opened this issue Aug 10, 2021 · 8 comments
Labels

Comments

@ent-lhirschi
Copy link

ent-lhirschi commented Aug 10, 2021

Goal: Have terratest use or detect my terraform.tfvars to pass variables.

Issue: I believe Terratest failed to read the terraform.tfvars because it wasn't copied from my terraform directory to the temporary directory. Or I didn't write the code properly. (The more likely scenario, but would like help) When I go to check the temporary directory (as you'll see in the error below) the terraform.tfvars doesn't exist.

I was able to run terraform plan successfully in the examples/ directory, which isolates the issue to my go code.

My current code:


package test

import (
	"testing"

	"github.com/gruntwork-io/terratest/modules/terraform"
	test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
	"github.com/stretchr/testify/assert"
)

func TestTerraformAlb(t *testing.T) {
	t.Parallel()

	// Make a copy of the terraform module to a temporary directory. This allows running multiple tests in parallel
	// against the same terraform module.
	exampleDir := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/")
	terratestOptions := &terraform.Options{
		TerraformDir: exampleDir,
		// TerraformDir: exampleDir,
		Vars:     map[string]interface{}{},
		VarFiles: []string{exampleDir, "/terraform.tfvars"},
	}

	t.Logf("Running in %s", exampleDir)

	// This will run 'terraform init' and 'terraform plan' and will fail the test if any errors occur
	output := terraform.InitAndPlan(t, terratestOptions)

	// To get the value of an output variable, run 'terraform output'
	// Arn := terraform.Output(t, terraformOptions, "arn")

	assert.Contains(t, output, "4 to add, 0 to change, 0 to destroy", "Plan OK and should attempt to create 4 resources")
} ```


 Error: Failed to read variables file
                                │
                                │ Error while reading
                                │ C:\Users\lhirschi\AppData\Local\Temp\TestTerraformAlb217072087\alb\examples:
                                │ read
                                │ C:\Users\lhirschi\AppData\Local\Temp\TestTerraformAlb217072087\alb\examples:
                                │ The handle is invalid..
                                ╵
                                ╷
                                │ Error: Failed to read variables file
                                │
                                │ Given variables file /terraform.tfvars does not exist.
                                
                                
                             
@ent-lhirschi
Copy link
Author

I looked at the syntax for VarFiles here https://github.com/gruntwork-io/terratest/blob/master/modules/terraform/options.go#L12

Am I suppose to use the -var-files flag, if so how? I've searched for examples and since it's a more recent PR, unable to find any examples from searching the web.

@ent-lhirschi
Copy link
Author

ent-lhirschi commented Aug 10, 2021

So I found out the following information. https://github.com/gruntwork-io/terratest/blob/master/modules/files/files.go

// This is useful when running multiple tests in parallel against the same set of Terraform files to ensure the
// tests don't overwrite each other's .terraform working directory and terraform.tfstate files. This method returns
// the path to the temp folder with the copied contents. Hidden files and folders, Terraform state files, and
// terraform.tfvars files are not copied to this temp folder, as you typically don't want them interfering with your
// tests.

How do I make an exception for the CopyTerraformFolderToTemp to include my terraform.tfvars?

@brikis98
Copy link
Member

So I found out the following information. https://github.com/gruntwork-io/terratest/blob/master/modules/files/files.go

// This is useful when running multiple tests in parallel against the same set of Terraform files to ensure the
// tests don't overwrite each other's .terraform working directory and terraform.tfstate files. This method returns
// the path to the temp folder with the copied contents. Hidden files and folders, Terraform state files, and
// terraform.tfvars files are not copied to this temp folder, as you typically don't want them interfering with your
// tests.

How do I make an exception for the CopyTerraformFolderToTemp to include my terraform.tfvars?

Only thing I can think of is to create a version of the method that accepts a filter function as a param. The original method can pass in a filter that excludes the files listed in that comment (state files, .tfvars, etc), but you could pass in your own filter in your test.

@bj-1795
Copy link

bj-1795 commented Sep 7, 2021

@brikis98 ent-lhirschi Did you find the way to declare the tfvars file?? I am getting error like file does not exist.

@Sanardi
Copy link

Sanardi commented Sep 9, 2021

Following, I am facing the same problem

@bj-1795
Copy link

bj-1795 commented Sep 9, 2021

I was able to solve this, just use file name in varfile don't use full path and keep the file in same dir as Terraformdir.

@Sanardi
Copy link

Sanardi commented Sep 9, 2021

could you show me an example please?
infraPath := test_structure.CopyTerraformFolderToTemp(t, "../", "infra/my-infra")

terraformOptions := &terraform.Options{
	// The path to where our Terraform code is located
	TerraformDir: infraPath,

	Vars: <How did you declare it here?>
}

defer terraform.Destroy(t, terraformOptions)

@bj-1795
Copy link

bj-1795 commented Sep 9, 2021

Use the following:

terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: infraPath,

VarFiles: []string{"test.vars.json"},

}

defer terraform.Destroy(t, terraformOptions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants