Skip to content

Commit e13d3ed

Browse files
gabibeyermatthewrsj
authored andcommitted
Remove network dependencies for pkg_integrity test
Create a mock function that copies files from the testfiles directory to a tmp directory, when attempting to download files from the network. Create a mock function for head_request that returns 404 for a few specified urls, and 200 for the rest. The mock calls will allow the functionality of the pkg_integrity program to be tested and not dependent on networking. Tests will no longer need to be skipped in travis with the removal of network dependency. Removed a few tests that were not specific to pkg_integrity, but instead were testing the importing and exporting abilities of the gpg key server. Added mock as a requirement to the requirements.txt file. Signed-off-by: Gabi Beyer <[email protected]>
1 parent 55feeec commit e13d3ed

12 files changed

+73
-83
lines changed

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
flake8>=3.4.0
22
pycurl>=7.43.0
33
toml>=0.9.0
4+
mock>=2.0.0

tests/test_pkg_integrity.py

+37-83
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,42 @@
11
import os
2+
import shutil
3+
import mock
24
import unittest
35
import tempfile
46
import pkg_integrity
57

68

9+
TESTDIR = os.path.join(os.getcwd(), "tests/testfiles/pkg_integrity")
10+
711
PACKAGE_URL = "http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.1.tar.gz"
812
XATTR_PKT_URL = "http://pypi.debian.net/xattr/xattr-0.9.1.tar.gz"
913
NO_SIGN_PKT_URL = "http://www.ferzkopp.net/Software/SDL_gfx-2.0/SDL_gfx-2.0.25.tar.gz"
1014
GEM_PKT = "https://rubygems.org/downloads/hoe-debugging-1.2.1.gem"
15+
NOSIGN_PKT_URL_BAD = "http://gnu.mirrors.pair.com/savannah/savannah/quagga/bad_quagga-1.1.0.tar.gz"
1116
NOSIGN_PKT_URL = "http://download.savannah.gnu.org/releases/quagga/quagga-1.1.0.tar.gz"
1217
NOSIGN_SIGN_URL = "http://download.savannah.gnu.org/releases/quagga/quagga-1.1.0.tar.gz.asc"
1318
PYPI_MD5_ONLY_PKG = "http://pypi.debian.net/tappy/tappy-0.9.2.tar.gz"
1419
GNOME_SHA256_PKG = "https://download.gnome.org/sources/pygobject/3.24/pygobject-3.24.0.tar.xz"
1520
KEYID = "EC2392F2EDE74488680DA3CF5F2B4756ED873D23"
1621

1722

18-
class TestGPGCli(unittest.TestCase):
19-
20-
def test_import_export(self):
21-
with pkg_integrity.cli_gpg_ctx() as ctx:
22-
err, output = ctx.export_key(KEYID)
23-
self.assertTrue(err is not None)
24-
err, output = ctx.import_key(KEYID)
25-
self.assertTrue(err is None)
26-
err, output = ctx.export_key(KEYID)
27-
self.assertTrue(err is None)
28-
self.assertTrue('PGP PUBLIC KEY' in output)
29-
30-
def test_import_non_existing_key(self):
31-
with pkg_integrity.cli_gpg_ctx() as ctx:
32-
keyid = '0' + KEYID[1:]
33-
err, output = ctx.import_key(keyid)
34-
self.assertTrue(err is not None)
35-
36-
def test_display_key_info(self):
37-
with pkg_integrity.cli_gpg_ctx() as ctx:
38-
err, output = ctx.import_key(KEYID)
39-
self.assertTrue(err is None)
40-
err, output = ctx.export_key(KEYID)
41-
with open('key_test.pkey', 'w') as out_key:
42-
out_key.write(output)
43-
err, output = ctx.display_keyinfo('key_test.pkey')
44-
os.remove('key_test.pkey')
45-
self.assertTrue('keyid' in output)
23+
def mock_attempt_to_download(path, dest=None):
24+
if dest:
25+
shutil.copyfile(os.path.join(TESTDIR, os.path.basename(path)), dest)
26+
return 200
27+
28+
def mock_head_request(url):
29+
bad_sigs = ["http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.1.tar.gz.sig",
30+
"http://www.ferzkopp.net/Software/SDL_gfx-2.0/SDL_gfx-2.0.25.tar.gz.sig",
31+
"http://www.ferzkopp.net/Software/SDL_gfx-2.0/SDL_gfx-2.0.25.tar.gz.asc"]
4632

