Add skip for cloud e2e tests when env vars missing

This commit is contained in:
Omar Ismail 2021-11-12 12:00:24 -05:00
parent 57a4b51e87
commit 9b675f8b70
10 changed files with 108 additions and 89 deletions

View File

@ -13,6 +13,7 @@ import (
)
func Test_terraform_apply_autoApprove(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -178,76 +179,72 @@ func Test_terraform_apply_autoApprove(t *testing.T) {
},
},
}
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
organization, cleanup := createOrganization(t)
defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...)
if err != nil {
t.Fatal(err)
}
defer exp.Close()
for _, tc := range cases {
organization, cleanup := createOrganization(t)
defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...)
if err != nil {
t.Fatal(err)
}
defer exp.Close()
tmpDir, err := ioutil.TempDir("", "terraform-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir, err := ioutil.TempDir("", "terraform-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tf := e2e.NewBinary(terraformBin, tmpDir)
tf.AddEnv(cliConfigFileEnv)
defer tf.Close()
tf := e2e.NewBinary(terraformBin, tmpDir)
tf.AddEnv(cliConfigFileEnv)
defer tf.Close()
for _, op := range tc.operations {
op.prep(t, organization.Name, tf.WorkDir())
for _, tfCmd := range op.commands {
cmd := tf.Cmd(tfCmd.command...)
cmd.Stdin = exp.Tty()
cmd.Stdout = exp.Tty()
cmd.Stderr = exp.Tty()
for _, op := range tc.operations {
op.prep(t, organization.Name, tf.WorkDir())
for _, tfCmd := range op.commands {
cmd := tf.Cmd(tfCmd.command...)
cmd.Stdin = exp.Tty()
cmd.Stdout = exp.Tty()
cmd.Stderr = exp.Tty()
err = cmd.Start()
if err != nil {
t.Fatal(err)
}
err = cmd.Start()
if err != nil {
t.Fatal(err)
}
if tfCmd.expectedCmdOutput != "" {
_, err := exp.ExpectString(tfCmd.expectedCmdOutput)
if err != nil {
t.Fatal(err)
}
}
lenInput := len(tfCmd.userInput)
lenInputOutput := len(tfCmd.postInputOutput)
if lenInput > 0 {
for i := 0; i < lenInput; i++ {
input := tfCmd.userInput[i]
exp.SendLine(input)
// use the index to find the corresponding
// output that matches the input.
if lenInputOutput-1 >= i {
output := tfCmd.postInputOutput[i]
_, err := exp.ExpectString(output)
if err != nil {
t.Fatal(err)
}
}
}
}
err = cmd.Wait()
if tfCmd.expectedCmdOutput != "" {
_, err := exp.ExpectString(tfCmd.expectedCmdOutput)
if err != nil {
t.Fatal(err)
}
}
}
if tc.validations != nil {
tc.validations(t, organization.Name)
lenInput := len(tfCmd.userInput)
lenInputOutput := len(tfCmd.postInputOutput)
if lenInput > 0 {
for i := 0; i < lenInput; i++ {
input := tfCmd.userInput[i]
exp.SendLine(input)
// use the index to find the corresponding
// output that matches the input.
if lenInputOutput-1 >= i {
output := tfCmd.postInputOutput[i]
_, err := exp.ExpectString(output)
if err != nil {
t.Fatal(err)
}
}
}
}
err = cmd.Wait()
if err != nil {
t.Fatal(err)
}
}
})
}
if tc.validations != nil {
tc.validations(t, organization.Name)
}
}
}

View File

@ -10,6 +10,7 @@ import (
)
func Test_backend_apply_before_init(t *testing.T) {
skipIfMissingEnvVar(t)
t.Parallel()
skipWithoutRemoteTerraformVersion(t)

View File

@ -10,6 +10,7 @@ import (
)
func Test_init_with_empty_tags(t *testing.T) {
skipIfMissingEnvVar(t)
t.Parallel()
skipWithoutRemoteTerraformVersion(t)

View File

@ -23,12 +23,6 @@ var tfeToken string
var verboseMode bool
func TestMain(m *testing.M) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
hasRequiredEnvVars := accTest() && hasHostname() && hasToken()
if !hasRequiredEnvVars {
// if the above three required variables are not set, then skip all tests.
return
}
teardown := setup()
code := m.Run()
teardown()
@ -50,6 +44,16 @@ func hasToken() bool {
return os.Getenv("TFE_TOKEN") != ""
}
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`")
}
}
func setup() func() {
tfOutput := flag.Bool("tfoutput", false, "This flag produces the terraform output from tests.")
flag.Parse()
@ -64,41 +68,38 @@ func setup() func() {
}
func setTfeClient() {
hostname := os.Getenv("TFE_HOSTNAME")
token := os.Getenv("TFE_TOKEN")
if hostname == "" {
log.Fatal("hostname cannot be empty")
}
if token == "" {
log.Fatal("token cannot be empty")
}
tfeHostname = hostname
tfeToken = token
tfeHostname = os.Getenv("TFE_HOSTNAME")
tfeToken = os.Getenv("TFE_TOKEN")
cfg := &tfe.Config{
Address: fmt.Sprintf("https://%s", hostname),
Token: token,
Address: fmt.Sprintf("https://%s", tfeHostname),
Token: tfeToken,
}
// Create a new TFE client.
client, err := tfe.NewClient(cfg)
if err != nil {
log.Fatal(err)
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
}
tfeClient = client
}
func setupBinary() func() {
log.Println("Setting up terraform binary")
tmpTerraformBinaryDir, err := ioutil.TempDir("", "terraform-test")
if err != nil {
log.Fatal(err)
fmt.Printf("Could not create temp directory: %v\n", err)
os.Exit(1)
}
log.Println(tmpTerraformBinaryDir)
currentDir, err := os.Getwd()
defer os.Chdir(currentDir)
if err != nil {
log.Fatal(err)
fmt.Printf("Could not change directories: %v\n", err)
os.Exit(1)
}
// Getting top level dir
dirPaths := strings.Split(currentDir, "/")
@ -107,7 +108,8 @@ func setupBinary() func() {
topDir := strings.Join(dirPaths[0:topLevel], "/")
if err := os.Chdir(topDir); err != nil {
log.Fatal(err)
fmt.Printf("Could not change directories: %v\n", err)
os.Exit(1)
}
cmd := exec.Command(
@ -118,7 +120,8 @@ func setupBinary() func() {
)
err = cmd.Run()
if err != nil {
log.Fatal(err)
fmt.Printf("Could not run exec command: %v\n", err)
os.Exit(1)
}
credFile := fmt.Sprintf("%s/dev.tfrc", tmpTerraformBinaryDir)
@ -136,11 +139,13 @@ func writeCredRC(file string) {
creds := credentialBlock()
f, err := os.Create(file)
if err != nil {
log.Fatal(err)
fmt.Printf("Could not create file: %v\n", err)
os.Exit(1)
}
_, err = f.WriteString(creds)
if err != nil {
log.Fatal(err)
fmt.Printf("Could not write credentials: %v\n", err)
os.Exit(1)
}
f.Close()
}

View File

@ -13,6 +13,7 @@ import (
)
func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -233,6 +234,7 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) {
}
func Test_migrate_multi_to_tfc_cloud_tags_strategy(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()

View File

@ -12,6 +12,7 @@ import (
)
func Test_migrate_remote_backend_name_to_tfc_name(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -133,6 +134,7 @@ func Test_migrate_remote_backend_name_to_tfc_name(t *testing.T) {
}
func Test_migrate_remote_backend_name_to_tfc_same_name(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
operations := []operationSets{
@ -251,6 +253,7 @@ func Test_migrate_remote_backend_name_to_tfc_same_name(t *testing.T) {
}
func Test_migrate_remote_backend_name_to_tfc_name_different_org(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -377,6 +380,7 @@ func Test_migrate_remote_backend_name_to_tfc_name_different_org(t *testing.T) {
}
func Test_migrate_remote_backend_name_to_tfc_tags(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -509,6 +513,7 @@ func Test_migrate_remote_backend_name_to_tfc_tags(t *testing.T) {
}
func Test_migrate_remote_backend_prefix_to_tfc_name_strategy_single_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -632,6 +637,7 @@ func Test_migrate_remote_backend_prefix_to_tfc_name_strategy_single_workspace(t
}
func Test_migrate_remote_backend_prefix_to_tfc_name_strategy_multi_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -785,6 +791,7 @@ func Test_migrate_remote_backend_prefix_to_tfc_name_strategy_multi_workspace(t *
}
func Test_migrate_remote_backend_prefix_to_tfc_tags_strategy_single_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -909,6 +916,7 @@ func Test_migrate_remote_backend_prefix_to_tfc_tags_strategy_single_workspace(t
}
func Test_migrate_remote_backend_prefix_to_tfc_tags_strategy_multi_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()

View File

@ -12,6 +12,7 @@ import (
)
func Test_migrate_single_to_tfc(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()

View File

@ -10,6 +10,7 @@ import (
)
func Test_migrate_tfc_to_other(t *testing.T) {
skipIfMissingEnvVar(t)
cases := map[string]struct {
operations []operationSets
}{

View File

@ -13,6 +13,7 @@ import (
)
func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()
@ -290,6 +291,7 @@ func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
}
func Test_migrate_tfc_to_tfc_multiple_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()

View File

@ -45,6 +45,7 @@ output "test_env" {
}
func Test_cloud_run_variables(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
cases := testCases{