Iomatex_OS/offline/test_snapshot_update.py

74 lines
4.0 KiB
Python

import hashlib
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import git_delivery as d
class SnapshotUpdateTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.parent = Path(self.tmp.name)
self.root = self.parent / 'installer'
self.root.mkdir()
self.entry = {'path': 'install.sh', 'sha': d.blob_hash(b'original'), 'mode': '100755'}
self.source = {'repo': 'An-J/Iomatex_OS', 'commit': 'a'*40, 'files':[self.entry]}
(self.root / '.iomatex-source.json').write_text(json.dumps(self.source))
(self.root / 'install.sh').write_bytes(b'local hotfix')
(self.root / 'operator.conf').write_bytes(b'operator configuration')
def bootstrap(self):
entry = {**self.entry, 'sha':d.blob_hash(b'new release'), 'type':'blob'}
with patch.object(d, 'platform_check'), patch.object(d, 'auth_from_env', return_value={}), \
patch.object(d, 'open_git', return_value=io.StringIO(json.dumps({'tree':[entry]}))), \
patch.object(d, 'recovery_gate'), patch.object(d, 'catalog'), \
patch.object(d, 'snapshot_bytes', return_value=b'new release'), \
patch.object(d, 'AUTH', self.parent / 'auth.json'), \
patch.object(d.subprocess, 'run'):
d.bootstrap('https://git.it-screen.com/An-J/Iomatex_OS.git', 'ci-cd', str(self.root),
commit='b'*40, preserve_local_changes=True)
def test_confirmed_update_preserves_hotfix_and_operator_files(self):
self.bootstrap()
self.assertEqual(b'new release', (self.root/'install.sh').read_bytes())
self.assertEqual('git.iomatex.com', json.loads((self.root/'.iomatex-source.json').read_text())['host'])
self.assertEqual(b'operator configuration', (self.root/'operator.conf').read_bytes())
backup, = self.parent.glob('.installer-local-changes-*')
self.assertEqual(0o700, backup.stat().st_mode & 0o777)
self.assertEqual(b'local hotfix', (backup/'files/install.sh').read_bytes())
manifest = json.loads((backup/'backup.json').read_text())
self.assertEqual(self.source, manifest['source'])
self.assertEqual(hashlib.sha256(b'local hotfix').hexdigest(),manifest['changes'][0]['sha256'])
d.check_existing(self.root) # Further updates now see a clean snapshot.
def test_backup_failure_never_replaces_existing_snapshot(self):
with patch.object(d.os, 'fsync', side_effect=OSError('disk full')):
with self.assertRaises(OSError): self.bootstrap()
self.assertEqual(b'local hotfix',(self.root/'install.sh').read_bytes())
self.assertEqual([],list(self.parent.glob('.installer-local-changes-*')))
def test_deleted_file_is_recorded_before_release_restores_it(self):
(self.root/'install.sh').unlink()
self.bootstrap()
backup, = self.parent.glob('.installer-local-changes-*')
self.assertTrue(json.loads((backup/'backup.json').read_text())['changes'][0]['deleted'])
self.assertEqual(b'new release',(self.root/'install.sh').read_bytes())
def test_paths_and_symlinks_cannot_escape_snapshot(self):
(self.root/'install.sh').unlink()
(self.root/'install.sh').symlink_to(self.parent/'outside')
with self.assertRaisesRegex(ValueError,'symlink'): d.check_existing(self.root, True)
self.source['files'][0]['path']='../outside'
(self.root/'.iomatex-source.json').write_text(json.dumps(self.source))
with self.assertRaisesRegex(ValueError,'Unsafe'): d.check_existing(self.root, True)
def test_developer_git_checkout_still_requires_preserving_edits(self):
(self.root/'.git').mkdir()
with patch.object(d.subprocess,'check_output',return_value=' M install.sh'):
with self.assertRaisesRegex(ValueError,'tracked local changes'):d.check_existing(self.root,True)
if __name__ == '__main__': unittest.main()