Merge lp:~elopio/snappy/add_tests_tests into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Leo Arias
Status: Merged
Approved by: Leo Arias
Approved revision: 607
Merged at revision: 599
Proposed branch: lp:~elopio/snappy/add_tests_tests
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~elopio/snappy/one_tests_package
Diff against target: 515 lines (+153/-68)
20 files modified
_integration-tests/helpers/common/common.go (+11/-20)
_integration-tests/helpers/config/config.go (+26/-26)
_integration-tests/helpers/config/config_test.go (+93/-0)
_integration-tests/helpers/image/image.go (+1/-1)
_integration-tests/main.go (+3/-3)
_integration-tests/tests/apt_test.go (+1/-1)
_integration-tests/tests/build_test.go (+1/-1)
_integration-tests/tests/failover_rclocal_crash_test.go (+1/-1)
_integration-tests/tests/failover_systemd_loop_test.go (+1/-1)
_integration-tests/tests/failover_test.go (+1/-1)
_integration-tests/tests/failover_zero_size_file_test.go (+1/-1)
_integration-tests/tests/info_test.go (+2/-2)
_integration-tests/tests/installApp_test.go (+1/-1)
_integration-tests/tests/installFramework_test.go (+1/-1)
_integration-tests/tests/list_test.go (+1/-1)
_integration-tests/tests/rollback_test.go (+1/-1)
_integration-tests/tests/search_test.go (+1/-1)
_integration-tests/tests/update_test.go (+1/-1)
_integration-tests/tests/writablePaths_test.go (+1/-1)
run-checks (+4/-3)
To merge this branch: bzr merge lp:~elopio/snappy/add_tests_tests
Reviewer Review Type Date Requested Status
Federico Gimenez (community) Approve
Review via email: mp+265583@code.launchpad.net

Commit message

Refactored the helpers to be able to add tests for them.
Added tests for the config helpers.

Description of the change

I used as an example the config package. Now thinking about it again is the worst example, because we will get rid of it in favor of the test arguments. Oh well...

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