33+
if url in bad_sigs:
34+
return 404
35+
return 200
4736

37+
38+
@mock.patch('pkg_integrity.attempt_to_download', mock_attempt_to_download)
39+
@mock.patch('pkg_integrity.head_request', mock_head_request)
4840
class TestCheckFn(unittest.TestCase):
4941

5042
def setUp(self):
@@ -53,8 +45,6 @@ def mock_rewrite(path):
5345
pkg_integrity.config.rewrite_config_opts = mock_rewrite
5446
pkg_integrity.config.config_opts['verify_required'] = False
5547

56-
@unittest.skipIf("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true",
57-
"Skipping this test on Travis CI.")
5848
def test_check_matching_sign_url(self):
5949
with tempfile.TemporaryDirectory() as tmpd:
6050
out_file = os.path.join(tmpd, os.path.basename(PACKAGE_URL))
@@ -73,6 +63,7 @@ def test_check_with_existing_sign(self):
7363
self.assertTrue(result)
7464

7565

66+
@mock.patch('pkg_integrity.attempt_to_download', mock_attempt_to_download)
7667
class TestDomainBasedVerifiers(unittest.TestCase):
7768

7869
def run_test_for_domain(self, Verifier, url):
@@ -85,8 +76,6 @@ def run_test_for_domain(self, Verifier, url):
8576
return None
8677

8778

88-
@unittest.skipIf("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true",
89-
"Skipping this test on Travis CI.")
9079
def test_pypi(self):
9180
result = self.run_test_for_domain(pkg_integrity.PyPiVerifier, PYPI_MD5_ONLY_PKG)
9281
self.assertTrue(result)
@@ -96,6 +85,7 @@ def test_gnome_org(self):
9685
self.assertTrue(result)
9786

9887

88+
@mock.patch('pkg_integrity.attempt_to_download', mock_attempt_to_download)
9989
class TestGEMShaVerifier(unittest.TestCase):
10090

10191
def setUp(self):
@@ -122,6 +112,8 @@ def test_non_matchingsha(self):
122112
self.assertEqual(a.exception.code, 1)
123113

124114

115+
@mock.patch('pkg_integrity.attempt_to_download', mock_attempt_to_download)
116+
@mock.patch('pkg_integrity.head_request', mock_head_request)
125117
class TestGPGVerifier(unittest.TestCase):
126118

127119
def setUp(self):
@@ -130,8 +122,6 @@ def mock_rewrite(path):
130122
pkg_integrity.config.rewrite_config_opts = mock_rewrite
131123
pkg_integrity.config.config_opts['verify_required'] = False
132124

133-
@unittest.skipIf("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true",
134-
"Skipping this test on Travis CI.")
135125
def test_from_url(self):
136126
with tempfile.TemporaryDirectory() as tmpd:
137127
out_file = os.path.join(tmpd, os.path.basename(PACKAGE_URL))
@@ -143,12 +133,14 @@ def test_from_url(self):
143133

144134
def test_check_quit(self):
145135
with tempfile.TemporaryDirectory() as tmpd:
146-
#with self.assertRaises(SystemExit) as a:
147-
pkg_integrity.check(NO_SIGN_PKT_URL, tmpd, interactive=False)
148-
#self.assertEqual(a.exception.code, 1)
136+
with self.assertRaises(SystemExit) as a:
137+
out_file = os.path.join(tmpd, os.path.basename(NOSIGN_PKT_URL_BAD))
138+
pkg_integrity.attempt_to_download(NOSIGN_PKT_URL_BAD, out_file)
139+
key_file = os.path.join(tmpd, os.path.basename(NOSIGN_PKT_URL_BAD))
140+
pkg_integrity.attempt_to_download(NOSIGN_SIGN_URL, key_file + '.asc')
141+
result = pkg_integrity.check(NOSIGN_PKT_URL_BAD, tmpd)
142+
self.assertEqual(a.exception.code, 1)
149143

150-
@unittest.skipIf("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true",
151-
"Skipping this test on Travis CI.")
152144
def test_from_disk(self):
153145
with tempfile.TemporaryDirectory() as tmpd:
154146
out_file = os.path.join(tmpd, os.path.basename(PACKAGE_URL))
@@ -158,8 +150,6 @@ def test_from_disk(self):
158150
result = pkg_integrity.from_disk(PACKAGE_URL, out_file, out_key)
159151
self.assertTrue(result)
160152

