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

Proposed by Leo Arias
Status: Merged
Approved by: Sergio Schvezov
Approved revision: 541
Merged at revision: 540
Proposed branch: lp:~elopio/snappy/lint_and_vet_tests
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 285 lines (+89/-61)
5 files modified
_integration-tests/tests/common/common.go (+83/-56)
_integration-tests/tests/failover/failover_test.go (+1/-1)
_integration-tests/tests/latest/install_test.go (+1/-1)
_integration-tests/tests/update/update_test.go (+2/-2)
run-checks (+2/-1)
To merge this branch: bzr merge lp:~elopio/snappy/lint_and_vet_tests
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve
Review via email: mp+263388@code.launchpad.net

Commit message

Added lint and vet checks for the integration tests.
Fixed the existing errors.

To post a comment you must log in.
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

nice

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/common/common.go'
2--- _integration-tests/tests/common/common.go 2015-06-29 16:04:21 +0000
3+++ _integration-tests/tests/common/common.go 2015-06-30 17:01:39 +0000
4@@ -29,100 +29,156 @@
5 "strconv"
6 "strings"
7
8- . "gopkg.in/check.v1"
9+ check "gopkg.in/check.v1"
10 )
11
12 const (
13 needsRebootFile = "/tmp/needs-reboot"
14 )
15
16-type CommonSuite struct{}
17-
18-func ExecCommand(c *C, cmds ...string) string {
19+// SnappySuite is a structure used as a base test suite for all the snappy
20+// integration tests.
21+type SnappySuite struct{}
22+
23+// SetUpSuite disables the snappy autopilot. It will run before all the
24+// integration suites.
25+func (s *SnappySuite) SetUpSuite(c *check.C) {
26+ ExecCommand(c, "sudo", "systemctl", "stop", "snappy-autopilot.timer")
27+ ExecCommand(c, "sudo", "systemctl", "disable", "snappy-autopilot.timer")
28+}
29+
30+// SetUpTest handles reboots and stores version information. It will run before
31+// all the integration tests. Before running a test, it will save the
32+// ubuntu-core version. If a reboot was requested by a previous test, it
33+// will skip all the following tests. If the suite is being called after the
34+// test bed was rebooted, it will resume the test that requested the reboot.
35+func (s *SnappySuite) SetUpTest(c *check.C) {
36+ if needsReboot() {
37+ contents, err := ioutil.ReadFile(needsRebootFile)
38+ c.Assert(err, check.IsNil, check.Commentf("Error reading needs-reboot file %v", err))
39+ c.Skip(fmt.Sprintf("****** Skipped %s during reboot caused by %s",
40+ c.TestName(), contents))
41+ } else {
42+
43+ afterReboot := os.Getenv("ADT_REBOOT_MARK")
44+
45+ if afterReboot == "" {
46+ c.Logf("****** Running %s", c.TestName())
47+ SetSavedVersion(c, GetCurrentVersion(c))
48+ } else {
49+ if afterReboot == c.TestName() {
50+ c.Logf("****** Resuming %s after reboot", c.TestName())
51+ } else {
52+ c.Skip(fmt.Sprintf("****** Skipped %s after reboot caused by %s",
53+ c.TestName(), afterReboot))
54+ }
55+ }
56+ }
57+}
58+
59+// ExecCommand executes a shell command and returns a string with the output
60+// of the command. In case of error, it will fail the test.
61+func ExecCommand(c *check.C, cmds ...string) string {
62 fmt.Println(strings.Join(cmds, " "))
63 cmd := exec.Command(cmds[0], cmds[1:len(cmds)]...)
64 output, err := cmd.CombinedOutput()
65 stringOutput := string(output)
66- c.Assert(err, IsNil, Commentf("Error: %v", stringOutput))
67+ c.Assert(err, check.IsNil, check.Commentf("Error: %v", stringOutput))
68 return stringOutput
69 }
70
71-func ExecCommandToFile(c *C, filename string, cmds ...string) {
72+// ExecCommandToFile executes a shell command and saves the output of the
73+// command to a file. In case of error, it will fail the test.
74+func ExecCommandToFile(c *check.C, filename string, cmds ...string) {
75 cmd := exec.Command(cmds[0], cmds[1:len(cmds)]...)
76 outfile, err := os.Create(filename)
77- c.Assert(err, IsNil, Commentf("Error creating output file %s", filename))
78+ c.Assert(err, check.IsNil, check.Commentf("Error creating output file %s", filename))
79
80 defer outfile.Close()
81 cmd.Stdout = outfile
82
83 err = cmd.Run()
84- c.Assert(err, IsNil, Commentf("Error executing command '%v': %v", cmds, err))
85+ c.Assert(err, check.IsNil, check.Commentf("Error executing command '%v': %v", cmds, err))
86 }
87
88-func GetCurrentVersion(c *C) int {
89+// GetCurrentVersion returns the version number of the installed and active
90+// ubuntu-core.
91+func GetCurrentVersion(c *check.C) int {
92 output := ExecCommand(c, "snappy", "list")
93 pattern := "(?mU)^ubuntu-core (.*)$"
94 re := regexp.MustCompile(pattern)
95 match := re.FindStringSubmatch(string(output))
96- c.Assert(match, NotNil, Commentf("Version not found in %s", output))
97+ c.Assert(match, check.NotNil, check.Commentf("Version not found in %s", output))
98
99 // match is like "ubuntu-core 2015-06-18 93 ubuntu"
100 items := strings.Fields(match[0])
101 version, err := strconv.Atoi(items[2])
102- c.Assert(err, IsNil, Commentf("Error converting version to int %v", version))
103+ c.Assert(err, check.IsNil, check.Commentf("Error converting version to int %v", version))
104 return version
105 }
106
107-func CallUpdate(c *C) {
108+// CallUpdate executes an snappy update.
109+func CallUpdate(c *check.C) {
110 c.Log("Calling snappy update...")
111 ExecCommand(c, "sudo", "snappy", "update")
112 }
113
114-func Reboot(c *C) {
115+// Reboot requests a reboot using the test name as the mark.
116+func Reboot(c *check.C) {
117 RebootWithMark(c, c.TestName())
118 }
119
120-func RebootWithMark(c *C, mark string) {
121+// RebootWithMark requests a reboot using a specified mark.
122+func RebootWithMark(c *check.C, mark string) {
123 c.Log("Preparing reboot with mark " + mark)
124 err := ioutil.WriteFile(needsRebootFile, []byte(mark), 0777)
125- c.Assert(err, IsNil, Commentf("Error writing needs-reboot file: %v", err))
126+ c.Assert(err, check.IsNil, check.Commentf("Error writing needs-reboot file: %v", err))
127 }
128
129-func needsReboot(c *C) bool {
130+func needsReboot() bool {
131 _, err := os.Stat(needsRebootFile)
132 return err == nil
133 }
134
135-func BeforeReboot(c *C) bool {
136- return checkRebootMark(c, "")
137+// BeforeReboot returns True if the test is running before the test bed has
138+// been rebooted, or after the test that requested the reboot handled it.
139+func BeforeReboot() bool {
140+ return checkRebootMark("")
141 }
142
143-func AfterReboot(c *C) bool {
144+// AfterReboot returns True if the test is running after the test bed has been
145+// rebooted.
146+func AfterReboot(c *check.C) bool {
147 // $ADT_REBOOT_MARK contains the reboot mark, if we have rebooted it'll be the test name
148- return checkRebootMark(c, c.TestName())
149+ return checkRebootMark(c.TestName())
150 }
151
152-func checkRebootMark(c *C, mark string) bool {
153+func checkRebootMark(mark string) bool {
154 return os.Getenv("ADT_REBOOT_MARK") == mark
155 }
156
157-func RemoveRebootMark(c *C) {
158+// RemoveRebootMark removes the reboot mark to signal that the reboot has been
159+// handled.
160+func RemoveRebootMark(c *check.C) {
161 os.Setenv("ADT_REBOOT_MARK", "")
162 }
163
164-func SetSavedVersion(c *C, version int) {
165+// SetSavedVersion saves a version number into a file so it can be used on
166+// tests after reboots.
167+func SetSavedVersion(c *check.C, version int) {
168 versionFile := getVersionFile()
169 err := ioutil.WriteFile(versionFile, []byte(strconv.Itoa(version)), 0777)
170- c.Assert(err, IsNil, Commentf("Error writing version file %s with %s", versionFile, version))
171+ c.Assert(err, check.IsNil, check.Commentf("Error writing version file %s with %s", versionFile, version))
172 }
173
174-func GetSavedVersion(c *C) int {
175+// GetSavedVersion returns the saved version number.
176+func GetSavedVersion(c *check.C) int {
177 versionFile := getVersionFile()
178 contents, err := ioutil.ReadFile(versionFile)
179- c.Assert(err, IsNil, Commentf("Error reading version file %s", versionFile))
180+ c.Assert(err, check.IsNil, check.Commentf("Error reading version file %s", versionFile))
181
182 version, err := strconv.Atoi(string(contents))
183- c.Assert(err, IsNil, Commentf("Error converting version %v", contents))
184+ c.Assert(err, check.IsNil, check.Commentf("Error converting version %v", contents))
185
186 return version
187 }
188@@ -130,32 +186,3 @@
189 func getVersionFile() string {
190 return filepath.Join(os.Getenv("ADT_ARTIFACTS"), "version")
191 }
192-
193-func (s *CommonSuite) SetUpSuite(c *C) {
194- ExecCommand(c, "sudo", "systemctl", "stop", "snappy-autopilot.timer")
195- ExecCommand(c, "sudo", "systemctl", "disable", "snappy-autopilot.timer")
196-}
197-
198-func (s *CommonSuite) SetUpTest(c *C) {
199- if needsReboot(c) {
200- contents, err := ioutil.ReadFile(needsRebootFile)
201- c.Assert(err, IsNil, Commentf("Error reading needs-reboot file %v", err))
202- c.Skip(fmt.Sprintf("****** Skipped %s during reboot caused by %s",
203- c.TestName(), contents))
204- } else {
205-
206- afterReboot := os.Getenv("ADT_REBOOT_MARK")
207-
208- if afterReboot == "" {
209- c.Logf("****** Running %s", c.TestName())
210- SetSavedVersion(c, GetCurrentVersion(c))
211- } else {
212- if afterReboot == c.TestName() {
213- c.Logf("****** Resuming %s after reboot", c.TestName())
214- } else {
215- c.Skip(fmt.Sprintf("****** Skipped %s after reboot caused by %s",
216- c.TestName(), afterReboot))
217- }
218- }
219- }
220-}
221
222=== modified file '_integration-tests/tests/failover/failover_test.go'
223--- _integration-tests/tests/failover/failover_test.go 2015-06-29 16:06:12 +0000
224+++ _integration-tests/tests/failover/failover_test.go 2015-06-30 17:01:39 +0000
225@@ -36,7 +36,7 @@
226 var _ = Suite(&failoverSuite{})
227
228 type failoverSuite struct {
229- CommonSuite
230+ SnappySuite
231 }
232
233 const (
234
235=== modified file '_integration-tests/tests/latest/install_test.go'
236--- _integration-tests/tests/latest/install_test.go 2015-06-30 15:02:22 +0000
237+++ _integration-tests/tests/latest/install_test.go 2015-06-30 17:01:39 +0000
238@@ -34,7 +34,7 @@
239 var _ = Suite(&installSuite{})
240
241 type installSuite struct {
242- CommonSuite
243+ SnappySuite
244 }
245
246 func installSnap(c *C, packageName string) string {
247
248=== modified file '_integration-tests/tests/update/update_test.go'
249--- _integration-tests/tests/update/update_test.go 2015-06-24 17:19:58 +0000
250+++ _integration-tests/tests/update/update_test.go 2015-06-30 17:01:39 +0000
251@@ -33,7 +33,7 @@
252 var _ = Suite(&updateSuite{})
253
254 type updateSuite struct {
255- CommonSuite
256+ SnappySuite
257 }
258
259 func rollback(c *C) {
260@@ -46,7 +46,7 @@
261 }
262
263 func (s *updateSuite) TestUpdateMustInstallNewerVersion(c *C) {
264- if BeforeReboot(c) {
265+ if BeforeReboot() {
266 CallUpdate(c)
267 Reboot(c)
268 } else if AfterReboot(c) {
269
270=== modified file 'run-checks'
271--- run-checks 2015-06-29 23:15:00 +0000
272+++ run-checks 2015-06-30 17:01:39 +0000
273@@ -45,10 +45,11 @@
274 # go vet
275 echo Running vet
276 go vet ./...
277+go vet ./_integration-tests/tests/...
278
279 # golint
280 echo Running lint
281-lint=$(golint ./...)
282+lint=$(golint ./... && golint ./_integration-tests/tests/...)
283 if [ -n "$lint" ]; then
284 echo "Lint complains:"
285 echo $lint

Subscribers

People subscribed via source and target branches