Merge lp:~elopio/snapcraft/base_plugin_tests into lp:~snappy-dev/snapcraft/core

Proposed by Leo Arias
Status: Merged
Approved by: Michael Terry
Approved revision: 125
Merged at revision: 119
Proposed branch: lp:~elopio/snapcraft/base_plugin_tests
Merge into: lp:~snappy-dev/snapcraft/core
Diff against target: 202 lines (+129/-10)
5 files modified
debian/control (+2/-0)
snapcraft/__init__.py (+1/-4)
snapcraft/common.py (+1/-1)
snapcraft/tests/__init__.py (+2/-1)
snapcraft/tests/test_base_plugin.py (+123/-4)
To merge this branch: bzr merge lp:~elopio/snapcraft/base_plugin_tests
Reviewer Review Type Date Requested Status
Michael Terry (community) Approve
Review via email: mp+266896@code.launchpad.net

Commit message

Added unit tests for the base plugin.
Added python3-testscenarios and wget as a dependency.

Description of the change

The important parts of this branch are:
- added a simple local server to test downloads.
- added test scenarios to simplify repetitive unit tests. (already installed in tarmac.)

Once I put this into test I found a bug I introduced by leaving the argument in the fatal function.
I also simplified the makedirs method.

To post a comment you must log in.
122. By Leo Arias

Added tests for makedirs.

123. By Leo Arias

Added testscenarios to the debian deps.

Revision history for this message
Michael Terry (mterry) wrote :

Looks great, thanks, especially the fatal() typo fix! :)

But you'll want to add wget to Build-Depends too (we didn't use it during tests before).

review: Needs Fixing
124. By Leo Arias

Added wget to the test deps.

125. By Leo Arias

Merged with trunk.

Revision history for this message
Michael Terry (mterry) wrote :

