2018-06-09 14:29:45 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
2023-03-30 10:34:06 +02:00
|
|
|
from pathlib import Path
|
2018-06-09 14:29:45 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
localmodule = os.path.realpath(
|
2021-06-07 11:49:21 +02:00
|
|
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
|
|
|
|
)
|
2018-06-09 14:29:45 +02:00
|
|
|
if localmodule not in sys.path:
|
|
|
|
sys.path.insert(0, localmodule)
|
|
|
|
|
|
|
|
import fdroidserver.common
|
2020-10-21 09:52:58 +02:00
|
|
|
import fdroidserver.deploy
|
2023-06-01 20:23:00 +02:00
|
|
|
from fdroidserver.exception import FDroidException
|
2023-03-30 10:34:06 +02:00
|
|
|
from testcommon import TmpCwd, mkdtemp
|
2018-06-09 14:29:45 +02:00
|
|
|
|
|
|
|
|
2020-10-21 09:52:58 +02:00
|
|
|
class DeployTest(unittest.TestCase):
|
|
|
|
'''fdroidserver/deploy.py'''
|
2018-06-09 14:29:45 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
self.basedir = os.path.join(localmodule, 'tests')
|
2023-03-30 10:34:06 +02:00
|
|
|
os.chdir(self.basedir)
|
|
|
|
self._td = mkdtemp()
|
|
|
|
self.testdir = self._td.name
|
2018-06-09 14:29:45 +02:00
|
|
|
|
2023-03-30 10:34:06 +02:00
|
|
|
def tearDown(self):
|
|
|
|
self._td.cleanup()
|
|
|
|
|
|
|
|
def test_update_serverwebroot(self):
|
2024-01-23 14:39:57 +01:00
|
|
|
"""rsync works with file paths, so this test uses paths for the URLs"""
|
2023-03-30 10:34:06 +02:00
|
|
|
os.chdir(self.testdir)
|
|
|
|
repo = Path('repo')
|
|
|
|
repo.mkdir(parents=True)
|
|
|
|
fake_apk = repo / 'fake.apk'
|
|
|
|
with fake_apk.open('w') as fp:
|
|
|
|
fp.write('not an APK, but has the right filename')
|
2024-01-22 21:58:12 +01:00
|
|
|
url = Path('url')
|
|
|
|
url.mkdir()
|
2023-03-30 10:34:06 +02:00
|
|
|
|
2024-01-22 21:58:12 +01:00
|
|
|
dest_apk = url / fake_apk
|
2023-03-30 10:34:06 +02:00
|
|
|
self.assertFalse(dest_apk.is_file())
|
2024-01-22 21:58:12 +01:00
|
|
|
fdroidserver.deploy.update_serverwebroot({'url': str(url)}, 'repo')
|
2023-03-30 10:34:06 +02:00
|
|
|
self.assertTrue(dest_apk.is_file())
|
|
|
|
|
2023-06-01 20:23:00 +02:00
|
|
|
@mock.patch.dict(os.environ, clear=True)
|
|
|
|
def test_update_serverwebroot_no_rsync_error(self):
|
|
|
|
os.environ['PATH'] = self.testdir
|
|
|
|
os.chdir(self.testdir)
|
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
fdroidserver.deploy.update_serverwebroot('serverwebroot', 'repo')
|
|
|
|
|
2018-06-09 14:29:45 +02:00
|
|
|
def test_update_serverwebroot_make_cur_version_link(self):
|
|
|
|
# setup parameters for this test run
|
2024-01-23 14:39:57 +01:00
|
|
|
fdroidserver.deploy.options = mock.Mock()
|
2023-03-30 10:34:06 +02:00
|
|
|
fdroidserver.deploy.options.no_checksum = True
|
2020-10-21 09:52:58 +02:00
|
|
|
fdroidserver.deploy.options.identity_file = None
|
|
|
|
fdroidserver.deploy.options.verbose = False
|
|
|
|
fdroidserver.deploy.options.quiet = True
|
2024-01-23 14:39:57 +01:00
|
|
|
fdroidserver.deploy.config = {'make_current_version_link': True}
|
2024-01-22 21:58:12 +01:00
|
|
|
url = "example.com:/var/www/fdroid"
|
2018-06-09 14:29:45 +02:00
|
|
|
repo_section = 'repo'
|
|
|
|
|
|
|
|
# setup function for asserting subprocess.call invocations
|
|
|
|
call_iteration = 0
|
|
|
|
|
|
|
|
def update_server_webroot_call(cmd):
|
|
|
|
nonlocal call_iteration
|
|
|
|
if call_iteration == 0:
|
2021-06-07 11:49:21 +02:00
|
|
|
self.assertListEqual(
|
|
|
|
cmd,
|
|
|
|
[
|
|
|
|
'rsync',
|
|
|
|
'--archive',
|
|
|
|
'--delete-after',
|
|
|
|
'--safe-links',
|
|
|
|
'--quiet',
|
|
|
|
'--exclude',
|
2022-05-23 15:25:05 +02:00
|
|
|
'repo/entry.jar',
|
2021-06-07 11:49:21 +02:00
|
|
|
'--exclude',
|
2022-05-23 15:25:05 +02:00
|
|
|
'repo/entry.json',
|
|
|
|
'--exclude',
|
|
|
|
'repo/entry.json.asc',
|
2021-06-07 11:49:21 +02:00
|
|
|
'--exclude',
|
|
|
|
'repo/index-v1.jar',
|
2022-02-21 22:40:22 +01:00
|
|
|
'--exclude',
|
|
|
|
'repo/index-v1.json',
|
|
|
|
'--exclude',
|
|
|
|
'repo/index-v1.json.asc',
|
2022-05-23 15:25:05 +02:00
|
|
|
'--exclude',
|
|
|
|
'repo/index-v2.json',
|
|
|
|
'--exclude',
|
|
|
|
'repo/index-v2.json.asc',
|
|
|
|
'--exclude',
|
|
|
|
'repo/index.jar',
|
|
|
|
'--exclude',
|
|
|
|
'repo/index.xml',
|
2021-06-07 11:49:21 +02:00
|
|
|
'repo',
|
|
|
|
'example.com:/var/www/fdroid',
|
|
|
|
],
|
|
|
|
)
|
2018-06-09 14:29:45 +02:00
|
|
|
elif call_iteration == 1:
|
2021-06-07 11:49:21 +02:00
|
|
|
self.assertListEqual(
|
|
|
|
cmd,
|
|
|
|
[
|
|
|
|
'rsync',
|
|
|
|
'--archive',
|
|
|
|
'--delete-after',
|
|
|
|
'--safe-links',
|
|
|
|
'--quiet',
|
|
|
|
'repo',
|
2024-01-22 21:58:12 +01:00
|
|
|
url,
|
2021-06-07 11:49:21 +02:00
|
|
|
],
|
|
|
|
)
|
2018-06-09 14:29:45 +02:00
|
|
|
elif call_iteration == 2:
|
2021-06-07 11:49:21 +02:00
|
|
|
self.assertListEqual(
|
|
|
|
cmd,
|
|
|
|
[
|
|
|
|
'rsync',
|
|
|
|
'--archive',
|
|
|
|
'--delete-after',
|
|
|
|
'--safe-links',
|
|
|
|
'--quiet',
|
|
|
|
'Sym.apk',
|
|
|
|
'Sym.apk.asc',
|
|
|
|
'Sym.apk.sig',
|
|
|
|
'example.com:/var/www/fdroid',
|
|
|
|
],
|
|
|
|
)
|
2018-06-09 14:29:45 +02:00
|
|
|
else:
|
|
|
|
self.fail('unexpected subprocess.call invocation')
|
|
|
|
call_iteration += 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
|
|
|
|
os.mkdir('repo')
|
|
|
|
os.symlink('repo/com.example.sym.apk', 'Sym.apk')
|
|
|
|
os.symlink('repo/com.example.sym.apk.asc', 'Sym.apk.asc')
|
|
|
|
os.symlink('repo/com.example.sym.apk.sig', 'Sym.apk.sig')
|
|
|
|
with mock.patch('subprocess.call', side_effect=update_server_webroot_call):
|
2024-01-22 21:58:12 +01:00
|
|
|
fdroidserver.deploy.update_serverwebroot({'url': url}, repo_section)
|
2018-06-09 14:29:45 +02:00
|
|
|
self.assertEqual(call_iteration, 3, 'expected 3 invocations of subprocess.call')
|
|
|
|
|
|
|
|
def test_update_serverwebroot_with_id_file(self):
|
|
|
|
# setup parameters for this test run
|
2024-01-23 14:39:57 +01:00
|
|
|
fdroidserver.deploy.options = mock.Mock()
|
|
|
|
fdroidserver.deploy.options.identity_file = None
|
|
|
|
fdroidserver.deploy.options.no_checksum = True
|
2020-10-21 09:52:58 +02:00
|
|
|
fdroidserver.deploy.options.verbose = True
|
|
|
|
fdroidserver.deploy.options.quiet = False
|
|
|
|
fdroidserver.deploy.options.identity_file = None
|
2024-01-23 14:39:57 +01:00
|
|
|
fdroidserver.deploy.config = {'identity_file': './id_rsa'}
|
2024-01-22 21:58:12 +01:00
|
|
|
url = "example.com:/var/www/fdroid"
|
2018-06-09 14:29:45 +02:00
|
|
|
repo_section = 'archive'
|
|
|
|
|
|
|
|
# setup function for asserting subprocess.call invocations
|
|
|
|
call_iteration = 0
|
|
|
|
|
|
|
|
def update_server_webroot_call(cmd):
|
|
|
|
nonlocal call_iteration
|
|
|
|
if call_iteration == 0:
|
2021-06-07 11:49:21 +02:00
|
|
|
self.assertListEqual(
|
|
|
|
cmd,
|
|
|
|
[
|
|
|
|
'rsync',
|
|
|
|
'--archive',
|
|
|
|
'--delete-after',
|
|
|
|
'--safe-links',
|
|
|
|
'--verbose',
|
|
|
|
'-e',
|
|
|
|
'ssh -oBatchMode=yes -oIdentitiesOnly=yes -i '
|
|
|
|
+ fdroidserver.deploy.config['identity_file'],
|
|
|
|
'--exclude',
|
2022-05-23 15:25:05 +02:00
|
|
|
'archive/entry.jar',
|
2021-06-07 11:49:21 +02:00
|
|
|
'--exclude',
|
2022-05-23 15:25:05 +02:00
|
|
|
'archive/entry.json',
|
|
|
|
'--exclude',
|
|
|
|
'archive/entry.json.asc',
|
2021-06-07 11:49:21 +02:00
|
|
|
'--exclude',
|
|
|
|
'archive/index-v1.jar',
|
2022-02-21 22:40:22 +01:00
|
|
|
'--exclude',
|
|
|
|
'archive/index-v1.json',
|
|
|
|
'--exclude',
|
|
|
|
'archive/index-v1.json.asc',
|
2022-05-23 15:25:05 +02:00
|
|
|
'--exclude',
|
|
|
|
'archive/index-v2.json',
|
|
|
|
'--exclude',
|
|
|
|
'archive/index-v2.json.asc',
|
|
|
|
'--exclude',
|
|
|
|
'archive/index.jar',
|
|
|
|
'--exclude',
|
|
|
|
'archive/index.xml',
|
2021-06-07 11:49:21 +02:00
|
|
|
'archive',
|
2024-01-22 21:58:12 +01:00
|
|
|
url,
|
2021-06-07 11:49:21 +02:00
|
|
|
],
|
|
|
|
)
|
2018-06-09 14:29:45 +02:00
|
|
|
elif call_iteration == 1:
|
2021-06-07 11:49:21 +02:00
|
|
|
self.assertListEqual(
|
|
|
|
cmd,
|
|
|
|
[
|
|
|
|
'rsync',
|
|
|
|
'--archive',
|
|
|
|
'--delete-after',
|
|
|
|
'--safe-links',
|
|
|
|
'--verbose',
|
|
|
|
'-e',
|
|
|
|
'ssh -oBatchMode=yes -oIdentitiesOnly=yes -i '
|
|
|
|
+ fdroidserver.deploy.config['identity_file'],
|
|
|
|
'archive',
|
2024-01-22 21:58:12 +01:00
|
|
|
url,
|
2021-06-07 11:49:21 +02:00
|
|
|
],
|
|
|
|
)
|
2018-06-09 14:29:45 +02:00
|
|
|
else:
|
|
|
|
self.fail('unexpected subprocess.call invocation')
|
|
|
|
call_iteration += 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
with mock.patch('subprocess.call', side_effect=update_server_webroot_call):
|
2024-01-22 21:58:12 +01:00
|
|
|
fdroidserver.deploy.update_serverwebroot({'url': url}, repo_section)
|
2018-06-09 14:29:45 +02:00
|
|
|
self.assertEqual(call_iteration, 2, 'expected 2 invocations of subprocess.call')
|
|
|
|
|
2021-06-07 11:49:21 +02:00
|
|
|
@unittest.skipIf(
|
|
|
|
not os.getenv('VIRUSTOTAL_API_KEY'), 'VIRUSTOTAL_API_KEY is not set'
|
|
|
|
)
|
2020-03-03 14:38:14 +01:00
|
|
|
def test_upload_to_virustotal(self):
|
2020-10-21 09:52:58 +02:00
|
|
|
fdroidserver.deploy.options.verbose = True
|
2020-03-03 14:38:14 +01:00
|
|
|
virustotal_apikey = os.getenv('VIRUSTOTAL_API_KEY')
|
2020-10-21 09:52:58 +02:00
|
|
|
fdroidserver.deploy.upload_to_virustotal('repo', virustotal_apikey)
|
2020-03-03 14:38:14 +01:00
|
|
|
|
2020-07-09 09:34:04 +02:00
|
|
|
def test_remote_hostname_regex(self):
|
|
|
|
for remote_url, name in (
|
2021-06-07 11:49:21 +02:00
|
|
|
('git@github.com:guardianproject/fdroid-repo', 'github'),
|
|
|
|
('git@gitlab.com:guardianproject/fdroid-repo', 'gitlab'),
|
|
|
|
('https://github.com:guardianproject/fdroid-repo', 'github'),
|
|
|
|
('https://gitlab.com/guardianproject/fdroid-repo', 'gitlab'),
|
|
|
|
('https://salsa.debian.org/foo/repo', 'salsa'),
|
2020-07-09 09:34:04 +02:00
|
|
|
):
|
|
|
|
self.assertEqual(
|
2021-06-07 11:49:21 +02:00
|
|
|
name, fdroidserver.deploy.REMOTE_HOSTNAME_REGEX.sub(r'\1', remote_url)
|
2020-07-09 09:34:04 +02:00
|
|
|
)
|
|
|
|
|
2018-06-09 14:29:45 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
2021-06-07 11:49:21 +02:00
|
|
|
parser.add_option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Spew out even more information than normal",
|
|
|
|
)
|
2018-06-09 14:29:45 +02:00
|
|
|
(fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
|
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
2020-10-21 09:52:58 +02:00
|
|
|
newSuite.addTest(unittest.makeSuite(DeployTest))
|
2018-06-09 14:29:45 +02:00
|
|
|
unittest.main(failfast=False)
|