github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/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/internal/e2e"
    10  )
    11  
    12  var terraformBin string
    13  
    14  // canRunGoBuild is a short-term compromise to account for the fact that we
    15  // have a small number of tests that work by building helper programs using
    16  // "go build" at runtime, but we can't do that in our isolated test mode
    17  // driven by the make-archive.sh script.
    18  //
    19  // FIXME: Rework this a bit so that we build the necessary helper programs
    20  // (test plugins, etc) as part of the initial suite setup, and in the
    21  // make-archive.sh script, so that we can run all of the tests in both
    22  // situations with the tests just using the executable already built for
    23  // them, as we do for terraformBin.
    24  var canRunGoBuild bool
    25  
    26  func TestMain(m *testing.M) {
    27  	teardown := setup()
    28  	code := m.Run()
    29  	teardown()
    30  	os.Exit(code)
    31  }
    32  
    33  func setup() func() {
    34  	if terraformBin != "" {
    35  		// this is pre-set when we're running in a binary produced from
    36  		// the make-archive.sh script, since that is for testing an
    37  		// executable obtained from a real release package. However, we do
    38  		// need to turn it into an absolute path so that we can find it
    39  		// when we change the working directory during tests.
    40  		var err error
    41  		terraformBin, err = filepath.Abs(terraformBin)
    42  		if err != nil {
    43  			panic(fmt.Sprintf("failed to find absolute path of terraform executable: %s", err))
    44  		}
    45  		return func() {}
    46  	}
    47  
    48  	tmpFilename := e2e.GoBuild("github.com/hashicorp/terraform", "terraform")
    49  
    50  	// Make the executable available for use in tests
    51  	terraformBin = tmpFilename
    52  
    53  	// Tests running in the ad-hoc testing mode are allowed to use "go build"
    54  	// and similar to produce other test executables.
    55  	// (See the comment on this variable's declaration for more information.)
    56  	canRunGoBuild = true
    57  
    58  	return func() {
    59  		os.Remove(tmpFilename)
    60  	}
    61  }
    62  
    63  func canAccessNetwork() bool {
    64  	// We re-use the flag normally used for acceptance tests since that's
    65  	// established as a way to opt-in to reaching out to real systems that
    66  	// may suffer transient errors.
    67  	return os.Getenv("TF_ACC") != ""
    68  }
    69  
    70  func skipIfCannotAccessNetwork(t *testing.T) {
    71  	t.Helper()
    72  
    73  	if !canAccessNetwork() {
    74  		t.Skip("network access not allowed; use TF_ACC=1 to enable")
    75  	}
    76  }