2021-09-22 23:53:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-03 21:47:14 +01:00
|
|
|
"flag"
|
2021-09-22 23:53:33 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
2021-10-28 01:07:12 +02:00
|
|
|
tfversion "github.com/hashicorp/terraform/version"
|
2021-09-22 23:53:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var terraformBin string
|
|
|
|
var cliConfigFileEnv string
|
|
|
|
|
|
|
|
var tfeClient *tfe.Client
|
|
|
|
var tfeHostname string
|
|
|
|
var tfeToken string
|
2021-11-03 21:47:14 +01:00
|
|
|
var verboseMode bool
|
2021-09-22 23:53:33 +02:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
teardown := setup()
|
|
|
|
code := m.Run()
|
|
|
|
teardown()
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func accTest() bool {
|
|
|
|
// TF_ACC is set when we want to run acceptance tests, meaning it relies on
|
|
|
|
// network access.
|
|
|
|
return os.Getenv("TF_ACC") != ""
|
|
|
|
}
|
|
|
|
|
2021-11-11 21:21:21 +01:00
|
|
|
func hasHostname() bool {
|
|
|
|
return os.Getenv("TFE_HOSTNAME") != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasToken() bool {
|
|
|
|
return os.Getenv("TFE_TOKEN") != ""
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:00:24 +01:00
|
|
|
func hasRequiredEnvVars() bool {
|
|
|
|
return accTest() && hasHostname() && hasToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
func skipIfMissingEnvVar(t *testing.T) {
|
|
|
|
if !hasRequiredEnvVars() {
|
|
|
|
t.Skip("Skipping test, required environment variables missing. Use `TF_ACC`, `TFE_HOSTNAME`, `TFE_TOKEN`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:53:33 +02:00
|
|
|
func setup() func() {
|
2021-11-03 21:47:14 +01:00
|
|
|
tfOutput := flag.Bool("tfoutput", false, "This flag produces the terraform output from tests.")
|
|
|
|
flag.Parse()
|
|
|
|
verboseMode = *tfOutput
|
|
|
|
|
2021-09-22 23:53:33 +02:00
|
|
|
setTfeClient()
|
|
|
|
teardown := setupBinary()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
teardown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setTfeClient() {
|
2021-11-12 18:00:24 +01:00
|
|
|
tfeHostname = os.Getenv("TFE_HOSTNAME")
|
|
|
|
tfeToken = os.Getenv("TFE_TOKEN")
|
2021-09-22 23:53:33 +02:00
|
|
|
|
|
|
|
cfg := &tfe.Config{
|
2021-11-12 18:00:24 +01:00
|
|
|
Address: fmt.Sprintf("https://%s", tfeHostname),
|
|
|
|
Token: tfeToken,
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 18:00:24 +01:00
|
|
|
if tfeHostname != "" && tfeToken != "" {
|
|
|
|
// Create a new TFE client.
|
|
|
|
client, err := tfe.NewClient(cfg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Could not create new tfe client: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
tfeClient = client
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupBinary() func() {
|
|
|
|
log.Println("Setting up terraform binary")
|
|
|
|
tmpTerraformBinaryDir, err := ioutil.TempDir("", "terraform-test")
|
|
|
|
if err != nil {
|
2021-11-12 18:00:24 +01:00
|
|
|
fmt.Printf("Could not create temp directory: %v\n", err)
|
|
|
|
os.Exit(1)
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
log.Println(tmpTerraformBinaryDir)
|
|
|
|
currentDir, err := os.Getwd()
|
|
|
|
defer os.Chdir(currentDir)
|
|
|
|
if err != nil {
|
2021-11-12 18:00:24 +01:00
|
|
|
fmt.Printf("Could not change directories: %v\n", err)
|
|
|
|
os.Exit(1)
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
// Getting top level dir
|
|
|
|
dirPaths := strings.Split(currentDir, "/")
|
|
|
|
log.Println(currentDir)
|
|
|
|
topLevel := len(dirPaths) - 3
|
|
|
|
topDir := strings.Join(dirPaths[0:topLevel], "/")
|
|
|
|
|
|
|
|
if err := os.Chdir(topDir); err != nil {
|
2021-11-12 18:00:24 +01:00
|
|
|
fmt.Printf("Could not change directories: %v\n", err)
|
|
|
|
os.Exit(1)
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 01:07:12 +02:00
|
|
|
cmd := exec.Command(
|
|
|
|
"go",
|
|
|
|
"build",
|
|
|
|
"-o", tmpTerraformBinaryDir,
|
|
|
|
"-ldflags", fmt.Sprintf("-X \"github.com/hashicorp/terraform/version.Prerelease=%s\"", tfversion.Prerelease),
|
|
|
|
)
|
2021-09-22 23:53:33 +02:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2021-11-12 18:00:24 +01:00
|
|
|
fmt.Printf("Could not run exec command: %v\n", err)
|
|
|
|
os.Exit(1)
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
credFile := fmt.Sprintf("%s/dev.tfrc", tmpTerraformBinaryDir)
|
|
|
|
writeCredRC(credFile)
|
|
|
|
|
|
|
|
terraformBin = fmt.Sprintf("%s/terraform", tmpTerraformBinaryDir)
|
|
|
|
cliConfigFileEnv = fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", credFile)
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
os.RemoveAll(tmpTerraformBinaryDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeCredRC(file string) {
|
|
|
|
creds := credentialBlock()
|
|
|
|
f, err := os.Create(file)
|
|
|
|
if err != nil {
|
2021-11-12 18:00:24 +01:00
|
|
|
fmt.Printf("Could not create file: %v\n", err)
|
|
|
|
os.Exit(1)
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
_, err = f.WriteString(creds)
|
|
|
|
if err != nil {
|
2021-11-12 18:00:24 +01:00
|
|
|
fmt.Printf("Could not write credentials: %v\n", err)
|
|
|
|
os.Exit(1)
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func credentialBlock() string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
credentials "%s" {
|
|
|
|
token = "%s"
|
|
|
|
}`, tfeHostname, tfeToken)
|
|
|
|
}
|