xref: /unit/test/test_static_fallback.py (revision 2066:242192963d93)
11902Szelenkov@nginx.comimport os
21902Szelenkov@nginx.comfrom pathlib import Path
31902Szelenkov@nginx.com
41902Szelenkov@nginx.comimport pytest
51902Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
61902Szelenkov@nginx.com
71902Szelenkov@nginx.com
81902Szelenkov@nginx.comclass TestStaticFallback(TestApplicationProto):
91902Szelenkov@nginx.com    prerequisites = {}
101902Szelenkov@nginx.com
111902Szelenkov@nginx.com    @pytest.fixture(autouse=True)
121902Szelenkov@nginx.com    def setup_method_fixture(self, temp_dir):
131902Szelenkov@nginx.com        os.makedirs(temp_dir + '/assets/dir')
141902Szelenkov@nginx.com        Path(temp_dir + '/assets/index.html').write_text('0123456789')
151902Szelenkov@nginx.com
161902Szelenkov@nginx.com        os.makedirs(temp_dir + '/assets/403')
171902Szelenkov@nginx.com        os.chmod(temp_dir + '/assets/403', 0o000)
181902Szelenkov@nginx.com
191902Szelenkov@nginx.com        self._load_conf(
201902Szelenkov@nginx.com            {
211902Szelenkov@nginx.com                "listeners": {
221902Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
231902Szelenkov@nginx.com                    "*:7081": {"pass": "routes"},
241902Szelenkov@nginx.com                },
251960Sz.hong@f5.com                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
261902Szelenkov@nginx.com                "applications": {},
271902Szelenkov@nginx.com            }
281902Szelenkov@nginx.com        )
291902Szelenkov@nginx.com
301902Szelenkov@nginx.com        yield
311902Szelenkov@nginx.com
321902Szelenkov@nginx.com        try:
331902Szelenkov@nginx.com            os.chmod(temp_dir + '/assets/403', 0o777)
341902Szelenkov@nginx.com        except FileNotFoundError:
351902Szelenkov@nginx.com            pass
361902Szelenkov@nginx.com
371902Szelenkov@nginx.com    def action_update(self, conf):
381902Szelenkov@nginx.com        assert 'success' in self.conf(conf, 'routes/0/action')
391902Szelenkov@nginx.com
401902Szelenkov@nginx.com    def test_static_fallback(self):
411902Szelenkov@nginx.com        self.action_update({"share": "/blah"})
421902Szelenkov@nginx.com        assert self.get()['status'] == 404, 'bad path no fallback'
431902Szelenkov@nginx.com
441902Szelenkov@nginx.com        self.action_update({"share": "/blah", "fallback": {"return": 200}})
451902Szelenkov@nginx.com
461902Szelenkov@nginx.com        resp = self.get()
471902Szelenkov@nginx.com        assert resp['status'] == 200, 'bad path fallback status'
481902Szelenkov@nginx.com        assert resp['body'] == '', 'bad path fallback'
491902Szelenkov@nginx.com
501902Szelenkov@nginx.com    def test_static_fallback_valid_path(self, temp_dir):
511902Szelenkov@nginx.com        self.action_update(
521960Sz.hong@f5.com            {"share": temp_dir + "/assets$uri", "fallback": {"return": 200}}
531902Szelenkov@nginx.com        )
541902Szelenkov@nginx.com        resp = self.get()
551902Szelenkov@nginx.com        assert resp['status'] == 200, 'fallback status'
561902Szelenkov@nginx.com        assert resp['body'] == '0123456789', 'fallback'
571902Szelenkov@nginx.com
581902Szelenkov@nginx.com        resp = self.get(url='/403/')
591902Szelenkov@nginx.com        assert resp['status'] == 200, 'fallback status 403'
601902Szelenkov@nginx.com        assert resp['body'] == '', 'fallback 403'
611902Szelenkov@nginx.com
621902Szelenkov@nginx.com        resp = self.post()
631902Szelenkov@nginx.com        assert resp['status'] == 200, 'fallback status 405'
641902Szelenkov@nginx.com        assert resp['body'] == '', 'fallback 405'
651902Szelenkov@nginx.com
661902Szelenkov@nginx.com        assert self.get(url='/dir')['status'] == 301, 'fallback status 301'
671902Szelenkov@nginx.com
681902Szelenkov@nginx.com    def test_static_fallback_nested(self):
691902Szelenkov@nginx.com        self.action_update(
701902Szelenkov@nginx.com            {
711902Szelenkov@nginx.com                "share": "/blah",
721902Szelenkov@nginx.com                "fallback": {
731902Szelenkov@nginx.com                    "share": "/blah/blah",
741902Szelenkov@nginx.com                    "fallback": {"return": 200},
751902Szelenkov@nginx.com                },
761902Szelenkov@nginx.com            }
771902Szelenkov@nginx.com        )
781902Szelenkov@nginx.com
791902Szelenkov@nginx.com        resp = self.get()
801902Szelenkov@nginx.com        assert resp['status'] == 200, 'fallback nested status'
811902Szelenkov@nginx.com        assert resp['body'] == '', 'fallback nested'
821902Szelenkov@nginx.com
831902Szelenkov@nginx.com    def test_static_fallback_share(self, temp_dir):
841902Szelenkov@nginx.com        self.action_update(
85*2066Szelenkov@nginx.com            {
86*2066Szelenkov@nginx.com                "share": "/blah",
87*2066Szelenkov@nginx.com                "fallback": {"share": temp_dir + "/assets$uri"},
88*2066Szelenkov@nginx.com            }
891902Szelenkov@nginx.com        )
901902Szelenkov@nginx.com
911902Szelenkov@nginx.com        resp = self.get()
921902Szelenkov@nginx.com        assert resp['status'] == 200, 'fallback share status'
931902Szelenkov@nginx.com        assert resp['body'] == '0123456789', 'fallback share'
941902Szelenkov@nginx.com
951902Szelenkov@nginx.com        resp = self.head()
961902Szelenkov@nginx.com        assert resp['status'] == 200, 'fallback share status HEAD'
971902Szelenkov@nginx.com        assert resp['body'] == '', 'fallback share HEAD'
981902Szelenkov@nginx.com
991902Szelenkov@nginx.com        assert (
1001902Szelenkov@nginx.com            self.get(url='/dir')['status'] == 301
1011902Szelenkov@nginx.com        ), 'fallback share status 301'
1021902Szelenkov@nginx.com
1031902Szelenkov@nginx.com    def test_static_fallback_proxy(self):
1041902Szelenkov@nginx.com        assert 'success' in self.conf(
1051902Szelenkov@nginx.com            [
1061902Szelenkov@nginx.com                {
1071902Szelenkov@nginx.com                    "match": {"destination": "*:7081"},
1081902Szelenkov@nginx.com                    "action": {"return": 200},
1091902Szelenkov@nginx.com                },
1101902Szelenkov@nginx.com                {
1111902Szelenkov@nginx.com                    "action": {
1121902Szelenkov@nginx.com                        "share": "/blah",
1131902Szelenkov@nginx.com                        "fallback": {"proxy": "http://127.0.0.1:7081"},
1141902Szelenkov@nginx.com                    }
1151902Szelenkov@nginx.com                },
1161902Szelenkov@nginx.com            ],
1171902Szelenkov@nginx.com            'routes',
1181902Szelenkov@nginx.com        ), 'configure fallback proxy route'
1191902Szelenkov@nginx.com
1201902Szelenkov@nginx.com        resp = self.get()
1211902Szelenkov@nginx.com        assert resp['status'] == 200, 'fallback proxy status'
1221902Szelenkov@nginx.com        assert resp['body'] == '', 'fallback proxy'
1231902Szelenkov@nginx.com
1241902Szelenkov@nginx.com    @pytest.mark.skip('not yet')
1251902Szelenkov@nginx.com    def test_static_fallback_proxy_loop(self, skip_alert):
1261902Szelenkov@nginx.com        skip_alert(
1271902Szelenkov@nginx.com            r'open.*/blah/index.html.*failed',
1281902Szelenkov@nginx.com            r'accept.*failed',
1291902Szelenkov@nginx.com            r'socket.*failed',
1301902Szelenkov@nginx.com            r'new connections are not accepted',
1311902Szelenkov@nginx.com        )
1321902Szelenkov@nginx.com
1331902Szelenkov@nginx.com        self.action_update(
1341902Szelenkov@nginx.com            {"share": "/blah", "fallback": {"proxy": "http://127.0.0.1:7080"}}
1351902Szelenkov@nginx.com        )
1361902Szelenkov@nginx.com        self.get(no_recv=True)
1371902Szelenkov@nginx.com
1381902Szelenkov@nginx.com        assert 'success' in self.conf_delete('listeners/*:7081')
1391902Szelenkov@nginx.com        self.get(read_timeout=1)
1401902Szelenkov@nginx.com
1411902Szelenkov@nginx.com    def test_static_fallback_invalid(self):
1421902Szelenkov@nginx.com        def check_error(conf):
1431902Szelenkov@nginx.com            assert 'error' in self.conf(conf, 'routes/0/action')
1441902Szelenkov@nginx.com
1451902Szelenkov@nginx.com        check_error({"share": "/blah", "fallback": {}})
1461902Szelenkov@nginx.com        check_error({"share": "/blah", "fallback": ""})
1471902Szelenkov@nginx.com        check_error({"return": 200, "fallback": {"share": "/blah"}})
1481902Szelenkov@nginx.com        check_error(
1491902Szelenkov@nginx.com            {"proxy": "http://127.0.0.1:7081", "fallback": {"share": "/blah"}}
1501902Szelenkov@nginx.com        )
1511902Szelenkov@nginx.com        check_error({"fallback": {"share": "/blah"}})
152