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

Proposed by Leo Arias
Status: Merged
Approved by: Federico Gimenez
Approved revision: 618
Merged at revision: 615
Proposed branch: lp:~elopio/snappy/extract_integration_adt-run
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~elopio/snappy/extract_integration_build
Diff against target: 309 lines (+161/-95)
4 files modified
_integration-tests/helpers/autopkgtest/autopkgtest.go (+99/-0)
_integration-tests/helpers/autopkgtest/ssh.go (+46/-0)
_integration-tests/helpers/utils/utils.go (+9/-0)
_integration-tests/main.go (+7/-95)
To merge this branch: bzr merge lp:~elopio/snappy/extract_integration_adt-run
Reviewer Review Type Date Requested Status
Federico Gimenez (community) Approve
Review via email: mp+266027@code.launchpad.net

Commit message

Extract the integration adt-run helpers to a package.

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

Very nice main.go! :D

Instead of having a single AdtRun method and passing to it the results of calling other public methods of the same package I would leave it as a private adtRun and expose an AdtRunRemote, which takes an ip and a port as its last arguments, and a AdtRunLocal with an imagePath parameter.

Maybe we could also design a type for gathering the common options of both methods, what do you think?

Thanks!

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

Or maybe create a type with fields for the common options and pass them to the constructor, this would be cleaner IMO, let me know what do you think

617. By Leo Arias

Merged with prerequisite.

618. By Leo Arias

Refactored as suggested by fgimenez.

Revision history for this message
Leo Arias (elopio) wrote :

Federico, please check it again. I've pushed most things to autopkgtest.go, but didn't introduce any type.

I'm not quite convinced about rootPath and baseDir, basically because the name says the same but they are totally different. I'll think of a better name for each, and for a better name for both in a type. I'm about to leave, so if you are happy with this as a first step, please top-approve. And if you have the change you want in mind, please make an MP.

Thanks for the review!

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

Ok, thanks, i'll propose another branch for us to discuss how to structure this

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/autopkgtest'
2=== added file '_integration-tests/helpers/autopkgtest/autopkgtest.go'
3--- _integration-tests/helpers/autopkgtest/autopkgtest.go 1970-01-01 00:00:00 +0000
4+++ _integration-tests/helpers/autopkgtest/autopkgtest.go 2015-07-28 12:22:11 +0000
5@@ -0,0 +1,99 @@
6+// -*- Mode: Go; indent-tabs-mode: t -*-
7+
8+/*
9+ * Copyright (C) 2015 Canonical Ltd
10+ *
11+ * This program is free software: you can redistribute it and/or modify
12+ * it under the terms of the GNU General Public License version 3 as
13+ * published by the Free Software Foundation.
14+ *
15+ * This program is distributed in the hope that it will be useful,
16+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+ * GNU General Public License for more details.
19+ *
20+ * You should have received a copy of the GNU General Public License
21+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22+ *
23+ */
24+
25+package autopkgtest
26+
27+import (
28+ "fmt"
29+ "os"
30+ "path/filepath"
31+ "strconv"
32+ "text/template"
33+
34+ "log"
35+
36+ "launchpad.net/snappy/_integration-tests/helpers/build"
37+ "launchpad.net/snappy/_integration-tests/helpers/image"
38+ "launchpad.net/snappy/_integration-tests/helpers/utils"
39+)
40+
41+const (
42+ controlTpl = "_integration-tests/data/tpl/control"
43+ dataOutputDir = "_integration-tests/data/output/"
44+)
45+
46+var controlFile = filepath.Join(dataOutputDir, "control")
47+
48+// AdtRunLocal starts a kvm running the image passed as argument and runs the
49+// autopkgtests using it as the testbed.
50+func AdtRunLocal(rootPath, baseDir, testFilter string, img image.Image) {
51+ // Run the tests on the latest rolling edge image.
52+ if imagePath, err := img.UdfCreate(); err == nil {
53+ adtRun(rootPath, baseDir, testFilter, kvmSSHOptions(imagePath))
54+ }
55+}
56+
57+// AdtRunRemote runs the autopkgtests using a remote machine as the testbed.
58+func AdtRunRemote(rootPath, baseDir, testFilter, testbedIP string, testbedPort int) {
59+ utils.ExecCommand("ssh-copy-id", "-p", strconv.Itoa(testbedPort),
60+ "ubuntu@"+testbedIP)
61+ adtRun(
62+ rootPath, baseDir, testFilter, remoteTestbedSSHOptions(testbedIP, testbedPort))
63+}
64+
65+func adtRun(rootPath, baseDir, testFilter string, testbedOptions []string) {
66+ createControlFile(testFilter)
67+
68+ fmt.Println("Calling adt-run...")
69+ outputDir := filepath.Join(baseDir, "output")
70+ utils.PrepareTargetDir(outputDir)
71+
72+ cmd := []string{
73+ "adt-run", "-B",
74+ "--setup-commands", "touch /run/autopkgtest_no_reboot.stamp",
75+ "--override-control", controlFile,
76+ "--built-tree", rootPath,
77+ "--output-dir", outputDir}
78+
79+ utils.ExecCommand(append(cmd, testbedOptions...)...)
80+}
81+
82+func createControlFile(testFilter string) {
83+ type controlData struct {
84+ Filter string
85+ Test string
86+ }
87+
88+ tpl, err := template.ParseFiles(controlTpl)
89+ if err != nil {
90+ log.Panicf("Error reading adt-run control template %s", controlTpl)
91+ }
92+
93+ outputFile, err := os.Create(controlFile)
94+ if err != nil {
95+ log.Panicf("Error creating control file %s", controlFile)
96+ }
97+ defer outputFile.Close()
98+
99+ err = tpl.Execute(outputFile,
100+ controlData{Test: build.IntegrationTestName, Filter: testFilter})
101+ if err != nil {
102+ log.Panicf("execution: %s", err)
103+ }
104+}
105
106=== added file '_integration-tests/helpers/autopkgtest/ssh.go'
107--- _integration-tests/helpers/autopkgtest/ssh.go 1970-01-01 00:00:00 +0000
108+++ _integration-tests/helpers/autopkgtest/ssh.go 2015-07-28 12:22:11 +0000
109@@ -0,0 +1,46 @@
110+// -*- Mode: Go; indent-tabs-mode: t -*-
111+
112+/*
113+ * Copyright (C) 2015 Canonical Ltd
114+ *
115+ * This program is free software: you can redistribute it and/or modify
116+ * it under the terms of the GNU General Public License version 3 as
117+ * published by the Free Software Foundation.
118+ *
119+ * This program is distributed in the hope that it will be useful,
120+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
121+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
122+ * GNU General Public License for more details.
123+ *
124+ * You should have received a copy of the GNU General Public License
125+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
126+ *
127+ */
128+
129+package autopkgtest
130+
131+import (
132+ "os"
133+ "path/filepath"
134+ "strconv"
135+)
136+
137+var commonSSHOptions = []string{"---", "ssh"}
138+
139+func kvmSSHOptions(imagePath string) []string {
140+ return append(
141+ commonSSHOptions,
142+ []string{
143+ "-s", "/usr/share/autopkgtest/ssh-setup/snappy",
144+ "--", "-i", imagePath}...)
145+}
146+
147+func remoteTestbedSSHOptions(testbedIP string, testbedPort int) []string {
148+ options := []string{
149+ "-H", testbedIP,
150+ "-p", strconv.Itoa(testbedPort),
151+ "-l", "ubuntu",
152+ "-i", filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa"),
153+ "--reboot"}
154+ return append(commonSSHOptions, options...)
155+}
156
157=== modified file '_integration-tests/helpers/utils/utils.go'
158--- _integration-tests/helpers/utils/utils.go 2015-07-28 12:05:11 +0000
159+++ _integration-tests/helpers/utils/utils.go 2015-07-28 12:22:11 +0000
160@@ -36,6 +36,15 @@
161 os.MkdirAll(targetDir, 0777)
162 }
163
164+// RootPath return the test current working directory.
165+func RootPath() string {
166+ dir, err := os.Getwd()
167+ if err != nil {
168+ log.Panic(err)
169+ }
170+ return dir
171+}
172+
173 // ExecCommand executes the given command and pipes the results to os.Stdout and os.Stderr, returning the resulting error
174 func ExecCommand(cmds ...string) error {
175 fmt.Println(strings.Join(cmds, " "))
176
177=== modified file '_integration-tests/main.go'
178--- _integration-tests/main.go 2015-07-27 18:36:32 +0000
179+++ _integration-tests/main.go 2015-07-28 12:22:11 +0000
180@@ -21,13 +21,11 @@
181
182 import (
183 "flag"
184- "fmt"
185- "log"
186 "os"
187 "path/filepath"
188 "strconv"
189- "text/template"
190
191+ "launchpad.net/snappy/_integration-tests/helpers/autopkgtest"
192 "launchpad.net/snappy/_integration-tests/helpers/build"
193 "launchpad.net/snappy/_integration-tests/helpers/config"
194 "launchpad.net/snappy/_integration-tests/helpers/image"
195@@ -40,94 +38,9 @@
196 defaultChannel = "edge"
197 defaultSSHPort = 22
198 dataOutputDir = "_integration-tests/data/output/"
199- controlTpl = "_integration-tests/data/tpl/control"
200-)
201-
202-var (
203- commonSSHOptions = []string{"---", "ssh"}
204- configFileName = filepath.Join(dataOutputDir, "testconfig.json")
205- controlFile = filepath.Join(dataOutputDir, "control")
206-)
207-
208-func setupAndRunLocalTests(rootPath, testFilter string, img image.Image) {
209- // Run the tests on the latest rolling edge image.
210- if imageName, err := img.UdfCreate(); err == nil {
211- adtRun(rootPath, testFilter, kvmSSHOptions(imageName))
212- }
213-}
214-
215-func setupAndRunRemoteTests(rootPath, testFilter, testbedIP string, testbedPort int) {
216- utils.ExecCommand("ssh-copy-id", "-p", strconv.Itoa(testbedPort),
217- "ubuntu@"+testbedIP)
218- adtRun(rootPath, testFilter, remoteTestbedSSHOptions(testbedIP, testbedPort))
219-}
220-
221-func adtRun(rootPath, testFilter string, testbedOptions []string) {
222- createControlFile(testFilter)
223-
224- fmt.Println("Calling adt-run...")
225- outputDir := filepath.Join(baseDir, "output")
226- utils.PrepareTargetDir(outputDir)
227-
228- cmd := []string{
229- "adt-run", "-B",
230- "--setup-commands", "touch /run/autopkgtest_no_reboot.stamp",
231- "--override-control", controlFile,
232- "--built-tree", rootPath,
233- "--output-dir", outputDir}
234-
235- utils.ExecCommand(append(cmd, testbedOptions...)...)
236-}
237-
238-func kvmSSHOptions(imagePath string) []string {
239- return append(
240- commonSSHOptions,
241- []string{
242- "-s", "/usr/share/autopkgtest/ssh-setup/snappy",
243- "--", "-i", imagePath}...)
244-}
245-
246-func createControlFile(testFilter string) {
247- type controlData struct {
248- Filter string
249- Test string
250- }
251-
252- tpl, err := template.ParseFiles(controlTpl)
253- if err != nil {
254- log.Panicf("Error reading adt-run control template %s", controlTpl)
255- }
256-
257- outputFile, err := os.Create(controlFile)
258- if err != nil {
259- log.Panicf("Error creating control file %s", controlFile)
260- }
261- defer outputFile.Close()
262-
263- err = tpl.Execute(outputFile,
264- controlData{Test: build.IntegrationTestName, Filter: testFilter})
265- if err != nil {
266- log.Panicf("execution: %s", err)
267- }
268-}
269-
270-func remoteTestbedSSHOptions(testbedIP string, testbedPort int) []string {
271- options := []string{
272- "-H", testbedIP,
273- "-p", strconv.Itoa(testbedPort),
274- "-l", "ubuntu",
275- "-i", filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa"),
276- "--reboot"}
277- return append(commonSSHOptions, options...)
278-}
279-
280-func getRootPath() string {
281- dir, err := os.Getwd()
282- if err != nil {
283- log.Panic(err)
284- }
285- return dir
286-}
287+)
288+
289+var configFileName = filepath.Join(dataOutputDir, "testconfig.json")
290
291 func main() {
292 var (
293@@ -172,13 +85,12 @@
294 *update, *rollback)
295 cfg.Write()
296
297- rootPath := getRootPath()
298+ rootPath := utils.RootPath()
299
300 if *testbedIP == "" {
301 img := image.NewImage(*imgRelease, *imgChannel, *imgRevision, baseDir)
302- setupAndRunLocalTests(rootPath, *testFilter, *img)
303-
304+ autopkgtest.AdtRunLocal(rootPath, baseDir, *testFilter, *img)
305 } else {
306- setupAndRunRemoteTests(rootPath, *testFilter, *testbedIP, *testbedPort)
307+ autopkgtest.AdtRunRemote(rootPath, baseDir, *testFilter, *testbedIP, *testbedPort)
308 }
309 }

Subscribers

People subscribed via source and target branches