github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/agent/tools/diskmanager_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package tools_test
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/json"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/version"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	agenttools "github.com/juju/juju/agent/tools"
    17  	coretesting "github.com/juju/juju/testing"
    18  	coretools "github.com/juju/juju/tools"
    19  )
    20  
    21  var _ = gc.Suite(&DiskManagerSuite{})
    22  
    23  var _ agenttools.ToolsManager = (*agenttools.DiskManager)(nil)
    24  
    25  type DiskManagerSuite struct {
    26  	coretesting.BaseSuite
    27  	dataDir string
    28  	manager agenttools.ToolsManager
    29  }
    30  
    31  func (s *DiskManagerSuite) SetUpTest(c *gc.C) {
    32  	s.BaseSuite.SetUpTest(c)
    33  	s.dataDir = c.MkDir()
    34  	s.manager = agenttools.NewDiskManager(s.dataDir)
    35  }
    36  
    37  func (s *DiskManagerSuite) toolsDir() string {
    38  	// TODO: Somehow hide this behind the DataManager
    39  	return filepath.Join(s.dataDir, "tools")
    40  }
    41  
    42  // Copied from environs/agent/tools_test.go
    43  func (s *DiskManagerSuite) TestUnpackToolsContents(c *gc.C) {
    44  	files := []*coretesting.TarFile{
    45  		coretesting.NewTarFile("amd64", agenttools.DirPerm, "bar contents"),
    46  		coretesting.NewTarFile("quantal", agenttools.DirPerm, "foo contents"),
    47  	}
    48  	gzfile, checksum := coretesting.TarGz(files...)
    49  	t1 := &coretools.Tools{
    50  		URL:     "http://foo/bar",
    51  		Version: version.MustParseBinary("1.2.3-quantal-amd64"),
    52  		Size:    int64(len(gzfile)),
    53  		SHA256:  checksum,
    54  	}
    55  
    56  	err := s.manager.UnpackTools(t1, bytes.NewReader(gzfile))
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	assertDirNames(c, s.toolsDir(), []string{"1.2.3-quantal-amd64"})
    59  	s.assertToolsContents(c, t1, files)
    60  
    61  	// Try to unpack the same version of tools again - it should succeed,
    62  	// leaving the original version around.
    63  	files2 := []*coretesting.TarFile{
    64  		coretesting.NewTarFile("bar", agenttools.DirPerm, "bar2 contents"),
    65  		coretesting.NewTarFile("x", agenttools.DirPerm, "x contents"),
    66  	}
    67  	gzfile2, checksum2 := coretesting.TarGz(files2...)
    68  	t2 := &coretools.Tools{
    69  		URL:     "http://arble",
    70  		Version: version.MustParseBinary("1.2.3-quantal-amd64"),
    71  		Size:    int64(len(gzfile2)),
    72  		SHA256:  checksum2,
    73  	}
    74  	err = s.manager.UnpackTools(t2, bytes.NewReader(gzfile2))
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	assertDirNames(c, s.toolsDir(), []string{"1.2.3-quantal-amd64"})
    77  	s.assertToolsContents(c, t1, files)
    78  }
    79  
    80  func (t *DiskManagerSuite) TestSharedToolsDir(c *gc.C) {
    81  	manager := agenttools.NewDiskManager("/var/lib/juju")
    82  	dir := manager.SharedToolsDir(version.MustParseBinary("1.2.3-precise-amd64"))
    83  	c.Assert(dir, gc.Equals, "/var/lib/juju/tools/1.2.3-precise-amd64")
    84  }
    85  
    86  // assertToolsContents asserts that the directory for the tools
    87  // has the given contents.
    88  func (s *DiskManagerSuite) assertToolsContents(c *gc.C, t *coretools.Tools, files []*coretesting.TarFile) {
    89  	var wantNames []string
    90  	for _, f := range files {
    91  		wantNames = append(wantNames, f.Header.Name)
    92  	}
    93  	wantNames = append(wantNames, agenttools.ToolsFile)
    94  	dir := s.manager.(*agenttools.DiskManager).SharedToolsDir(t.Version)
    95  	assertDirNames(c, dir, wantNames)
    96  	expectedFileContents, err := json.Marshal(t)
    97  	c.Assert(err, jc.ErrorIsNil)
    98  	assertFileContents(c, dir, agenttools.ToolsFile, string(expectedFileContents), 0200)
    99  	for _, f := range files {
   100  		assertFileContents(c, dir, f.Header.Name, f.Contents, 0400)
   101  	}
   102  	gotTools, err := s.manager.ReadTools(t.Version)
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(*gotTools, gc.Equals, *t)
   105  	// Make sure that the tools directory is readable by the ubuntu user (for
   106  	// juju-run)
   107  	info, err := os.Stat(dir)
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	c.Assert(info.Mode().Perm(), gc.Equals, os.FileMode(agenttools.DirPerm))
   110  }