Merge ~kissiel/cc-mini-ci-legacy:python-snapcraft-test into cc-mini-ci-legacy:master

Proposed by Maciej Kisielewski
Status: Merged
Merged at revision: 7d5ec4a497397774e3a9533ddee273ac32f878a8
Proposed branch: ~kissiel/cc-mini-ci-legacy:python-snapcraft-test
Merge into: cc-mini-ci-legacy:master
Diff against target: 268 lines (+140/-101)
3 files modified
dev/null (+0/-99)
src/www/snapcraft/run-snapcraft-test-launcher.sh (+2/-2)
src/www/snapcraft/run_snapcraft_test.py (+138/-0)
Reviewer Review Type Date Requested Status
Paul Larson Approve
Review via email: mp+325482@code.launchpad.net

Description of the change

this MR switches ci to use python version of snapcraft test

To post a comment you must log in.
Revision history for this message
Paul Larson (pwlars) wrote :

A couple of questions:
1. Should we use pastebinit? Is there a risk that we could expose something in some private checkbox branch related to a non-public project?
2. Any intention to add parameters for things like dont_clean? It looked like that might be where you were headed with it, but we'd need to modify the script to do things like that now.

review: Needs Information
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

1. It's meant for testing checkbox-snappy, non-public pastes would require OAuth/OpenID magic, or dropping bins altogether.
2. I wanted to have it in the final version from the get-go. There are many ways that stuff can go wrong, and then it's easier to debug. I also though that the next person looking closer to this won't hesitate to skim it over and find it handy.

Revision history for this message
Paul Larson (pwlars) wrote :

