forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charm_test.go
112 lines (99 loc) · 2.8 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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charm_test
import (
"bytes"
"io"
"io/ioutil"
stdtesting "testing"
"github.com/juju/charm"
charmtesting "github.com/juju/charm/testing"
gc "launchpad.net/gocheck"
"launchpad.net/goyaml"
)
func Test(t *stdtesting.T) {
gc.TestingT(t)
}
type CharmSuite struct{}
var _ = gc.Suite(&CharmSuite{})
func (s *CharmSuite) TestRead(c *gc.C) {
bPath := charmtesting.Charms.BundlePath(c.MkDir(), "dummy")
ch, err := charm.Read(bPath)
c.Assert(err, gc.IsNil)
c.Assert(ch.Meta().Name, gc.Equals, "dummy")
dPath := charmtesting.Charms.DirPath("dummy")
ch, err = charm.Read(dPath)
c.Assert(err, gc.IsNil)
c.Assert(ch.Meta().Name, gc.Equals, "dummy")
}
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)
curl, err := charm.InferURL(t.url, "precise")
c.Assert(err, gc.IsNil)
repo, err := charm.InferRepository(curl.Reference, "/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)
}
}
curl, err := charm.InferURL("local:whatever", "precise")
c.Assert(err, gc.IsNil)
_, err = charm.InferRepository(curl.Reference, "")
c.Assert(err, gc.ErrorMatches, "path to local repository not specified")
curl.Schema = "foo"
_, err = charm.InferRepository(curl.Reference, "")
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.Bundle:
c.Assert(f.Path, gc.Equals, path)
case *charm.Dir:
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 = goyaml.Unmarshal(data, m)
if err != nil {
panic(err)
}
return YamlHacker(m)
}
func (yh YamlHacker) Reader() io.Reader {
data, err := goyaml.Marshal(yh)
if err != nil {
panic(err)
}
return bytes.NewBuffer(data)
}