Merge lp:~elopio/snappy/return-string into lp:~fgimenez/snappy/go-functional-tests

Proposed by Leo Arias
Status: Merged
Approved by: Federico Gimenez
Approved revision: 517
Merged at revision: 513
Proposed branch: lp:~elopio/snappy/return-string
Merge into: lp:~fgimenez/snappy/go-functional-tests
Diff against target: 351 lines (+102/-92)
5 files modified
_integration-tests/tests/snappy_test.go (+8/-7)
provisioning/provisioning.go (+29/-5)
provisioning/provisioning_test.go (+58/-40)
snappy/install.go (+7/-25)
snappy/install_test.go (+0/-15)
To merge this branch: bzr merge lp:~elopio/snappy/return-string
Reviewer Review Type Date Requested Status
Federico Gimenez Approve
Review via email: mp+262646@code.launchpad.net

Commit message

Return a string on exec.

Description of the change

If the error we log is not converted to string, it will just print bytes. And if we convert it to string on the execCommand function, then it makes sense to return that too.

Also, merged with trunk.

To post a comment you must log in.
Revision history for this message
Federico Gimenez (fgimenez) wrote :

There are places where we don't use the returned value from the execCommand function, in that cases maybe the conversion is not needed. Maybe we could add another function that calls execComand and returns the converted value?

Thanks!

Revision history for this message
Federico Gimenez (fgimenez) wrote :

