gruntwork-io / terratest

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.
https://terratest.gruntwork.io/
Apache License 2.0
7.52k stars 1.33k forks source link

Terratest v0.46.1 issues with tofu #1359

Closed nguyenhung1121990 closed 1 year ago

nguyenhung1121990 commented 1 year ago

Describe the bug I am using Terratest v0.46.1 to test Terraform scope, however on my latest console logs, it said there is an issue with tofu

$ go test -v === RUN TestTerraformAzureResource === PAUSE TestTerraformAzureResource === CONT TestTerraformAzureResource TestTerraformAzureResource 2023-10-24T12:53:26+07:00 retry.go:91: tofu [init -upgrade=false] TestTerraformAzureResource 2023-10-24T12:53:26+07:00 logger.go:66: Running command tofu with args [init -upgrade=false] TestTerraformAzureResource 2023-10-24T12:53:26+07:00 retry.go:99: Returning due to fatal error: FatalError{Underlying: error while running command: exec: "tofu": executable file not found in %PATH%; } apply.go:15: Error Trace: C:/Users/user/go/pkg/mod/github.com/gruntwork-io/terratest@v0.46.1/modules/terraform/apply.go:15 /Terraform/ml-multi/tests/rg_integration_test.go:38 Error: Received unexpected error: FatalError{Underlying: error while running command: exec: "tofu": executable file not found in %PATH%; } Test: TestTerraformAzureResource --- FAIL: TestTerraformAzureResource (0.37s)

    // Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
    terraform.InitAndApply(t, terraformOptions)

Expected behavior How to clear the error?

Nice to have

Versions

denis256 commented 1 year ago

Hello, terraform binary is installed in %PATH% ? Looks like the code tried to run terraform, failed, and tried to use tofu

nguyenhung1121990 commented 1 year ago

I don't run any command using terraform. I just wrote a Golang file to do Integration Test that included terraform.InitAndApply(t, terraformOptions).

go test -v

Any suggestion? I tested the same on Red Hat Linux agent and error still the same.

nguyenhung1121990 commented 1 year ago

I ran again on my local Win10:

$ go test -v

=== RUN   TestTerraformAzureResource
=== PAUSE TestTerraformAzureResource
=== CONT  TestTerraformAzureResource
TestTerraformAzureResource 2023-10-25T15:51:53+07:00 retry.go:91: terraform [init -upgrade=false]
TestTerraformAzureResource 2023-10-25T15:51:53+07:00 logger.go:66: Running command terraform with args [init -upgrade=false]
TestTerraformAzureResource 2023-10-25T15:51:53+07:00 retry.go:99: Returning due to fatal error: FatalError{Underlying: error while running command: exec: "terraform": executable file not found in %PATH%; }
    apply.go:15: 
                Error Trace:    D:\path\to\project\tests-integration\apply.go:15
                                                        D:\path\to\project\tests-integration\rg_integration_test.go:38
                Error:          Received unexpected error:
                                FatalError{Underlying: error while running command: exec: "terraform": executable file not found in %PATH%; }
                Test:           TestTerraformAzureResource
--- FAIL: TestTerraformAzureResource (0.33s)
FAIL
exit status 1
FAIL    tftest  2.816s

Why it always either "terraform" or "tofu"?

denis256 commented 1 year ago

Hello, function terraform.InitAndApply() in the end invokes terraform or tofu executable, so it should be installed in the environment

nguyenhung1121990 commented 1 year ago

Understood. I am now coming back to Linux Red Hat (RHEL 8) agent and fix these above "tofu" issue. I am manually installing tofu using following command:

wget https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_darwin_arm64.zip
unzip tofu_1.6.0-alpha2_darwin_arm64.zip
mv tofu /usr/local/bin/tofu

After that, I believe tofu also added into $PATH. However when I try to run tofu --version, it says:

[root@vm001 tests-integration]# tofu --version
bash: /usr/local/bin/tofu: cannot execute binary file: Exec format error

Does it make sense? I anyway ran go test -v again, and once again, error! Is this not compatible with RHEL 8? :|

[root@vm001 tests-integration]# go test -v
=== RUN   TestTerraformAzureResource
=== PAUSE TestTerraformAzureResource
=== CONT  TestTerraformAzureResource
TestTerraformAzureResource 2023-10-25T09:17:33Z retry.go:91: tofu [init -upgrade=false]
TestTerraformAzureResource 2023-10-25T09:17:33Z logger.go:66: Running command tofu with args [init -upgrade=false]
TestTerraformAzureResource 2023-10-25T09:17:33Z retry.go:99: Returning due to fatal error: FatalError{Underlying: error while running command: fork/exec /usr/local/bin/tofu: exec forma
t error; }
    apply.go:15: 
        Error Trace:/root/go/pkg/mod/github.com/gruntwork-io/terratest@v0.46.1/modules/terraform/apply.go:15
                    /myagent/_work/1/s/Terraform/4_1-azureml-multiws/tests-integration/rg_integration_test.go:38
        Error:      Received unexpected error:
                    FatalError{Underlying: error while running command: fork/exec /usr/local/bin/tofu: exec format error; }
        Test:       TestTerraformAzureResource
--- FAIL: TestTerraformAzureResource (0.01s)
FAIL
exit status 1
FAILtftest0.038s
denis256 commented 1 year ago

Hm, looks like was downloaded version for darwin (tofu_1.6.0-alpha2_darwin_arm64) in RHEL environment... not sure if the architectures match, most probably should be downloaded linux_amd64.zip

nguyenhung1121990 commented 1 year ago

Thank you. It looks working. I have download wrong version of tofu. Check correct one at https://github.com/opentofu/opentofu/releases/ and enjoy!