github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/testutil/testing.go (about)

     1  package testutil
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"testing"
    10  
    11  	"github.com/evergreen-ci/evergreen"
    12  )
    13  
    14  const (
    15  	envAll      = "EVERGREEN_ALL"
    16  	envOverride = "SETTINGS_OVERRIDE"
    17  )
    18  
    19  var (
    20  	// run the integration tests
    21  	runAllTests = flag.Bool("evergreen.all", false, "Run integration tests")
    22  	// path to an mci settings file containing sensitive information
    23  	settingsOverride = flag.String("evergreen.settingsOverride", "", "Settings file"+
    24  		" to be used to override sensitive info in the testing mci settings"+
    25  		" file")
    26  )
    27  
    28  // HandleTestingErr catches errors that we do not want to treat
    29  // as relevant a goconvey statement. HandleTestingErr is used
    30  // to terminate unit tests that fail for reasons that are orthogonal to
    31  // the test (filesystem errors, database errors, etc).
    32  func HandleTestingErr(err error, t *testing.T, format string, a ...interface{}) {
    33  	if err != nil {
    34  		_, file, line, ok := runtime.Caller(1)
    35  		if ok {
    36  			t.Fatalf("%v:%v: %q: %+v", file, line, fmt.Sprintf(format, a), err)
    37  		} else {
    38  			t.Fatalf("%q: %+v", fmt.Sprintf(format, a), err)
    39  		}
    40  	}
    41  }
    42  
    43  // GetDirectoryOfFile returns the path to of the file that calling
    44  // this function. Use this to ensure that references to testdata and
    45  // other file system locations in tests are not dependent on the working
    46  // directory of the "go test" invocation.
    47  func GetDirectoryOfFile() string {
    48  	_, file, _, _ := runtime.Caller(1)
    49  
    50  	return filepath.Dir(file)
    51  }
    52  
    53  // SkipTestUnlessAll skips the current test.
    54  func SkipTestUnlessAll(t *testing.T, testName string) {
    55  	// Note: in the future we could/should be able to eliminate
    56  	// the testName arg by using runtime.Caller(1)
    57  
    58  	if !(*runAllTests) && os.Getenv(envAll) == "" {
    59  		t.Skip(fmt.Sprintf("skipping %v because 'evergreen.all' is not specified...",
    60  			testName))
    61  	}
    62  }
    63  
    64  func ConfigureIntegrationTest(t *testing.T, testSettings *evergreen.Settings,
    65  	testName string) {
    66  
    67  	SkipTestUnlessAll(t, testName)
    68  
    69  	// make sure an override file is provided
    70  	if (*settingsOverride) == "" {
    71  		msg := "Integration tests need a settings override file to be provided"
    72  		if os.Getenv(envOverride) == "" {
    73  			panic(msg)
    74  		} else {
    75  			*settingsOverride = os.Getenv(envOverride)
    76  		}
    77  	}
    78  
    79  	// grab the file with the integration test settings
    80  	integrationSettings, err := evergreen.NewSettings(*settingsOverride)
    81  	if err != nil {
    82  		panic(fmt.Sprintf("Error opening settings override file %v: %v",
    83  			*settingsOverride, err))
    84  	}
    85  
    86  	// override the appropriate params
    87  	t.Logf("Loading cloud provider settings from %v", *settingsOverride)
    88  
    89  	testSettings.Providers = integrationSettings.Providers
    90  	testSettings.Credentials = integrationSettings.Credentials
    91  	testSettings.AuthConfig = integrationSettings.AuthConfig
    92  	testSettings.Plugins = integrationSettings.Plugins
    93  	testSettings.Jira = integrationSettings.Jira
    94  }