github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/tools_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "time" 11 12 jc "github.com/juju/testing/checkers" 13 "github.com/juju/utils" 14 "github.com/juju/utils/arch" 15 "github.com/juju/utils/series" 16 "github.com/juju/utils/symlink" 17 "github.com/juju/version" 18 gc "gopkg.in/check.v1" 19 20 "github.com/juju/juju/agent/tools" 21 "github.com/juju/juju/juju/names" 22 jujuversion "github.com/juju/juju/version" 23 "github.com/juju/juju/worker/uniter/runner/jujuc" 24 ) 25 26 type ToolsSuite struct { 27 dataDir, toolsDir string 28 } 29 30 var _ = gc.Suite(&ToolsSuite{}) 31 32 func (s *ToolsSuite) SetUpTest(c *gc.C) { 33 s.dataDir = c.MkDir() 34 s.toolsDir = tools.SharedToolsDir(s.dataDir, version.Binary{ 35 Number: jujuversion.Current, 36 Arch: arch.HostArch(), 37 Series: series.HostSeries(), 38 }) 39 err := os.MkdirAll(s.toolsDir, 0755) 40 c.Assert(err, jc.ErrorIsNil) 41 err = symlink.New(s.toolsDir, tools.ToolsDir(s.dataDir, "unit-u-123")) 42 c.Assert(err, jc.ErrorIsNil) 43 } 44 45 func (s *ToolsSuite) TestEnsureSymlinks(c *gc.C) { 46 s.testEnsureSymlinks(c, s.toolsDir) 47 } 48 49 func (s *ToolsSuite) TestEnsureSymlinksSymlinkedDir(c *gc.C) { 50 toolsDirSymlink := filepath.Join(c.MkDir(), "unit-ubuntu-0") 51 err := symlink.New(s.toolsDir, toolsDirSymlink) 52 c.Assert(err, jc.ErrorIsNil) 53 s.testEnsureSymlinks(c, toolsDirSymlink) 54 } 55 56 func (s *ToolsSuite) testEnsureSymlinks(c *gc.C, dir string) { 57 jujudPath := filepath.Join(s.toolsDir, names.Jujud) 58 err := ioutil.WriteFile(jujudPath, []byte("assume sane"), 0755) 59 c.Assert(err, jc.ErrorIsNil) 60 61 assertLink := func(path string) time.Time { 62 target, err := symlink.Read(path) 63 c.Assert(err, jc.ErrorIsNil) 64 c.Assert(target, jc.SamePath, jujudPath) 65 fi, err := os.Lstat(path) 66 c.Assert(err, jc.ErrorIsNil) 67 return fi.ModTime() 68 } 69 70 // Check that EnsureSymlinks writes appropriate symlinks. 71 err = jujuc.EnsureSymlinks(dir) 72 c.Assert(err, jc.ErrorIsNil) 73 mtimes := map[string]time.Time{} 74 for _, name := range jujuc.CommandNames() { 75 tool := filepath.Join(s.toolsDir, name) 76 mtimes[tool] = assertLink(tool) 77 } 78 79 // Check that EnsureSymlinks doesn't overwrite things that don't need to be. 80 err = jujuc.EnsureSymlinks(s.toolsDir) 81 c.Assert(err, jc.ErrorIsNil) 82 for tool, mtime := range mtimes { 83 c.Assert(assertLink(tool), gc.Equals, mtime) 84 } 85 } 86 87 func (s *ToolsSuite) TestEnsureSymlinksBadDir(c *gc.C) { 88 err := jujuc.EnsureSymlinks(filepath.Join(c.MkDir(), "noexist")) 89 c.Assert(err, gc.ErrorMatches, "cannot initialize hook commands in .*: "+utils.NoSuchFileErrRegexp) 90 }