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
=== modified file '_integration-tests/tests/common/common.go'
--- _integration-tests/tests/common/common.go 2015-06-29 16:04:21 +0000
+++ _integration-tests/tests/common/common.go 2015-06-30 17:01:39 +0000
@@ -29,100 +29,156 @@
29 "strconv"29 "strconv"
30 "strings"30 "strings"
3131
32 . "gopkg.in/check.v1"32 check "gopkg.in/check.v1"
33)33)
3434
35const (35const (
36 needsRebootFile = "/tmp/needs-reboot"36 needsRebootFile = "/tmp/needs-reboot"
37)37)
3838
39type CommonSuite struct{}39// SnappySuite is a structure used as a base test suite for all the snappy
4040// integration tests.
41func ExecCommand(c *C, cmds ...string) string {41type SnappySuite struct{}
42
43// SetUpSuite disables the snappy autopilot. It will run before all the
44// integration suites.
45func (s *SnappySuite) SetUpSuite(c *check.C) {
46 ExecCommand(c, "sudo", "systemctl", "stop", "snappy-autopilot.timer")
47 ExecCommand(c, "sudo", "systemctl", "disable", "snappy-autopilot.timer")
48}
49
50// SetUpTest handles reboots and stores version information. It will run before
51// all the integration tests. Before running a test, it will save the
52// ubuntu-core version. If a reboot was requested by a previous test, it
53// will skip all the following tests. If the suite is being called after the
54// test bed was rebooted, it will resume the test that requested the reboot.
55func (s *SnappySuite) SetUpTest(c *check.C) {
56 if needsReboot() {
57 contents, err := ioutil.ReadFile(needsRebootFile)
58 c.Assert(err, check.IsNil, check.Commentf("Error reading needs-reboot file %v", err))
59 c.Skip(fmt.Sprintf("****** Skipped %s during reboot caused by %s",
60 c.TestName(), contents))
61 } else {
62
63 afterReboot := os.Getenv("ADT_REBOOT_MARK")
64
65 if afterReboot == "" {
66 c.Logf("****** Running %s", c.TestName())
67 SetSavedVersion(c, GetCurrentVersion(c))
68 } else {
69 if afterReboot == c.TestName() {
70 c.Logf("****** Resuming %s after reboot", c.TestName())
71 } else {
72 c.Skip(fmt.Sprintf("****** Skipped %s after reboot caused by %s",
73 c.TestName(), afterReboot))
74 }
75 }
76 }
77}
78
79// ExecCommand executes a shell command and returns a string with the output
80// of the command. In case of error, it will fail the test.
81func ExecCommand(c *check.C, cmds ...string) string {
42 fmt.Println(strings.Join(cmds, " "))82 fmt.Println(strings.Join(cmds, " "))
43 cmd := exec.Command(cmds[0], cmds[1:len(cmds)]...)83 cmd := exec.Command(cmds[0], cmds[1:len(cmds)]...)
44 output, err := cmd.CombinedOutput()84 output, err := cmd.CombinedOutput()
45 stringOutput := string(output)85 stringOutput := string(output)
46 c.Assert(err, IsNil, Commentf("Error: %v", stringOutput))86 c.Assert(err, check.IsNil, check.Commentf("Error: %v", stringOutput))
47 return stringOutput87 return stringOutput
48}88}
4989
50func ExecCommandToFile(c *C, filename string, cmds ...string) {90// ExecCommandToFile executes a shell command and saves the output of the
91// command to a file. In case of error, it will fail the test.
92func ExecCommandToFile(c *check.C, filename string, cmds ...string) {
51 cmd := exec.Command(cmds[0], cmds[1:len(cmds)]...)93 cmd := exec.Command(cmds[0], cmds[1:len(cmds)]...)
52 outfile, err := os.Create(filename)94 outfile, err := os.Create(filename)
53 c.Assert(err, IsNil, Commentf("Error creating output file %s", filename))95 c.Assert(err, check.IsNil, check.Commentf("Error creating output file %s", filename))
5496
55 defer outfile.Close()97 defer outfile.Close()
56 cmd.Stdout = outfile98 cmd.Stdout = outfile
5799
58 err = cmd.Run()100 err = cmd.Run()
59 c.Assert(err, IsNil, Commentf("Error executing command '%v': %v", cmds, err))101 c.Assert(err, check.IsNil, check.Commentf("Error executing command '%v': %v", cmds, err))
60}102}
61103
62func GetCurrentVersion(c *C) int {104// GetCurrentVersion returns the version number of the installed and active
105// ubuntu-core.
106func GetCurrentVersion(c *check.C) int {
63 output := ExecCommand(c, "snappy", "list")107 output := ExecCommand(c, "snappy", "list")
64 pattern := "(?mU)^ubuntu-core (.*)$"108 pattern := "(?mU)^ubuntu-core (.*)$"
65 re := regexp.MustCompile(pattern)109 re := regexp.MustCompile(pattern)
66 match := re.FindStringSubmatch(string(output))110 match := re.FindStringSubmatch(string(output))
67 c.Assert(match, NotNil, Commentf("Version not found in %s", output))111 c.Assert(match, check.NotNil, check.Commentf("Version not found in %s", output))
68112
69 // match is like "ubuntu-core 2015-06-18 93 ubuntu"113 // match is like "ubuntu-core 2015-06-18 93 ubuntu"
70 items := strings.Fields(match[0])114 items := strings.Fields(match[0])
71 version, err := strconv.Atoi(items[2])115 version, err := strconv.Atoi(items[2])
72 c.Assert(err, IsNil, Commentf("Error converting version to int %v", version))116 c.Assert(err, check.IsNil, check.Commentf("Error converting version to int %v", version))
73 return version117 return version
74}118}
75119
76func CallUpdate(c *C) {120// CallUpdate executes an snappy update.
121func CallUpdate(c *check.C) {
77 c.Log("Calling snappy update...")122 c.Log("Calling snappy update...")
78 ExecCommand(c, "sudo", "snappy", "update")123 ExecCommand(c, "sudo", "snappy", "update")
79}124}
80125
81func Reboot(c *C) {126// Reboot requests a reboot using the test name as the mark.
127func Reboot(c *check.C) {
82 RebootWithMark(c, c.TestName())128 RebootWithMark(c, c.TestName())
83}129}
84130
85func RebootWithMark(c *C, mark string) {131// RebootWithMark requests a reboot using a specified mark.
132func RebootWithMark(c *check.C, mark string) {
86 c.Log("Preparing reboot with mark " + mark)133 c.Log("Preparing reboot with mark " + mark)
87 err := ioutil.WriteFile(needsRebootFile, []byte(mark), 0777)134 err := ioutil.WriteFile(needsRebootFile, []byte(mark), 0777)
88 c.Assert(err, IsNil, Commentf("Error writing needs-reboot file: %v", err))135 c.Assert(err, check.IsNil, check.Commentf("Error writing needs-reboot file: %v", err))
89}136}
90137
91func needsReboot(c *C) bool {138func needsReboot() bool {
92 _, err := os.Stat(needsRebootFile)139 _, err := os.Stat(needsRebootFile)
93 return err == nil140 return err == nil
94}141}
95142
96func BeforeReboot(c *C) bool {143// BeforeReboot returns True if the test is running before the test bed has
97 return checkRebootMark(c, "")144// been rebooted, or after the test that requested the reboot handled it.
145func BeforeReboot() bool {
146 return checkRebootMark("")
98}147}
99148
100func AfterReboot(c *C) bool {149// AfterReboot returns True if the test is running after the test bed has been
150// rebooted.
151func AfterReboot(c *check.C) bool {
101 // $ADT_REBOOT_MARK contains the reboot mark, if we have rebooted it'll be the test name152 // $ADT_REBOOT_MARK contains the reboot mark, if we have rebooted it'll be the test name
102 return checkRebootMark(c, c.TestName())153 return checkRebootMark(c.TestName())
103}154}
104155
105func checkRebootMark(c *C, mark string) bool {156func checkRebootMark(mark string) bool {
106 return os.Getenv("ADT_REBOOT_MARK") == mark157 return os.Getenv("ADT_REBOOT_MARK") == mark
107}158}
108159
109func RemoveRebootMark(c *C) {160// RemoveRebootMark removes the reboot mark to signal that the reboot has been
161// handled.
162func RemoveRebootMark(c *check.C) {
110 os.Setenv("ADT_REBOOT_MARK", "")163 os.Setenv("ADT_REBOOT_MARK", "")
111}164}
112165
113func SetSavedVersion(c *C, version int) {166// SetSavedVersion saves a version number into a file so it can be used on
167// tests after reboots.
168func SetSavedVersion(c *check.C, version int) {
114 versionFile := getVersionFile()169 versionFile := getVersionFile()
115 err := ioutil.WriteFile(versionFile, []byte(strconv.Itoa(version)), 0777)170 err := ioutil.WriteFile(versionFile, []byte(strconv.Itoa(version)), 0777)
116 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))
117}172}
118173
119func GetSavedVersion(c *C) int {174// GetSavedVersion returns the saved version number.
175func GetSavedVersion(c *check.C) int {
120 versionFile := getVersionFile()176 versionFile := getVersionFile()
121 contents, err := ioutil.ReadFile(versionFile)177 contents, err := ioutil.ReadFile(versionFile)
122 c.Assert(err, IsNil, Commentf("Error reading version file %s", versionFile))178 c.Assert(err, check.IsNil, check.Commentf("Error reading version file %s", versionFile))
123179
124 version, err := strconv.Atoi(string(contents))180 version, err := strconv.Atoi(string(contents))
125 c.Assert(err, IsNil, Commentf("Error converting version %v", contents))181 c.Assert(err, check.IsNil, check.Commentf("Error converting version %v", contents))
126182
127 return version183 return version
128}184}
@@ -130,32 +186,3 @@
130func getVersionFile() string {186func getVersionFile() string {
131 return filepath.Join(os.Getenv("ADT_ARTIFACTS"), "version")187 return filepath.Join(os.Getenv("ADT_ARTIFACTS"), "version")
132}188}
133
134func (s *CommonSuite) SetUpSuite(c *C) {
135 ExecCommand(c, "sudo", "systemctl", "stop", "snappy-autopilot.timer")
136 ExecCommand(c, "sudo", "systemctl", "disable", "snappy-autopilot.timer")
137}
138
139func (s *CommonSuite) SetUpTest(c *C) {
140 if needsReboot(c) {
141 contents, err := ioutil.ReadFile(needsRebootFile)
142 c.Assert(err, IsNil, Commentf("Error reading needs-reboot file %v", err))
143 c.Skip(fmt.Sprintf("****** Skipped %s during reboot caused by %s",
144 c.TestName(), contents))
145 } else {
146
147 afterReboot := os.Getenv("ADT_REBOOT_MARK")
148
149 if afterReboot == "" {
150 c.Logf("****** Running %s", c.TestName())
151 SetSavedVersion(c, GetCurrentVersion(c))
152 } else {
153 if afterReboot == c.TestName() {
154 c.Logf("****** Resuming %s after reboot", c.TestName())
155 } else {
156 c.Skip(fmt.Sprintf("****** Skipped %s after reboot caused by %s",
157 c.TestName(), afterReboot))
158 }
159 }
160 }
161}
162189
=== modified file '_integration-tests/tests/failover/failover_test.go'
--- _integration-tests/tests/failover/failover_test.go 2015-06-29 16:06:12 +0000
+++ _integration-tests/tests/failover/failover_test.go 2015-06-30 17:01:39 +0000
@@ -36,7 +36,7 @@
36var _ = Suite(&failoverSuite{})36var _ = Suite(&failoverSuite{})
3737
38type failoverSuite struct {38type failoverSuite struct {
39 CommonSuite39 SnappySuite
40}40}
4141
42const (42const (
4343
=== modified file '_integration-tests/tests/latest/install_test.go'
--- _integration-tests/tests/latest/install_test.go 2015-06-30 15:02:22 +0000
+++ _integration-tests/tests/latest/install_test.go 2015-06-30 17:01:39 +0000
@@ -34,7 +34,7 @@
34var _ = Suite(&installSuite{})34var _ = Suite(&installSuite{})
3535
36type installSuite struct {36type installSuite struct {
37 CommonSuite37 SnappySuite
38}38}
3939
40func installSnap(c *C, packageName string) string {40func installSnap(c *C, packageName string) string {
4141
=== modified file '_integration-tests/tests/update/update_test.go'
--- _integration-tests/tests/update/update_test.go 2015-06-24 17:19:58 +0000
+++ _integration-tests/tests/update/update_test.go 2015-06-30 17:01:39 +0000
@@ -33,7 +33,7 @@
33var _ = Suite(&updateSuite{})33var _ = Suite(&updateSuite{})
3434
35type updateSuite struct {35type updateSuite struct {
36 CommonSuite36 SnappySuite
37}37}
3838
39func rollback(c *C) {39func rollback(c *C) {
@@ -46,7 +46,7 @@
46}46}
4747
48func (s *updateSuite) TestUpdateMustInstallNewerVersion(c *C) {48func (s *updateSuite) TestUpdateMustInstallNewerVersion(c *C) {
49 if BeforeReboot(c) {49 if BeforeReboot() {
50 CallUpdate(c)50 CallUpdate(c)
51 Reboot(c)51 Reboot(c)
52 } else if AfterReboot(c) {52 } else if AfterReboot(c) {
5353
=== modified file 'run-checks'
--- run-checks 2015-06-29 23:15:00 +0000
+++ run-checks 2015-06-30 17:01:39 +0000
@@ -45,10 +45,11 @@
45# go vet45# go vet
46echo Running vet46echo Running vet
47go vet ./...47go vet ./...
48go vet ./_integration-tests/tests/...
4849
49# golint50# golint
50echo Running lint51echo Running lint
51lint=$(golint ./...)52lint=$(golint ./... && golint ./_integration-tests/tests/...)
52if [ -n "$lint" ]; then53if [ -n "$lint" ]; then
53 echo "Lint complains:"54 echo "Lint complains:"
54 echo $lint55 echo $lint

Subscribers

People subscribed via source and target branches