launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	"path/filepath"
    10  
    11  	gc "launchpad.net/gocheck"
    12  
    13  	agenttools "launchpad.net/juju-core/agent/tools"
    14  	coretesting "launchpad.net/juju-core/testing"
    15  	"launchpad.net/juju-core/testing/testbase"
    16  	coretools "launchpad.net/juju-core/tools"
    17  	"launchpad.net/juju-core/version"
    18  )
    19  
    20  var _ = gc.Suite(&DiskManagerSuite{})
    21  
    22  var _ agenttools.ToolsManager = (*agenttools.DiskManager)(nil)
    23  
    24  type DiskManagerSuite struct {
    25  	testbase.LoggingSuite
    26  	dataDir string
    27  	manager agenttools.ToolsManager
    28  }
    29  
    30  func (s *DiskManagerSuite) SetUpTest(c *gc.C) {
    31  	s.LoggingSuite.SetUpTest(c)
    32  	s.dataDir = c.MkDir()
    33  	s.manager = agenttools.NewDiskManager(s.dataDir)
    34  }
    35  
    36  func (s *DiskManagerSuite) toolsDir() string {
    37  	// TODO: Somehow hide this behind the DataManager
    38  	return filepath.Join(s.dataDir, "tools")
    39  }
    40  
    41  // Copied from environs/agent/tools_test.go
    42  func (s *DiskManagerSuite) TestUnpackToolsContents(c *gc.C) {
    43  	files := []*coretesting.TarFile{
    44  		coretesting.NewTarFile("bar", 0755, "bar contents"),
    45  		coretesting.NewTarFile("foo", 0755, "foo contents"),
    46  	}
    47  	gzfile, checksum := coretesting.TarGz(files...)
    48  	t1 := &coretools.Tools{
    49  		URL:     "http://foo/bar",
    50  		Version: version.MustParseBinary("1.2.3-foo-bar"),
    51  		Size:    int64(len(gzfile)),
    52  		SHA256:  checksum,
    53  	}
    54  
    55  	err := s.manager.UnpackTools(t1, bytes.NewReader(gzfile))
    56  	c.Assert(err, gc.IsNil)
    57  	assertDirNames(c, s.toolsDir(), []string{"1.2.3-foo-bar"})
    58  	s.assertToolsContents(c, t1, files)
    59  
    60  	// Try to unpack the same version of tools again - it should succeed,
    61  	// leaving the original version around.
    62  	files2 := []*coretesting.TarFile{
    63  		coretesting.NewTarFile("bar", 0755, "bar2 contents"),
    64  		coretesting.NewTarFile("x", 0755, "x contents"),
    65  	}
    66  	gzfile2, checksum2 := coretesting.TarGz(files2...)
    67  	t2 := &coretools.Tools{
    68  		URL:     "http://arble",
    69  		Version: version.MustParseBinary("1.2.3-foo-bar"),
    70  		Size:    int64(len(gzfile2)),
    71  		SHA256:  checksum2,
    72  	}
    73  	err = s.manager.UnpackTools(t2, bytes.NewReader(gzfile2))
    74  	c.Assert(err, gc.IsNil)
    75  	assertDirNames(c, s.toolsDir(), []string{"1.2.3-foo-bar"})
    76  	s.assertToolsContents(c, t1, files)
    77  }
    78  
    79  func (t *DiskManagerSuite) TestSharedToolsDir(c *gc.C) {
    80  	manager := agenttools.NewDiskManager("/var/lib/juju")
    81  	dir := manager.SharedToolsDir(version.MustParseBinary("1.2.3-precise-amd64"))
    82  	c.Assert(dir, gc.Equals, "/var/lib/juju/tools/1.2.3-precise-amd64")
    83  }
    84  
    85  // assertToolsContents asserts that the directory for the tools
    86  // has the given contents.
    87  func (s *DiskManagerSuite) assertToolsContents(c *gc.C, t *coretools.Tools, files []*coretesting.TarFile) {
    88  	var wantNames []string
    89  	for _, f := range files {
    90  		wantNames = append(wantNames, f.Header.Name)
    91  	}
    92  	wantNames = append(wantNames, toolsFile)
    93  	dir := s.manager.(*agenttools.DiskManager).SharedToolsDir(t.Version)
    94  	assertDirNames(c, dir, wantNames)
    95  	expectedFileContents, err := json.Marshal(t)
    96  	c.Assert(err, gc.IsNil)
    97  	assertFileContents(c, dir, toolsFile, string(expectedFileContents), 0200)
    98  	for _, f := range files {
    99  		assertFileContents(c, dir, f.Header.Name, f.Contents, 0400)
   100  	}
   101  	gotTools, err := s.manager.ReadTools(t.Version)
   102  	c.Assert(err, gc.IsNil)
   103  	c.Assert(*gotTools, gc.Equals, *t)
   104  }