Skip to content

Commit

Permalink
Add skip for cloud e2e tests when env vars missing
Browse files Browse the repository at this point in the history
  • Loading branch information
omarismail committed Nov 15, 2021
1 parent 57a4b51 commit 9b675f8
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 85 deletions.
109 changes: 53 additions & 56 deletions internal/cloud/e2e/apply_auto_approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func Test_terraform_apply_autoApprove(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)

ctx := context.Background()
Expand Down Expand Up @@ -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()
err = cmd.Start()
if err != nil {
t.Fatal(err)
}

if tfCmd.expectedCmdOutput != "" {
_, err := exp.ExpectString(tfCmd.expectedCmdOutput)
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)
}
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)
}
err = cmd.Wait()
if err != nil {
t.Fatal(err)
}
}
}

if tc.validations != nil {
tc.validations(t, organization.Name)
}
})
if tc.validations != nil {
tc.validations(t, organization.Name)
}
}
}
1 change: 1 addition & 0 deletions internal/cloud/e2e/backend_apply_before_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func Test_backend_apply_before_init(t *testing.T) {
skipIfMissingEnvVar(t)
t.Parallel()
skipWithoutRemoteTerraformVersion(t)

Expand Down
1 change: 1 addition & 0 deletions internal/cloud/e2e/init_with_empty_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func Test_init_with_empty_tags(t *testing.T) {
skipIfMissingEnvVar(t)
t.Parallel()
skipWithoutRemoteTerraformVersion(t)

Expand Down
63 changes: 34 additions & 29 deletions internal/cloud/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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, "/")
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/cloud/e2e/migrate_state_multi_to_tfc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)

ctx := context.Background()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func Test_migrate_remote_backend_name_to_tfc_name(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)

ctx := context.Background()
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions internal/cloud/e2e/migrate_state_single_to_tfc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func Test_migrate_single_to_tfc(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)

ctx := context.Background()
Expand Down
1 change: 1 addition & 0 deletions internal/cloud/e2e/migrate_state_tfc_to_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func Test_migrate_tfc_to_other(t *testing.T) {
skipIfMissingEnvVar(t)
cases := map[string]struct {
operations []operationSets
}{
Expand Down
2 changes: 2 additions & 0 deletions internal/cloud/e2e/migrate_state_tfc_to_tfc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)
ctx := context.Background()

Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions internal/cloud/e2e/run_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ output "test_env" {
}

func Test_cloud_run_variables(t *testing.T) {
skipIfMissingEnvVar(t)
skipWithoutRemoteTerraformVersion(t)

cases := testCases{
Expand Down

0 comments on commit 9b675f8

Please sign in to comment.