github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/meterstatus/context_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package meterstatus_test 5 6 import ( 7 "runtime" 8 9 jc "github.com/juju/testing/checkers" 10 "github.com/juju/utils/keyvalues" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/worker/meterstatus" 14 ) 15 16 type ContextSuite struct{} 17 18 var _ = gc.Suite(&ContextSuite{}) 19 20 type dummyPaths struct{} 21 22 func (*dummyPaths) GetToolsDir() string { return "/dummy/tools" } 23 func (*dummyPaths) GetCharmDir() string { return "/dummy/charm" } 24 func (*dummyPaths) GetJujucSocket() string { return "/dummy/jujuc.sock" } 25 func (*dummyPaths) GetMetricsSpoolDir() string { return "/dummy/spool" } 26 func (*dummyPaths) ComponentDir(name string) string { return "/dummy/" + name } 27 28 func (s *ContextSuite) TestHookContextEnv(c *gc.C) { 29 ctx := meterstatus.NewLimitedContext("u/0") 30 paths := &dummyPaths{} 31 vars, err := ctx.HookVars(paths) 32 c.Assert(err, jc.ErrorIsNil) 33 varMap, err := keyvalues.Parse(vars, true) 34 c.Assert(err, jc.ErrorIsNil) 35 c.Assert(varMap["JUJU_AGENT_SOCKET"], gc.Equals, "/dummy/jujuc.sock") 36 c.Assert(varMap["JUJU_UNIT_NAME"], gc.Equals, "u/0") 37 key := "PATH" 38 if runtime.GOOS == "windows" { 39 key = "Path" 40 } 41 c.Assert(varMap[key], gc.Not(gc.Equals), "") 42 } 43 44 func (s *ContextSuite) TestHookContextSetEnv(c *gc.C) { 45 ctx := meterstatus.NewLimitedContext("u/0") 46 setVars := map[string]string{ 47 "somekey": "somevalue", 48 "anotherkey": "anothervalue", 49 } 50 ctx.SetEnvVars(setVars) 51 paths := &dummyPaths{} 52 vars, err := ctx.HookVars(paths) 53 c.Assert(err, jc.ErrorIsNil) 54 varMap, err := keyvalues.Parse(vars, true) 55 c.Assert(err, jc.ErrorIsNil) 56 for key, value := range setVars { 57 c.Assert(varMap[key], gc.Equals, value) 58 } 59 c.Assert(varMap["JUJU_AGENT_SOCKET"], gc.Equals, "/dummy/jujuc.sock") 60 c.Assert(varMap["JUJU_UNIT_NAME"], gc.Equals, "u/0") 61 }