github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/runner/env_test.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package runner_test 5 6 import ( 7 "os" 8 "path/filepath" 9 "sort" 10 "strings" 11 12 "github.com/juju/names" 13 envtesting "github.com/juju/testing" 14 jc "github.com/juju/testing/checkers" 15 "github.com/juju/utils/proxy" 16 gc "gopkg.in/check.v1" 17 18 "github.com/juju/juju/version" 19 "github.com/juju/juju/worker/uniter/runner" 20 ) 21 22 type MergeEnvSuite struct { 23 envtesting.IsolationSuite 24 } 25 26 var _ = gc.Suite(&MergeEnvSuite{}) 27 28 func (e *MergeEnvSuite) TestMergeEnviron(c *gc.C) { 29 // environment does not get fully cleared on Windows 30 // when using testing.IsolationSuite 31 origEnv := os.Environ() 32 extraExpected := []string{ 33 "DUMMYVAR=foo", 34 "DUMMYVAR2=bar", 35 "NEWVAR=ImNew", 36 } 37 expectEnv := make([]string, 0, len(origEnv)+len(extraExpected)) 38 39 // os.Environ prepends some garbage on Windows that we need to strip out. 40 // All the garbage starts and ends with = (for example "=C:="). 41 for _, v := range origEnv { 42 if !(strings.HasPrefix(v, "=") && strings.HasSuffix(v, "=")) { 43 expectEnv = append(expectEnv, v) 44 } 45 } 46 expectEnv = append(expectEnv, extraExpected...) 47 os.Setenv("DUMMYVAR2", "ChangeMe") 48 os.Setenv("DUMMYVAR", "foo") 49 50 newEnv := make([]string, 0, len(expectEnv)) 51 for _, v := range runner.MergeWindowsEnvironment([]string{"dummyvar2=bar", "NEWVAR=ImNew"}, os.Environ()) { 52 if !(strings.HasPrefix(v, "=") && strings.HasSuffix(v, "=")) { 53 newEnv = append(newEnv, v) 54 } 55 } 56 c.Assert(expectEnv, jc.SameContents, newEnv) 57 } 58 59 func (s *MergeEnvSuite) TestMergeEnvWin(c *gc.C) { 60 initial := []string{"a=foo", "b=bar", "foo=val"} 61 newValues := []string{"a=baz", "c=omg", "FOO=val2", "d=another"} 62 63 created := runner.MergeWindowsEnvironment(newValues, initial) 64 expected := []string{"a=baz", "b=bar", "c=omg", "foo=val2", "d=another"} 65 c.Check(created, jc.SameContents, expected) 66 } 67 68 type EnvSuite struct { 69 envtesting.IsolationSuite 70 } 71 72 var _ = gc.Suite(&EnvSuite{}) 73 74 func (s *EnvSuite) assertVars(c *gc.C, actual []string, expect ...[]string) { 75 var fullExpect []string 76 for _, someExpect := range expect { 77 fullExpect = append(fullExpect, someExpect...) 78 } 79 sort.Strings(actual) 80 sort.Strings(fullExpect) 81 c.Assert(actual, jc.DeepEquals, fullExpect) 82 } 83 84 func (s *EnvSuite) getPaths() (paths runner.Paths, expectVars []string) { 85 // note: path-munging is os-dependent, not included in expectVars 86 return MockEnvPaths{}, []string{ 87 "CHARM_DIR=path-to-charm", 88 "JUJU_CHARM_DIR=path-to-charm", 89 "JUJU_AGENT_SOCKET=path-to-jujuc.socket", 90 } 91 } 92 93 func (s *EnvSuite) getContext() (ctx *runner.HookContext, expectVars []string) { 94 return runner.NewEnvironmentHookContext( 95 "some-context-id", 96 "env-uuid-deadbeef", 97 "some-env-name", 98 "this-unit/123", 99 "PURPLE", 100 "proceed with care", 101 "some-zone", 102 []string{"he.re:12345", "the.re:23456"}, 103 proxy.Settings{ 104 Http: "some-http-proxy", 105 Https: "some-https-proxy", 106 Ftp: "some-ftp-proxy", 107 NoProxy: "some-no-proxy", 108 }, 109 names.NewMachineTag("42"), 110 ), []string{ 111 "JUJU_CONTEXT_ID=some-context-id", 112 "JUJU_ENV_UUID=env-uuid-deadbeef", 113 "JUJU_ENV_NAME=some-env-name", 114 "JUJU_UNIT_NAME=this-unit/123", 115 "JUJU_METER_STATUS=PURPLE", 116 "JUJU_METER_INFO=proceed with care", 117 "JUJU_API_ADDRESSES=he.re:12345 the.re:23456", 118 "JUJU_MACHINE_ID=42", 119 "JUJU_AVAILABILITY_ZONE=some-zone", 120 "http_proxy=some-http-proxy", 121 "HTTP_PROXY=some-http-proxy", 122 "https_proxy=some-https-proxy", 123 "HTTPS_PROXY=some-https-proxy", 124 "ftp_proxy=some-ftp-proxy", 125 "FTP_PROXY=some-ftp-proxy", 126 "no_proxy=some-no-proxy", 127 "NO_PROXY=some-no-proxy", 128 } 129 } 130 131 func (s *EnvSuite) setRelation(ctx *runner.HookContext) (expectVars []string) { 132 runner.SetEnvironmentHookContextRelation( 133 ctx, 22, "an-endpoint", "that-unit/456", 134 ) 135 return []string{ 136 "JUJU_RELATION=an-endpoint", 137 "JUJU_RELATION_ID=an-endpoint:22", 138 "JUJU_REMOTE_UNIT=that-unit/456", 139 } 140 } 141 142 func (s *EnvSuite) TestEnvWindows(c *gc.C) { 143 s.PatchValue(&version.Current.OS, version.Windows) 144 os.Setenv("Path", "foo;bar") 145 os.Setenv("PSModulePath", "ping;pong") 146 windowsVars := []string{ 147 "Path=path-to-tools;foo;bar", 148 "PSModulePath=ping;pong;" + filepath.FromSlash("path-to-charm/lib/Modules"), 149 } 150 151 ctx, contextVars := s.getContext() 152 paths, pathsVars := s.getPaths() 153 actualVars := ctx.HookVars(paths) 154 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars) 155 156 relationVars := s.setRelation(ctx) 157 actualVars = ctx.HookVars(paths) 158 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars, relationVars) 159 } 160 161 func (s *EnvSuite) TestEnvUbuntu(c *gc.C) { 162 s.PatchValue(&version.Current.OS, version.Ubuntu) 163 os.Setenv("PATH", "foo:bar") 164 ubuntuVars := []string{ 165 "PATH=path-to-tools:foo:bar", 166 "APT_LISTCHANGES_FRONTEND=none", 167 "DEBIAN_FRONTEND=noninteractive", 168 } 169 170 ctx, contextVars := s.getContext() 171 paths, pathsVars := s.getPaths() 172 actualVars := ctx.HookVars(paths) 173 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars) 174 175 relationVars := s.setRelation(ctx) 176 actualVars = ctx.HookVars(paths) 177 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars, relationVars) 178 }