1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
13
gc "launchpad.net/gocheck"
15
coretesting "launchpad.net/juju-core/testing"
16
"launchpad.net/juju-core/tools"
17
"launchpad.net/juju-core/version"
20
var _ = gc.Suite(&DiskManagerSuite{})
22
var _ tools.ToolsManager = (*tools.DiskManager)(nil)
24
type DiskManagerSuite struct {
25
coretesting.LoggingSuite
27
manager tools.ToolsManager
30
func (s *DiskManagerSuite) SetUpTest(c *gc.C) {
31
s.LoggingSuite.SetUpTest(c)
33
s.manager = tools.NewDiskManager(s.dataDir)
36
func (s *DiskManagerSuite) toolsDir() string {
37
// TODO: Somehow hide this behind the DataManager
38
return filepath.Join(s.dataDir, "tools")
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"),
48
URL: "http://foo/bar",
49
Binary: version.MustParseBinary("1.2.3-foo-bar"),
52
err := s.manager.UnpackTools(t1, bytes.NewReader(coretesting.TarGz(files...)))
53
c.Assert(err, gc.IsNil)
54
assertDirNames(c, s.toolsDir(), []string{"1.2.3-foo-bar"})
55
s.assertToolsContents(c, t1, files)
57
// Try to unpack the same version of tools again - it should succeed,
58
// leaving the original version around.
61
Binary: version.MustParseBinary("1.2.3-foo-bar"),
63
files2 := []*coretesting.TarFile{
64
coretesting.NewTarFile("bar", 0755, "bar2 contents"),
65
coretesting.NewTarFile("x", 0755, "x contents"),
67
err = s.manager.UnpackTools(t2, bytes.NewReader(coretesting.TarGz(files2...)))
68
c.Assert(err, gc.IsNil)
69
assertDirNames(c, s.toolsDir(), []string{"1.2.3-foo-bar"})
70
s.assertToolsContents(c, t1, files)
73
func (t *DiskManagerSuite) TestSharedToolsDir(c *gc.C) {
74
manager := tools.NewDiskManager("/var/lib/juju")
75
dir := manager.SharedToolsDir(version.MustParseBinary("1.2.3-precise-amd64"))
76
c.Assert(dir, gc.Equals, "/var/lib/juju/tools/1.2.3-precise-amd64")
79
const urlFile = "downloaded-url.txt"
81
// assertToolsContents asserts that the directory for the tools
82
// has the given contents.
83
func (s *DiskManagerSuite) assertToolsContents(c *gc.C, t *tools.Tools, files []*coretesting.TarFile) {
84
var wantNames []string
85
for _, f := range files {
86
wantNames = append(wantNames, f.Header.Name)
88
wantNames = append(wantNames, urlFile)
89
dir := s.manager.(*tools.DiskManager).SharedToolsDir(t.Binary)
90
assertDirNames(c, dir, wantNames)
91
assertFileContents(c, dir, urlFile, t.URL, 0200)
92
for _, f := range files {
93
assertFileContents(c, dir, f.Header.Name, f.Contents, 0400)
95
gotTools, err := s.manager.ReadTools(t.Binary)
96
c.Assert(err, gc.IsNil)
97
c.Assert(*gotTools, gc.Equals, *t)
100
// assertFileContents asserts that the given file in the
101
// given directory has the given contents.
102
func assertFileContents(c *gc.C, dir, file, contents string, mode os.FileMode) {
103
file = filepath.Join(dir, file)
104
info, err := os.Stat(file)
105
c.Assert(err, gc.IsNil)
106
c.Assert(info.Mode()&(os.ModeType|mode), gc.Equals, mode)
107
data, err := ioutil.ReadFile(file)
108
c.Assert(err, gc.IsNil)
109
c.Assert(string(data), gc.Equals, contents)
112
// assertDirNames asserts that the given directory
113
// holds the given file or directory names.
114
func assertDirNames(c *gc.C, dir string, names []string) {
115
f, err := os.Open(dir)
116
c.Assert(err, gc.IsNil)
118
dnames, err := f.Readdirnames(0)
119
c.Assert(err, gc.IsNil)
122
c.Assert(dnames, gc.DeepEquals, names)