summaryrefslogtreecommitdiff
path: root/tests/sandboxes/mounting/mount_simple.py
blob: 0e78a5603f5410c2ec0ff3bedaf77bd6c701bdc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import tempfile
from contextlib import ExitStack

import pytest

from buildstream.sandbox._mounter import Mounter


@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions")
def test_bind_mount():
    with ExitStack() as stack:
        src = stack.enter_context(tempfile.TemporaryDirectory())
        target = stack.enter_context(tempfile.TemporaryDirectory())

        with open(os.path.join(src, "test"), "a") as test:
            test.write("Test")

        with Mounter.bind_mount(target, src) as dest:
            # Ensure we get the correct path back
            assert dest == target

            # Ensure we can access files from src from target
            with open(os.path.join(target, "test"), "r") as test:
                assert test.read() == "Test"

        # Ensure the files from src are gone from target
        with pytest.raises(FileNotFoundError):
            with open(os.path.join(target, "test"), "r"):
                # Actual contents don't matter
                pass

        # Ensure the files in src are still in src
        with open(os.path.join(src, "test"), "r") as test:
            assert test.read() == "Test"


@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions")
def test_mount_proc():
    with ExitStack() as stack:
        src = "/proc"
        target = stack.enter_context(tempfile.TemporaryDirectory())

        with Mounter.mount(target, src, mount_type="proc", ro=True) as dest:
            # Ensure we get the correct path back
            assert dest == target

            # Ensure /proc is actually mounted
            assert os.listdir(src) == os.listdir(target)

        # Ensure /proc is unmounted correctly
        assert os.listdir(target) == []