Great! Very good idea the config encapsulation and the helpers folder =)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory '_integration-tests/helpers'
2=== renamed directory '_integration-tests/common' => '_integration-tests/helpers/common'
3=== modified file '_integration-tests/helpers/common/common.go'
4--- _integration-tests/common/common.go 2015-07-22 15:12:24 +0000
5+++ _integration-tests/helpers/common/common.go 2015-07-22 19:10:34 +0000
6@@ -20,7 +20,6 @@
7 package common
8
9 import (
10- "encoding/json"
11 "fmt"
12 "io/ioutil"
13 "os"
14@@ -31,6 +30,8 @@
15 "strings"
16
17 check "gopkg.in/check.v1"
18+
19+ "launchpad.net/snappy/_integration-tests/helpers/config"
20 )
21
22 const (
23@@ -40,9 +41,9 @@
24 channelCfgFile = "/etc/system-image/channel.ini"
25 )
26
27-// Config is a map of strings that contains the configurations values passed
28-// from the host to the testbed.
29-var Config map[string]string
30+// Cfg is a struct that contains the configurations values passed from the
31+// host to the testbed.
32+var Cfg config.Config
33
34 // SnappySuite is a structure used as a base test suite for all the snappy
35 // integration tests.
36@@ -56,9 +57,11 @@
37 ExecCommand(c, "sudo", "systemctl", "stop", "snappy-autopilot.timer")
38 ExecCommand(c, "sudo", "systemctl", "disable", "snappy-autopilot.timer")
39 if !isInRebootProcess() {
40- Config = readConfig(c)
41- if Config["update"] == "true" || Config["rollback"] == "true" {
42- switchSystemImageConf(c, Config["targetRelease"], Config["targetChannel"], "0")
43+ Cfg, err := config.ReadConfig(
44+ "_integration-tests/data/output/testconfig.json")
45+ c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))
46+ if Cfg.Update || Cfg.Rollback {
47+ switchSystemImageConf(c, Cfg.TargetRelease, Cfg.TargetChannel, "0")
48 // Always use the installed snappy because we are updating from an old
49 // image, so we should not use the snappy from the branch.
50 output := ExecCommand(c, "sudo", "/usr/bin/snappy", "update")
51@@ -68,7 +71,7 @@
52 }
53 } else if CheckRebootMark("setupsuite-update") {
54 RemoveRebootMark(c)
55- if Config["rollback"] == "true" {
56+ if Cfg.Rollback {
57 ExecCommand(c, "sudo", "snappy", "rollback", "ubuntu-core")
58 RebootWithMark(c, "setupsuite-rollback")
59 }
60@@ -137,18 +140,6 @@
61 s.cleanupHandlers = append(s.cleanupHandlers, f)
62 }
63
64-func readConfig(c *check.C) map[string]string {
65- b, err := ioutil.ReadFile("_integration-tests/data/output/testconfig.json")
66- c.Assert(
67- err, check.IsNil, check.Commentf("Failed to read test config: %v", err))
68-
69- var decoded map[string]string
70- err = json.Unmarshal(b, &decoded)
71- c.Assert(
72- err, check.IsNil, check.Commentf("Failed to decode test config: %v", err))
73- return decoded
74-}
75-
76 func switchSystemImageConf(c *check.C, release, channel, version string) {
77 targets := []string{"/", BaseOtherPath}
78 for _, target := range targets {
79
80=== renamed directory '_integration-tests/config' => '_integration-tests/helpers/config'
81=== modified file '_integration-tests/helpers/config/config.go'
82--- _integration-tests/config/config.go 2015-07-22 15:12:24 +0000
83+++ _integration-tests/helpers/config/config.go 2015-07-22 19:10:34 +0000
84@@ -24,51 +24,51 @@
85 "fmt"
86 "io/ioutil"
87 "log"
88- "strconv"
89 )
90
91 // Config contains the values to pass to the test bed from the host.
92 type Config struct {
93- fileName string
94- release string
95- channel string
96- targetRelease string
97- targetChannel string
98- update bool
99- rollback bool
100+ FileName string
101+ Release string
102+ Channel string
103+ TargetRelease string
104+ TargetChannel string
105+ Update bool
106+ Rollback bool
107 }
108
109 // NewConfig is the Config constructor
110 func NewConfig(fileName, release, channel, targetRelease, targetChannel string, update, rollback bool) *Config {
111 return &Config{
112- fileName: fileName, release: release, channel: channel,
113- targetRelease: targetRelease, targetChannel: targetChannel,
114- update: update, rollback: rollback,
115+ FileName: fileName, Release: release, Channel: channel,
116+ TargetRelease: targetRelease, TargetChannel: targetChannel,
117+ Update: update, Rollback: rollback,
118 }
119 }
120
121 // Write writes the config to a file that will be copied to the test bed.
122 func (cfg Config) Write() {
123 fmt.Println("Writing test config...")
124- testConfig := map[string]string{
125- "release": cfg.release,
126- "channel": cfg.channel,
127- }
128- if cfg.targetRelease != "" {
129- testConfig["targetRelease"] = cfg.targetRelease
130- }
131- if cfg.targetChannel != "" {
132- testConfig["targetChannel"] = cfg.targetChannel
133- }
134- testConfig["update"] = strconv.FormatBool(cfg.update)
135- testConfig["rollback"] = strconv.FormatBool(cfg.rollback)
136- fmt.Println(testConfig)
137- encoded, err := json.Marshal(testConfig)
138+ fmt.Println(cfg)
139+ encoded, err := json.Marshal(cfg)
140 if err != nil {
141 log.Fatalf("Error encoding the test config: %v", err)
142 }
143- err = ioutil.WriteFile(cfg.fileName, encoded, 0644)
144+ err = ioutil.WriteFile(cfg.FileName, encoded, 0644)
145 if err != nil {
146 log.Fatalf("Error writing the test config: %v", err)
147 }
148 }
149+
150+// ReadConfig the config from a file
151+func ReadConfig(fileName string) (*Config, error) {
152+ b, err := ioutil.ReadFile(fileName)
153+ if err != nil {
154+ return nil, err
155+ }
156+ var decoded Config
157+ if err = json.Unmarshal(b, &decoded); err != nil {
158+ return nil, err
159+ }
160+ return &decoded, nil
161+}
162
163=== added file '_integration-tests/helpers/config/config_test.go'
164--- _integration-tests/helpers/config/config_test.go 1970-01-01 00:00:00 +0000
165+++ _integration-tests/helpers/config/config_test.go 2015-07-22 19:10:34 +0000
166@@ -0,0 +1,93 @@
167+// -*- Mode: Go; indent-tabs-mode: t -*-
168+
169+/*
170+ * Copyright (C) 2014-2015 Canonical Ltd
171+ *
172+ * This program is free software: you can redistribute it and/or modify
173+ * it under the terms of the GNU General Public License version 3 as
174+ * published by the Free Software Foundation.
175+ *
176+ * This program is distributed in the hope that it will be useful,
177+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
178+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
179+ * GNU General Public License for more details.
180+ *
181+ * You should have received a copy of the GNU General Public License
182+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
183+ *
184+ */
185+
186+package config
187+
188+import (
189+ "fmt"
190+ "io/ioutil"
191+ "os"
192+ "path/filepath"
193+ "testing"
194+
195+ check "gopkg.in/check.v1"
196+)
197+
198+// Hook up check.v1 into the "go test" runner
199+func Test(t *testing.T) { check.TestingT(t) }
200+
201+type ConfigSuite struct{}
202+
203+var _ = check.Suite(&ConfigSuite{})
204+
205+func testConfigFileName(c *check.C) string {
206+ tmpDir, err := ioutil.TempDir("", "")
207+ c.Assert(err, check.IsNil, check.Commentf(
208+ "Error creating a temporary directory: %v", err))
209+ return filepath.Join(tmpDir, "test.config")
210+}
211+
212+func testConfigStruct(fileName string) *Config {
213+ return NewConfig(
214+ fileName,
215+ "testrelease", "testchannel", "testtargetrelease", "testtargetchannel",
216+ true, true)
217+}
218+func testConfigContents(fileName string) string {
219+ return `{` +
220+ fmt.Sprintf(`"FileName":"%s",`, fileName) +
221+ `"Release":"testrelease",` +
222+ `"Channel":"testchannel",` +
223+ `"TargetRelease":"testtargetrelease",` +
224+ `"TargetChannel":"testtargetchannel",` +
225+ `"Update":true,` +
226+ `"Rollback":true` +
227+ `}`
228+}
229+
230+func (s *ConfigSuite) TestWriteConfig(c *check.C) {
231+ // Do not print to stdout.
232+ devnull, err := os.Open(os.DevNull)
233+ c.Assert(err, check.IsNil)
234+ oldStdout := os.Stdout
235+ os.Stdout = devnull
236+ defer func() {
237+ os.Stdout = oldStdout
238+ }()
239+ configFileName := testConfigFileName(c)
240+
241+ cfg := testConfigStruct(configFileName)
242+ cfg.Write()
243+
244+ writtenConfig, err := ioutil.ReadFile(configFileName)
245+ c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))
246+ c.Assert(string(writtenConfig), check.Equals, testConfigContents(configFileName))
247+}
248+
249+func (s *ConfigSuite) TestReadConfig(c *check.C) {
250+ configFileName := testConfigFileName(c)
251+
252+ configContents := testConfigContents(configFileName)
253+ ioutil.WriteFile(configFileName, []byte(configContents), 0644)
254+
255+ cfg, err := ReadConfig(configFileName)
256+
257+ c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))
258+ c.Assert(cfg, check.DeepEquals, testConfigStruct(configFileName))
259+}
260
261=== renamed directory '_integration-tests/image' => '_integration-tests/helpers/image'
262=== modified file '_integration-tests/helpers/image/image.go'
263--- _integration-tests/image/image.go 2015-07-22 15:12:24 +0000
264+++ _integration-tests/helpers/image/image.go 2015-07-22 19:10:34 +0000
265@@ -24,7 +24,7 @@
266 "path/filepath"
267 "strings"
268
269- utils "../utils"
270+ "launchpad.net/snappy/_integration-tests/helpers/utils"
271 )
272
273 // Image type encapsulates image actions
274
275=== renamed directory '_integration-tests/utils' => '_integration-tests/helpers/utils'
276=== modified file '_integration-tests/main.go'
277--- _integration-tests/main.go 2015-07-22 19:10:34 +0000
278+++ _integration-tests/main.go 2015-07-22 19:10:34 +0000
279@@ -28,9 +28,9 @@
280 "strconv"
281 "text/template"
282
283- config "./config"
284- image "./image"
285- utils "./utils"
286+ "launchpad.net/snappy/_integration-tests/helpers/config"
287+ "launchpad.net/snappy/_integration-tests/helpers/image"
288+ "launchpad.net/snappy/_integration-tests/helpers/utils"
289 )
290
291 const (
292
293=== modified file '_integration-tests/tests/apt_test.go'
294--- _integration-tests/tests/apt_test.go 2015-07-22 19:10:34 +0000
295+++ _integration-tests/tests/apt_test.go 2015-07-22 19:10:34 +0000
296@@ -20,7 +20,7 @@
297 package tests
298
299 import (
300- . "launchpad.net/snappy/_integration-tests/common"
301+ . "launchpad.net/snappy/_integration-tests/helpers/common"
302
303 check "gopkg.in/check.v1"
304 )
305
306=== modified file '_integration-tests/tests/build_test.go'
307--- _integration-tests/tests/build_test.go 2015-07-22 19:10:34 +0000
308+++ _integration-tests/tests/build_test.go 2015-07-22 19:10:34 +0000
309@@ -24,7 +24,7 @@
310 "os"
311 "os/exec"
312
313- . "launchpad.net/snappy/_integration-tests/common"
314+ . "launchpad.net/snappy/_integration-tests/helpers/common"
315
316 . "gopkg.in/check.v1"
317 )
318
319=== modified file '_integration-tests/tests/failover_rclocal_crash_test.go'
320--- _integration-tests/tests/failover_rclocal_crash_test.go 2015-07-22 19:10:34 +0000
321+++ _integration-tests/tests/failover_rclocal_crash_test.go 2015-07-22 19:10:34 +0000
322@@ -22,7 +22,7 @@
323 import (
324 "fmt"
325
326- . "launchpad.net/snappy/_integration-tests/common"
327+ . "launchpad.net/snappy/_integration-tests/helpers/common"
328
329 check "gopkg.in/check.v1"
330 )
331
332=== modified file '_integration-tests/tests/failover_systemd_loop_test.go'
333--- _integration-tests/tests/failover_systemd_loop_test.go 2015-07-22 19:10:34 +0000
334+++ _integration-tests/tests/failover_systemd_loop_test.go 2015-07-22 19:10:34 +0000
335@@ -22,7 +22,7 @@
336 import (
337 "fmt"
338
339- . "launchpad.net/snappy/_integration-tests/common"
340+ . "launchpad.net/snappy/_integration-tests/helpers/common"
341
342 check "gopkg.in/check.v1"
343 )
344
345=== modified file '_integration-tests/tests/failover_test.go'
346--- _integration-tests/tests/failover_test.go 2015-07-22 19:10:34 +0000
347+++ _integration-tests/tests/failover_test.go 2015-07-22 19:10:34 +0000
348@@ -22,7 +22,7 @@
349 import (
350 check "gopkg.in/check.v1"
351
352- . "launchpad.net/snappy/_integration-tests/common"
353+ . "launchpad.net/snappy/_integration-tests/helpers/common"
354 )
355
356 var _ = check.Suite(&failoverSuite{})
357
358=== modified file '_integration-tests/tests/failover_zero_size_file_test.go'
359--- _integration-tests/tests/failover_zero_size_file_test.go 2015-07-22 19:10:34 +0000
360+++ _integration-tests/tests/failover_zero_size_file_test.go 2015-07-22 19:10:34 +0000
361@@ -25,7 +25,7 @@
362 "path/filepath"
363 "strings"
364
365- . "launchpad.net/snappy/_integration-tests/common"
366+ . "launchpad.net/snappy/_integration-tests/helpers/common"
367
368 check "gopkg.in/check.v1"
369 )
370
371=== modified file '_integration-tests/tests/info_test.go'
372--- _integration-tests/tests/info_test.go 2015-07-22 19:10:34 +0000
373+++ _integration-tests/tests/info_test.go 2015-07-22 19:10:34 +0000
374@@ -22,7 +22,7 @@
375 import (
376 "fmt"
377
378- . "launchpad.net/snappy/_integration-tests/common"
379+ . "launchpad.net/snappy/_integration-tests/helpers/common"
380
381 check "gopkg.in/check.v1"
382 )
383@@ -37,7 +37,7 @@
384 infoOutput := ExecCommand(c, "snappy", "info")
385
386 expected := "(?ms)" +
387- fmt.Sprintf("^release: ubuntu-core/%s/%s\n", Config["release"], Config["channel"]) +
388+ fmt.Sprintf("^release: ubuntu-core/%s/%s\n", Cfg.Release, Cfg.Channel) +
389 ".*"
390
391 c.Assert(infoOutput, check.Matches, expected)
392
393=== modified file '_integration-tests/tests/installApp_test.go'
394--- _integration-tests/tests/installApp_test.go 2015-07-22 19:10:34 +0000
395+++ _integration-tests/tests/installApp_test.go 2015-07-22 19:10:34 +0000
396@@ -24,7 +24,7 @@
397 "os/exec"
398 "time"
399
400- . "launchpad.net/snappy/_integration-tests/common"
401+ . "launchpad.net/snappy/_integration-tests/helpers/common"
402
403 check "gopkg.in/check.v1"
404 )
405
406=== modified file '_integration-tests/tests/installFramework_test.go'
407--- _integration-tests/tests/installFramework_test.go 2015-07-22 19:10:34 +0000
408+++ _integration-tests/tests/installFramework_test.go 2015-07-22 19:10:34 +0000
409@@ -23,7 +23,7 @@
410 "fmt"
411 "regexp"
412
413- . "launchpad.net/snappy/_integration-tests/common"
414+ . "launchpad.net/snappy/_integration-tests/helpers/common"
415
416 check "gopkg.in/check.v1"
417 )
418
419=== modified file '_integration-tests/tests/list_test.go'
420--- _integration-tests/tests/list_test.go 2015-07-22 19:10:34 +0000
421+++ _integration-tests/tests/list_test.go 2015-07-22 19:10:34 +0000
422@@ -23,7 +23,7 @@
423 "fmt"
424 "os"
425
426- . "launchpad.net/snappy/_integration-tests/common"
427+ . "launchpad.net/snappy/_integration-tests/helpers/common"
428
429 "github.com/mvo5/goconfigparser"
430 check "gopkg.in/check.v1"
431
432=== modified file '_integration-tests/tests/rollback_test.go'
433--- _integration-tests/tests/rollback_test.go 2015-07-22 19:10:34 +0000
434+++ _integration-tests/tests/rollback_test.go 2015-07-22 19:10:34 +0000
435@@ -22,7 +22,7 @@
436 import (
437 "strconv"
438
439- . "launchpad.net/snappy/_integration-tests/common"
440+ . "launchpad.net/snappy/_integration-tests/helpers/common"
441
442 check "gopkg.in/check.v1"
443 )
444
445=== modified file '_integration-tests/tests/search_test.go'
446--- _integration-tests/tests/search_test.go 2015-07-22 19:10:34 +0000
447+++ _integration-tests/tests/search_test.go 2015-07-22 19:10:34 +0000
448@@ -20,7 +20,7 @@
449 package tests
450
451 import (
452- . "launchpad.net/snappy/_integration-tests/common"
453+ . "launchpad.net/snappy/_integration-tests/helpers/common"
454
455 . "gopkg.in/check.v1"
456 )
457
458=== modified file '_integration-tests/tests/update_test.go'
459--- _integration-tests/tests/update_test.go 2015-07-22 19:10:34 +0000
460+++ _integration-tests/tests/update_test.go 2015-07-22 19:10:34 +0000
461@@ -20,7 +20,7 @@
462 package tests
463
464 import (
465- . "launchpad.net/snappy/_integration-tests/common"
466+ . "launchpad.net/snappy/_integration-tests/helpers/common"
467
468 check "gopkg.in/check.v1"
469 )
470
471=== modified file '_integration-tests/tests/writablePaths_test.go'
472--- _integration-tests/tests/writablePaths_test.go 2015-07-22 19:10:34 +0000
473+++ _integration-tests/tests/writablePaths_test.go 2015-07-22 19:10:34 +0000
474@@ -27,7 +27,7 @@
475 "path/filepath"
476 "strings"
477
478- . "launchpad.net/snappy/_integration-tests/common"
479+ . "launchpad.net/snappy/_integration-tests/helpers/common"
480
481 check "gopkg.in/check.v1"
482 )
483
484=== modified file 'run-checks'
485--- run-checks 2015-07-22 19:10:34 +0000
486+++ run-checks 2015-07-22 19:10:34 +0000
487@@ -1,5 +1,5 @@
488 #!/bin/sh
489-
490+1;3803;0c
491 set -ev
492
493 if which goctest >/dev/null; then
494@@ -46,10 +46,11 @@
495 echo Running vet
496 go vet ./...
497 go vet ./_integration-tests/tests/...
498+go vet ./_integration-tests/helpers/...
499
500 # golint
501 echo Running lint
502-lint=$(golint ./... && golint ./_integration-tests/tests/...)
503+lint=$(golint ./... && golint ./_integration-tests/helpers/... && golint ./_integration-tests/tests/...)
504 if [ -n "$lint" ]; then
505 echo "Lint complains:"
506 echo $lint
507@@ -72,7 +73,7 @@
508
509 # the rabbit hole
510 echo Running the tests for the integration helpers
511-$goctest -v -cover ./_integration-tests/common/...
512+$goctest -v -cover ./_integration-tests/helpers/...
513
514 # integration suite in kvm
515 if which adt-run >/dev/null 2>&1; then

Subscribers

People subscribed via source and target branches