commit | cff1cb8939e568fc9feb421dd29429859c7a0276 | [log] [tgz] |
---|---|---|
author | Brian Flad <bflad417@gmail.com> | Thu Aug 31 05:44:39 2023 -0400 |
committer | GitHub <noreply@github.com> | Thu Aug 31 10:44:39 2023 +0100 |
tree | d2d9aee7116556fef4ec43c5c36e46329d8cf723 | |
parent | f8daf3dc8e047bd082f80a0918488f06e3ce233b [diff] |
tfexec: Initial test command support (#400) Reference: https://github.com/hashicorp/terraform-exec/issues/398 Reference: https://github.com/hashicorp/terraform/pull/33454 Adds support for the `terraform test` command, which currently supports JSON machine-readable output and one flag for configuring the tests directory away from the command default of `tests`. The command will return a non-zero status if any of the tests fail, which returns an error back to callers of the `Test` function. If consumers need access to the pass/fail test results, the terraform-json Go module will need to be enhanced to support the test summary JSON, e.g. ``` {"@level":"info","@message":"Failure! 0 passed, 1 failed.","@module":"terraform.ui","@timestamp":"2023-07-25T10:03:42.980799-04:00","test_summary":{"status":"fail","passed":0,"failed":1,"errored":0,"skipped":0},"type":"test_summary"} ``` Output of new end-to-end testing: ``` $ TFEXEC_E2ETEST_VERSIONS=1.5.3,1.6.0-alpha20230719 go test -count=1 -run='TestTest' -v ./tfexec/internal/e2etest ... --- PASS: TestTest (9.50s) --- SKIP: TestTest/test_command_passing-1.5.3 (4.06s) --- PASS: TestTest/test_command_passing-1.6.0-alpha20230719 (5.44s) ... --- PASS: TestTestError (0.48s) --- SKIP: TestTestError/test_command_failing-1.5.3 (0.27s) --- PASS: TestTestError/test_command_failing-1.6.0-alpha20230719 (0.21s) ```
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.
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.18 or later must be used.
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.
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" }
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.
Please see CONTRIBUTING.md.