summaryrefslogtreecommitdiffhomepage
path: root/test/test_static_mount.py
blob: 41b436e250c575a79882c82f9559110f98f9e8eb (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import subprocess
from pathlib import Path

import pytest

from unit.applications.proto import ApplicationProto

prerequisites = {'features': {'chroot': True}, 'privileged_user': True}

client = ApplicationProto()


@pytest.fixture(autouse=True)
def setup_method_fixture(temp_dir):
    os.makedirs(f'{temp_dir}/assets/dir/mount')
    os.makedirs(f'{temp_dir}/assets/dir/dir')
    os.makedirs(f'{temp_dir}/assets/mount')
    Path(f'{temp_dir}/assets/index.html').write_text('index', encoding='utf-8')
    Path(f'{temp_dir}/assets/dir/dir/file').write_text('file', encoding='utf-8')
    Path(f'{temp_dir}/assets/mount/index.html').write_text(
        'mount', encoding='utf-8'
    )

    try:
        subprocess.check_output(
            [
                "mount",
                "--bind",
                f'{temp_dir}/assets/mount',
                f'{temp_dir}/assets/dir/mount',
            ],
            stderr=subprocess.STDOUT,
        )

    except KeyboardInterrupt:
        raise

    except subprocess.CalledProcessError:
        pytest.fail("Can't run mount process.")

    assert 'success' in client.conf(
        {
            "listeners": {"*:8080": {"pass": "routes"}},
            "routes": [{"action": {"share": f'{temp_dir}/assets/dir$uri'}}],
        }
    )

    yield

    try:
        subprocess.check_output(
            ["umount", "--lazy", f'{temp_dir}/assets/dir/mount'],
            stderr=subprocess.STDOUT,
        )

    except KeyboardInterrupt:
        raise

    except subprocess.CalledProcessError:
        pytest.fail("Can't run umount process.")


def test_static_mount(temp_dir, skip_alert):
    skip_alert(r'opening.*failed')

    resp = client.get(url='/mount/')
    assert resp['status'] == 200
    assert resp['body'] == 'mount'

    assert 'success' in client.conf(
        {"share": f'{temp_dir}/assets/dir$uri', "traverse_mounts": False},
        'routes/0/action',
    ), 'configure mount disable'

    assert client.get(url='/mount/')['status'] == 403

    assert 'success' in client.conf(
        {"share": f'{temp_dir}/assets/dir$uri', "traverse_mounts": True},
        'routes/0/action',
    ), 'configure mount enable'

    resp = client.get(url='/mount/')
    assert resp['status'] == 200
    assert resp['body'] == 'mount'


def test_static_mount_two_blocks(temp_dir, skip_alert):
    skip_alert(r'opening.*failed')

    os.symlink(f'{temp_dir}/assets/dir', f'{temp_dir}/assets/link')

    assert 'success' in client.conf(
        [
            {
                "match": {"method": "HEAD"},
                "action": {
                    "share": f'{temp_dir}/assets/dir$uri',
                    "traverse_mounts": False,
                },
            },
            {
                "match": {"method": "GET"},
                "action": {
                    "share": f'{temp_dir}/assets/dir$uri',
                    "traverse_mounts": True,
                },
            },
        ],
        'routes',
    ), 'configure two options'

    assert client.get(url='/mount/')['status'] == 200, 'block enabled'
    assert client.head(url='/mount/')['status'] == 403, 'block disabled'


def test_static_mount_chroot(temp_dir, skip_alert):
    skip_alert(r'opening.*failed')

    assert 'success' in client.conf(
        {
            "share": f'{temp_dir}/assets/dir$uri',
            "chroot": f'{temp_dir}/assets',
        },
        'routes/0/action',
    ), 'configure chroot mount default'

    assert client.get(url='/mount/')['status'] == 200, 'chroot'

    assert 'success' in client.conf(
        {
            "share": f'{temp_dir}/assets/dir$uri',
            "chroot": f'{temp_dir}/assets',
            "traverse_mounts": False,
        },
        'routes/0/action',
    ), 'configure chroot mount disable'

    assert client.get(url='/mount/')['status'] == 403, 'chroot mount'