-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathapi_download_test.go
197 lines (171 loc) · 5.11 KB
/
api_download_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon_test
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"gopkg.in/check.v1"
"github.com/snapcore/snapd/daemon"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/overlord"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/store/storetest"
)
type fakeStore struct{}
var _ = check.Suite(&snapDownloadSuite{})
type snapDownloadSuite struct {
storetest.Store
d *daemon.Daemon
snaps []string
}
func (s *snapDownloadSuite) SetUpTest(c *check.C) {
s.snaps = nil
o := overlord.Mock()
s.d = daemon.NewWithOverlord(o)
st := o.State()
st.Lock()
defer st.Unlock()
snapstate.ReplaceStore(st, s)
dirs.SetRootDir(c.MkDir())
}
var content = "SNAP"
func (s *snapDownloadSuite) SnapInfo(ctx context.Context, spec store.SnapSpec, user *auth.UserState) (*snap.Info, error) {
switch spec.Name {
case "bar":
return &snap.Info{
DownloadInfo: snap.DownloadInfo{
Size: int64(len(content)),
AnonDownloadURL: "http://localhost/bar",
},
}, nil
case "download-error-trigger-snap":
return &snap.Info{
DownloadInfo: snap.DownloadInfo{
Size: 100,
AnonDownloadURL: "http://localhost/foo",
},
}, nil
default:
return nil, store.ErrSnapNotFound
}
}
func (s *snapDownloadSuite) DownloadStream(ctx context.Context, name string, downloadInfo *snap.DownloadInfo, user *auth.UserState) (io.ReadCloser, error) {
if name == "bar" {
return ioutil.NopCloser(bytes.NewReader([]byte(content))), nil
}
return nil, fmt.Errorf("unexpected error")
}
func (s *snapDownloadSuite) TestDownloadSnapErrors(c *check.C) {
type scenario struct {
dataJSON string
status int
err string
}
for _, scen := range []scenario{
{
dataJSON: `{"action": "download"}`,
status: 400,
err: "download operation requires one snap name",
},
{
dataJSON: `{"action": "foo", "snaps": ["foo"]}`,
status: 400,
err: `unknown download operation "foo"`,
},
{
dataJSON: `{"snaps": ["foo"]}`,
status: 400,
err: `download operation requires action`,
},
{
dataJSON: `{"action": "foo", "snaps": ["foo", "bar"]}`,
status: 400,
err: `download operation supports only one snap`,
},
{
dataJSON: `{"}`,
status: 400,
err: `cannot decode request body into download operation: unexpected EOF`,
},
} {
var err error
data := []byte(scen.dataJSON)
req, err := http.NewRequest("POST", "/v2/download", bytes.NewBuffer(data))
c.Assert(err, check.IsNil)
rsp := daemon.PostSnapDownload(daemon.SnapDownloadCmd, req, nil)
c.Assert(rsp.(*daemon.Resp).Status, check.Equals, scen.status)
if scen.err == "" {
c.Errorf("error was expected")
}
result := rsp.(*daemon.Resp).Result
c.Check(result.(*daemon.ErrorResult).Message, check.Matches, scen.err)
}
}
func (s *snapDownloadSuite) TestStreamOneSnap(c *check.C) {
type scenario struct {
dataJSON string
status int
err string
}
for _, s := range []scenario{
{
dataJSON: `{"action": "download", "snaps": ["doom"]}`,
status: 404,
err: "snap not found",
},
{
dataJSON: `{"action": "download", "snaps": ["download-error-trigger-snap"]}`,
status: 500,
err: "unexpected error",
},
{
dataJSON: `{"action": "download", "snaps": ["bar"]}`,
status: 200,
err: "",
},
} {
req, err := http.NewRequest("POST", "/v2/download", strings.NewReader(s.dataJSON))
c.Assert(err, check.IsNil)
rsp := daemon.SnapDownloadCmd.POST(daemon.SnapDownloadCmd, req, nil)
if s.err != "" {
c.Assert(rsp.(*daemon.Resp).Status, check.Equals, s.status)
result := rsp.(*daemon.Resp).Result
c.Check(result.(*daemon.ErrorResult).Message, check.Matches, s.err)
} else {
c.Assert(rsp.(daemon.FileStream).SnapName, check.Equals, "bar")
c.Assert(rsp.(daemon.FileStream).Info.Size, check.Equals, int64(len(content)))
w := httptest.NewRecorder()
rsp.(daemon.FileStream).ServeHTTP(w, nil)
expectedLength := fmt.Sprintf("%d", len(content))
c.Assert(w.Code, check.Equals, s.status)
c.Assert(w.Header().Get("Content-Length"), check.Equals, expectedLength)
c.Assert(w.Header().Get("Content-Type"), check.Equals, "application/octet-stream")
c.Assert(w.Header().Get("Content-Disposition"), check.Equals, "attachment; filename=bar")
c.Assert(w.Body.String(), check.Equals, "SNAP")
}
}
}