1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
12
"github.com/juju/errors"
13
"github.com/juju/testing"
14
jc "github.com/juju/testing/checkers"
15
gc "gopkg.in/check.v1"
16
"gopkg.in/juju/names.v2"
18
basetesting "github.com/juju/juju/api/base/testing"
19
"github.com/juju/juju/apiserver/params"
20
"github.com/juju/juju/resource"
21
"github.com/juju/juju/resource/api"
22
"github.com/juju/juju/resource/resourcetesting"
25
type BaseSuite struct {
26
testing.IsolationSuite
30
response *api.UploadResult
33
func (s *BaseSuite) SetUpTest(c *gc.C) {
34
s.IsolationSuite.SetUpTest(c)
36
s.stub = &testing.Stub{}
37
s.facade = newStubFacade(c, s.stub)
38
s.response = &api.UploadResult{}
41
func (s *BaseSuite) Do(req *http.Request, body io.ReadSeeker, resp interface{}) error {
42
s.stub.AddCall("Do", req, body, resp)
43
if err := s.stub.NextErr(); err != nil {
44
return errors.Trace(err)
47
result, ok := resp.(*api.UploadResult)
49
msg := fmt.Sprintf("bad response type %T, expected api.UploadResult", resp)
50
return errors.NewNotValid(nil, msg)
57
func newResourceResult(c *gc.C, applicationID string, names ...string) ([]resource.Resource, api.ResourcesResult) {
58
var resources []resource.Resource
59
var apiResult api.ResourcesResult
60
for _, name := range names {
61
data := name + "...spamspamspam"
62
res, apiRes := newResource(c, name, "a-user", data)
63
resources = append(resources, res)
64
apiResult.Resources = append(apiResult.Resources, apiRes)
66
return resources, apiResult
69
func newResource(c *gc.C, name, username, data string) (resource.Resource, api.Resource) {
70
opened := resourcetesting.NewResource(c, nil, name, "a-application", data)
71
res := opened.Resource
73
res.Username = username
75
// Note that resourcetesting.NewResource() returns a resources
76
// with a username and timestamp set. So if the username was
77
// "un-set" then we have to also unset the timestamp.
78
res.Timestamp = time.Time{}
81
apiRes := api.Resource{
82
CharmResource: api.CharmResource{
88
Fingerprint: res.Fingerprint.Bytes(),
92
ApplicationID: res.ApplicationID,
94
Timestamp: res.Timestamp,
100
type stubFacade struct {
101
basetesting.StubFacadeCaller
103
apiResults map[string]api.ResourcesResult
107
func newStubFacade(c *gc.C, stub *testing.Stub) *stubFacade {
109
StubFacadeCaller: basetesting.StubFacadeCaller{
112
apiResults: make(map[string]api.ResourcesResult),
115
s.FacadeCallFn = func(_ string, args, response interface{}) error {
116
switch typedResponse := response.(type) {
117
case *api.ResourcesResults:
118
typedArgs, ok := args.(*api.ListResourcesArgs)
119
c.Assert(ok, jc.IsTrue)
121
for _, e := range typedArgs.Entities {
122
tag, err := names.ParseTag(e.Tag)
123
c.Assert(err, jc.ErrorIsNil)
126
apiResult, ok := s.apiResults[service]
128
apiResult.Error = ¶ms.Error{
129
Message: fmt.Sprintf("application %q not found", service),
130
Code: params.CodeNotFound,
133
typedResponse.Results = append(typedResponse.Results, apiResult)
135
case *api.AddPendingResourcesResult:
136
typedResponse.PendingIDs = s.pendingIDs
138
c.Errorf("bad type %T", response)
146
func (s *stubFacade) Close() error {
147
s.Stub.AddCall("Close")
148
if err := s.Stub.NextErr(); err != nil {
149
return errors.Trace(err)