That makes sense. I'd say let's land this so we can get moving with the other mini-ci plans

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/www/snapcraft/run-snapcraft-test-launcher.sh b/src/www/snapcraft/run-snapcraft-test-launcher.sh
2index 1d76ffe..d60bb3f 100755
3--- a/src/www/snapcraft/run-snapcraft-test-launcher.sh
4+++ b/src/www/snapcraft/run-snapcraft-test-launcher.sh
5@@ -5,12 +5,12 @@ minici_snapcraft_log="/home/ubuntu/minici-snapcraft/minici-snapcraft.log"
6 sleep 60
7 # run the real test and fetch the log
8 cd /home/ubuntu/minici-snapcraft
9-./run-snapcraft-test.sh > ./minici-snapcraft.log 2>&1
10+./run_snapcraft_test.py > ./minici-snapcraft.log 2>&1
11 execute_status=$?
12
13 # TODO: submit the hostname to cgi
14
15-curl -F snapcraft_subject="./run-snapcraft-test.sh" -F snapcraft_output=@${minici_snapcraft_log} -F snapcraft_status="$execute_status" $cgi_url
16+curl -F snapcraft_subject="./run_snapcraft_test.py" -F snapcraft_output=@${minici_snapcraft_log} -F snapcraft_status="$execute_status" $cgi_url
17 echo "snapcraft_status=$execute_status"
18 curl -d "email_mini_ci_snapcraft=yes" -d "snapcraft_status=$execute_status" $cgi_url
19 # protect the test machine and polar bears!
20diff --git a/src/www/snapcraft/run-snapcraft-test.sh b/src/www/snapcraft/run-snapcraft-test.sh
21deleted file mode 100755
22index c54522c..0000000
23--- a/src/www/snapcraft/run-snapcraft-test.sh
24+++ /dev/null
25@@ -1,99 +0,0 @@
26-#!/usr/bin/env bash
27-# This file is part of Checkbox.
28-#
29-# Copyright 2016-2017 Canonical Ltd.
30-# Written by:
31-# Maciej Kisielewski <maciej.kisielewski@canonical.com>
32-#
33-# Checkbox is free software: you can redistribute it and/or modify
34-# it under the terms of the GNU General Public License version 3,
35-# as published by the Free Software Foundation.
36-
37-SAVED_DIR=$PWD
38-WORK_DIR=`mktemp -d`
39-
40-cd $WORK_DIR
41-mkdir -p logs
42-LOG_DIR=$WORK_DIR/logs
43-
44-function clean_up {
45- cd $SAVED_DIR
46- rm -rf $WORK_DIR
47-}
48-
49-function time_it {
50- /usr/bin/time -f "%E elapsed. %P CPU" -o $LOG_DIR/timing.dat $@
51-}
52-
53-function pastebin_logs {
54- if [ -s $LOG_DIR/stdout ]; then
55- echo stdout: $(pastebinit $LOG_DIR/stdout)
56- else
57- echo stdout: empty
58- fi
59- if [ -s $LOG_DIR/stderr ]; then
60- echo stderr: $(pastebinit $LOG_DIR/stderr)
61- else
62- echo stderr: empty
63- fi
64-}
65-
66-function run_step {
67- echo -n Step \"$1\"
68- if ! time_it $2 >>$LOG_DIR/stdout 2>>$LOG_DIR/stderr; then
69- echo -n "- FAIL - "
70- outcome=1
71- else
72- echo -n "- PASS - "
73- outcome=0
74- fi
75- echo -n elapsed: ; cat $LOG_DIR/timing.dat
76- if [ $outcome -ne 0 ]; then
77- pastebin_logs
78- return 1
79- fi
80-}
81-
82-# master branch test
83-function test_master {
84- run_step "git-clone" "git clone -b master https://git.launchpad.net/checkbox-snappy packaging" || return 1
85- run_step "update-snapcraft" "snapcraft update" || return 1
86- (cd packaging && run_step "run-snapcraft" "snapcraft snap -o ../checkbox-snappy.snap") || return 1
87- run_step "verify-build" "ls -al checkbox-snappy.snap" || return 1
88- run_step "install-checkbox-snappy" "sudo snap install --devmode ./checkbox-snappy.snap" || return 1
89- (cd && run_step "run smoke-test" "checkbox-snappy.smoke-test") || return 1
90- return 0
91-}
92-test_master
93-master_result=$?
94-if [[ $master_result -eq 1 ]]; then
95- echo "Testing Checkbox snap for branch master failed!"
96-fi
97-pastebin_logs
98-
99-# beta branch test
100-function test_beta {
101- run_step "git-clone" "git clone -b beta https://git.launchpad.net/checkbox-snappy packaging-beta" || return 1
102- run_step "update-snapcraft" "snapcraft update" || return 1
103- (cd packaging-beta && run_step "run-snapcraft" "snapcraft snap -o ../checkbox-snappy-beta.snap") || return 1
104- run_step "verify-build" "ls -al checkbox-snappy-beta.snap" || return 1
105- run_step "install-checkbox-snappy" "sudo snap install --devmode ./checkbox-snappy-beta.snap" || return 1
106- (cd && run_step "run smoke-test" "checkbox-snappy.smoke-test") || return 1
107- return 0
108-}
109-test_beta
110-beta_result=$?
111-if [[ $beta_result -eq 1 ]]; then
112- echo "Testing Checkbox snap for branch beta failed!"
113-fi
114-pastebin_logs
115-
116-clean_up
117-echo "$master_result"
118-echo "$beta_result"
119-if [[ $master_result -eq 0 && $beta_result -eq 0 ]]; then
120- echo "ALL STEPS PASSED"
121- exit 0
122-else
123- exit 1
124-fi
125diff --git a/src/www/snapcraft/run_snapcraft_test.py b/src/www/snapcraft/run_snapcraft_test.py
126new file mode 100755
127index 0000000..6f00589
128--- /dev/null
129+++ b/src/www/snapcraft/run_snapcraft_test.py
130@@ -0,0 +1,138 @@
131+#!/usr/bin/env python3
132+# This file is part of Checkbox.
133+#
134+# Copyright 2017 Canonical Ltd.
135+# Written by:
136+# Maciej Kisielewski <maciej.kisielewski@canonical.com>
137+#
138+# Checkbox is free software: you can redistribute it and/or modify
139+# it under the terms of the GNU General Public License version 3,
140+# as published by the Free Software Foundation.
141+
142+import os
143+import shutil
144+import subprocess
145+import sys
146+import tempfile
147+import time
148+
149+
150+"""
151+This program checks if checkbox-snappy can be snapped and whether "Smoke Tests"
152+test plan passes after installing that snap.
153+"""
154+
155+"""
156+Have snapcraft, git installed
157+"""
158+
159+REPO = "https://git.launchpad.net/checkbox-snappy"
160+
161+BRANCHES = ['master', 'beta']
162+
163+
164+def update_snapcraft():
165+ subprocess.run(['snapcraft', 'update'])
166+
167+
168+class Snap:
169+ def __init__(self, repo, branch, dont_clean=False):
170+ self._repo = repo
171+ self._branch = branch
172+ self._dont_clean = dont_clean
173+ self._work_dir = ''
174+ self._start_dir = os.path.abspath(os.curdir)
175+ self._log_out = ''
176+ self._log_err = ''
177+
178+ def __enter__(self):
179+ self._work_dir = tempfile.mkdtemp('-checkbox-snappy-' + self._branch)
180+ os.chdir(self._work_dir)
181+ return self
182+
183+ def clone(self):
184+ if self._run_cmd(
185+ ['git', 'clone', '-b', self._branch, self._repo, '.']) != 0:
186+ raise Exception('Failed to git-clone. Repo: {}. Branch: {}'.format(
187+ self._repo, self._branch))
188+ print('cloned into {}'.format(self._work_dir))
189+ def snap(self):
190+ if self._run_cmd(['snapcraft']) != 0:
191+ raise Exception('Failed to snap checkbox-snappy')
192+ # the last line in the log should contain "Snapped $SNAPNAME"
193+ self._snap_path = self._tail_out().split()[1]
194+ print('{} snapped.'.format(self._snap_path))
195+
196+
197+ def __exit__(self, exc_type, exc_value, traceback):
198+ if not self._dont_clean:
199+ shutil.rmtree(self._work_dir)
200+ os.chdir(self._start_dir)
201+
202+ def _run_cmd(self, args):
203+ cp = subprocess.run(
204+ args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
205+ self._log_out += cp.stdout.decode(sys.stdout.encoding)
206+ self._log_err += cp.stderr.decode(sys.stderr.encoding)
207+ if cp.returncode != 0:
208+ print('Command `{}` failed with return code {}'.format(
209+ ' '.join(args), cp.returncode), file=sys.stderr)
210+ return cp.returncode
211+
212+ def _tail_out(self, n=1):
213+ return '\n'.join(self._log_out.strip().split('\n')[-n:])
214+ def _tail_err(self, n=1):
215+ return '\n'.join(self._log_err.strip().split('\n')[-n:])
216+
217+
218+ def install(self):
219+ if self._run_cmd(
220+ ['sudo', 'snap', 'install', '--devmode', self._snap_path]) != 0:
221+ raise Exception('Failed to install checkbox-snappy')
222+
223+ def smoke_test(self):
224+ if self._run_cmd(['checkbox-snappy.smoke-test']) != 0:
225+ raise Exception('Smoke test failed')
226+
227+ def print_tails(self):
228+ print('Last 10 lines of stdout:')
229+ print(self._tail_out(10))
230+ print('Last 10 lines of stderr:')
231+ print(self._tail_err(10))
232+
233+ def submit_logs(self):
234+ with open('stdout.log', 'wt') as f:
235+ f.write(self._log_out)
236+ with open('stderr.log', 'wt') as f:
237+ f.write(self._log_err)
238+ if self._log_out:
239+ self._run_cmd(['pastebinit', 'stdout.log'])
240+ print("stdout: {}".format(self._tail_out()))
241+ else:
242+ print("stdout: empty")
243+ if self._log_err:
244+ self._run_cmd(['pastebinit', 'stderr.log'])
245+ print("stderr: {}".format(self._tail_out()))
246+ else:
247+ print("stderr: empty")
248+
249+def main():
250+ failed = False
251+ for branch in BRANCHES:
252+ with Snap(REPO, branch) as snap:
253+ try:
254+ snap.clone()
255+ snap.snap()
256+ snap.install()
257+ snap.smoke_test()
258+ except Exception as exc:
259+ failed = True
260+ print(exc, file=sys.stderr)
261+ snap.print_tails()
262+ finally:
263+ snap.submit_logs()
264+
265+ return 1 if failed else 0
266+
267+if __name__ == '__main__':
268+ main()

Subscribers

People subscribed via source and target branches