161-
@unittest.skipIf("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true",
162-
"Skipping this test on Travis CI.")
163153
def test_non_matchingsig(self):
164154
with tempfile.TemporaryDirectory() as tmpd:
165155
out_file = os.path.join(tmpd, os.path.basename(PACKAGE_URL))
@@ -174,35 +164,14 @@ def test_result_on_non_existent_pkg_path(self):
174164
result = pkg_integrity.from_disk('http://nokey.com/package.tar.gz',
175165
'NonExistentPKG.tar.gz',
176166
'NonExistentKey.asc')
177-
self.assertTrue(result is None)
167+
self.assertIsNone(result)
178168

179169
def test_result_on_nosign_package(self):
180170
with tempfile.TemporaryDirectory() as tmpd:
181171
out_file = os.path.join(tmpd, os.path.basename(NO_SIGN_PKT_URL))
182172
pkg_integrity.attempt_to_download(NO_SIGN_PKT_URL, out_file)
183173
result = pkg_integrity.check(NO_SIGN_PKT_URL, tmpd)
184-
self.assertTrue(result is None)
185-
186-
@unittest.skipIf("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true",
187-
"Skipping this test on Travis CI.")
188-
def test_pubkey_import(self):
189-
def say_yes(_):
190-
return True
191-
_ = pkg_integrity.InputGetter.get_answer
192-
pkg_integrity.InputGetter.get_answer = say_yes
193-
keyid = '0' + KEYID[1:]
194-
result = pkg_integrity.attempt_key_import(keyid)
195-
self.assertTrue(result is False)
196-
result = pkg_integrity.attempt_key_import(KEYID)
197-
self.assertTrue(result)
198-
pkg_integrity.InputGetter.get_answer = _
199-
self.removeKey()
200-
201-
def removeKey(self):
202-
key_path = os.path.dirname(os.path.realpath(__file__))
203-
key_path = os.path.dirname(key_path) + '/autospec/keyring/{}.pkey'.format(KEYID)
204-
if os.path.exists(key_path):
205-
os.unlink(key_path)
174+
self.assertIsNone(result)
206175

207176

208177
class TestInputGetter(unittest.TestCase):
@@ -212,12 +181,14 @@ class TestInputGetter(unittest.TestCase):
212181
def test_timput(self):
213182
ig = pkg_integrity.InputGetter(default='N', timeout=2)
214183
answer = ig.get_answer()
215-
self.assertTrue(answer is None)
184+
self.assertIsNone(answer)
216185
ig = pkg_integrity.InputGetter(default='Y', timeout=2)
217186
answer = ig.get_answer()
218-
self.assertTrue(answer is None)
187+
self.assertIsNone(answer)
219188

220189

190+
@mock.patch('pkg_integrity.attempt_to_download', mock_attempt_to_download)
191+
@mock.patch('pkg_integrity.head_request', mock_head_request)
221192
class TestUtils(unittest.TestCase):
222193

223194
def setUp(self):
@@ -252,23 +223,6 @@ def test_get_keyid_none(self):
252223
false_name = '/false/name'
253224
self.assertTrue(pkg_integrity.get_keyid(false_name) is None)
254225

255-
@unittest.skipIf("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true",
256-
"Skipping this test on Travis CI.")
257-
def test_attempt_to_download(self):
258-
fakeURL = "https://download.my.url.com/file.tar.gz"
259-
realURLnoFile = "http://pypi.debian.net/alembic/alembic-0.8.8.non-existent.tar.gz"
260-
realURL = "http://pypi.debian.net/alembic/alembic-0.8.8.tar.gz"
261-
262-
tmpf = tempfile.NamedTemporaryFile()
263-
fname = tmpf.name
264-
tmpf.close()
265-
266-
self.assertEqual(pkg_integrity.attempt_to_download(fakeURL, fname), None)
267-
self.assertEqual(pkg_integrity.attempt_to_download(realURLnoFile, fname), 404)
268-
self.assertEqual(pkg_integrity.attempt_to_download(realURL, fname), 200)
269-
270-
os.unlink(fname)
271-
272226
def test_get_signature_url(self):
273227

