Iomatex_OS/offline/test_git_delivery.py

73 lines
3.8 KiB
Python

import hashlib
import io
import json
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).parent))
import git_delivery as d
class DeliveryTest(unittest.TestCase):
def test_payloads_excluded_from_edge_snapshot(self):
for name in ['third_party/debian/bullseye-arm64/repo/a.deb', 'third_party/zigbee2mqtt/z.tar.gz', 'stage_itscreen/itscreen']:
self.assertTrue(d.is_payload(name))
for name in ['third_party/node/Packages', 'third_party/SHA256SUMS', 'local_installer.sh']:
self.assertFalse(d.is_payload(name))
def test_private_git_commit_url_escapes_literal_package_percent(self):
url=d.raw_endpoint('third_party/a_2%3a1.deb', 'a'*40)
self.assertIn('a_2%253a1.deb?ref=',url)
with self.assertRaises(ValueError): d.raw_endpoint('../secret','a'*40)
with self.assertRaises(ValueError): d.raw_endpoint('valid', 'ci-cd')
with self.assertRaises(ValueError): d.repo_name('https://git.it-screen.com.evil/repo/test')
def test_download_checks_hash_and_removes_failed_payload(self):
with tempfile.TemporaryDirectory() as folder:
target=Path(folder)/'package.deb'
data=b'package contents'
config={'repo':'An-J/Iomatex_OS','commit':'a'*40,'objects':{'debian/test.deb':{'path':'third_party/test.deb','sha256':hashlib.sha256(data).hexdigest(),'size':len(data)}}}
with patch.object(d,'open_git',return_value=io.BytesIO(data)):
d.download_object(config,'debian/test.deb',target,{})
self.assertEqual(target.read_bytes(),data)
with patch.object(d,'open_git',return_value=io.BytesIO(b'corrupt')):
with self.assertRaises(ValueError): d.download_object(config,'debian/test.deb',target,{})
self.assertFalse(target.exists())
def test_catalog_includes_node_and_vendor_plugin(self):
text, objects=d.catalog(Path(__file__).resolve().parents[1])
self.assertIn('Package: nodejs\n',text)
self.assertIn('Package: qt-video-oes-plugins\n',text)
self.assertEqual(582,len([k for k in objects if k.endswith('.deb') and not k.startswith('third_party/')]))
def test_snapshot_refuses_local_edits(self):
with tempfile.TemporaryDirectory() as folder:
root=Path(folder)
(root/'local_installer.sh').write_bytes(b'changed')
(root/'.iomatex-source.json').write_text(json.dumps({'files':[{'path':'local_installer.sh','sha':d.blob_hash(b'original')}]}))
with self.assertRaisesRegex(ValueError,'Local change'): d.check_existing(root)
def test_apt_epoch_filename_decoded_only_once(self):
key='debian/xserver-common_2%3a1.0+deb11_all.deb'
objects={key:{}}
for path in [key, 'debian/xserver-common_2%253a1.0%2bdeb11_all.deb']:
self.assertEqual(key,d.apt_key('iomatex://git.it-screen.com/'+path,objects))
def test_unchanged_snapshot_file_is_reused_without_network(self):
with tempfile.TemporaryDirectory() as folder:
existing=Path(folder)/'old'; staged=Path(folder)/'new'
existing.mkdir(); staged.mkdir()
(existing/'installer.sh').write_bytes(b'unchanged')
entry={'path':'installer.sh','mode':'100755','sha':d.blob_hash(b'unchanged')}
with patch.object(d,'open_git',side_effect=AssertionError('Unnecessary download')):
d.materialize_file('An-J/Iomatex_OS','a'*40,{},entry,existing,staged)
self.assertEqual(b'unchanged',(staged/'installer.sh').read_bytes())
def test_no_redirect_even_on_same_host(self):
with self.assertRaisesRegex(ValueError,'redirect refused'):
d.NoRedirect().redirect_request(None,None,302,'',{},'https://git.it-screen.com/other')
if __name__=='__main__': unittest.main()