~juju-qa/ubuntu/trusty/juju/juju-1.25.8

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charm.v5/charm_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-12-02 18:01:10 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161202180110-dl1helep8qfebmhx
ImportĀ upstreamĀ 1.25.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2011, 2012, 2013 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package charm_test
 
5
 
 
6
import (
 
7
        "bytes"
 
8
        "io"
 
9
        "io/ioutil"
 
10
        "path/filepath"
 
11
        stdtesting "testing"
 
12
 
 
13
        jc "github.com/juju/testing/checkers"
 
14
        gc "gopkg.in/check.v1"
 
15
        "gopkg.in/yaml.v1"
 
16
 
 
17
        "gopkg.in/juju/charm.v5"
 
18
        charmtesting "gopkg.in/juju/charm.v5/testing"
 
19
)
 
20
 
 
21
func Test(t *stdtesting.T) {
 
22
        gc.TestingT(t)
 
23
}
 
24
 
 
25
var TestCharms = charmtesting.NewRepo("internal/test-charm-repo", "quantal")
 
26
 
 
27
type CharmSuite struct{}
 
28
 
 
29
var _ = gc.Suite(&CharmSuite{})
 
30
 
 
31
func (s *CharmSuite) TestReadCharm(c *gc.C) {
 
32
        bPath := TestCharms.CharmArchivePath(c.MkDir(), "dummy")
 
33
        ch, err := charm.ReadCharm(bPath)
 
34
        c.Assert(err, gc.IsNil)
 
35
        c.Assert(ch.Meta().Name, gc.Equals, "dummy")
 
36
        dPath := TestCharms.CharmDirPath("dummy")
 
37
        ch, err = charm.ReadCharm(dPath)
 
38
        c.Assert(err, gc.IsNil)
 
39
        c.Assert(ch.Meta().Name, gc.Equals, "dummy")
 
40
}
 
41
 
 
42
func (s *CharmSuite) TestReadCharmDirError(c *gc.C) {
 
43
        ch, err := charm.ReadCharm(c.MkDir())
 
44
        c.Assert(err, gc.NotNil)
 
45
        c.Assert(ch, gc.Equals, nil)
 
46
}
 
47
 
 
48
func (s *CharmSuite) TestReadCharmArchiveError(c *gc.C) {
 
49
        path := filepath.Join(c.MkDir(), "path")
 
50
        err := ioutil.WriteFile(path, []byte("foo"), 0644)
 
51
        c.Assert(err, gc.IsNil)
 
52
        ch, err := charm.ReadCharm(path)
 
53
        c.Assert(err, gc.NotNil)
 
54
        c.Assert(ch, gc.Equals, nil)
 
55
}
 
56
 
 
57
func checkDummy(c *gc.C, f charm.Charm, path string) {
 
58
        c.Assert(f.Revision(), gc.Equals, 1)
 
59
        c.Assert(f.Meta().Name, gc.Equals, "dummy")
 
60
        c.Assert(f.Config().Options["title"].Default, gc.Equals, "My Title")
 
61
        c.Assert(f.Actions(), jc.DeepEquals,
 
62
                &charm.Actions{
 
63
                        map[string]charm.ActionSpec{
 
64
                                "snapshot": charm.ActionSpec{
 
65
                                        Description: "Take a snapshot of the database.",
 
66
                                        Params: map[string]interface{}{
 
67
                                                "type":        "object",
 
68
                                                "description": "Take a snapshot of the database.",
 
69
                                                "title":       "snapshot",
 
70
                                                "properties": map[string]interface{}{
 
71
                                                        "outfile": map[string]interface{}{
 
72
                                                                "description": "The file to write out to.",
 
73
                                                                "type":        "string",
 
74
                                                                "default":     "foo.bz2",
 
75
                                                        }}}}}})
 
76
        switch f := f.(type) {
 
77
        case *charm.CharmArchive:
 
78
                c.Assert(f.Path, gc.Equals, path)
 
79
        case *charm.CharmDir:
 
80
                c.Assert(f.Path, gc.Equals, path)
 
81
        }
 
82
}
 
83
 
 
84
type YamlHacker map[interface{}]interface{}
 
85
 
 
86
func ReadYaml(r io.Reader) YamlHacker {
 
87
        data, err := ioutil.ReadAll(r)
 
88
        if err != nil {
 
89
                panic(err)
 
90
        }
 
91
        m := make(map[interface{}]interface{})
 
92
        err = yaml.Unmarshal(data, m)
 
93
        if err != nil {
 
94
                panic(err)
 
95
        }
 
96
        return YamlHacker(m)
 
97
}
 
98
 
 
99
func (yh YamlHacker) Reader() io.Reader {
 
100
        data, err := yaml.Marshal(yh)
 
101
        if err != nil {
 
102
                panic(err)
 
103
        }
 
104
        return bytes.NewBuffer(data)
 
105
}