Beautiful!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'debian/control'
2--- debian/control 2015-08-04 20:46:05 +0000
3+++ debian/control 2015-08-04 21:37:20 +0000
4@@ -15,8 +15,10 @@
5 python3-apt,
6 python3-fixtures,
7 python3-setuptools,
8+ python3-testscenarios,
9 python3-yaml,
10 ubuntu-snappy-cli,
11+ wget,
12 xz-utils,
13 Homepage: https://launchpad.net/snapcraft
14 Standards-Version: 3.9.6
15
16=== modified file 'snapcraft/__init__.py'
17--- snapcraft/__init__.py 2015-07-29 04:33:25 +0000
18+++ snapcraft/__init__.py 2015-08-04 21:37:20 +0000
19@@ -196,7 +196,4 @@
20 source_branch=sbranch)
21
22 def makedirs(self, d):
23- try:
24- os.makedirs(d)
25- except FileExistsError:
26- pass
27+ os.makedirs(d, exist_ok=True)
28
29=== modified file 'snapcraft/common.py'
30--- snapcraft/common.py 2015-07-31 20:14:25 +0000
31+++ snapcraft/common.py 2015-08-04 21:37:20 +0000
32@@ -46,7 +46,7 @@
33 return subprocess.call(['/bin/sh', f.name] + cmd, **kwargs) == 0
34
35
36-def fatal(msg):
37+def fatal():
38 sys.exit(1)
39
40
41
42=== modified file 'snapcraft/tests/__init__.py'
43--- snapcraft/tests/__init__.py 2015-07-24 16:16:46 +0000
44+++ snapcraft/tests/__init__.py 2015-08-04 21:37:20 +0000
45@@ -15,12 +15,13 @@
46 # along with this program. If not, see <http://www.gnu.org/licenses/>.
47
48 import fixtures
49+import testscenarios
50
51 from snapcraft import common
52 from snapcraft.tests import fixture_setup
53
54
55-class TestCase(fixtures.TestWithFixtures):
56+class TestCase(testscenarios.WithScenarios, fixtures.TestWithFixtures):
57
58 def setUp(self):
59 super().setUp()
60
61=== modified file 'snapcraft/tests/test_base_plugin.py'
62--- snapcraft/tests/test_base_plugin.py 2015-07-22 20:58:50 +0000
63+++ snapcraft/tests/test_base_plugin.py 2015-08-04 21:37:20 +0000
64@@ -14,11 +14,32 @@
65 # You should have received a copy of the GNU General Public License
66 # along with this program. If not, see <http://www.gnu.org/licenses/>.
67
68+import http.server
69+import fixtures
70+import logging
71+import os
72+import threading
73+
74 import snapcraft
75-from snapcraft.tests import TestCase
76-
77-
78-class TestBasePlugin(TestCase):
79+from snapcraft import tests
80+
81+
82+class FakeTarballHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
83+
84+ def do_GET(self):
85+ data = 'Test fake tarball file'
86+ self.send_response(200)
87+ self.send_header('Content-Length', len(data))
88+ self.send_header('Content-type', 'text/html')
89+ self.end_headers()
90+ self.wfile.write(data.encode())
91+
92+ def log_message(self, *args):
93+ # Overwritten so the test does not write to stderr.
94+ pass
95+
96+
97+class TestBasePlugin(tests.TestCase):
98
99 def test_isurl(self):
100 plugin = snapcraft.BasePlugin('mock', {})
101@@ -27,3 +48,101 @@
102 self.assertFalse(plugin.isurl('./'))
103 self.assertFalse(plugin.isurl('/foo'))
104 self.assertFalse(plugin.isurl('/fo:o'))
105+
106+ def test_pull_git_with_tag_and_branch_must_raise_error(self):
107+ fake_logger = fixtures.FakeLogger(level=logging.ERROR)
108+ self.useFixture(fake_logger)
109+
110+ plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
111+ with self.assertRaises(SystemExit) as raised:
112+ plugin.pull_git(
113+ 'dummy_source', source_tag='test_tag',
114+ source_branch='test_branch')
115+
116+ self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
117+ expected = (
118+ "You can't specify both source-tag and source-branch for a git "
119+ "source (part 'test_plugin').\n")
120+ self.assertEqual(expected, fake_logger.output)
121+
122+ def test_pull_tarball_must_download_to_sourcedir(self):
123+ server = http.server.HTTPServer(('', 0), FakeTarballHTTPRequestHandler)
124+ server_thread = threading.Thread(target=server.serve_forever)
125+ self.addCleanup(server_thread.join)
126+ self.addCleanup(server.server_close)
127+ self.addCleanup(server.shutdown)
128+ server_thread.start()
129+
130+ plugin_name = 'test_plugin'
131+ dest_dir = os.path.join('parts', plugin_name, 'src')
132+ os.makedirs(dest_dir)
133+ tar_file_name = 'test.tar'
134+ source = 'http://{}:{}/{file_name}'.format(
135+ *server.server_address, file_name=tar_file_name)
136+ plugin = snapcraft.BasePlugin(plugin_name, 'dummy_options')
137+ plugin.pull_tarball(source)
138+
139+ with open(os.path.join(dest_dir, tar_file_name), 'r') as tar_file:
140+ self.assertEqual('Test fake tarball file', tar_file.read())
141+
142+ def test_get_source_with_unrecognized_source_must_raise_error(self):
143+ fake_logger = fixtures.FakeLogger(level=logging.ERROR)
144+ self.useFixture(fake_logger)
145+
146+ plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
147+ with self.assertRaises(SystemExit) as raised:
148+ plugin.get_source('unrecognized://test_source')
149+
150+ self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
151+ expected = (
152+ "Unrecognized source 'unrecognized://test_source' for part "
153+ "'test_plugin'.\n")
154+ self.assertEqual(expected, fake_logger.output)
155+
156+ def test_makedirs_with_existing_dir(self):
157+ plugin = snapcraft.BasePlugin('dummy_plugin', 'dummy_options')
158+ plugin.makedirs(self.path)
159+ self.assertTrue(os.path.exists(self.path))
160+
161+ def test_makedirs_with_unexisting_dir(self):
162+ path = os.path.join(self.path, 'unexisting')
163+ plugin = snapcraft.BasePlugin('dummy_plugin', 'dummy_options')
164+ plugin.makedirs(path)
165+ self.assertTrue(os.path.exists(path))
166+
167+
168+class GetSourceTestCase(tests.TestCase):
169+
170+ scenarios = [
171+ ('bzr with source branch', {
172+ 'source_type': 'bzr',
173+ 'source_branch': 'test_branch',
174+ 'source_tag': None,
175+ 'error': 'source-branch'}),
176+ ('tar with source branch', {
177+ 'source_type': 'tar',
178+ 'source_branch': 'test_branch',
179+ 'source_tag': None,
180+ 'error': 'source-branch'}),
181+ ('tar with source tag', {
182+ 'source_type': 'tar',
183+ 'source_branch': None,
184+ 'source_tag': 'test_tag',
185+ 'error': 'source-tag'})
186+ ]
187+
188+ def test_get_source_with_branch_must_raise_error(self):
189+ fake_logger = fixtures.FakeLogger(level=logging.ERROR)
190+ self.useFixture(fake_logger)
191+
192+ plugin = snapcraft.BasePlugin('test_plugin', 'dummy_options')
193+ with self.assertRaises(SystemExit) as raised:
194+ plugin.get_source(
195+ 'dummy_source', source_type=self.source_type,
196+ source_branch=self.source_branch, source_tag=self.source_tag)
197+
198+ self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.')
199+ expected = (
200+ "You can't specify {} for a {} source "
201+ "(part 'test_plugin').\n".format(self.error, self.source_type))
202+ self.assertEqual(expected, fake_logger.output)

Subscribers

People subscribed via source and target branches

to all changes: