~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/resource/api/client/base_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 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package client_test
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "io"
 
9
        "net/http"
 
10
        "time"
 
11
 
 
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"
 
17
 
 
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"
 
23
)
 
24
 
 
25
type BaseSuite struct {
 
26
        testing.IsolationSuite
 
27
 
 
28
        stub     *testing.Stub
 
29
        facade   *stubFacade
 
30
        response *api.UploadResult
 
31
}
 
32
 
 
33
func (s *BaseSuite) SetUpTest(c *gc.C) {
 
34
        s.IsolationSuite.SetUpTest(c)
 
35
 
 
36
        s.stub = &testing.Stub{}
 
37
        s.facade = newStubFacade(c, s.stub)
 
38
        s.response = &api.UploadResult{}
 
39
}
 
40
 
 
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)
 
45
        }
 
46
 
 
47
        result, ok := resp.(*api.UploadResult)
 
48
        if !ok {
 
49
                msg := fmt.Sprintf("bad response type %T, expected api.UploadResult", resp)
 
50
                return errors.NewNotValid(nil, msg)
 
51
        }
 
52
 
 
53
        *result = *s.response
 
54
        return nil
 
55
}
 
56
 
 
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)
 
65
        }
 
66
        return resources, apiResult
 
67
}
 
68
 
 
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
 
72
        res.Revision = 1
 
73
        res.Username = username
 
74
        if 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{}
 
79
        }
 
80
 
 
81
        apiRes := api.Resource{
 
82
                CharmResource: api.CharmResource{
 
83
                        Name:        name,
 
84
                        Type:        "file",
 
85
                        Path:        res.Path,
 
86
                        Origin:      "upload",
 
87
                        Revision:    1,
 
88
                        Fingerprint: res.Fingerprint.Bytes(),
 
89
                        Size:        res.Size,
 
90
                },
 
91
                ID:            res.ID,
 
92
                ApplicationID: res.ApplicationID,
 
93
                Username:      username,
 
94
                Timestamp:     res.Timestamp,
 
95
        }
 
96
 
 
97
        return res, apiRes
 
98
}
 
99
 
 
100
type stubFacade struct {
 
101
        basetesting.StubFacadeCaller
 
102
 
 
103
        apiResults map[string]api.ResourcesResult
 
104
        pendingIDs []string
 
105
}
 
106
 
 
107
func newStubFacade(c *gc.C, stub *testing.Stub) *stubFacade {
 
108
        s := &stubFacade{
 
109
                StubFacadeCaller: basetesting.StubFacadeCaller{
 
110
                        Stub: stub,
 
111
                },
 
112
                apiResults: make(map[string]api.ResourcesResult),
 
113
        }
 
114
 
 
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)
 
120
 
 
121
                        for _, e := range typedArgs.Entities {
 
122
                                tag, err := names.ParseTag(e.Tag)
 
123
                                c.Assert(err, jc.ErrorIsNil)
 
124
                                service := tag.Id()
 
125
 
 
126
                                apiResult, ok := s.apiResults[service]
 
127
                                if !ok {
 
128
                                        apiResult.Error = &params.Error{
 
129
                                                Message: fmt.Sprintf("application %q not found", service),
 
130
                                                Code:    params.CodeNotFound,
 
131
                                        }
 
132
                                }
 
133
                                typedResponse.Results = append(typedResponse.Results, apiResult)
 
134
                        }
 
135
                case *api.AddPendingResourcesResult:
 
136
                        typedResponse.PendingIDs = s.pendingIDs
 
137
                default:
 
138
                        c.Errorf("bad type %T", response)
 
139
                }
 
140
                return nil
 
141
        }
 
142
 
 
143
        return s
 
144
}
 
145
 
 
146
func (s *stubFacade) Close() error {
 
147
        s.Stub.AddCall("Close")
 
148
        if err := s.Stub.NextErr(); err != nil {
 
149
                return errors.Trace(err)
 
150
        }
 
151
 
 
152
        return nil
 
153
}