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

Subscribers

People subscribed via source and target branches

to all changes: