github.com/opentofu/opentofu@v1.7.1/internal/command/e2etest/main_test.go (about)

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