~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/charmstore/latest_test.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 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package charmstore
 
5
 
 
6
import (
 
7
        "sort"
 
8
 
 
9
        "github.com/juju/errors"
 
10
        "github.com/juju/testing"
 
11
        jc "github.com/juju/testing/checkers"
 
12
        gc "gopkg.in/check.v1"
 
13
        "gopkg.in/juju/charm.v6-unstable"
 
14
        charmresource "gopkg.in/juju/charm.v6-unstable/resource"
 
15
        "gopkg.in/juju/charmrepo.v2-unstable/csclient/params"
 
16
)
 
17
 
 
18
type LatestCharmInfoSuite struct {
 
19
        testing.IsolationSuite
 
20
 
 
21
        lowLevel *fakeWrapper
 
22
        cache    *fakeMacCache
 
23
}
 
24
 
 
25
var _ = gc.Suite(&LatestCharmInfoSuite{})
 
26
 
 
27
func (s *LatestCharmInfoSuite) SetUpTest(c *gc.C) {
 
28
        s.IsolationSuite.SetUpTest(c)
 
29
 
 
30
        s.lowLevel = &fakeWrapper{
 
31
                stub:       &testing.Stub{},
 
32
                stableStub: &testing.Stub{},
 
33
                devStub:    &testing.Stub{},
 
34
        }
 
35
 
 
36
        s.cache = &fakeMacCache{
 
37
                stub: &testing.Stub{},
 
38
        }
 
39
}
 
40
 
 
41
func (s *LatestCharmInfoSuite) TestSuccess(c *gc.C) {
 
42
        spam := charm.MustParseURL("cs:quantal/spam-17")
 
43
        eggs := charm.MustParseURL("cs:quantal/eggs-2")
 
44
        ham := charm.MustParseURL("cs:quantal/ham-1")
 
45
        charms := []CharmID{
 
46
                {URL: spam, Channel: "stable"},
 
47
                {URL: eggs, Channel: "stable"},
 
48
                {URL: ham, Channel: "stable"},
 
49
        }
 
50
        notFound := errors.New("not found")
 
51
        s.lowLevel.ReturnLatestStable = [][]params.CharmRevision{{{
 
52
                Revision: 17,
 
53
        }}, {{
 
54
                Revision: 3,
 
55
        }}, {{
 
56
                Err: notFound,
 
57
        }}}
 
58
 
 
59
        fakeRes := fakeParamsResource("foo", nil)
 
60
 
 
61
        s.lowLevel.ReturnListResourcesStable = []resourceResult{
 
62
                oneResourceResult(fakeRes),
 
63
                resourceResult{err: params.ErrNotFound},
 
64
                resourceResult{err: params.ErrUnauthorized},
 
65
        }
 
66
 
 
67
        client, err := newCachingClient(s.cache, nil, s.lowLevel.makeWrapper)
 
68
        c.Assert(err, jc.ErrorIsNil)
 
69
 
 
70
        results, err := LatestCharmInfo(client, charms, "foobar")
 
71
        c.Assert(err, jc.ErrorIsNil)
 
72
 
 
73
        s.lowLevel.stableStub.CheckCall(c, 0, "Latest", params.StableChannel, []*charm.URL{spam}, map[string][]string{"Juju-Metadata": []string{"environment_uuid=foobar"}})
 
74
        s.lowLevel.stableStub.CheckCall(c, 1, "Latest", params.StableChannel, []*charm.URL{eggs}, map[string][]string{"Juju-Metadata": []string{"environment_uuid=foobar"}})
 
75
        s.lowLevel.stableStub.CheckCall(c, 2, "Latest", params.StableChannel, []*charm.URL{ham}, map[string][]string{"Juju-Metadata": []string{"environment_uuid=foobar"}})
 
76
        s.lowLevel.stableStub.CheckCall(c, 3, "ListResources", params.StableChannel, spam)
 
77
        s.lowLevel.stableStub.CheckCall(c, 4, "ListResources", params.StableChannel, eggs)
 
78
        s.lowLevel.stableStub.CheckCall(c, 5, "ListResources", params.StableChannel, ham)
 
79
 
 
80
        expectedRes, err := params.API2Resource(fakeRes)
 
81
        c.Assert(err, jc.ErrorIsNil)
 
82
 
 
83
        timestamp := results[0].Timestamp
 
84
        results[2].Error = errors.Cause(results[2].Error)
 
85
        expected := []CharmInfoResult{{
 
86
                CharmInfo: CharmInfo{
 
87
                        OriginalURL:    charm.MustParseURL("cs:quantal/spam-17"),
 
88
                        Timestamp:      timestamp,
 
89
                        LatestRevision: 17,
 
90
                        LatestResources: []charmresource.Resource{
 
91
                                expectedRes,
 
92
                        },
 
93
                },
 
94
        }, {
 
95
                CharmInfo: CharmInfo{
 
96
                        OriginalURL:    charm.MustParseURL("cs:quantal/eggs-2"),
 
97
                        Timestamp:      timestamp,
 
98
                        LatestRevision: 3,
 
99
                },
 
100
        }, {
 
101
                CharmInfo: CharmInfo{
 
102
                        OriginalURL: charm.MustParseURL("cs:quantal/ham-1"),
 
103
                        Timestamp:   timestamp,
 
104
                },
 
105
                Error: notFound,
 
106
        }}
 
107
        sort.Sort(byURL(results))
 
108
        sort.Sort(byURL(expected))
 
109
        c.Check(results, jc.DeepEquals, expected)
 
110
}
 
111
 
 
112
type byURL []CharmInfoResult
 
113
 
 
114
func (b byURL) Len() int           { return len(b) }
 
115
func (b byURL) Swap(i, j int)      { b[i], b[j] = b[j], b[i] }
 
116
func (b byURL) Less(i, j int) bool { return b[i].OriginalURL.String() < b[j].OriginalURL.String() }