1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
// Package testcharms holds a corpus of charms
11
jc "github.com/juju/testing/checkers"
12
gc "gopkg.in/check.v1"
13
"gopkg.in/juju/charm.v6-unstable"
14
"gopkg.in/juju/charmrepo.v2-unstable/csclient"
15
"gopkg.in/juju/charmrepo.v2-unstable/csclient/params"
16
"gopkg.in/juju/charmrepo.v2-unstable/testing"
19
// Repo provides access to the test charm repository.
20
var Repo = testing.NewRepo("charm-repo", "quantal")
22
// UploadCharm uploads a charm using the given charm store client, and returns
23
// the resulting charm URL and charm.
25
// It also adds any required resources that haven't already been uploaded
26
// with the content "<resourcename> content".
27
func UploadCharm(c *gc.C, client *csclient.Client, url, name string) (*charm.URL, charm.Charm) {
28
id := charm.MustParseURL(url)
29
promulgatedRevision := -1
31
// We still need a user even if we are uploading a promulgated charm.
33
promulgatedRevision = id.Revision
35
ch := Repo.CharmArchive(c.MkDir(), name)
38
err := client.UploadCharmWithRevision(id, ch, promulgatedRevision)
39
c.Assert(err, jc.ErrorIsNil)
41
// Upload any resources required for publishing.
42
var resources map[string]int
43
if len(ch.Meta().Resources) > 0 {
44
// The charm has resources.
45
// Ensure that all the required resources are uploaded
47
resources = make(map[string]int)
48
current, err := client.WithChannel(params.UnpublishedChannel).ListResources(id)
49
c.Assert(err, gc.IsNil)
50
for _, r := range current {
52
// The resource doesn't exist so upload one.
53
_, err := client.UploadResource(id, r.Name, "", strings.NewReader(r.Name+" content"))
54
c.Assert(err, jc.ErrorIsNil)
57
resources[r.Name] = r.Revision
61
SetPublicWithResources(c, client, id, resources)
66
// UploadCharmMultiSeries uploads a charm with revision using the given charm store client,
67
// and returns the resulting charm URL and charm. This API caters for new multi-series charms
68
// which do not specify a series in the URL.
69
func UploadCharmMultiSeries(c *gc.C, client *csclient.Client, url, name string) (*charm.URL, charm.Charm) {
70
id := charm.MustParseURL(url)
72
// We still need a user even if we are uploading a promulgated charm.
75
ch := Repo.CharmArchive(c.MkDir(), name)
78
curl, err := client.UploadCharm(id, ch)
79
c.Assert(err, jc.ErrorIsNil)
81
SetPublic(c, client, curl)
83
// Return the charm and its URL.
87
// UploadBundle uploads a bundle using the given charm store client, and
88
// returns the resulting bundle URL and bundle.
89
func UploadBundle(c *gc.C, client *csclient.Client, url, name string) (*charm.URL, charm.Bundle) {
90
id := charm.MustParseURL(url)
91
promulgatedRevision := -1
93
// We still need a user even if we are uploading a promulgated bundle.
95
promulgatedRevision = id.Revision
97
b := Repo.BundleArchive(c.MkDir(), name)
100
err := client.UploadBundleWithRevision(id, b, promulgatedRevision)
101
c.Assert(err, jc.ErrorIsNil)
103
SetPublic(c, client, id)
105
// Return the bundle and its URL.
109
// SetPublicWithResources sets the charm or bundle with the given id to be
110
// published with global read permissions to the stable channel.
112
// The named resources with their associated revision
113
// numbers are also published.
114
func SetPublicWithResources(c *gc.C, client *csclient.Client, id *charm.URL, resources map[string]int) {
115
// Publish to the stable channel.
116
err := client.Publish(id, []params.Channel{params.StableChannel}, resources)
117
c.Assert(err, jc.ErrorIsNil)
119
// Allow stable read permissions to everyone.
120
err = client.WithChannel(params.StableChannel).Put("/"+id.Path()+"/meta/perm/read", []string{params.Everyone})
121
c.Assert(err, jc.ErrorIsNil)
124
// SetPublic sets the charm or bundle with the given id to be
125
// published with global read permissions to the stable channel.
126
func SetPublic(c *gc.C, client *csclient.Client, id *charm.URL) {
127
SetPublicWithResources(c, client, id, nil)