~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charmrepo.v2-unstable/testing/testcharm.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// Copyright 2012, 2013 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
3
3
 
4
 
package testing
 
4
package testing // import "gopkg.in/juju/charmrepo.v2-unstable/testing"
5
5
 
6
6
import (
7
7
        "archive/zip"
8
8
        "bytes"
9
9
        "fmt"
 
10
        "io"
10
11
        "os"
11
12
        "path"
12
13
        "strings"
15
16
        "github.com/juju/testing/filetesting"
16
17
        gc "gopkg.in/check.v1"
17
18
        "gopkg.in/juju/charm.v6-unstable"
 
19
        "gopkg.in/yaml.v2"
18
20
)
19
21
 
20
22
// Charm holds a charm for testing. It does not
61
63
        Revision int
62
64
}
63
65
 
64
 
// NewCharm returns a new charm
 
66
type file struct {
 
67
        path string
 
68
        data []byte
 
69
        perm os.FileMode
 
70
}
 
71
 
 
72
// NewCharm returns a charm following the given specification.
65
73
func NewCharm(c *gc.C, spec CharmSpec) *Charm {
 
74
        return newCharm(spec)
 
75
}
 
76
 
 
77
// newCharm is the internal version of NewCharm that
 
78
// doesn't take a *gc.C so it can be used in NewCharmWithMeta.
 
79
func newCharm(spec CharmSpec) *Charm {
66
80
        ch := &Charm{
67
81
                revision: spec.Revision,
68
82
        }
69
83
        var err error
70
84
        ch.meta, err = charm.ReadMeta(strings.NewReader(spec.Meta))
71
 
        c.Assert(err, gc.IsNil)
 
85
        if err != nil {
 
86
                panic(err)
 
87
        }
 
88
 
72
89
        ch.files = append(ch.files, filetesting.File{
73
90
                Path: "metadata.yaml",
74
91
                Data: spec.Meta,
77
94
 
78
95
        if spec.Config != "" {
79
96
                ch.config, err = charm.ReadConfig(strings.NewReader(spec.Config))
80
 
                c.Assert(err, gc.IsNil)
 
97
                if err != nil {
 
98
                        panic(err)
 
99
                }
81
100
                ch.files = append(ch.files, filetesting.File{
82
101
                        Path: "config.yaml",
83
102
                        Data: spec.Config,
86
105
        }
87
106
        if spec.Actions != "" {
88
107
                ch.actions, err = charm.ReadActionsYaml(strings.NewReader(spec.Actions))
89
 
                c.Assert(err, gc.IsNil)
 
108
                if err != nil {
 
109
                        panic(err)
 
110
                }
90
111
                ch.files = append(ch.files, filetesting.File{
91
112
                        Path: "actions.yaml",
92
113
                        Data: spec.Actions,
95
116
        }
96
117
        if spec.Metrics != "" {
97
118
                ch.metrics, err = charm.ReadMetrics(strings.NewReader(spec.Metrics))
98
 
                c.Assert(err, gc.IsNil)
 
119
                if err != nil {
 
120
                        panic(err)
 
121
                }
99
122
                ch.files = append(ch.files, filetesting.File{
100
123
                        Path: "metrics.yaml",
101
124
                        Data: spec.Metrics,
127
150
        return ch
128
151
}
129
152
 
 
153
// NewCharmMeta returns a charm with the given metadata.
 
154
// It doesn't take a *gc.C, so it can be used at init time,
 
155
// for example in table-driven tests.
 
156
func NewCharmMeta(meta *charm.Meta) *Charm {
 
157
        if meta == nil {
 
158
                meta = new(charm.Meta)
 
159
        }
 
160
        metaYAML, err := yaml.Marshal(meta)
 
161
        if err != nil {
 
162
                panic(err)
 
163
        }
 
164
        return newCharm(CharmSpec{
 
165
                Meta: string(metaYAML),
 
166
        })
 
167
}
 
168
 
130
169
// Meta implements charm.Charm.Meta.
131
170
func (ch *Charm) Meta() *charm.Meta {
132
171
        return ch.meta
173
212
        return ch.archiveBytes
174
213
}
175
214
 
 
215
// ArchiveTo implements ArchiveTo as implemented
 
216
// by *charm.Dir, enabling the charm to be used in some APIs
 
217
// that check for that method.
 
218
func (c *Charm) ArchiveTo(w io.Writer) error {
 
219
        _, err := w.Write(c.ArchiveBytes())
 
220
        return err
 
221
}
 
222
 
 
223
// Size returns the size of the charm's archive blob.
 
224
func (c *Charm) Size() int64 {
 
225
        return int64(len(c.ArchiveBytes()))
 
226
}
 
227
 
176
228
func (ch *Charm) makeArchive() {
177
229
        var buf bytes.Buffer
178
230
        zw := zip.NewWriter(&buf)