Skip to content

Commit

Permalink
Setup acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Jul 27, 2020
1 parent 32fed2f commit 83d7701
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ vet:
test: deps
go test ./...

.PHONY: testacc
testacc: deps
TEST_ACC=1 go test -count=1 ./...

.PHONY: check
check: lint vet test build
2 changes: 1 addition & 1 deletion tfexec/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (e *executor) NewCommandContext(ctx context.Context, name string, args ...s
// Run executes a command.
func (e *executor) Run(cmd Command) error {
err := cmd.Run()
log.Printf("[DEBUG] run command: %s", spew.Sdump(cmd))
log.Printf("[DEBUG] run command in %s: %s ", e.dir, spew.Sdump(cmd))
if err != nil {
log.Printf("[DEBUG] failed to run command: %s", spew.Sdump(err))
if osExecErr, ok := err.(*exec.ExitError); ok {
Expand Down
15 changes: 15 additions & 0 deletions tfexec/terraform_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,18 @@ func TestTerraformCLIInit(t *testing.T) {
})
}
}

func TestAccTerraformCLIInit(t *testing.T) {
if !isAcceptanceTestEnabled() {
t.Skip("skip acceptance tests")
}

source := `resource "null_resource" "foo" {}`
e := setupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "", "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
}
49 changes: 49 additions & 0 deletions tfexec/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,58 @@ package tfexec

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

// setupTestAcc is a common setup helper for acceptance tests.
func setupTestAcc(t *testing.T, source string) Executor {
workDir, err := setupTestWorkDir(source)
if err != nil {
t.Fatalf("failed to setup work dir: %s", err)
}
t.Cleanup(func() { os.RemoveAll(workDir) })

e := NewExecutor(workDir, os.Environ())
if err := setupTestPluginCacheDir(e); err != nil {
t.Fatalf("failed to set plugin cache dir: %s", err)
}

return e
}

// isAcceptanceTestEnabled returns true if acceptance tests should be run.
func isAcceptanceTestEnabled() bool {
return os.Getenv("TEST_ACC") == "1"
}

// setupTestWorkDir creates temporary working directory with a given source for testing.
func setupTestWorkDir(source string) (string, error) {
workDir, err := ioutil.TempDir("", "workDir")
if err != nil {
return "", fmt.Errorf("failed to create work dir: %s", err)
}

if err := ioutil.WriteFile(filepath.Join(workDir, "main.tf"), []byte(source), 0644); err != nil {
os.RemoveAll(workDir)
return "", fmt.Errorf("failed to create main.tf: %s", err)
}
return workDir, nil
}

// setupTestPluginCacheDir sets TF_PLUGIN_CACHE_DIR to a given executor.
func setupTestPluginCacheDir(e Executor) error {
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current dir: %s", err)
}
e.AppendEnv("TF_PLUGIN_CACHE_DIR", filepath.Join(pwd, "tmp/plugin-cache"))
return nil
}

func TestTerraformCLIRun(t *testing.T) {
cases := []struct {
desc string
Expand Down
19 changes: 19 additions & 0 deletions tfexec/terraform_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tfexec

import (
"context"
"fmt"
"os"
"testing"
)

Expand Down Expand Up @@ -70,3 +72,20 @@ is 0.12.29. You can update by downloading from https://www.terraform.io/download
})
}
}

func TestAccTerraformCLIVersion(t *testing.T) {
if !isAcceptanceTestEnabled() {
t.Skip("skip acceptance tests")
}

e := NewExecutor("", os.Environ())
terraformCLI := NewTerraformCLI(e)
got, err := terraformCLI.Version(context.Background())
if err != nil {
t.Fatalf("failed to run terraform version: %s", err)
}
if got == "" {
t.Error("failed to parse terraform version")
}
fmt.Printf("got = %s\n", got)
}

0 comments on commit 83d7701

Please sign in to comment.