~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/testcharms/charm.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
// Package testcharms holds a corpus of charms
 
5
// for testing.
 
6
package testcharms
 
7
 
 
8
import (
 
9
        "strings"
 
10
 
 
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"
 
17
)
 
18
 
 
19
// Repo provides access to the test charm repository.
 
20
var Repo = testing.NewRepo("charm-repo", "quantal")
 
21
 
 
22
// UploadCharm uploads a charm using the given charm store client, and returns
 
23
// the resulting charm URL and charm.
 
24
//
 
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
 
30
        if id.User == "" {
 
31
                // We still need a user even if we are uploading a promulgated charm.
 
32
                id.User = "who"
 
33
                promulgatedRevision = id.Revision
 
34
        }
 
35
        ch := Repo.CharmArchive(c.MkDir(), name)
 
36
 
 
37
        // Upload the charm.
 
38
        err := client.UploadCharmWithRevision(id, ch, promulgatedRevision)
 
39
        c.Assert(err, jc.ErrorIsNil)
 
40
 
 
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
 
46
                // before we publish.
 
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 {
 
51
                        if r.Revision == -1 {
 
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)
 
55
                                r.Revision = 0
 
56
                        }
 
57
                        resources[r.Name] = r.Revision
 
58
                }
 
59
        }
 
60
 
 
61
        SetPublicWithResources(c, client, id, resources)
 
62
 
 
63
        return id, ch
 
64
}
 
65
 
 
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)
 
71
        if id.User == "" {
 
72
                // We still need a user even if we are uploading a promulgated charm.
 
73
                id.User = "who"
 
74
        }
 
75
        ch := Repo.CharmArchive(c.MkDir(), name)
 
76
 
 
77
        // Upload the charm.
 
78
        curl, err := client.UploadCharm(id, ch)
 
79
        c.Assert(err, jc.ErrorIsNil)
 
80
 
 
81
        SetPublic(c, client, curl)
 
82
 
 
83
        // Return the charm and its URL.
 
84
        return curl, ch
 
85
}
 
86
 
 
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
 
92
        if id.User == "" {
 
93
                // We still need a user even if we are uploading a promulgated bundle.
 
94
                id.User = "who"
 
95
                promulgatedRevision = id.Revision
 
96
        }
 
97
        b := Repo.BundleArchive(c.MkDir(), name)
 
98
 
 
99
        // Upload the bundle.
 
100
        err := client.UploadBundleWithRevision(id, b, promulgatedRevision)
 
101
        c.Assert(err, jc.ErrorIsNil)
 
102
 
 
103
        SetPublic(c, client, id)
 
104
 
 
105
        // Return the bundle and its URL.
 
106
        return id, b
 
107
}
 
108
 
 
109
// SetPublicWithResources sets the charm or bundle with the given id to be
 
110
// published with global read permissions to the stable channel.
 
111
//
 
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)
 
118
 
 
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)
 
122
}
 
123
 
 
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)
 
128
}