github.com/gavriloga/terraform@v0.11.12-beta1/command/e2etest/main_test.go (about)

     1  package e2etest
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/e2e"
    10  )
    11  
    12  var terraformBin string
    13  
    14  func TestMain(m *testing.M) {
    15  	teardown := setup()
    16  	code := m.Run()
    17  	teardown()
    18  	os.Exit(code)
    19  }
    20  
    21  func setup() func() {
    22  	if terraformBin != "" {
    23  		// this is pre-set when we're running in a binary produced from
    24  		// the make-archive.sh script, since that builds a ready-to-go
    25  		// binary into the archive. However, we do need to turn it into
    26  		// an absolute path so that we can find it when we change the
    27  		// working directory during tests.
    28  		var err error
    29  		terraformBin, err = filepath.Abs(terraformBin)
    30  		if err != nil {
    31  			panic(fmt.Sprintf("failed to find absolute path of terraform executable: %s", err))
    32  		}
    33  		return func() {}
    34  	}
    35  
    36  	tmpFilename := e2e.GoBuild("github.com/hashicorp/terraform", "terraform")
    37  
    38  	// Make the executable available for use in tests
    39  	terraformBin = tmpFilename
    40  
    41  	return func() {
    42  		os.Remove(tmpFilename)
    43  	}
    44  }
    45  
    46  func canAccessNetwork() bool {
    47  	// We re-use the flag normally used for acceptance tests since that's
    48  	// established as a way to opt-in to reaching out to real systems that
    49  	// may suffer transient errors.
    50  	return os.Getenv("TF_ACC") != ""
    51  }
    52  
    53  func skipIfCannotAccessNetwork(t *testing.T) {
    54  	t.Helper()
    55  
    56  	if !canAccessNetwork() {
    57  		t.Skip("network access not allowed; use TF_ACC=1 to enable")
    58  	}
    59  }