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
=== modified file 'snappy/common_test.go'
--- snappy/common_test.go 2016-01-27 17:59:12 +0000
+++ snappy/common_test.go 2016-01-27 19:29:34 +0000
@@ -120,6 +120,10 @@
120 return nil, errors.New("the package has no services")120 return nil, errors.New("the package has no services")
121}121}
122122
123func (f *fakeSnapdClient) Package(name string) (*client.Package, error) {
124 return nil, errors.New("the package could not be retrieved")
125}
126
123var _ snapdClient = (*fakeSnapdClient)(nil)127var _ snapdClient = (*fakeSnapdClient)(nil)
124128
125type fakeSnapdClientServicesNoExternalUI struct {129type fakeSnapdClientServicesNoExternalUI struct {
@@ -173,3 +177,23 @@
173177
174 return services, nil178 return services, nil
175}179}
180
181type fakeSnapdClientPackage struct {
182 fakeSnapdClient
183}
184
185func (f *fakeSnapdClientPackage) Package(name string) (*client.Package, error) {
186 pkg := &client.Package{
187 Description: "WebRTC Video chat server for Snappy",
188 DownloadSize: 6930947,
189 Icon: "/1.0/icons/chatroom.ogra/icon",
190 InstalledSize: 18976651,
191 Name: "chatroom",
192 Origin: "ogra",
193 Status: client.StatusActive,
194 Type: client.TypeApp,
195 Version: "0.1-8",
196 }
197
198 return pkg, nil
199}
176200
=== modified file 'snappy/converge.go'
--- snappy/converge.go 2016-01-27 17:59:12 +0000
+++ snappy/converge.go 2016-01-27 19:29:34 +0000
@@ -59,33 +59,13 @@
59 query string59 query string
60}60}
6161
62// for easier stubbing during testing
63var activeSnapByName = snappy.ActiveSnapByName
64
65func (h *Handler) packagePayload(resource string) (snapPkg, error) {62func (h *Handler) packagePayload(resource string) (snapPkg, error) {
66 var pkgName, origin string63 pkg, err := h.snapdClient.Package(resource)
67 if s := strings.Split(resource, "."); len(s) == 2 {64 if err != nil {
68 pkgName = s[0]65 return snapPkg{}, err
69 origin = s[1]66 }
70 } else {67
71 pkgName = resource68 return h.snapQueryToPayload(packagePart{*pkg}), nil
72 }
73
74 snapQ := activeSnapByName(pkgName)
75 if snapQ != nil {
76 // the second check is for locally installed snaps that lose their origin.
77 if snapQ.Origin() == origin || snapQ.Type() != snap.TypeApp {
78 return h.snapQueryToPayload(snapQ), nil
79 }
80 }
81
82 mStore := snappy.NewMetaStoreRepository()
83 found, err := mStore.Details(pkgName, origin)
84 if err == nil && len(found) != 0 {
85 return h.snapQueryToPayload(found[0]), nil
86 }
87
88 return snapPkg{}, snappy.ErrPackageNotFound
89}69}
9070
91func (h *Handler) allPackages(filter *listFilter) ([]snapPkg, error) {71func (h *Handler) allPackages(filter *listFilter) ([]snapPkg, error) {
9272
=== modified file 'snappy/converge_test.go'
--- snappy/converge_test.go 2016-01-27 17:59:12 +0000
+++ snappy/converge_test.go 2016-01-27 19:29:34 +0000
@@ -25,6 +25,42 @@
25 "launchpad.net/webdm/webprogress"25 "launchpad.net/webdm/webprogress"
26)26)
2727
28type PackagePayloadSuite struct {
29 h Handler
30}
31
32var _ = Suite(&PackagePayloadSuite{})
33
34func (s *PackagePayloadSuite) SetUpTest(c *C) {
35 os.Setenv("SNAP_APP_DATA_PATH", c.MkDir())
36 s.h.statusTracker = webprogress.New()
37 s.h.setClient(&fakeSnapdClient{})
38}
39
40func (s *PackagePayloadSuite) TestPackageNotFound(c *C) {
41 _, err := s.h.packagePayload("chatroom.ogra")
42 c.Assert(err, NotNil)
43}
44
45func (s *PackagePayloadSuite) TestPackage(c *C) {
46 s.h.setClient(&fakeSnapdClientPackage{})
47
48 pkg, err := s.h.packagePayload("chatroom.ogra")
49 c.Assert(err, IsNil)
50 c.Assert(pkg, DeepEquals, snapPkg{
51 ID: "chatroom.ogra",
52 Description: "WebRTC Video chat server for Snappy",
53 DownloadSize: 0,
54 Icon: "/icons/chatroom.ogra_icon.png",
55 InstalledSize: 18976651,
56 Name: "chatroom",
57 Origin: "ogra",
58 Status: "installed",
59 Type: "app",
60 Version: "0.1-8",
61 })
62}
63
28type PayloadSuite struct {64type PayloadSuite struct {
29 h Handler65 h Handler
30}66}
3167
=== added file 'snappy/package_part.go'
--- snappy/package_part.go 1970-01-01 00:00:00 +0000
+++ snappy/package_part.go 2016-01-27 19:29:34 +0000
@@ -0,0 +1,110 @@
1/*
2 * Copyright (C) 2016 Canonical Ltd
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18package snappy
19
20import (
21 "time"
22
23 "github.com/ubuntu-core/snappy/client"
24 "github.com/ubuntu-core/snappy/progress"
25 "github.com/ubuntu-core/snappy/snap"
26 "github.com/ubuntu-core/snappy/snappy"
27)
28
29// packagePart is an interim bridge between client.Package and snappy.Part.
30type packagePart struct {
31 client.Package
32}
33
34var _ snappy.Part = (*packagePart)(nil)
35
36func (p packagePart) Name() string {
37 return p.Package.Name
38}
39
40func (p packagePart) Version() string {
41 return p.Package.Version
42}
43
44func (p packagePart) Origin() string {
45 return p.Package.Origin
46}
47
48func (p packagePart) Description() string {
49 return p.Package.Description
50}
51
52func (p packagePart) InstalledSize() int64 {
53 return p.Package.InstalledSize
54}
55
56func (p packagePart) DownloadSize() int64 {
57 return p.Package.DownloadSize
58}
59
60func (p packagePart) Icon() string {
61 return p.Package.Icon
62}
63
64func (p packagePart) IsInstalled() bool {
65 return p.Status == client.StatusInstalled || p.Status == client.StatusActive
66}
67
68func (p packagePart) IsActive() bool {
69 return p.Status == client.StatusActive
70}
71
72func (p packagePart) Type() snap.Type {
73 return snap.Type(p.Package.Type)
74}
75
76func (p packagePart) Hash() string {
77 return ""
78}
79
80func (p packagePart) Channel() string {
81 return ""
82}
83
84func (p packagePart) NeedsReboot() bool {
85 return false
86}
87
88func (p packagePart) Config(configuration []byte) (newConfig string, err error) {
89 return "", nil
90}
91
92func (p packagePart) Date() time.Time {
93 return time.Now()
94}
95
96func (p packagePart) Install(pb progress.Meter, flags snappy.InstallFlags) (name string, err error) {
97 return "", nil
98}
99
100func (p packagePart) Uninstall(pb progress.Meter) error {
101 return nil
102}
103
104func (p packagePart) SetActive(bool, progress.Meter) error {
105 return nil
106}
107
108func (p packagePart) Frameworks() ([]string, error) {
109 return nil, nil
110}
0111
=== added file 'snappy/package_part_test.go'
--- snappy/package_part_test.go 1970-01-01 00:00:00 +0000
+++ snappy/package_part_test.go 2016-01-27 19:29:34 +0000
@@ -0,0 +1,59 @@
1/*
2 * Copyright (C) 2016 Canonical Ltd
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18package snappy
19
20import (
21 "github.com/ubuntu-core/snappy/client"
22 "github.com/ubuntu-core/snappy/snap"
23
24 . "gopkg.in/check.v1"
25)
26
27type PackagePartSuite struct {
28 pkg client.Package
29 pkgPart packagePart
30}
31
32var _ = Suite(&PackagePartSuite{})
33
34func (s *PackagePartSuite) SetUpTest(c *C) {
35 s.pkg = client.Package{
36 Description: "WebRTC Video chat server for Snappy",
37 DownloadSize: 6930947,
38 Icon: "/1.0/icons/chatroom.ogra/icon",
39 InstalledSize: 18976651,
40 Name: "chatroom",
41 Origin: "ogra",
42 Status: client.StatusActive,
43 Type: client.TypeApp,
44 Version: "0.1-8",
45 }
46 s.pkgPart = packagePart{s.pkg}
47}
48
49func (s *PackagePartSuite) TestPackagePart(c *C) {
50 c.Assert(s.pkgPart.Name(), Equals, s.pkg.Name)
51 c.Assert(s.pkgPart.Version(), Equals, s.pkg.Version)
52 c.Assert(s.pkgPart.Origin(), Equals, s.pkg.Origin)
53 c.Assert(s.pkgPart.Description(), Equals, s.pkg.Description)
54 c.Assert(s.pkgPart.InstalledSize(), Equals, s.pkg.InstalledSize)
55 c.Assert(s.pkgPart.DownloadSize(), Equals, s.pkg.DownloadSize)
56 c.Assert(s.pkgPart.Icon(), Equals, s.pkg.Icon)
57 c.Assert(s.pkgPart.IsInstalled(), Equals, true)
58 c.Assert(s.pkgPart.Type(), Equals, snap.TypeApp)
59}
060
=== modified file 'snappy/snapd_client.go'
--- snappy/snapd_client.go 2016-01-27 17:59:12 +0000
+++ snappy/snapd_client.go 2016-01-27 19:29:34 +0000
@@ -24,4 +24,5 @@
24type snapdClient interface {24type snapdClient interface {
25 Icon(pkgID string) (*client.Icon, error)25 Icon(pkgID string) (*client.Icon, error)
26 Services(pkg string) (map[string]*client.Service, error)26 Services(pkg string) (map[string]*client.Service, error)
27 Package(name string) (*client.Package, error)
27}28}

Subscribers

People subscribed via source and target branches