github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 11 "github.com/juju/names" 12 envtesting "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 "github.com/juju/utils/proxy" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/version" 18 "github.com/juju/juju/worker/uniter/runner" 19 ) 20 21 type MergeEnvSuite struct { 22 envtesting.IsolationSuite 23 } 24 25 var _ = gc.Suite(&MergeEnvSuite{}) 26 27 func (e *MergeEnvSuite) TestMergeEnviron(c *gc.C) { 28 // environment does not get fully cleared on Windows 29 // when using testing.IsolationSuite 30 origEnv := os.Environ() 31 extraExpected := []string{ 32 "DUMMYVAR=foo", 33 "DUMMYVAR2=bar", 34 "NEWVAR=ImNew", 35 } 36 expectEnv := append(origEnv, extraExpected...) 37 os.Setenv("DUMMYVAR2", "ChangeMe") 38 os.Setenv("DUMMYVAR", "foo") 39 40 newEnv := runner.MergeEnvironment([]string{"DUMMYVAR2=bar", "NEWVAR=ImNew"}) 41 c.Assert(expectEnv, jc.SameContents, newEnv) 42 } 43 44 type EnvSuite struct { 45 envtesting.IsolationSuite 46 } 47 48 var _ = gc.Suite(&EnvSuite{}) 49 50 func (s *EnvSuite) assertVars(c *gc.C, actual []string, expect ...[]string) { 51 var fullExpect []string 52 for _, someExpect := range expect { 53 fullExpect = append(fullExpect, someExpect...) 54 } 55 sort.Strings(actual) 56 sort.Strings(fullExpect) 57 c.Assert(actual, jc.DeepEquals, fullExpect) 58 } 59 60 func (s *EnvSuite) getPaths() (paths runner.Paths, expectVars []string) { 61 // note: path-munging is os-dependent, not included in expectVars 62 return MockEnvPaths{}, []string{ 63 "CHARM_DIR=path-to-charm", 64 "JUJU_CHARM_DIR=path-to-charm", 65 "JUJU_AGENT_SOCKET=path-to-jujuc.socket", 66 } 67 } 68 69 func (s *EnvSuite) getContext() (ctx *runner.HookContext, expectVars []string) { 70 return runner.NewEnvironmentHookContext( 71 "some-context-id", 72 "env-uuid-deadbeef", 73 "some-env-name", 74 "this-unit/123", 75 "PURPLE", 76 "proceed with care", 77 "some-zone", 78 []string{"he.re:12345", "the.re:23456"}, 79 proxy.Settings{ 80 Http: "some-http-proxy", 81 Https: "some-https-proxy", 82 Ftp: "some-ftp-proxy", 83 NoProxy: "some-no-proxy", 84 }, 85 names.NewMachineTag("42"), 86 ), []string{ 87 "JUJU_CONTEXT_ID=some-context-id", 88 "JUJU_ENV_UUID=env-uuid-deadbeef", 89 "JUJU_ENV_NAME=some-env-name", 90 "JUJU_UNIT_NAME=this-unit/123", 91 "JUJU_METER_STATUS=PURPLE", 92 "JUJU_METER_INFO=proceed with care", 93 "JUJU_API_ADDRESSES=he.re:12345 the.re:23456", 94 "JUJU_MACHINE_ID=42", 95 "JUJU_AVAILABILITY_ZONE=some-zone", 96 "http_proxy=some-http-proxy", 97 "HTTP_PROXY=some-http-proxy", 98 "https_proxy=some-https-proxy", 99 "HTTPS_PROXY=some-https-proxy", 100 "ftp_proxy=some-ftp-proxy", 101 "FTP_PROXY=some-ftp-proxy", 102 "no_proxy=some-no-proxy", 103 "NO_PROXY=some-no-proxy", 104 } 105 } 106 107 func (s *EnvSuite) setRelation(ctx *runner.HookContext) (expectVars []string) { 108 runner.SetEnvironmentHookContextRelation( 109 ctx, 22, "an-endpoint", "that-unit/456", 110 ) 111 return []string{ 112 "JUJU_RELATION=an-endpoint", 113 "JUJU_RELATION_ID=an-endpoint:22", 114 "JUJU_REMOTE_UNIT=that-unit/456", 115 } 116 } 117 118 func (s *EnvSuite) TestEnvWindows(c *gc.C) { 119 s.PatchValue(&version.Current.OS, version.Windows) 120 os.Setenv("Path", "foo;bar") 121 os.Setenv("PSModulePath", "ping;pong") 122 windowsVars := []string{ 123 "Path=path-to-tools;foo;bar", 124 "PSModulePath=ping;pong;" + filepath.FromSlash("path-to-charm/lib/Modules"), 125 } 126 127 ctx, contextVars := s.getContext() 128 paths, pathsVars := s.getPaths() 129 actualVars := ctx.HookVars(paths) 130 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars) 131 132 relationVars := s.setRelation(ctx) 133 actualVars = ctx.HookVars(paths) 134 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars, relationVars) 135 } 136 137 func (s *EnvSuite) TestEnvUbuntu(c *gc.C) { 138 s.PatchValue(&version.Current.OS, version.Ubuntu) 139 os.Setenv("PATH", "foo:bar") 140 ubuntuVars := []string{ 141 "PATH=path-to-tools:foo:bar", 142 "APT_LISTCHANGES_FRONTEND=none", 143 "DEBIAN_FRONTEND=noninteractive", 144 } 145 146 ctx, contextVars := s.getContext() 147 paths, pathsVars := s.getPaths() 148 actualVars := ctx.HookVars(paths) 149 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars) 150 151 relationVars := s.setRelation(ctx) 152 actualVars = ctx.HookVars(paths) 153 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars, relationVars) 154 }