forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest_test.go
141 lines (121 loc) · 4.17 KB
/
latest_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
132
133
134
135
136
137
138
139
140
141
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore
import (
"sort"
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v6"
"gopkg.in/juju/charm.v6/resource"
"gopkg.in/juju/charmrepo.v3/csclient/params"
"github.com/juju/juju/version"
)
type LatestCharmInfoSuite struct {
testing.IsolationSuite
lowLevel *fakeWrapper
cache *fakeMacCache
}
var _ = gc.Suite(&LatestCharmInfoSuite{})
func (s *LatestCharmInfoSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
s.lowLevel = &fakeWrapper{
stub: &testing.Stub{},
stableStub: &testing.Stub{},
devStub: &testing.Stub{},
}
s.cache = &fakeMacCache{
stub: &testing.Stub{},
}
}
func (s *LatestCharmInfoSuite) TestSuccess(c *gc.C) {
spam := charm.MustParseURL("cs:quantal/spam-17")
eggs := charm.MustParseURL("cs:quantal/eggs-2")
ham := charm.MustParseURL("cs:quantal/ham-1")
charms := []CharmID{
{URL: spam, Channel: "stable", Metadata: map[string]string{"series": "quantal", "arch": "amd64,arm64"}},
{URL: eggs, Channel: "stable", Metadata: map[string]string{"series": "quantal", "arch": "amd64,arm64"}},
{URL: ham, Channel: "stable", Metadata: map[string]string{"series": "quantal", "arch": "amd64,arm64"}},
}
notFound := errors.New("not found")
s.lowLevel.ReturnLatestStable = [][]params.CharmRevision{{{
Revision: 17,
}}, {{
Revision: 3,
}}, {{
Err: notFound,
}}}
fakeRes := fakeParamsResource("foo", nil)
s.lowLevel.ReturnListResourcesStable = []resourceResult{
oneResourceResult(fakeRes),
{err: params.ErrNotFound},
{err: params.ErrUnauthorized},
}
client, err := newCachingClient(s.cache, "", s.lowLevel.makeWrapper)
c.Assert(err, jc.ErrorIsNil)
metadata := map[string]string{
"environment_uuid": "foouuid",
"model_uuid": "foouuid",
"controller_uuid": "controlleruuid",
"cloud": "foocloud",
"cloud_region": "fooregion",
"provider": "fooprovider",
"controller_version": version.Current.String(),
"is_controller": "false",
}
results, err := LatestCharmInfo(client, charms, metadata)
c.Assert(err, jc.ErrorIsNil)
header := []string{
"arch=amd64",
"arch=arm64",
"cloud=foocloud",
"cloud_region=fooregion",
"controller_uuid=controlleruuid",
"controller_version=" + version.Current.String(),
"environment_uuid=foouuid",
"is_controller=false",
"model_uuid=foouuid",
"provider=fooprovider",
"series=quantal",
}
s.lowLevel.stableStub.CheckCall(c, 0, "Latest", params.StableChannel, []*charm.URL{spam}, map[string][]string{"Juju-Metadata": header})
s.lowLevel.stableStub.CheckCall(c, 1, "Latest", params.StableChannel, []*charm.URL{eggs}, map[string][]string{"Juju-Metadata": header})
s.lowLevel.stableStub.CheckCall(c, 2, "Latest", params.StableChannel, []*charm.URL{ham}, map[string][]string{"Juju-Metadata": header})
s.lowLevel.stableStub.CheckCall(c, 3, "ListResources", params.StableChannel, spam)
s.lowLevel.stableStub.CheckCall(c, 4, "ListResources", params.StableChannel, eggs)
s.lowLevel.stableStub.CheckCall(c, 5, "ListResources", params.StableChannel, ham)
expectedRes, err := params.API2Resource(fakeRes)
c.Assert(err, jc.ErrorIsNil)
timestamp := results[0].Timestamp
results[2].Error = errors.Cause(results[2].Error)
expected := []CharmInfoResult{{
CharmInfo: CharmInfo{
OriginalURL: charm.MustParseURL("cs:quantal/spam-17"),
Timestamp: timestamp,
LatestRevision: 17,
LatestResources: []resource.Resource{
expectedRes,
},
},
}, {
CharmInfo: CharmInfo{
OriginalURL: charm.MustParseURL("cs:quantal/eggs-2"),
Timestamp: timestamp,
LatestRevision: 3,
},
}, {
CharmInfo: CharmInfo{
OriginalURL: charm.MustParseURL("cs:quantal/ham-1"),
Timestamp: timestamp,
},
Error: notFound,
}}
sort.Sort(byURL(results))
sort.Sort(byURL(expected))
c.Check(results, jc.DeepEquals, expected)
}
type byURL []CharmInfoResult
func (b byURL) Len() int { return len(b) }
func (b byURL) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byURL) Less(i, j int) bool { return b[i].OriginalURL.String() < b[j].OriginalURL.String() }