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
=== modified file 'snappy/common_test.go'
--- snappy/common_test.go 2016-01-27 14:16:35 +0000
+++ snappy/common_test.go 2016-01-27 18:32:39 +0000
@@ -18,6 +18,7 @@
18package snappy18package snappy
1919
20import (20import (
21 "errors"
21 "testing"22 "testing"
2223
23 "github.com/ubuntu-core/snappy/client"24 "github.com/ubuntu-core/snappy/client"
@@ -40,11 +41,6 @@
40 snapType snap.Type41 snapType snap.Type
41}42}
4243
43type fakeSnappyPartServices struct {
44 fakeSnappyPart
45 serviceYamls []snappy.ServiceYaml
46}
47
48func newDefaultFakePart() *fakeSnappyPart {44func newDefaultFakePart() *fakeSnappyPart {
49 return &fakeSnappyPart{45 return &fakeSnappyPart{
50 name: "camlistore",46 name: "camlistore",
@@ -66,31 +62,6 @@
66 }62 }
67}63}
6864
69func newParametrizedFake(name, version string, installed bool, snapType snap.Type) *fakeSnappyPart {
70 return &fakeSnappyPart{
71 name: name,
72 version: version,
73 installed: installed,
74 snapType: snapType,
75 }
76}
77
78func newDefaultFakeServices() *fakeSnappyPartServices {
79 return &fakeSnappyPartServices{
80 fakeSnappyPart: fakeSnappyPart{
81 name: "camlistore",
82 origin: "sergiusens",
83 version: "2.0",
84 installed: true,
85 snapType: snap.TypeApp,
86 },
87 }
88}
89
90func (p fakeSnappyPartServices) ServiceYamls() []snappy.ServiceYaml {
91 return p.serviceYamls
92}
93
94func (p fakeSnappyPart) IsInstalled() bool {65func (p fakeSnappyPart) IsInstalled() bool {
95 return p.installed66 return p.installed
96}67}
@@ -135,50 +106,6 @@
135 return p.description106 return p.description
136}107}
137108
138func newFakeServicesNoExternalUI() []snappy.ServiceYaml {
139 services := make([]snappy.ServiceYaml, 0, 2)
140
141 internal1 := map[string]snappy.Port{"ui": snappy.Port{Port: "200/tcp"}}
142 external1 := map[string]snappy.Port{"web": snappy.Port{Port: "1024/tcp"}}
143 s1 := snappy.ServiceYaml{
144 Name: "s1",
145 Ports: &snappy.Ports{
146 Internal: internal1,
147 External: external1,
148 },
149 }
150 services = append(services, s1)
151
152 s2 := snappy.ServiceYaml{
153 Name: "s2",
154 }
155 services = append(services, s2)
156
157 return services
158}
159
160func newFakeServicesWithExternalUI() []snappy.ServiceYaml {
161 services := make([]snappy.ServiceYaml, 0, 2)
162
163 s1 := snappy.ServiceYaml{
164 Name: "s2",
165 }
166 services = append(services, s1)
167
168 internal2 := map[string]snappy.Port{"ui": snappy.Port{Port: "200/tcp"}}
169 external2 := map[string]snappy.Port{"ui": snappy.Port{Port: "1024/tcp"}}
170 s2 := snappy.ServiceYaml{
171 Name: "s1",
172 Ports: &snappy.Ports{
173 Internal: internal2,
174 External: external2,
175 },
176 }
177 services = append(services, s2)
178
179 return services
180}
181
182type fakeSnapdClient struct{}109type fakeSnapdClient struct{}
183110
184func (f *fakeSnapdClient) Icon(pkgID string) (*client.Icon, error) {111func (f *fakeSnapdClient) Icon(pkgID string) (*client.Icon, error) {
@@ -189,4 +116,60 @@
189 return icon, nil116 return icon, nil
190}117}
191118
119func (f *fakeSnapdClient) Services(pkg string) (map[string]*client.Service, error) {
120 return nil, errors.New("the package has no services")
121}
122
192var _ snapdClient = (*fakeSnapdClient)(nil)123var _ snapdClient = (*fakeSnapdClient)(nil)
124
125type fakeSnapdClientServicesNoExternalUI struct {
126 fakeSnapdClient
127}
128
129func (f *fakeSnapdClientServicesNoExternalUI) Services(pkg string) (map[string]*client.Service, error) {
130 internal := map[string]client.ServicePort{"ui": client.ServicePort{Port: "200/tcp"}}
131 external := map[string]client.ServicePort{"web": client.ServicePort{Port: "1024/tcp"}}
132 s1 := &client.Service{
133 Spec: client.ServiceSpec{
134 Ports: client.ServicePorts{
135 Internal: internal,
136 External: external,
137 },
138 },
139 }
140
141 s2 := &client.Service{}
142
143 services := map[string]*client.Service{
144 "s1": s1,
145 "s2": s2,
146 }
147
148 return services, nil
149}
150
151type fakeSnapdClientServicesExternalUI struct {
152 fakeSnapdClient
153}
154
155func (f *fakeSnapdClientServicesExternalUI) Services(pkg string) (map[string]*client.Service, error) {
156 s1 := &client.Service{}
157
158 internal := map[string]client.ServicePort{"ui": client.ServicePort{Port: "200/tcp"}}
159 external := map[string]client.ServicePort{"ui": client.ServicePort{Port: "1024/tcp"}}
160 s2 := &client.Service{
161 Spec: client.ServiceSpec{
162 Ports: client.ServicePorts{
163 Internal: internal,
164 External: external,
165 },
166 },
167 }
168
169 services := map[string]*client.Service{
170 "s1": s1,
171 "s2": s2,
172 }
173
174 return services, nil
175}
193176
=== modified file 'snappy/converge.go'
--- snappy/converge.go 2016-01-27 14:16:35 +0000
+++ snappy/converge.go 2016-01-27 18:32:39 +0000
@@ -25,6 +25,7 @@
2525
26 "log"26 "log"
2727
28 "github.com/ubuntu-core/snappy/client"
28 "github.com/ubuntu-core/snappy/snap"29 "github.com/ubuntu-core/snappy/snap"
29 "github.com/ubuntu-core/snappy/snappy"30 "github.com/ubuntu-core/snappy/snappy"
30 "launchpad.net/webdm/webprogress"31 "launchpad.net/webdm/webprogress"
@@ -239,8 +240,8 @@
239 }240 }
240241
241 if hasPortInformation(snapQ) {242 if hasPortInformation(snapQ) {
242 if snapInstalled, ok := snapQ.(snappy.ServiceYamler); ok {243 if services, err := h.snapdClient.Services(snap.ID); err == nil {
243 snap.UIPort = uiAccess(snapInstalled.ServiceYamls())244 snap.UIPort = uiAccess(services)
244 }245 }
245 }246 }
246247
@@ -280,13 +281,13 @@
280 return snap281 return snap
281}282}
282283
283func uiAccess(services []snappy.ServiceYaml) uint64 {284func uiAccess(services map[string]*client.Service) uint64 {
284 for i := range services {285 for i := range services {
285 if services[i].Ports == nil {286 if services[i].Spec.Ports.External == nil {
286 continue287 continue
287 }288 }
288289
289 if ui, ok := services[i].Ports.External["ui"]; ok {290 if ui, ok := services[i].Spec.Ports.External["ui"]; ok {
290 ui := strings.Split(ui.Port, "/")291 ui := strings.Split(ui.Port, "/")
291 if len(ui) == 2 {292 if len(ui) == 2 {
292 port, err := strconv.ParseUint(ui[0], 0, 64)293 port, err := strconv.ParseUint(ui[0], 0, 64)
293294
=== modified file 'snappy/converge_test.go'
--- snappy/converge_test.go 2016-01-27 14:16:35 +0000
+++ snappy/converge_test.go 2016-01-27 18:32:39 +0000
@@ -52,8 +52,9 @@
52}52}
5353
54func (s *PayloadSuite) TestPayloadWithServicesButNoUI(c *C) {54func (s *PayloadSuite) TestPayloadWithServicesButNoUI(c *C) {
55 fakeSnap := newDefaultFakeServices()55 s.h.setClient(&fakeSnapdClientServicesNoExternalUI{})
56 fakeSnap.serviceYamls = newFakeServicesNoExternalUI()56
57 fakeSnap := newDefaultFakePart()
57 q := s.h.snapQueryToPayload(fakeSnap)58 q := s.h.snapQueryToPayload(fakeSnap)
5859
59 c.Assert(q.Name, Equals, fakeSnap.name)60 c.Assert(q.Name, Equals, fakeSnap.name)
@@ -64,8 +65,9 @@
64}65}
6566
66func (s *PayloadSuite) TestPayloadWithServicesUI(c *C) {67func (s *PayloadSuite) TestPayloadWithServicesUI(c *C) {
67 fakeSnap := newDefaultFakeServices()68 s.h.setClient(&fakeSnapdClientServicesExternalUI{})
68 fakeSnap.serviceYamls = newFakeServicesWithExternalUI()69
70 fakeSnap := newDefaultFakePart()
69 q := s.h.snapQueryToPayload(fakeSnap)71 q := s.h.snapQueryToPayload(fakeSnap)
7072
71 c.Assert(q.Name, Equals, fakeSnap.name)73 c.Assert(q.Name, Equals, fakeSnap.name)
@@ -75,9 +77,10 @@
75 c.Assert(q.UIPort, Equals, uint64(1024))77 c.Assert(q.UIPort, Equals, uint64(1024))
76}78}
7779
78func (s *PayloadSuite) TestPayloadTypeOem(c *C) {80func (s *PayloadSuite) TestPayloadTypeGadget(c *C) {
79 fakeSnap := newDefaultFakeServices()81 s.h.setClient(&fakeSnapdClientServicesExternalUI{})
80 fakeSnap.serviceYamls = newFakeServicesWithExternalUI()82
83 fakeSnap := newDefaultFakePart()
81 fakeSnap.snapType = snap.TypeGadget84 fakeSnap.snapType = snap.TypeGadget
8285
83 q := s.h.snapQueryToPayload(fakeSnap)86 q := s.h.snapQueryToPayload(fakeSnap)
8487
=== modified file 'snappy/snapd_client.go'
--- snappy/snapd_client.go 2016-01-27 14:16:35 +0000
+++ snappy/snapd_client.go 2016-01-27 18:32:39 +0000
@@ -23,4 +23,5 @@
2323
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}27}

Subscribers

People subscribed via source and target branches