github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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/symlink"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/agent/tools"
    18  	"github.com/juju/juju/juju/names"
    19  	"github.com/juju/juju/version"
    20  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    21  )
    22  
    23  type ToolsSuite struct {
    24  	dataDir, toolsDir string
    25  }
    26  
    27  var _ = gc.Suite(&ToolsSuite{})
    28  
    29  func (s *ToolsSuite) SetUpTest(c *gc.C) {
    30  	s.dataDir = c.MkDir()
    31  	s.toolsDir = tools.SharedToolsDir(s.dataDir, version.Current)
    32  	err := os.MkdirAll(s.toolsDir, 0755)
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	err = symlink.New(s.toolsDir, tools.ToolsDir(s.dataDir, "unit-u-123"))
    35  	c.Assert(err, jc.ErrorIsNil)
    36  }
    37  
    38  func (s *ToolsSuite) TestEnsureSymlinks(c *gc.C) {
    39  	s.testEnsureSymlinks(c, s.toolsDir)
    40  }
    41  
    42  func (s *ToolsSuite) TestEnsureSymlinksSymlinkedDir(c *gc.C) {
    43  	toolsDirSymlink := filepath.Join(c.MkDir(), "unit-ubuntu-0")
    44  	err := symlink.New(s.toolsDir, toolsDirSymlink)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	s.testEnsureSymlinks(c, toolsDirSymlink)
    47  }
    48  
    49  func (s *ToolsSuite) testEnsureSymlinks(c *gc.C, dir string) {
    50  	jujudPath := filepath.Join(s.toolsDir, names.Jujud)
    51  	err := ioutil.WriteFile(jujudPath, []byte("assume sane"), 0755)
    52  	c.Assert(err, jc.ErrorIsNil)
    53  
    54  	assertLink := func(path string) time.Time {
    55  		target, err := symlink.Read(path)
    56  		c.Assert(err, jc.ErrorIsNil)
    57  		c.Assert(target, jc.SamePath, jujudPath)
    58  		fi, err := os.Lstat(path)
    59  		c.Assert(err, jc.ErrorIsNil)
    60  		return fi.ModTime()
    61  	}
    62  
    63  	// Check that EnsureSymlinks writes appropriate symlinks.
    64  	err = jujuc.EnsureSymlinks(dir)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	mtimes := map[string]time.Time{}
    67  	for _, name := range jujuc.CommandNames() {
    68  		tool := filepath.Join(s.toolsDir, name)
    69  		mtimes[tool] = assertLink(tool)
    70  	}
    71  
    72  	// Check that EnsureSymlinks doesn't overwrite things that don't need to be.
    73  	err = jujuc.EnsureSymlinks(s.toolsDir)
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	for tool, mtime := range mtimes {
    76  		c.Assert(assertLink(tool), gc.Equals, mtime)
    77  	}
    78  }
    79  
    80  func (s *ToolsSuite) TestEnsureSymlinksBadDir(c *gc.C) {
    81  	err := jujuc.EnsureSymlinks(filepath.Join(c.MkDir(), "noexist"))
    82  	c.Assert(err, gc.ErrorMatches, "cannot initialize hook commands in .*: "+utils.NoSuchFileErrRegexp)
    83  }