github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/testing/base.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/juju/loggo"
    11  	"github.com/juju/testing"
    12  	"github.com/juju/utils"
    13  	"github.com/juju/utils/featureflag"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/juju/osenv"
    17  	"github.com/juju/juju/network"
    18  	"github.com/juju/juju/wrench"
    19  )
    20  
    21  var logger = loggo.GetLogger("juju.testing")
    22  
    23  // JujuOSEnvSuite isolates the tests from Juju environment variables.
    24  // This is intended to be only used by existing suites, usually embedded in
    25  // BaseSuite and in FakeJujuHomeSuite. Eventually the tests relying on
    26  // JujuOSEnvSuite will be converted to use the IsolationSuite in
    27  // github.com/juju/testing, and this suite will be removed.
    28  // Do not use JujuOSEnvSuite when writing new tests.
    29  type JujuOSEnvSuite struct {
    30  	oldJujuHome    string
    31  	oldHomeEnv     string
    32  	oldEnvironment map[string]string
    33  }
    34  
    35  func (s *JujuOSEnvSuite) SetUpSuite(c *gc.C) {
    36  }
    37  
    38  func (s *JujuOSEnvSuite) TearDownSuite(c *gc.C) {
    39  }
    40  
    41  func (s *JujuOSEnvSuite) SetUpTest(c *gc.C) {
    42  	s.oldEnvironment = make(map[string]string)
    43  	for _, name := range []string{
    44  		osenv.JujuHomeEnvKey,
    45  		osenv.JujuEnvEnvKey,
    46  		osenv.JujuLoggingConfigEnvKey,
    47  		osenv.JujuFeatureFlagEnvKey,
    48  	} {
    49  		s.oldEnvironment[name] = os.Getenv(name)
    50  		os.Setenv(name, "")
    51  	}
    52  	s.oldHomeEnv = utils.Home()
    53  	utils.SetHome("")
    54  	// Update the feature flag set to be empty (given we have just set the
    55  	// environment value to the empty string)
    56  	featureflag.SetFlagsFromEnvironment(osenv.JujuFeatureFlagEnvKey)
    57  	s.oldJujuHome = osenv.SetJujuHome("")
    58  }
    59  
    60  func (s *JujuOSEnvSuite) TearDownTest(c *gc.C) {
    61  	for name, value := range s.oldEnvironment {
    62  		os.Setenv(name, value)
    63  	}
    64  	utils.SetHome(s.oldHomeEnv)
    65  	osenv.SetJujuHome(s.oldJujuHome)
    66  }
    67  
    68  func (s *JujuOSEnvSuite) SetFeatureFlags(flag ...string) {
    69  	flags := strings.Join(flag, ",")
    70  	if err := os.Setenv(osenv.JujuFeatureFlagEnvKey, flags); err != nil {
    71  		panic(err)
    72  	}
    73  	logger.Debugf("setting feature flags: %s", flags)
    74  	featureflag.SetFlagsFromEnvironment(osenv.JujuFeatureFlagEnvKey)
    75  }
    76  
    77  // BaseSuite provides required functionality for all test suites
    78  // when embedded in a gocheck suite type:
    79  // - logger redirect
    80  // - no outgoing network access
    81  // - protection of user's home directory
    82  // - scrubbing of env vars
    83  // TODO (frankban) 2014-06-09: switch to using IsolationSuite.
    84  // NOTE: there will be many tests that fail when you try to change
    85  // to the IsolationSuite that rely on external things in PATH.
    86  type BaseSuite struct {
    87  	testing.CleanupSuite
    88  	testing.LoggingSuite
    89  	JujuOSEnvSuite
    90  }
    91  
    92  func (s *BaseSuite) SetUpSuite(c *gc.C) {
    93  	wrench.SetEnabled(false)
    94  	s.CleanupSuite.SetUpSuite(c)
    95  	s.LoggingSuite.SetUpSuite(c)
    96  	s.JujuOSEnvSuite.SetUpSuite(c)
    97  	s.PatchValue(&utils.OutgoingAccessAllowed, false)
    98  }
    99  
   100  func (s *BaseSuite) TearDownSuite(c *gc.C) {
   101  	s.JujuOSEnvSuite.TearDownSuite(c)
   102  	s.LoggingSuite.TearDownSuite(c)
   103  	s.CleanupSuite.TearDownSuite(c)
   104  }
   105  
   106  func (s *BaseSuite) SetUpTest(c *gc.C) {
   107  	s.CleanupSuite.SetUpTest(c)
   108  	s.LoggingSuite.SetUpTest(c)
   109  	s.JujuOSEnvSuite.SetUpTest(c)
   110  
   111  	// We do this to isolate invocations of bash from pulling in the
   112  	// ambient user environment, and potentially affecting the tests.
   113  	// We can't always just use IsolationSuite because we still need
   114  	// PATH and possibly a couple other envars.
   115  	s.PatchEnvironment("BASH_ENV", "")
   116  	network.ResetGobalPreferIPv6()
   117  }
   118  
   119  func (s *BaseSuite) TearDownTest(c *gc.C) {
   120  	s.JujuOSEnvSuite.TearDownTest(c)
   121  	s.LoggingSuite.TearDownTest(c)
   122  	s.CleanupSuite.TearDownTest(c)
   123  }