1import os 2import tempfile 3from contextlib import ExitStack 4 5import pytest 6 7from buildstream.sandbox._mounter import Mounter 8 9 10@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions") 11def test_bind_mount(): 12 with ExitStack() as stack: 13 src = stack.enter_context(tempfile.TemporaryDirectory()) 14 target = stack.enter_context(tempfile.TemporaryDirectory()) 15 16 with open(os.path.join(src, 'test'), 'a') as test: 17 test.write('Test') 18 19 with Mounter.bind_mount(target, src) as dest: 20 # Ensure we get the correct path back 21 assert dest == target 22 23 # Ensure we can access files from src from target 24 with open(os.path.join(target, 'test'), 'r') as test: 25 assert test.read() == 'Test' 26 27 # Ensure the files from src are gone from target 28 with pytest.raises(FileNotFoundError): 29 with open(os.path.join(target, 'test'), 'r') as test: 30 # Actual contents don't matter 31 pass 32 33 # Ensure the files in src are still in src 34 with open(os.path.join(src, 'test'), 'r') as test: 35 assert test.read() == 'Test' 36 37 38@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions") 39def test_mount_proc(): 40 with ExitStack() as stack: 41 src = '/proc' 42 target = stack.enter_context(tempfile.TemporaryDirectory()) 43 44 with Mounter.mount(target, src, mount_type='proc', ro=True) as dest: 45 # Ensure we get the correct path back 46 assert dest == target 47 48 # Ensure /proc is actually mounted 49 assert os.listdir(src) == os.listdir(target) 50 51 # Ensure /proc is unmounted correctly 52 assert os.listdir(target) == [] 53