forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharm_test.go
131 lines (114 loc) · 3.27 KB
/
charm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charm_test
import (
"bytes"
"io"
"io/ioutil"
"path/filepath"
stdtesting "testing"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v1"
"gopkg.in/juju/charm.v4"
charmtesting "gopkg.in/juju/charm.v4/testing"
)
func Test(t *stdtesting.T) {
gc.TestingT(t)
}
var TestCharms = charmtesting.NewRepo("internal/test-charm-repo", "quantal")
type CharmSuite struct{}
var _ = gc.Suite(&CharmSuite{})
func (s *CharmSuite) TestReadCharm(c *gc.C) {
bPath := TestCharms.CharmArchivePath(c.MkDir(), "dummy")
ch, err := charm.ReadCharm(bPath)
c.Assert(err, gc.IsNil)
c.Assert(ch.Meta().Name, gc.Equals, "dummy")
dPath := TestCharms.CharmDirPath("dummy")
ch, err = charm.ReadCharm(dPath)
c.Assert(err, gc.IsNil)
c.Assert(ch.Meta().Name, gc.Equals, "dummy")
}
func (s *CharmSuite) TestReadCharmDirError(c *gc.C) {
ch, err := charm.ReadCharm(c.MkDir())
c.Assert(err, gc.NotNil)
c.Assert(ch, gc.Equals, nil)
}
func (s *CharmSuite) TestReadCharmArchiveError(c *gc.C) {
path := filepath.Join(c.MkDir(), "path")
err := ioutil.WriteFile(path, []byte("foo"), 0644)
c.Assert(err, gc.IsNil)
ch, err := charm.ReadCharm(path)
c.Assert(err, gc.NotNil)
c.Assert(ch, gc.Equals, nil)
}
var inferRepoTests = []struct {
url string
path string
}{
{"cs:precise/wordpress", ""},
{"local:oneiric/wordpress", "/some/path"},
}
func (s *CharmSuite) TestInferRepository(c *gc.C) {
for i, t := range inferRepoTests {
c.Logf("test %d", i)
ref, err := charm.ParseReference(t.url)
c.Assert(err, gc.IsNil)
repo, err := charm.InferRepository(ref, "/some/path")
c.Assert(err, gc.IsNil)
switch repo := repo.(type) {
case *charm.LocalRepository:
c.Assert(repo.Path, gc.Equals, t.path)
default:
c.Assert(repo, gc.Equals, charm.Store)
}
}
ref, err := charm.ParseReference("local:whatever")
c.Assert(err, gc.IsNil)
_, err = charm.InferRepository(ref, "")
c.Assert(err, gc.ErrorMatches, "path to local repository not specified")
ref.Schema = "foo"
_, err = charm.InferRepository(ref, "")
c.Assert(err, gc.ErrorMatches, "unknown schema for charm reference.*")
}
func checkDummy(c *gc.C, f charm.Charm, path string) {
c.Assert(f.Revision(), gc.Equals, 1)
c.Assert(f.Meta().Name, gc.Equals, "dummy")
c.Assert(f.Config().Options["title"].Default, gc.Equals, "My Title")
c.Assert(f.Actions(), gc.DeepEquals,
&charm.Actions{
map[string]charm.ActionSpec{
"snapshot": charm.ActionSpec{
Description: "Take a snapshot of the database.",
Params: map[string]interface{}{
"outfile": map[string]interface{}{
"description": "The file to write out to.",
"type": "string",
"default": "foo.bz2",
}}}}})
switch f := f.(type) {
case *charm.CharmArchive:
c.Assert(f.Path, gc.Equals, path)
case *charm.CharmDir:
c.Assert(f.Path, gc.Equals, path)
}
}
type YamlHacker map[interface{}]interface{}
func ReadYaml(r io.Reader) YamlHacker {
data, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
m := make(map[interface{}]interface{})
err = yaml.Unmarshal(data, m)
if err != nil {
panic(err)
}
return YamlHacker(m)
}
func (yh YamlHacker) Reader() io.Reader {
data, err := yaml.Marshal(yh)
if err != nil {
panic(err)
}
return bytes.NewBuffer(data)
}