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

Proposed by Steven Wilkin
Status: Merged
Approved by: Sergio Schvezov
Approved revision: 166
Merged at revision: 165
Proposed branch: lp:~stevenwilkin/snapweb/fetch-services-from-snapd-api
Merge into: lp:~snappy-dev/snapweb/trunk
Diff against target: 259 lines (+74/-86)
4 files modified
snappy/common_test.go (+57/-74)
snappy/converge.go (+6/-5)
snappy/converge_test.go (+10/-7)
snappy/snapd_client.go (+1/-0)
To merge this branch: bzr merge lp:~stevenwilkin/snapweb/fetch-services-from-snapd-api
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve
Review via email: mp+284170@code.launchpad.net

Commit message

Fetch services from snapd API

Description of the change

Fetch services from snapd API. Re-proposed to take into consideration a now-merged prerequisite branch.

To post a comment you must log in.
Revision history for this message
Sergio Schvezov (sergiusens) :
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 14:16:35 +0000
3+++ snappy/common_test.go 2016-01-27 18:32:39 +0000
4@@ -18,6 +18,7 @@
5 package snappy
6
7 import (
8+ "errors"
9 "testing"
10
11 "github.com/ubuntu-core/snappy/client"
12@@ -40,11 +41,6 @@
13 snapType snap.Type
14 }
15
16-type fakeSnappyPartServices struct {
17- fakeSnappyPart
18- serviceYamls []snappy.ServiceYaml
19-}
20-
21 func newDefaultFakePart() *fakeSnappyPart {
22 return &fakeSnappyPart{
23 name: "camlistore",
24@@ -66,31 +62,6 @@
25 }
26 }
27
28-func newParametrizedFake(name, version string, installed bool, snapType snap.Type) *fakeSnappyPart {
29- return &fakeSnappyPart{
30- name: name,
31- version: version,
32- installed: installed,
33- snapType: snapType,
34- }
35-}
36-
37-func newDefaultFakeServices() *fakeSnappyPartServices {
38- return &fakeSnappyPartServices{
39- fakeSnappyPart: fakeSnappyPart{
40- name: "camlistore",
41- origin: "sergiusens",
42- version: "2.0",
43- installed: true,
44- snapType: snap.TypeApp,
45- },
46- }
47-}
48-
49-func (p fakeSnappyPartServices) ServiceYamls() []snappy.ServiceYaml {
50- return p.serviceYamls
51-}
52-
53 func (p fakeSnappyPart) IsInstalled() bool {
54 return p.installed
55 }
56@@ -135,50 +106,6 @@
57 return p.description
58 }
59
60-func newFakeServicesNoExternalUI() []snappy.ServiceYaml {
61- services := make([]snappy.ServiceYaml, 0, 2)
62-
63- internal1 := map[string]snappy.Port{"ui": snappy.Port{Port: "200/tcp"}}
64- external1 := map[string]snappy.Port{"web": snappy.Port{Port: "1024/tcp"}}
65- s1 := snappy.ServiceYaml{
66- Name: "s1",
67- Ports: &snappy.Ports{
68- Internal: internal1,
69- External: external1,
70- },
71- }
72- services = append(services, s1)
73-
74- s2 := snappy.ServiceYaml{
75- Name: "s2",
76- }
77- services = append(services, s2)
78-
79- return services
80-}
81-
82-func newFakeServicesWithExternalUI() []snappy.ServiceYaml {
83- services := make([]snappy.ServiceYaml, 0, 2)
84-
85- s1 := snappy.ServiceYaml{
86- Name: "s2",
87- }
88- services = append(services, s1)
89-
90- internal2 := map[string]snappy.Port{"ui": snappy.Port{Port: "200/tcp"}}
91- external2 := map[string]snappy.Port{"ui": snappy.Port{Port: "1024/tcp"}}
92- s2 := snappy.ServiceYaml{
93- Name: "s1",
94- Ports: &snappy.Ports{
95- Internal: internal2,
96- External: external2,
97- },
98- }
99- services = append(services, s2)
100-
101- return services
102-}
103-
104 type fakeSnapdClient struct{}
105
106 func (f *fakeSnapdClient) Icon(pkgID string) (*client.Icon, error) {
107@@ -189,4 +116,60 @@
108 return icon, nil
109 }
110
111+func (f *fakeSnapdClient) Services(pkg string) (map[string]*client.Service, error) {
112+ return nil, errors.New("the package has no services")
113+}
114+
115 var _ snapdClient = (*fakeSnapdClient)(nil)
116+
117+type fakeSnapdClientServicesNoExternalUI struct {
118+ fakeSnapdClient
119+}
120+
121+func (f *fakeSnapdClientServicesNoExternalUI) Services(pkg string) (map[string]*client.Service, error) {
122+ internal := map[string]client.ServicePort{"ui": client.ServicePort{Port: "200/tcp"}}
123+ external := map[string]client.ServicePort{"web": client.ServicePort{Port: "1024/tcp"}}
124+ s1 := &client.Service{
125+ Spec: client.ServiceSpec{
126+ Ports: client.ServicePorts{
127+ Internal: internal,
128+ External: external,
129+ },
130+ },
131+ }
132+
133+ s2 := &client.Service{}
134+
135+ services := map[string]*client.Service{
136+ "s1": s1,
137+ "s2": s2,
138+ }
139+
140+ return services, nil
141+}
142+
143+type fakeSnapdClientServicesExternalUI struct {
144+ fakeSnapdClient
145+}
146+
147+func (f *fakeSnapdClientServicesExternalUI) Services(pkg string) (map[string]*client.Service, error) {
148+ s1 := &client.Service{}
149+
150+ internal := map[string]client.ServicePort{"ui": client.ServicePort{Port: "200/tcp"}}
151+ external := map[string]client.ServicePort{"ui": client.ServicePort{Port: "1024/tcp"}}
152+ s2 := &client.Service{
153+ Spec: client.ServiceSpec{
154+ Ports: client.ServicePorts{
155+ Internal: internal,
156+ External: external,
157+ },
158+ },
159+ }
160+
161+ services := map[string]*client.Service{
162+ "s1": s1,
163+ "s2": s2,
164+ }
165+
166+ return services, nil
167+}
168
169=== modified file 'snappy/converge.go'
170--- snappy/converge.go 2016-01-27 14:16:35 +0000
171+++ snappy/converge.go 2016-01-27 18:32:39 +0000
172@@ -25,6 +25,7 @@
173
174 "log"
175
176+ "github.com/ubuntu-core/snappy/client"
177 "github.com/ubuntu-core/snappy/snap"
178 "github.com/ubuntu-core/snappy/snappy"
179 "launchpad.net/webdm/webprogress"
180@@ -239,8 +240,8 @@
181 }
182
183 if hasPortInformation(snapQ) {
184- if snapInstalled, ok := snapQ.(snappy.ServiceYamler); ok {
185- snap.UIPort = uiAccess(snapInstalled.ServiceYamls())
186+ if services, err := h.snapdClient.Services(snap.ID); err == nil {
187+ snap.UIPort = uiAccess(services)
188 }
189 }
190
191@@ -280,13 +281,13 @@
192 return snap
193 }
194
195-func uiAccess(services []snappy.ServiceYaml) uint64 {
196+func uiAccess(services map[string]*client.Service) uint64 {
197 for i := range services {
198- if services[i].Ports == nil {
199+ if services[i].Spec.Ports.External == nil {
200 continue
201 }
202
203- if ui, ok := services[i].Ports.External["ui"]; ok {
204+ if ui, ok := services[i].Spec.Ports.External["ui"]; ok {
205 ui := strings.Split(ui.Port, "/")
206 if len(ui) == 2 {
207 port, err := strconv.ParseUint(ui[0], 0, 64)
208
209=== modified file 'snappy/converge_test.go'
210--- snappy/converge_test.go 2016-01-27 14:16:35 +0000
211+++ snappy/converge_test.go 2016-01-27 18:32:39 +0000
212@@ -52,8 +52,9 @@
213 }
214
215 func (s *PayloadSuite) TestPayloadWithServicesButNoUI(c *C) {
216- fakeSnap := newDefaultFakeServices()
217- fakeSnap.serviceYamls = newFakeServicesNoExternalUI()
218+ s.h.setClient(&fakeSnapdClientServicesNoExternalUI{})
219+
220+ fakeSnap := newDefaultFakePart()
221 q := s.h.snapQueryToPayload(fakeSnap)
222
223 c.Assert(q.Name, Equals, fakeSnap.name)
224@@ -64,8 +65,9 @@
225 }
226
227 func (s *PayloadSuite) TestPayloadWithServicesUI(c *C) {
228- fakeSnap := newDefaultFakeServices()
229- fakeSnap.serviceYamls = newFakeServicesWithExternalUI()
230+ s.h.setClient(&fakeSnapdClientServicesExternalUI{})
231+
232+ fakeSnap := newDefaultFakePart()
233 q := s.h.snapQueryToPayload(fakeSnap)
234
235 c.Assert(q.Name, Equals, fakeSnap.name)
236@@ -75,9 +77,10 @@
237 c.Assert(q.UIPort, Equals, uint64(1024))
238 }
239
240-func (s *PayloadSuite) TestPayloadTypeOem(c *C) {
241- fakeSnap := newDefaultFakeServices()
242- fakeSnap.serviceYamls = newFakeServicesWithExternalUI()
243+func (s *PayloadSuite) TestPayloadTypeGadget(c *C) {
244+ s.h.setClient(&fakeSnapdClientServicesExternalUI{})
245+
246+ fakeSnap := newDefaultFakePart()
247 fakeSnap.snapType = snap.TypeGadget
248
249 q := s.h.snapQueryToPayload(fakeSnap)
250
251=== modified file 'snappy/snapd_client.go'
252--- snappy/snapd_client.go 2016-01-27 14:16:35 +0000
253+++ snappy/snapd_client.go 2016-01-27 18:32:39 +0000
254@@ -23,4 +23,5 @@
255
256 type snapdClient interface {
257 Icon(pkgID string) (*client.Icon, error)
258+ Services(pkg string) (map[string]*client.Service, error)
259 }

Subscribers

People subscribed via source and target branches