~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/tools/diskmanager_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "io/ioutil"
 
9
        "os"
 
10
        "path/filepath"
 
11
        "sort"
 
12
 
 
13
        gc "launchpad.net/gocheck"
 
14
 
 
15
        coretesting "launchpad.net/juju-core/testing"
 
16
        "launchpad.net/juju-core/tools"
 
17
        "launchpad.net/juju-core/version"
 
18
)
 
19
 
 
20
var _ = gc.Suite(&DiskManagerSuite{})
 
21
 
 
22
var _ tools.ToolsManager = (*tools.DiskManager)(nil)
 
23
 
 
24
type DiskManagerSuite struct {
 
25
        coretesting.LoggingSuite
 
26
        dataDir string
 
27
        manager tools.ToolsManager
 
28
}
 
29
 
 
30
func (s *DiskManagerSuite) SetUpTest(c *gc.C) {
 
31
        s.LoggingSuite.SetUpTest(c)
 
32
        s.dataDir = c.MkDir()
 
33
        s.manager = tools.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
        t1 := &tools.Tools{
 
48
                URL:    "http://foo/bar",
 
49
                Binary: version.MustParseBinary("1.2.3-foo-bar"),
 
50
        }
 
51
 
 
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)
 
56
 
 
57
        // Try to unpack the same version of tools again - it should succeed,
 
58
        // leaving the original version around.
 
59
        t2 := &tools.Tools{
 
60
                URL:    "http://arble",
 
61
                Binary: version.MustParseBinary("1.2.3-foo-bar"),
 
62
        }
 
63
        files2 := []*coretesting.TarFile{
 
64
                coretesting.NewTarFile("bar", 0755, "bar2 contents"),
 
65
                coretesting.NewTarFile("x", 0755, "x contents"),
 
66
        }
 
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)
 
71
}
 
72
 
 
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")
 
77
}
 
78
 
 
79
const urlFile = "downloaded-url.txt"
 
80
 
 
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)
 
87
        }
 
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)
 
94
        }
 
95
        gotTools, err := s.manager.ReadTools(t.Binary)
 
96
        c.Assert(err, gc.IsNil)
 
97
        c.Assert(*gotTools, gc.Equals, *t)
 
98
}
 
99
 
 
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)
 
110
}
 
111
 
 
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)
 
117
        defer f.Close()
 
118
        dnames, err := f.Readdirnames(0)
 
119
        c.Assert(err, gc.IsNil)
 
120
        sort.Strings(dnames)
 
121
        sort.Strings(names)
 
122
        c.Assert(dnames, gc.DeepEquals, names)
 
123
}