github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 jujuos "github.com/juju/os" 13 "github.com/juju/proxy" 14 jc "github.com/juju/testing/checkers" 15 "github.com/juju/utils/keyvalues" 16 "github.com/juju/version" 17 gc "gopkg.in/check.v1" 18 "gopkg.in/juju/names.v2" 19 20 "github.com/juju/juju/testing" 21 jujuversion "github.com/juju/juju/version" 22 "github.com/juju/juju/worker/uniter/runner/context" 23 ) 24 25 type EnvSuite struct { 26 testing.BaseSuite 27 } 28 29 var _ = gc.Suite(&EnvSuite{}) 30 31 func (s *EnvSuite) assertVars(c *gc.C, actual []string, expect ...[]string) { 32 var fullExpect []string 33 for _, someExpect := range expect { 34 fullExpect = append(fullExpect, someExpect...) 35 } 36 sort.Strings(actual) 37 sort.Strings(fullExpect) 38 c.Assert(actual, jc.DeepEquals, fullExpect) 39 } 40 41 func (s *EnvSuite) getPaths() (paths context.Paths, expectVars []string) { 42 // note: path-munging is os-dependent, not included in expectVars 43 return MockEnvPaths{}, []string{ 44 "CHARM_DIR=path-to-charm", 45 "JUJU_CHARM_DIR=path-to-charm", 46 "JUJU_AGENT_SOCKET=path-to-jujuc.socket", 47 } 48 } 49 50 func (s *EnvSuite) getContext(newProxyOnly bool) (ctx *context.HookContext, expectVars []string) { 51 var ( 52 legacyProxy proxy.Settings 53 jujuProxy proxy.Settings 54 proxy = proxy.Settings{ 55 Http: "some-http-proxy", 56 Https: "some-https-proxy", 57 Ftp: "some-ftp-proxy", 58 NoProxy: "some-no-proxy", 59 } 60 ) 61 if newProxyOnly { 62 jujuProxy = proxy 63 } else { 64 legacyProxy = proxy 65 } 66 67 expected := []string{ 68 "JUJU_CONTEXT_ID=some-context-id", 69 "JUJU_MODEL_UUID=model-uuid-deadbeef", 70 "JUJU_PRINCIPAL_UNIT=this-unit/123", 71 "JUJU_MODEL_NAME=some-model-name", 72 "JUJU_UNIT_NAME=this-unit/123", 73 "JUJU_METER_STATUS=PURPLE", 74 "JUJU_METER_INFO=proceed with care", 75 "JUJU_SLA=essential", 76 "JUJU_API_ADDRESSES=he.re:12345 the.re:23456", 77 "JUJU_MACHINE_ID=42", 78 "JUJU_AVAILABILITY_ZONE=some-zone", 79 "JUJU_VERSION=1.2.3", 80 } 81 if newProxyOnly { 82 expected = append(expected, 83 "JUJU_CHARM_HTTP_PROXY=some-http-proxy", 84 "JUJU_CHARM_HTTPS_PROXY=some-https-proxy", 85 "JUJU_CHARM_FTP_PROXY=some-ftp-proxy", 86 "JUJU_CHARM_NO_PROXY=some-no-proxy", 87 ) 88 } else { 89 expected = append(expected, 90 "http_proxy=some-http-proxy", 91 "HTTP_PROXY=some-http-proxy", 92 "https_proxy=some-https-proxy", 93 "HTTPS_PROXY=some-https-proxy", 94 "ftp_proxy=some-ftp-proxy", 95 "FTP_PROXY=some-ftp-proxy", 96 "no_proxy=some-no-proxy", 97 "NO_PROXY=some-no-proxy", 98 // JUJU_CHARM prefixed proxy values are always specified 99 // even if empty. 100 "JUJU_CHARM_HTTP_PROXY=", 101 "JUJU_CHARM_HTTPS_PROXY=", 102 "JUJU_CHARM_FTP_PROXY=", 103 "JUJU_CHARM_NO_PROXY=", 104 ) 105 } 106 // It doesn't make sense that we set both legacy and juju proxy 107 // settings, but by setting both to different values, we can see 108 // what the environment values are. 109 return context.NewModelHookContext( 110 "some-context-id", 111 "model-uuid-deadbeef", 112 "some-model-name", 113 "this-unit/123", 114 "PURPLE", 115 "proceed with care", 116 "essential", 117 "some-zone", 118 []string{"he.re:12345", "the.re:23456"}, 119 legacyProxy, jujuProxy, 120 names.NewMachineTag("42"), 121 ), expected 122 } 123 124 func (s *EnvSuite) setRelation(ctx *context.HookContext) (expectVars []string) { 125 context.SetEnvironmentHookContextRelation( 126 ctx, 22, "an-endpoint", "that-unit/456", 127 ) 128 return []string{ 129 "JUJU_RELATION=an-endpoint", 130 "JUJU_RELATION_ID=an-endpoint:22", 131 "JUJU_REMOTE_UNIT=that-unit/456", 132 } 133 } 134 135 func (s *EnvSuite) TestEnvSetsPath(c *gc.C) { 136 paths := context.OSDependentEnvVars(MockEnvPaths{}) 137 c.Assert(paths, gc.Not(gc.HasLen), 0) 138 vars, err := keyvalues.Parse(paths, true) 139 c.Assert(err, jc.ErrorIsNil) 140 key := "PATH" 141 if runtime.GOOS == "windows" { 142 key = "Path" 143 } 144 c.Assert(vars[key], gc.Not(gc.Equals), "") 145 } 146 147 func (s *EnvSuite) TestEnvWindows(c *gc.C) { 148 s.PatchValue(&jujuos.HostOS, func() jujuos.OSType { return jujuos.Windows }) 149 s.PatchValue(&jujuversion.Current, version.MustParse("1.2.3")) 150 os.Setenv("Path", "foo;bar") 151 os.Setenv("PSModulePath", "ping;pong") 152 windowsVars := []string{ 153 "Path=path-to-tools;foo;bar", 154 "PSModulePath=ping;pong;" + filepath.FromSlash("path-to-charm/lib/Modules"), 155 } 156 157 ctx, contextVars := s.getContext(false) 158 paths, pathsVars := s.getPaths() 159 actualVars, err := ctx.HookVars(paths) 160 c.Assert(err, jc.ErrorIsNil) 161 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars) 162 163 relationVars := s.setRelation(ctx) 164 actualVars, err = ctx.HookVars(paths) 165 c.Assert(err, jc.ErrorIsNil) 166 s.assertVars(c, actualVars, contextVars, pathsVars, windowsVars, relationVars) 167 } 168 169 func (s *EnvSuite) TestEnvUbuntu(c *gc.C) { 170 s.PatchValue(&jujuos.HostOS, func() jujuos.OSType { return jujuos.Ubuntu }) 171 s.PatchValue(&jujuversion.Current, version.MustParse("1.2.3")) 172 os.Setenv("PATH", "foo:bar") 173 ubuntuVars := []string{ 174 "PATH=path-to-tools:foo:bar", 175 "APT_LISTCHANGES_FRONTEND=none", 176 "DEBIAN_FRONTEND=noninteractive", 177 } 178 179 ctx, contextVars := s.getContext(false) 180 paths, pathsVars := s.getPaths() 181 actualVars, err := ctx.HookVars(paths) 182 c.Assert(err, jc.ErrorIsNil) 183 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars) 184 185 relationVars := s.setRelation(ctx) 186 actualVars, err = ctx.HookVars(paths) 187 c.Assert(err, jc.ErrorIsNil) 188 s.assertVars(c, actualVars, contextVars, pathsVars, ubuntuVars, relationVars) 189 }