xref: /unit/test/test_static_mount.py (revision 2073:bc6ad31ce286)
11902Szelenkov@nginx.comimport os
21902Szelenkov@nginx.comimport subprocess
31902Szelenkov@nginx.comfrom pathlib import Path
41902Szelenkov@nginx.com
51902Szelenkov@nginx.comimport pytest
61902Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
71902Szelenkov@nginx.com
81902Szelenkov@nginx.com
91902Szelenkov@nginx.comclass TestStaticMount(TestApplicationProto):
101902Szelenkov@nginx.com    prerequisites = {'features': ['chroot']}
111902Szelenkov@nginx.com
121902Szelenkov@nginx.com    @pytest.fixture(autouse=True)
131902Szelenkov@nginx.com    def setup_method_fixture(self, is_su, temp_dir):
141902Szelenkov@nginx.com        if not is_su:
151902Szelenkov@nginx.com            pytest.skip('requires root')
161902Szelenkov@nginx.com
171902Szelenkov@nginx.com        os.makedirs(temp_dir + '/assets/dir/mount')
181902Szelenkov@nginx.com        os.makedirs(temp_dir + '/assets/dir/dir')
191902Szelenkov@nginx.com        os.makedirs(temp_dir + '/assets/mount')
201902Szelenkov@nginx.com        Path(temp_dir + '/assets/index.html').write_text('index')
211902Szelenkov@nginx.com        Path(temp_dir + '/assets/dir/dir/file').write_text('file')
221902Szelenkov@nginx.com        Path(temp_dir + '/assets/mount/index.html').write_text('mount')
231902Szelenkov@nginx.com
241902Szelenkov@nginx.com        try:
252004Szelenkov@nginx.com            subprocess.check_output(
261902Szelenkov@nginx.com                [
271902Szelenkov@nginx.com                    "mount",
281902Szelenkov@nginx.com                    "--bind",
291902Szelenkov@nginx.com                    temp_dir + "/assets/mount",
301902Szelenkov@nginx.com                    temp_dir + "/assets/dir/mount",
311902Szelenkov@nginx.com                ],
321902Szelenkov@nginx.com                stderr=subprocess.STDOUT,
331902Szelenkov@nginx.com            )
341902Szelenkov@nginx.com
351902Szelenkov@nginx.com        except KeyboardInterrupt:
361902Szelenkov@nginx.com            raise
371902Szelenkov@nginx.com
382004Szelenkov@nginx.com        except subprocess.CalledProcessError:
391902Szelenkov@nginx.com            pytest.fail('Can\'t run mount process.')
401902Szelenkov@nginx.com
411902Szelenkov@nginx.com        self._load_conf(
421902Szelenkov@nginx.com            {
431902Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes"}},
44*2073Szelenkov@nginx.com                "routes": [{"action": {"share": temp_dir + "/assets/dir$uri"}}],
451902Szelenkov@nginx.com            }
461902Szelenkov@nginx.com        )
471902Szelenkov@nginx.com
481902Szelenkov@nginx.com        yield
491902Szelenkov@nginx.com
501902Szelenkov@nginx.com        try:
512004Szelenkov@nginx.com            subprocess.check_output(
521902Szelenkov@nginx.com                ["umount", "--lazy", temp_dir + "/assets/dir/mount"],
531902Szelenkov@nginx.com                stderr=subprocess.STDOUT,
541902Szelenkov@nginx.com            )
551902Szelenkov@nginx.com
561902Szelenkov@nginx.com        except KeyboardInterrupt:
571902Szelenkov@nginx.com            raise
581902Szelenkov@nginx.com
592004Szelenkov@nginx.com        except subprocess.CalledProcessError:
601902Szelenkov@nginx.com            pytest.fail('Can\'t run umount process.')
611902Szelenkov@nginx.com
621902Szelenkov@nginx.com    def test_static_mount(self, temp_dir, skip_alert):
631902Szelenkov@nginx.com        skip_alert(r'opening.*failed')
641902Szelenkov@nginx.com
651902Szelenkov@nginx.com        resp = self.get(url='/mount/')
661902Szelenkov@nginx.com        assert resp['status'] == 200
671902Szelenkov@nginx.com        assert resp['body'] == 'mount'
681902Szelenkov@nginx.com
691902Szelenkov@nginx.com        assert 'success' in self.conf(
701960Sz.hong@f5.com            {"share": temp_dir + "/assets/dir$uri", "traverse_mounts": False},
711902Szelenkov@nginx.com            'routes/0/action',
721902Szelenkov@nginx.com        ), 'configure mount disable'
731902Szelenkov@nginx.com
741902Szelenkov@nginx.com        assert self.get(url='/mount/')['status'] == 403
751902Szelenkov@nginx.com
761902Szelenkov@nginx.com        assert 'success' in self.conf(
771960Sz.hong@f5.com            {"share": temp_dir + "/assets/dir$uri", "traverse_mounts": True},
781902Szelenkov@nginx.com            'routes/0/action',
791902Szelenkov@nginx.com        ), 'configure mount enable'
801902Szelenkov@nginx.com
811902Szelenkov@nginx.com        resp = self.get(url='/mount/')
821902Szelenkov@nginx.com        assert resp['status'] == 200
831902Szelenkov@nginx.com        assert resp['body'] == 'mount'
841902Szelenkov@nginx.com
851902Szelenkov@nginx.com    def test_static_mount_two_blocks(self, temp_dir, skip_alert):
861902Szelenkov@nginx.com        skip_alert(r'opening.*failed')
871902Szelenkov@nginx.com
881902Szelenkov@nginx.com        os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link')
891902Szelenkov@nginx.com
901902Szelenkov@nginx.com        assert 'success' in self.conf(
911902Szelenkov@nginx.com            [
921902Szelenkov@nginx.com                {
931902Szelenkov@nginx.com                    "match": {"method": "HEAD"},
941902Szelenkov@nginx.com                    "action": {
951960Sz.hong@f5.com                        "share": temp_dir + "/assets/dir$uri",
961902Szelenkov@nginx.com                        "traverse_mounts": False,
971902Szelenkov@nginx.com                    },
981902Szelenkov@nginx.com                },
991902Szelenkov@nginx.com                {
1001902Szelenkov@nginx.com                    "match": {"method": "GET"},
1011902Szelenkov@nginx.com                    "action": {
1021960Sz.hong@f5.com                        "share": temp_dir + "/assets/dir$uri",
1031902Szelenkov@nginx.com                        "traverse_mounts": True,
1041902Szelenkov@nginx.com                    },
1051902Szelenkov@nginx.com                },
1061902Szelenkov@nginx.com            ],
1071902Szelenkov@nginx.com            'routes',
1081902Szelenkov@nginx.com        ), 'configure two options'
1091902Szelenkov@nginx.com
1101902Szelenkov@nginx.com        assert self.get(url='/mount/')['status'] == 200, 'block enabled'
1111902Szelenkov@nginx.com        assert self.head(url='/mount/')['status'] == 403, 'block disabled'
1121902Szelenkov@nginx.com
1131902Szelenkov@nginx.com    def test_static_mount_chroot(self, temp_dir, skip_alert):
1141902Szelenkov@nginx.com        skip_alert(r'opening.*failed')
1151902Szelenkov@nginx.com
1161902Szelenkov@nginx.com        assert 'success' in self.conf(
1171902Szelenkov@nginx.com            {
1181960Sz.hong@f5.com                "share": temp_dir + "/assets/dir$uri",
1191902Szelenkov@nginx.com                "chroot": temp_dir + "/assets",
1201902Szelenkov@nginx.com            },
1211902Szelenkov@nginx.com            'routes/0/action',
1221902Szelenkov@nginx.com        ), 'configure chroot mount default'
1231902Szelenkov@nginx.com
1241902Szelenkov@nginx.com        assert self.get(url='/mount/')['status'] == 200, 'chroot'
1251902Szelenkov@nginx.com
1261902Szelenkov@nginx.com        assert 'success' in self.conf(
1271902Szelenkov@nginx.com            {
1281960Sz.hong@f5.com                "share": temp_dir + "/assets/dir$uri",
1291902Szelenkov@nginx.com                "chroot": temp_dir + "/assets",
1301902Szelenkov@nginx.com                "traverse_mounts": False,
1311902Szelenkov@nginx.com            },
1321902Szelenkov@nginx.com            'routes/0/action',
1331902Szelenkov@nginx.com        ), 'configure chroot mount disable'
1341902Szelenkov@nginx.com
1351902Szelenkov@nginx.com        assert self.get(url='/mount/')['status'] == 403, 'chroot mount'
136