github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/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 9 "github.com/juju/testing" 10 "github.com/juju/utils" 11 gc "launchpad.net/gocheck" 12 13 "github.com/juju/juju/juju/osenv" 14 ) 15 16 // JujuOSEnvSuite isolates the tests from Juju environment variables. 17 // This is intended to be only used by existing suites, usually embedded in 18 // BaseSuite and in FakeJujuHomeSuite. Eventually the tests relying on 19 // JujuOSEnvSuite will be converted to use the IsolationSuite in 20 // github.com/juju/testing, and this suite will be removed. 21 // Do not use JujuOSEnvSuite when writing new tests. 22 type JujuOSEnvSuite struct { 23 oldHomeEnv string 24 oldEnvironment map[string]string 25 } 26 27 func (s *JujuOSEnvSuite) SetUpSuite(c *gc.C) { 28 } 29 30 func (s *JujuOSEnvSuite) TearDownSuite(c *gc.C) { 31 } 32 33 func (s *JujuOSEnvSuite) SetUpTest(c *gc.C) { 34 s.oldEnvironment = make(map[string]string) 35 for _, name := range []string{ 36 osenv.JujuHomeEnvKey, 37 osenv.JujuEnvEnvKey, 38 osenv.JujuLoggingConfigEnvKey, 39 } { 40 s.oldEnvironment[name] = os.Getenv(name) 41 os.Setenv(name, "") 42 } 43 s.oldHomeEnv = utils.Home() 44 utils.SetHome("") 45 } 46 47 func (s *JujuOSEnvSuite) TearDownTest(c *gc.C) { 48 for name, value := range s.oldEnvironment { 49 os.Setenv(name, value) 50 } 51 utils.SetHome(s.oldHomeEnv) 52 } 53 54 // BaseSuite provides required functionality for all test suites 55 // when embedded in a gocheck suite type: 56 // - logger redirect 57 // - no outgoing network access 58 // - protection of user's home directory 59 // - scrubbing of env vars 60 // TODO (frankban) 2014-06-09: switch to using IsolationSuite. 61 type BaseSuite struct { 62 testing.CleanupSuite 63 testing.LoggingSuite 64 JujuOSEnvSuite 65 } 66 67 func (s *BaseSuite) SetUpSuite(c *gc.C) { 68 s.CleanupSuite.SetUpSuite(c) 69 s.LoggingSuite.SetUpSuite(c) 70 s.JujuOSEnvSuite.SetUpSuite(c) 71 s.PatchValue(&utils.OutgoingAccessAllowed, false) 72 } 73 74 func (s *BaseSuite) TearDownSuite(c *gc.C) { 75 s.JujuOSEnvSuite.TearDownSuite(c) 76 s.LoggingSuite.TearDownSuite(c) 77 s.CleanupSuite.TearDownSuite(c) 78 } 79 80 func (s *BaseSuite) SetUpTest(c *gc.C) { 81 s.CleanupSuite.SetUpTest(c) 82 s.LoggingSuite.SetUpTest(c) 83 s.JujuOSEnvSuite.SetUpTest(c) 84 } 85 86 func (s *BaseSuite) TearDownTest(c *gc.C) { 87 s.JujuOSEnvSuite.TearDownTest(c) 88 s.LoggingSuite.TearDownTest(c) 89 s.CleanupSuite.TearDownTest(c) 90 }