Approve, we can benchmark the alternative later

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '_integration-tests/tests/snappy_test.go'
2--- _integration-tests/tests/snappy_test.go 2015-06-19 04:26:01 +0000
3+++ _integration-tests/tests/snappy_test.go 2015-06-22 23:53:50 +0000
4@@ -33,15 +33,16 @@
5
6 type InstallSuite struct{}
7
8-func (s *InstallSuite) installSnap(c *C, packageName string) []byte {
9+func (s *InstallSuite) installSnap(c *C, packageName string) string {
10 return s.execCommand(c, "sudo", "snappy", "install", packageName)
11 }
12
13-func (s *InstallSuite) execCommand(c *C, cmds ...string) []byte {
14+func (s *InstallSuite) execCommand(c *C, cmds ...string) string {
15 cmd := exec.Command(cmds[0], cmds[1:len(cmds)]...)
16 output, err := cmd.CombinedOutput()
17- c.Assert(err, IsNil, Commentf("Error: %v", output))
18- return output
19+ stringOutput := string(output)
20+ c.Assert(err, IsNil, Commentf("Error: %v", stringOutput))
21+ return stringOutput
22 }
23
24 func (s *InstallSuite) SetUpSuite(c *C) {
25@@ -61,7 +62,7 @@
26 ".*\n" +
27 "hello-world .* .* canonical \n" +
28 ".*\n"
29- c.Assert(string(installOutput), Matches, expected)
30+ c.Assert(installOutput, Matches, expected)
31 }
32
33 func (s *InstallSuite) TestCallBinaryFromInstalledSnap(c *C) {
34@@ -69,7 +70,7 @@
35
36 echoOutput := s.execCommand(c, "hello-world.echo")
37
38- c.Assert(string(echoOutput), Equals, "Hello World!\n")
39+ c.Assert(echoOutput, Equals, "Hello World!\n")
40 }
41
42 func (s *InstallSuite) TestInfoMustPrintInstalledPackageInformation(c *C) {
43@@ -78,5 +79,5 @@
44 infoOutput := s.execCommand(c, "sudo", "snappy", "info")
45
46 expected := "(?ms).*^apps: hello-world\n"
47- c.Assert(string(infoOutput), Matches, expected)
48+ c.Assert(infoOutput, Matches, expected)
49 }
50
51=== modified file 'provisioning/provisioning.go'
52--- provisioning/provisioning.go 2015-06-09 18:07:51 +0000
53+++ provisioning/provisioning.go 2015-06-22 23:53:50 +0000
54@@ -29,18 +29,24 @@
55 "gopkg.in/yaml.v2"
56 )
57
58-var (
59+const (
60 // InstallYamlFile is the name of the file created by
61 // ubuntu-device-flash(1), created at system installation time,
62 // that contains metadata on the installation.
63 //
64 // XXX: Public for ubuntu-device-flash(1)
65 InstallYamlFile = "install.yaml"
66-
67- // ErrNoInstallYaml is emitted when InstallYamlFile does not exist.
68- ErrNoInstallYaml = fmt.Errorf("no %s", InstallYamlFile)
69 )
70
71+// ErrNoInstallYaml is emitted when InstallYamlFile does not exist.
72+type ErrNoInstallYaml struct {
73+ origErr error
74+}
75+
76+func (e *ErrNoInstallYaml) Error() string {
77+ return fmt.Sprintf("failed to read provisioning data: %s", e.origErr)
78+}
79+
80 // InstallMeta encapsulates the metadata for a system install.
81 type InstallMeta struct {
82 Timestamp time.Time
83@@ -79,7 +85,7 @@
84 func parseInstallYaml(path string) (*InstallYaml, error) {
85 data, err := ioutil.ReadFile(path)
86 if err != nil {
87- return nil, ErrNoInstallYaml
88+ return nil, &ErrNoInstallYaml{origErr: err}
89 }
90
91 return parseInstallYamlData(data)
92@@ -124,3 +130,21 @@
93
94 return false
95 }
96+
97+// InDeveloperMode returns true if the image was build with --developer-mode
98+func InDeveloperMode(bootloaderDir string) bool {
99+ file := filepath.Join(bootloaderDir, InstallYamlFile)
100+
101+ if !helpers.FileExists(file) {
102+ // no idea
103+ return false
104+ }
105+
106+ InstallYaml, err := parseInstallYaml(file)
107+ if err != nil {
108+ // no idea
109+ return false
110+ }
111+
112+ return InstallYaml.InstallOptions.DeveloperMode
113+}
114
115=== modified file 'provisioning/provisioning_test.go'
116--- provisioning/provisioning_test.go 2015-06-10 12:52:22 +0000
117+++ provisioning/provisioning_test.go 2015-06-22 23:53:50 +0000
118@@ -30,8 +30,8 @@
119 func Test(t *testing.T) { TestingT(t) }
120
121 type ProvisioningTestSuite struct {
122- tempdir string
123- installYamlFile string
124+ mockBootDir string
125+ mockYamlFile string
126 }
127
128 var _ = Suite(&ProvisioningTestSuite{})
129@@ -78,74 +78,70 @@
130 var garbageData = `Fooled you!?`
131
132 func (ts *ProvisioningTestSuite) SetUpTest(c *C) {
133- ts.tempdir = c.MkDir()
134-
135- ts.installYamlFile = filepath.Join(ts.tempdir, "install.yaml")
136-
137- InstallYamlFile = ts.installYamlFile
138- os.Remove(InstallYamlFile)
139+ ts.mockBootDir = c.MkDir()
140+ ts.mockYamlFile = filepath.Join(ts.mockBootDir, "install.yaml")
141 }
142
143 func (ts *ProvisioningTestSuite) TestSideLoadedSystemNoInstallYaml(c *C) {
144- c.Assert(IsSideLoaded(""), Equals, false)
145+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
146 }
147
148 func (ts *ProvisioningTestSuite) TestSideLoadedSystem(c *C) {
149- c.Assert(IsSideLoaded(""), Equals, false)
150+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
151
152- err := ioutil.WriteFile(InstallYamlFile, []byte(yamlData), 0750)
153+ err := ioutil.WriteFile(ts.mockYamlFile, []byte(yamlData), 0750)
154 c.Assert(err, IsNil)
155
156- c.Assert(IsSideLoaded(""), Equals, true)
157+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, true)
158
159- os.Remove(InstallYamlFile)
160- c.Assert(IsSideLoaded(""), Equals, false)
161+ os.Remove(ts.mockYamlFile)
162+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
163 }
164
165 func (ts *ProvisioningTestSuite) TestSideLoadedSystemNoDevicePart(c *C) {
166
167- c.Assert(IsSideLoaded(""), Equals, false)
168+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
169
170- err := ioutil.WriteFile(InstallYamlFile, []byte(yamlDataNoDevicePart), 0750)
171+ err := ioutil.WriteFile(ts.mockYamlFile, []byte(yamlDataNoDevicePart), 0750)
172 c.Assert(err, IsNil)
173
174- c.Assert(IsSideLoaded(""), Equals, false)
175+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
176
177- os.Remove(InstallYamlFile)
178- c.Assert(IsSideLoaded(""), Equals, false)
179+ os.Remove(ts.mockYamlFile)
180+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
181 }
182
183 func (ts *ProvisioningTestSuite) TestSideLoadedSystemGarbageInstallYaml(c *C) {
184- c.Assert(IsSideLoaded(""), Equals, false)
185+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
186
187- err := ioutil.WriteFile(InstallYamlFile, []byte(garbageData), 0750)
188+ err := ioutil.WriteFile(ts.mockYamlFile, []byte(garbageData), 0750)
189 c.Assert(err, IsNil)
190
191 // we assume sideloaded if the file isn't parseable
192- c.Assert(IsSideLoaded(""), Equals, true)
193+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, true)
194
195- os.Remove(InstallYamlFile)
196- c.Assert(IsSideLoaded(""), Equals, false)
197+ os.Remove(ts.mockYamlFile)
198+ c.Assert(IsSideLoaded(ts.mockBootDir), Equals, false)
199 }
200
201 func (ts *ProvisioningTestSuite) TestParseInstallYaml(c *C) {
202
203- _, err := parseInstallYaml(InstallYamlFile)
204- c.Check(err, Equals, ErrNoInstallYaml)
205-
206- err = ioutil.WriteFile(InstallYamlFile, []byte(yamlData), 0750)
207- c.Check(err, IsNil)
208- _, err = parseInstallYaml(InstallYamlFile)
209- c.Check(err, IsNil)
210-
211- err = ioutil.WriteFile(InstallYamlFile, []byte(yamlDataNoDevicePart), 0750)
212- c.Check(err, IsNil)
213- _, err = parseInstallYaml(InstallYamlFile)
214- c.Check(err, IsNil)
215-
216- err = ioutil.WriteFile(InstallYamlFile, []byte(garbageData), 0750)
217- c.Check(err, IsNil)
218- _, err = parseInstallYaml(InstallYamlFile)
219+ _, err := parseInstallYaml(ts.mockYamlFile)
220+ c.Check(err, ErrorMatches, `failed to read provisioning data: open .*/install.yaml: no such file or directory`)
221+
222+ err = ioutil.WriteFile(ts.mockYamlFile, []byte(yamlData), 0750)
223+ c.Check(err, IsNil)
224+ _, err = parseInstallYaml(ts.mockYamlFile)
225+ c.Check(err, IsNil)
226+
227+ err = ioutil.WriteFile(ts.mockYamlFile, []byte(yamlDataNoDevicePart), 0750)
228+ c.Check(err, IsNil)
229+ _, err = parseInstallYaml(ts.mockYamlFile)
230+ c.Check(err, IsNil)
231+
232+ err = ioutil.WriteFile(ts.mockYamlFile, []byte(garbageData), 0750)
233+ c.Check(err, IsNil)
234+ _, err = parseInstallYaml(ts.mockYamlFile)
235 c.Check(err, Not(Equals), nil)
236 }
237
238@@ -163,3 +159,25 @@
239 _, err = parseInstallYamlData([]byte(garbageData))
240 c.Check(err, Not(Equals), nil)
241 }
242+
243+func (ts *ProvisioningTestSuite) TestInDeveloperModeEmpty(c *C) {
244+ c.Assert(InDeveloperMode(""), Equals, false)
245+}
246+
247+func (ts *ProvisioningTestSuite) TestInDeveloperModeWithDevModeOn(c *C) {
248+ err := ioutil.WriteFile(ts.mockYamlFile, []byte(`
249+options:
250+ developer-mode: true
251+`), 0644)
252+ c.Assert(err, IsNil)
253+ c.Assert(InDeveloperMode(ts.mockBootDir), Equals, true)
254+}
255+
256+func (ts *ProvisioningTestSuite) TestInDeveloperModeWithDevModeOff(c *C) {
257+ err := ioutil.WriteFile(ts.mockYamlFile, []byte(`
258+options:
259+ developer-mode: false
260+`), 0644)
261+ c.Assert(err, IsNil)
262+ c.Assert(InDeveloperMode(ts.mockBootDir), Equals, false)
263+}
264
265=== modified file 'snappy/install.go'
266--- snappy/install.go 2015-06-10 13:38:10 +0000
267+++ snappy/install.go 2015-06-22 23:53:50 +0000
268@@ -21,13 +21,12 @@
269
270 import (
271 "fmt"
272- "io/ioutil"
273 "os"
274 "sort"
275- "strings"
276
277 "launchpad.net/snappy/logger"
278 "launchpad.net/snappy/progress"
279+ "launchpad.net/snappy/provisioning"
280 )
281
282 // InstallFlags can be used to pass additional flags to the install of a
283@@ -45,28 +44,6 @@
284 AllowOEM
285 )
286
287-// check if the image is in developer mode
288-// FIXME: this is a bit crude right now, but it seems like there is not more
289-// meta-data to check right now
290-// TODO: add feature to ubuntu-device-flash to write better info file when
291-// the image is in developer mode
292-func inDeveloperMode() bool {
293- f, err := os.Open(cloudMetaDataFile)
294- if err != nil {
295- return false
296- }
297- defer f.Close()
298- data, err := ioutil.ReadAll(f)
299- if err != nil {
300- return false
301- }
302- needle := "public-keys:\n"
303- if strings.Contains(string(data), needle) {
304- return true
305- }
306- return false
307-}
308-
309 // Update the installed snappy packages, it returns the updated Parts
310 // if updates where available and an error and nil if any of the updates
311 // fail to apply.
312@@ -115,7 +92,12 @@
313 if fi, err := os.Stat(name); err == nil && fi.Mode().IsRegular() {
314 // we allow unauthenticated package when in developer
315 // mode
316- if inDeveloperMode() {
317+ //
318+ // FIXME: this is terrible, we really need a single
319+ // bootloader dir like /boot or /boot/loader
320+ // instead of having to query the partition code
321+ p := newPartition()
322+ if provisioning.InDeveloperMode(p.BootloaderDir()) {
323 flags |= AllowUnauthenticated
324 }
325
326
327=== modified file 'snappy/install_test.go'
328--- snappy/install_test.go 2015-06-10 15:13:07 +0000
329+++ snappy/install_test.go 2015-06-22 23:53:50 +0000
330@@ -42,21 +42,6 @@
331 return w.Name()
332 }
333
334-func (s *SnapTestSuite) TestNotInDeveloperMode(c *C) {
335- cloudMetaDataFile = makeCloudInitMetaData(c, `instance-id: nocloud-static`)
336- defer os.Remove(cloudMetaDataFile)
337- c.Assert(inDeveloperMode(), Equals, false)
338-}
339-
340-func (s *SnapTestSuite) TestInDeveloperMode(c *C) {
341- cloudMetaDataFile = makeCloudInitMetaData(c, `instance-id: nocloud-static
342-public-keys:
343- - ssh-rsa AAAAB3NzAndSoOn
344-`)
345- defer os.Remove(cloudMetaDataFile)
346- c.Assert(inDeveloperMode(), Equals, true)
347-}
348-
349 func (s *SnapTestSuite) TestInstallInstall(c *C) {
350 snapFile := makeTestSnapPackage(c, "")
351 name, err := Install(snapFile, AllowUnauthenticated|DoInstallGC, &progress.NullProgress{})

Subscribers

People subscribed via source and target branches

to all changes: