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) { func Test_terraform_apply_autoApprove(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() ctx := context.Background()
@ -178,76 +179,72 @@ func Test_terraform_apply_autoApprove(t *testing.T) {
}, },
}, },
} }
for name, tc := range cases { for _, tc := range cases {
tc := tc organization, cleanup := createOrganization(t)
t.Run(name, func(t *testing.T) { defer cleanup()
t.Parallel() exp, err := expect.NewConsole(defaultOpts()...)
organization, cleanup := createOrganization(t) if err != nil {
defer cleanup() t.Fatal(err)
exp, err := expect.NewConsole(defaultOpts()...) }
if err != nil { defer exp.Close()
t.Fatal(err)
}
defer exp.Close()
tmpDir, err := ioutil.TempDir("", "terraform-test") tmpDir, err := ioutil.TempDir("", "terraform-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
tf := e2e.NewBinary(terraformBin, tmpDir) tf := e2e.NewBinary(terraformBin, tmpDir)
tf.AddEnv(cliConfigFileEnv) tf.AddEnv(cliConfigFileEnv)
defer tf.Close() defer tf.Close()
for _, op := range tc.operations { for _, op := range tc.operations {
op.prep(t, organization.Name, tf.WorkDir()) op.prep(t, organization.Name, tf.WorkDir())
for _, tfCmd := range op.commands { for _, tfCmd := range op.commands {
cmd := tf.Cmd(tfCmd.command...) cmd := tf.Cmd(tfCmd.command...)
cmd.Stdin = exp.Tty() cmd.Stdin = exp.Tty()
cmd.Stdout = exp.Tty() cmd.Stdout = exp.Tty()
cmd.Stderr = exp.Tty() cmd.Stderr = exp.Tty()
err = cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if tfCmd.expectedCmdOutput != "" { if tfCmd.expectedCmdOutput != "" {
_, err := exp.ExpectString(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 err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
}
if tc.validations != nil { lenInput := len(tfCmd.userInput)
tc.validations(t, organization.Name) 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) { func Test_backend_apply_before_init(t *testing.T) {
skipIfMissingEnvVar(t)
t.Parallel() t.Parallel()
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)

View File

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

View File

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

View File

@ -13,6 +13,7 @@ import (
) )
func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) { func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_multi_to_tfc_cloud_tags_strategy(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() ctx := context.Background()

View File

@ -12,6 +12,7 @@ import (
) )
func Test_migrate_remote_backend_name_to_tfc_name(t *testing.T) { func Test_migrate_remote_backend_name_to_tfc_name(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_remote_backend_name_to_tfc_same_name(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() ctx := context.Background()
operations := []operationSets{ 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) { func Test_migrate_remote_backend_name_to_tfc_name_different_org(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_remote_backend_name_to_tfc_tags(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_remote_backend_prefix_to_tfc_name_strategy_single_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_remote_backend_prefix_to_tfc_name_strategy_multi_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_remote_backend_prefix_to_tfc_tags_strategy_single_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_remote_backend_prefix_to_tfc_tags_strategy_multi_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() ctx := context.Background()

View File

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

View File

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

View File

@ -13,6 +13,7 @@ import (
) )
func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) { func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() 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) { func Test_migrate_tfc_to_tfc_multiple_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
ctx := context.Background() ctx := context.Background()

View File

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