github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/context/env_test.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package context_test 5 6 import ( 7 "os" 8 "path/filepath" 9 "runtime" 10 "sort" 11 12 envtesting "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 "github.com/juju/utils/keyvalues" 15 "github.com/juju/utils/proxy" 16 gc "gopkg.in/check.v1" 17 "gopkg.in/juju/names.v2" 18 19 "github.com/juju/juju/worker/uniter/runner/context" 20 jujuos "github.com/juju/utils/os" 21 ) 22 23 type EnvSuite struct { 24 envtesting.IsolationSuite 25 } 26 27 var _ = gc.Suite(&EnvSuite{}) 28 29 func (s *EnvSuite) assertVars(c *gc.C, actual []string, expect ...[]string) { 30 var fullExpect []string 31 for _, someExpect := range expect { 32 fullExpect = append(fullExpect, someExpect...) 33 } 34 sort.Strings(actual) 35 sort.Strings(fullExpect) 36 c.Assert(actual, jc.DeepEquals, fullExpect) 37 } 38 39 func (s *EnvSuite) getPaths() (paths context.Paths, expectVars []string) { 40 // note: path-munging is os-dependent, not included in expectVars 41 return MockEnvPaths{}, []string{ 42 "CHARM_DIR=path-to-charm", 43 "JUJU_CHARM_DIR=path-to-charm", 44 "JUJU_AGENT_SOCKET=path-to-jujuc.socket", 45 } 46 } 47 48 func (s *EnvSuite) getContext() (ctx *context.HookContext, expectVars []string) { 49 return context.NewModelHookContext( 50 "some-context-id", 51 "model-uuid-deadbeef", 52 "some-model-name", 53 "this-unit/123", 54 "PURPLE", 55 "proceed with care", 56 "some-zone", 57 []string{"he.re:12345", "the.re:23456"}, 58 proxy.Settings{ 59 Http: "some-http-proxy", 60 Https: "some-https-proxy", 61 Ftp: "some-ftp-proxy", 62 NoProxy: "some-no-proxy", 63 }, 64 names.NewMachineTag("42"), 65 ), []string{ 66 "JUJU_CONTEXT_ID=some-context-id", 67 "JUJU_MODEL_UUID=model-uuid-deadbeef", 68 "JUJU_MODEL_NAME=some-model-name", 69 "JUJU_UNIT_NAME=this-unit/123", 70 "JUJU_METER_STATUS=PURPLE", 71 "JUJU_METER_INFO=proceed with care", 72 "JUJU_API_ADDRESSES=he.re:12345 the.re:23456", 73 "JUJU_MACHINE_ID=42", 74 "JUJU_AVAILABILITY_ZONE=some-zone", 75 "http_proxy=some-http-proxy", 76 "HTTP_PROXY=some-http-proxy", 77 "https_proxy=some-https-proxy", 78 "HTTPS_PROXY=some-https-proxy", 79 "ftp_proxy=some-ftp-proxy", 80 "FTP_PROXY=some-ftp-proxy", 81 "no_proxy=some-no-proxy", 82 "NO_PROXY=some-no-proxy", 83 } 84 } 85 86 func (s *EnvSuite) setRelation(ctx *context.HookContext) (expectVars []string) { 87 context.SetEnvironmentHookContextRelation( 88 ctx, 22, "an-endpoint", "that-unit/456", 89 ) 90 return []string{ 91 "JUJU_RELATION=an-endpoint", 92 "JUJU_RELATION_ID=an-endpoint:22", 93 "JUJU_REMOTE_UNIT=that-unit/456", 94 } 95 } 96 97 func (s *EnvSuite) TestEnvSetsPath(c *gc.C) { 98 paths := context.OSDependentEnvVars(MockEnvPaths{}) 99 c.Assert(paths, gc.Not(gc.HasLen), 0) 100 vars, err := keyvalues.Parse(paths, true) 101 c.Assert(err, jc.ErrorIsNil) 102 key := "PATH" 103 if runtime.GOOS == "windows" { 104 key = "Path" 105 } 106 c.Assert(vars[key], gc.Not(gc.Equals), "") 107 } 108 109 func (s *EnvSuite) TestEnvWindows(c *gc.C) { 110 s.PatchValue(&jujuos.HostOS, func() jujuos.OSType { return jujuos.Windows }) 111 os.Setenv("Path", "foo;bar") 112 os.Setenv("PSModulePath", "ping;pong") 113 windowsVars := []string{ 114 "Path=path-to-tools;foo;bar", 115 "PSModulePath=ping;pong;" + filepath.FromSlash("path-to-charm/lib/Modules"), 116 } 117 118 ctx, contextVars := s.getContext() 119 paths, pathsVars := s.getPaths() 120 actualVars, err := ctx.HookVars(paths) 121 c.Assert(err, jc.ErrorIsNil) 122 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars) 123 124 relationVars := s.setRelation(ctx) 125 actualVars, err = ctx.HookVars(paths) 126 c.Assert(err, jc.ErrorIsNil) 127 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars, relationVars) 128 } 129 130 func (s *EnvSuite) TestEnvUbuntu(c *gc.C) { 131 s.PatchValue(&jujuos.HostOS, func() jujuos.OSType { return jujuos.Ubuntu }) 132 os.Setenv("PATH", "foo:bar") 133 ubuntuVars := []string{ 134 "PATH=path-to-tools:foo:bar", 135 "APT_LISTCHANGES_FRONTEND=none", 136 "DEBIAN_FRONTEND=noninteractive", 137 } 138 139 ctx, contextVars := s.getContext() 140 paths, pathsVars := s.getPaths() 141 actualVars, err := ctx.HookVars(paths) 142 c.Assert(err, jc.ErrorIsNil) 143 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars) 144 145 relationVars := s.setRelation(ctx) 146 actualVars, err = ctx.HookVars(paths) 147 c.Assert(err, jc.ErrorIsNil) 148 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars, relationVars) 149 }