Merge lp:~stevenwilkin/snapweb/fetch-package-from-snapd-api into lp:~snappy-dev/snapweb/trunk

Proposed by Steven Wilkin
Status: Merged
Approved by: Sergio Schvezov
Approved revision: 167
Merged at revision: 166
Proposed branch: lp:~stevenwilkin/snapweb/fetch-package-from-snapd-api
Merge into: lp:~snappy-dev/snapweb/trunk
Diff against target: 318 lines (+236/-26)
6 files modified
snappy/common_test.go (+24/-0)
snappy/converge.go (+6/-26)
snappy/converge_test.go (+36/-0)
snappy/package_part.go (+110/-0)
snappy/package_part_test.go (+59/-0)
snappy/snapd_client.go (+1/-0)
To merge this branch: bzr merge lp:~stevenwilkin/snapweb/fetch-package-from-snapd-api
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve
Review via email: mp+284177@code.launchpad.net

Commit message

Fetch individual packages from the snapd API

Description of the change

Re-proposed to take into consideration a now-merged prerequisite branch.

This introduces some interim code that will eventually be removed as further chunks of functionality are migrated over to use the new snapd REST API.

When displaying an individual package this will use the new API client library rather than pulling the package out of snappy itself.

To post a comment you must log in.
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snappy/common_test.go'
2--- snappy/common_test.go 2016-01-27 17:59:12 +0000
3+++ snappy/common_test.go 2016-01-27 19:29:34 +0000
4@@ -120,6 +120,10 @@
5 return nil, errors.New("the package has no services")
6 }
7
8+func (f *fakeSnapdClient) Package(name string) (*client.Package, error) {
9+ return nil, errors.New("the package could not be retrieved")
10+}
11+
12 var _ snapdClient = (*fakeSnapdClient)(nil)
13
14 type fakeSnapdClientServicesNoExternalUI struct {
15@@ -173,3 +177,23 @@
16
17 return services, nil
18 }
19+
20+type fakeSnapdClientPackage struct {
21+ fakeSnapdClient
22+}
23+
24+func (f *fakeSnapdClientPackage) Package(name string) (*client.Package, error) {
25+ pkg := &client.Package{
26+ Description: "WebRTC Video chat server for Snappy",
27+ DownloadSize: 6930947,
28+ Icon: "/1.0/icons/chatroom.ogra/icon",
29+ InstalledSize: 18976651,
30+ Name: "chatroom",
31+ Origin: "ogra",
32+ Status: client.StatusActive,
33+ Type: client.TypeApp,
34+ Version: "0.1-8",
35+ }
36+
37+ return pkg, nil
38+}
39
40=== modified file 'snappy/converge.go'
41--- snappy/converge.go 2016-01-27 17:59:12 +0000
42+++ snappy/converge.go 2016-01-27 19:29:34 +0000
43@@ -59,33 +59,13 @@
44 query string
45 }
46
47-// for easier stubbing during testing
48-var activeSnapByName = snappy.ActiveSnapByName
49-
50 func (h *Handler) packagePayload(resource string) (snapPkg, error) {
51- var pkgName, origin string
52- if s := strings.Split(resource, "."); len(s) == 2 {
53- pkgName = s[0]
54- origin = s[1]
55- } else {
56- pkgName = resource
57- }
58-
59- snapQ := activeSnapByName(pkgName)
60- if snapQ != nil {
61- // the second check is for locally installed snaps that lose their origin.
62- if snapQ.Origin() == origin || snapQ.Type() != snap.TypeApp {
63- return h.snapQueryToPayload(snapQ), nil
64- }
65- }
66-
67- mStore := snappy.NewMetaStoreRepository()
68- found, err := mStore.Details(pkgName, origin)
69- if err == nil && len(found) != 0 {
70- return h.snapQueryToPayload(found[0]), nil
71- }
72-
73- return snapPkg{}, snappy.ErrPackageNotFound
74+ pkg, err := h.snapdClient.Package(resource)
75+ if err != nil {
76+ return snapPkg{}, err
77+ }
78+
79+ return h.snapQueryToPayload(packagePart{*pkg}), nil
80 }
81
82 func (h *Handler) allPackages(filter *listFilter) ([]snapPkg, error) {
83
84=== modified file 'snappy/converge_test.go'
85--- snappy/converge_test.go 2016-01-27 17:59:12 +0000
86+++ snappy/converge_test.go 2016-01-27 19:29:34 +0000
87@@ -25,6 +25,42 @@
88 "launchpad.net/webdm/webprogress"
89 )
90
91+type PackagePayloadSuite struct {
92+ h Handler
93+}
94+
95+var _ = Suite(&PackagePayloadSuite{})
96+
97+func (s *PackagePayloadSuite) SetUpTest(c *C) {
98+ os.Setenv("SNAP_APP_DATA_PATH", c.MkDir())
99+ s.h.statusTracker = webprogress.New()
100+ s.h.setClient(&fakeSnapdClient{})
101+}
102+
103+func (s *PackagePayloadSuite) TestPackageNotFound(c *C) {
104+ _, err := s.h.packagePayload("chatroom.ogra")
105+ c.Assert(err, NotNil)
106+}
107+
108+func (s *PackagePayloadSuite) TestPackage(c *C) {
109+ s.h.setClient(&fakeSnapdClientPackage{})
110+
111+ pkg, err := s.h.packagePayload("chatroom.ogra")
112+ c.Assert(err, IsNil)
113+ c.Assert(pkg, DeepEquals, snapPkg{
114+ ID: "chatroom.ogra",
115+ Description: "WebRTC Video chat server for Snappy",
116+ DownloadSize: 0,
117+ Icon: "/icons/chatroom.ogra_icon.png",
118+ InstalledSize: 18976651,
119+ Name: "chatroom",
120+ Origin: "ogra",
121+ Status: "installed",
122+ Type: "app",
123+ Version: "0.1-8",
124+ })
125+}
126+
127 type PayloadSuite struct {
128 h Handler
129 }
130
131=== added file 'snappy/package_part.go'
132--- snappy/package_part.go 1970-01-01 00:00:00 +0000
133+++ snappy/package_part.go 2016-01-27 19:29:34 +0000
134@@ -0,0 +1,110 @@
135+/*
136+ * Copyright (C) 2016 Canonical Ltd
137+ *
138+ * This program is free software: you can redistribute it and/or modify
139+ * it under the terms of the GNU General Public License version 3 as
140+ * published by the Free Software Foundation.
141+ *
142+ * This program is distributed in the hope that it will be useful,
143+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
144+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
145+ * GNU General Public License for more details.
146+ *
147+ * You should have received a copy of the GNU General Public License
148+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
149+ *
150+ */
151+
152+package snappy
153+
154+import (
155+ "time"
156+
157+ "github.com/ubuntu-core/snappy/client"
158+ "github.com/ubuntu-core/snappy/progress"
159+ "github.com/ubuntu-core/snappy/snap"
160+ "github.com/ubuntu-core/snappy/snappy"
161+)
162+
163+// packagePart is an interim bridge between client.Package and snappy.Part.
164+type packagePart struct {
165+ client.Package
166+}
167+
168+var _ snappy.Part = (*packagePart)(nil)
169+
170+func (p packagePart) Name() string {
171+ return p.Package.Name
172+}
173+
174+func (p packagePart) Version() string {
175+ return p.Package.Version
176+}
177+
178+func (p packagePart) Origin() string {
179+ return p.Package.Origin
180+}
181+
182+func (p packagePart) Description() string {
183+ return p.Package.Description
184+}
185+
186+func (p packagePart) InstalledSize() int64 {
187+ return p.Package.InstalledSize
188+}
189+
190+func (p packagePart) DownloadSize() int64 {
191+ return p.Package.DownloadSize
192+}
193+
194+func (p packagePart) Icon() string {
195+ return p.Package.Icon
196+}
197+
198+func (p packagePart) IsInstalled() bool {
199+ return p.Status == client.StatusInstalled || p.Status == client.StatusActive
200+}
201+
202+func (p packagePart) IsActive() bool {
203+ return p.Status == client.StatusActive
204+}
205+
206+func (p packagePart) Type() snap.Type {
207+ return snap.Type(p.Package.Type)
208+}
209+
210+func (p packagePart) Hash() string {
211+ return ""
212+}
213+
214+func (p packagePart) Channel() string {
215+ return ""
216+}
217+
218+func (p packagePart) NeedsReboot() bool {
219+ return false
220+}
221+
222+func (p packagePart) Config(configuration []byte) (newConfig string, err error) {
223+ return "", nil
224+}
225+
226+func (p packagePart) Date() time.Time {
227+ return time.Now()
228+}
229+
230+func (p packagePart) Install(pb progress.Meter, flags snappy.InstallFlags) (name string, err error) {
231+ return "", nil
232+}
233+
234+func (p packagePart) Uninstall(pb progress.Meter) error {
235+ return nil
236+}
237+
238+func (p packagePart) SetActive(bool, progress.Meter) error {
239+ return nil
240+}
241+
242+func (p packagePart) Frameworks() ([]string, error) {
243+ return nil, nil
244+}
245
246=== added file 'snappy/package_part_test.go'
247--- snappy/package_part_test.go 1970-01-01 00:00:00 +0000
248+++ snappy/package_part_test.go 2016-01-27 19:29:34 +0000
249@@ -0,0 +1,59 @@
250+/*
251+ * Copyright (C) 2016 Canonical Ltd
252+ *
253+ * This program is free software: you can redistribute it and/or modify
254+ * it under the terms of the GNU General Public License version 3 as
255+ * published by the Free Software Foundation.
256+ *
257+ * This program is distributed in the hope that it will be useful,
258+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
259+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
260+ * GNU General Public License for more details.
261+ *
262+ * You should have received a copy of the GNU General Public License
263+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
264+ *
265+ */
266+
267+package snappy
268+
269+import (
270+ "github.com/ubuntu-core/snappy/client"
271+ "github.com/ubuntu-core/snappy/snap"
272+
273+ . "gopkg.in/check.v1"
274+)
275+
276+type PackagePartSuite struct {
277+ pkg client.Package
278+ pkgPart packagePart
279+}
280+
281+var _ = Suite(&PackagePartSuite{})
282+
283+func (s *PackagePartSuite) SetUpTest(c *C) {
284+ s.pkg = client.Package{
285+ Description: "WebRTC Video chat server for Snappy",
286+ DownloadSize: 6930947,
287+ Icon: "/1.0/icons/chatroom.ogra/icon",
288+ InstalledSize: 18976651,
289+ Name: "chatroom",
290+ Origin: "ogra",
291+ Status: client.StatusActive,
292+ Type: client.TypeApp,
293+ Version: "0.1-8",
294+ }
295+ s.pkgPart = packagePart{s.pkg}
296+}
297+
298+func (s *PackagePartSuite) TestPackagePart(c *C) {
299+ c.Assert(s.pkgPart.Name(), Equals, s.pkg.Name)
300+ c.Assert(s.pkgPart.Version(), Equals, s.pkg.Version)
301+ c.Assert(s.pkgPart.Origin(), Equals, s.pkg.Origin)
302+ c.Assert(s.pkgPart.Description(), Equals, s.pkg.Description)
303+ c.Assert(s.pkgPart.InstalledSize(), Equals, s.pkg.InstalledSize)
304+ c.Assert(s.pkgPart.DownloadSize(), Equals, s.pkg.DownloadSize)
305+ c.Assert(s.pkgPart.Icon(), Equals, s.pkg.Icon)
306+ c.Assert(s.pkgPart.IsInstalled(), Equals, true)
307+ c.Assert(s.pkgPart.Type(), Equals, snap.TypeApp)
308+}
309
310=== modified file 'snappy/snapd_client.go'
311--- snappy/snapd_client.go 2016-01-27 17:59:12 +0000
312+++ snappy/snapd_client.go 2016-01-27 19:29:34 +0000
313@@ -24,4 +24,5 @@
314 type snapdClient interface {
315 Icon(pkgID string) (*client.Icon, error)
316 Services(pkg string) (map[string]*client.Service, error)
317+ Package(name string) (*client.Package, error)
318 }

Subscribers

People subscribed via source and target branches