274228
url_from_gnu = "http://ftp.gnu.org/pub/gnu/gperf/gperf-3.0.4.tar.gz"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-----BEGIN PGP SIGNATURE-----
2+
Version: GnuPG v2
3+
4+
iQIcBAABCAAGBQJW2ZtDAAoJEAI6RCDH7GkUc0IP/1bH7KEJdUM+lrGM1SOuNHdq
5+
4VEwDp1II8abbBzHeGEXZ8p4+MwwSOYHFiy+NM1yldZkDXtqAlAqvIuEzc+PtgGd
6+
vFeNPb9infibNaEDK+zz4fcqJOSab1ZcQ/D3EIJXwKr5nIYP8RuCHu/zstf7o6R0
7+
/wnGWaAIB1+p9PxvUhMPMbBEQCw/cBzyZ2d7nApHF3b0OH2wM7P8VG8ot4cuglPq
8+
hzk27ZnrYeUDyUUhMRlL7sZZouJlSy/0OxsBK++tOjE6MiuAZhqtlSW+cFK4L7k/
9+
q4eLodX7GtF0psSgTRjTk2ozdSIDkB2ccLBN6CzgCcbPrbcz4tVQqaQBcSd0mCl7
10+
RWAKmSye7p+CY8mIIOjdYm+KaQRmJMKDXs49hMycti22jnu5T2BM6O7MZpiY+cb3
11+
O2UKUXbVyX/cXKwTYwf4VMddxJKFaqYac+7n5qWbdwBjk9E5OC2ltz94taM1pxZ5
12+
2jRtfyIb3s+Rj6M5cXI5UChrGqzMK6BmEbyZ0KbHAJ7Y0xvGqwydC6J+RwGIqRlp
13+
LmW3k1ggpUajoMcgq9KqJgVqo/9f4+6anADHRMNJ93MxR7h5BRQ1/GWSXpOzYsYv
14+
DnfQPrhc+z20m81qsvfUcBnN/k74yiDVqp3I/HrYGD+f8cXKPpBRESkPAXlUIcu2
15+
ALHZXEdBGcWdUrAIpJT2
16+
=SMbV
17+
-----END PGP SIGNATURE-----
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-----BEGIN PGP SIGNATURE-----
2+
Version: GnuPG v1
3+
4+
iQJMBAABCAA2BQJYBh/XLxpodHRwczovL3d3dy5qYWttYS5vcmcvfnBhdWwvcGdw
5+
X3BvbGljeS0xLjEudHh0AAoJEG/lfKjBpK6mNOMP/irlUcU/4NQ6BQQbF7Vc90wo
6+
hmTH/zGAodnndxSplIGV63BQqmQr5KiSOp6tNQ3OlCIwLFWlr/LiJz+MeThaMtZx
7+
/mlrZptWIhbEcJzVa6efg8UGIYl2NC+QxFgEyezfCYSEmczd1qE3kiX+5WjcqVPH
8+
pVjisPwaAYa0FTCsAwBMUKyJr2K3sq5hSJR7DFDVnvH1DJ1xZd9871ZdDpZBHz2z
9+
GvR+IuZcWbYleH1ArFvT6cMokTpIUMvd33/+Gdpfu9fihzQQn199nN2LFZJzuEGV
10+
vH6+IZVTmtXb5U/sdtbWGaDv8eFLAWl2NH9VNdlVw5FbWOYRC4YNN39/hBy7s0po
11+
hvq33ZugC7JqPuje+4W1oF1T/dbRCIBmWUzKsoH8+3z4KJHdotSR0cU+TT+w6SJC
12+
QhF9TAbjrfbeJs0D0NnZrllDLiLGFgLl5yULzMjRDqKcgqE5+nvYBPXHPUCCPU7+
13+
59QOkPMsz/kZGV1lRzoUoxlM6V/phJRPU7jit10puiNij0c4peWbjVmTWDei3Xeu
14+
kHpck5wIAUFzAIXBUpVhYdLl/kxq654Qqthoci869ATZIKH/SOId9Y0K1GE6xO6H
15+
7vQFUGn3dcdnbgapM+tpmC77EmV4BtzkTjcu2pX6s0qnI4P+M+zXS+7G96xP3zHN
16+
ZSFq45swEKd6SKbOYxcN
17+
=mt1F
18+
-----END PGP SIGNATURE-----
39.5 KB
Binary file not shown.
21.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)