PlotDirector/Tools/Deployment/test_sandbox_build.py

42 lines
2.6 KiB
Python

from pathlib import Path
import subprocess
import tempfile
import unittest
from sandbox_build import build_command, run_build
class IsolatedBuildTests(unittest.TestCase):
def test_secret_free_namespaces_and_readonly_git(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory);source=root/'source';source.mkdir();(source/'.git').mkdir()
command=build_command(['dotnet','publish','project'],source,root,root/'home')
for option in ('--clearenv','--unshare-all','--cap-drop','--new-session'):
self.assertIn(option,command)
self.assertIn('--ro-bind',command)
self.assertIn(str(source/'.git'),command)
self.assertNotIn('/etc/plotdirector',command)
self.assertNotIn('private.pem',' '.join(command))
def test_arbitrary_commands_and_worktree_git_pointer_refused(self):
with tempfile.TemporaryDirectory() as directory:
source=Path(directory);(source/'.git').write_text('gitdir: /elsewhere')
for command in (['sh','-c','id'],['dotnet','publish','project']):
with self.assertRaises(ValueError):build_command(command,source,source,source)
def test_real_build_namespace_hides_home_and_protects_git(self):
if not Path('/usr/bin/bwrap').exists():self.skipTest('Linux Bubblewrap installation gate')
with tempfile.TemporaryDirectory() as directory:
root=Path(directory);source=root/'source';source.mkdir();(source/'.git').mkdir()
(source/'.git/config').write_text('fixture')
secret=root/'host-secret';secret.write_text('synthetic secret')
# Use same mount construction; substitute a local probe for dotnet, no build or deployment.
command=build_command(['dotnet','publish','fixture'],source,root/'output',root/'home')
(root/'output').mkdir();(root/'home').mkdir()
index=command.index('--')
probe=f"from pathlib import Path; assert not Path({str(secret)!r}).exists(); assert not Path('/home/nick/.ssh').exists(); Path({str(source/'ok')!r}).write_text('ok'); Path({str(source/'.git/config')!r}).write_text('forbidden')"
result=subprocess.run(command[:index+1]+['/usr/bin/python3','-c',probe],capture_output=True,text=True,timeout=15)
self.assertNotEqual(result.returncode,0)
self.assertIn('Read-only file system',result.stderr)
self.assertEqual((source/'ok').read_text(),'ok')
self.assertEqual((source/'.git/config').read_text(),'fixture')
if __name__=='__main__':unittest.main()