tfexec/internal/e2etest: Skip TestShow_noInitEtcdBackend on >=1.3.0 and prereleases (#325)

Reference: https://github.com/hashicorp/terraform/pull/31326

Fixes the new test failures against Terraform `main` branch. This also adds testing for the `cloud` and `remote` backends, which should be more stable as they are integral to the Terraform experience.

Previously:

```console
$ TFEXEC_E2ETEST_VERSIONS=refs/heads/main go test -count=1 -run='TestShow_noInitEtcdBackend' -v ./tfexec/internal/e2etest
=== RUN   TestShow_noInitEtcdBackend
=== RUN   TestShow_noInitEtcdBackend/etcd_backend-refs/heads/main
    util_test.go:112: caching exec "gitref:refs/heads/main" in dir "/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/tfinstall2067874872/gitref-refs-heads-main"
2022/06/30 15:56:57 git_revision.go:88: running terraform pre-clone check (timeout: 1m0s)
2022/06/30 15:56:57 git_revision.go:94: terraform pre-clone check finished
2022/06/30 15:56:57 git_revision.go:123: cloning terraform repository from https://github.com/hashicorp/terraform.git to /var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/hc-install-build-terraform3971808670 (timeout: 5m0s)
2022/06/30 15:57:37 git_revision.go:136: cloning terraform finished
2022/06/30 15:57:37 git_revision.go:142: terraform repository HEAD is at acb79a75451e39126f1cdc596ff802a91d64d712
2022/06/30 15:57:37 git_revision.go:169: building terraform (timeout: 10m0s)
2022/06/30 15:57:37 go_build.go:55: executing go ["build" "-o" "/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/hc-install-terraform-acb79a75451e39126f1cdc596ff802a91d64d7123048666161/terraform"] in "/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/hc-install-build-terraform3971808670"
2022/06/30 15:57:51 git_revision.go:171: building of terraform finished
    util_test.go:173: [INFO] running Terraform command: /var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/hc-install-terraform-acb79a75451e39126f1cdc596ff802a91d64d7123048666161/terraform show -json -no-color
    show_test.go:232: expected error ErrNoInit, got *fmt.wrapError: exit status 1

        Error: Invalid backend type

          on main.tf line 2, in terraform:
           2:   backend "etcd" {

        The "etcd" backend is not supported in Terraform v1.3 or later.
--- FAIL: TestShow_noInitEtcdBackend (55.76s)
    --- FAIL: TestShow_noInitEtcdBackend/etcd_backend-refs/heads/main (55.76s)
FAIL
FAIL    github.com/hashicorp/terraform-exec/tfexec/internal/e2etest     56.060s
FAIL
```

After change:

```console
$ TFEXEC_E2ETEST_VERSIONS=refs/heads/main go test -count=1 -run='TestShow_noInitEtcdBackend' -v ./tfexec/internal/e2etest
=== RUN   TestShow_noInitEtcdBackend
=== RUN   TestShow_noInitEtcdBackend/etcd_backend-refs/heads/main
    util_test.go:112: caching exec "gitref:refs/heads/main" in dir "/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/tfinstall2843106250/gitref-refs-heads-main"
2022/06/30 15:59:17 git_revision.go:88: running terraform pre-clone check (timeout: 1m0s)
2022/06/30 15:59:17 git_revision.go:94: terraform pre-clone check finished
2022/06/30 15:59:17 git_revision.go:123: cloning terraform repository from https://github.com/hashicorp/terraform.git to /var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/hc-install-build-terraform22313428 (timeout: 5m0s)
2022/06/30 15:59:59 git_revision.go:136: cloning terraform finished
2022/06/30 15:59:59 git_revision.go:142: terraform repository HEAD is at acb79a75451e39126f1cdc596ff802a91d64d712
2022/06/30 15:59:59 git_revision.go:169: building terraform (timeout: 10m0s)
2022/06/30 15:59:59 go_build.go:55: executing go ["build" "-o" "/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/hc-install-terraform-acb79a75451e39126f1cdc596ff802a91d64d7122130413204/terraform"] in "/var/folders/w8/05f3x02n27x72g0mc2jy6_180000gp/T/hc-install-build-terraform22313428"
2022/06/30 16:00:12 git_revision.go:171: building of terraform finished
    show_test.go:223: etcd backend was removed in Terraform 1.3, so test is not valid
--- PASS: TestShow_noInitEtcdBackend (56.66s)
    --- SKIP: TestShow_noInitEtcdBackend/etcd_backend-refs/heads/main (56.66s)
PASS
ok      github.com/hashicorp/terraform-exec/tfexec/internal/e2etest     57.704s
```
3 files changed
tree: 3f7c9453056e33272e70287dc5404fed9b40072f
  1. .circleci/
  2. .github/
  3. internal/
  4. scripts/
  5. tfexec/
  6. .gitignore
  7. CHANGELOG.md
  8. CONTRIBUTING.md
  9. go.mod
  10. go.sum
  11. LICENSE
  12. README.md
README.md

PkgGoDev

terraform-exec

A Go module for constructing and running Terraform CLI commands. Structured return values use the data types defined in terraform-json.

The Terraform Plugin SDK is the canonical Go interface for Terraform plugins using the gRPC protocol. This library is intended for use in Go programs that make use of Terraform's other interface, the CLI. Importing this library is preferable to importing github.com/hashicorp/terraform/command, because the latter is not intended for use outside Terraform Core.

While terraform-exec is already widely used, please note that this module is not yet at v1.0.0, and that therefore breaking changes may occur in minor releases.

We strictly follow semantic versioning.

Go compatibility

This library is built in Go, and uses the support policy of Go as its support policy. The two latest major releases of Go are supported by terraform-exec.

Currently, that means Go 1.17 or later must be used.

Usage

The Terraform struct must be initialised with NewTerraform(workingDir, execPath).

Top-level Terraform commands each have their own function, which will return either error or (T, error), where T is a terraform-json type.

Example

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/hashicorp/go-version"
	"github.com/hashicorp/hc-install/product"
	"github.com/hashicorp/hc-install/releases"
	"github.com/hashicorp/terraform-exec/tfexec"
)

func main() {
	installer := &releases.ExactVersion{
		Product: product.Terraform,
		Version: version.Must(version.NewVersion("1.0.6")),
	}

	execPath, err := installer.Install(context.Background())
	if err != nil {
		log.Fatalf("error installing Terraform: %s", err)
	}

	workingDir := "/path/to/working/dir"
	tf, err := tfexec.NewTerraform(workingDir, execPath)
	if err != nil {
		log.Fatalf("error running NewTerraform: %s", err)
	}

	err = tf.Init(context.Background(), tfexec.Upgrade(true))
	if err != nil {
		log.Fatalf("error running Init: %s", err)
	}

	state, err := tf.Show(context.Background())
	if err != nil {
		log.Fatalf("error running Show: %s", err)
	}

	fmt.Println(state.FormatVersion) // "0.1"
}

Testing Terraform binaries

The terraform-exec test suite contains end-to-end tests which run realistic workflows against a real Terraform binary using tfexec.Terraform{}.

To run these tests with a local Terraform binary, set the environment variable TFEXEC_E2ETEST_TERRAFORM_PATH to its path and run:

go test -timeout=20m ./tfexec/internal/e2etest

For more information on terraform-exec's test suite, please see Contributing below.

Contributing

Please see CONTRIBUTING.md.