xref: /unit/test/test_asgi_lifespan.py (revision 2066:242192963d93)
11628Szelenkov@nginx.comimport os
21628Szelenkov@nginx.com
31654Szelenkov@nginx.comfrom conftest import unit_stop
4*2066Szelenkov@nginx.comfrom packaging import version
51628Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
61730Szelenkov@nginx.comfrom unit.option import option
71628Szelenkov@nginx.com
81628Szelenkov@nginx.com
91628Szelenkov@nginx.comclass TestASGILifespan(TestApplicationPython):
101628Szelenkov@nginx.com    prerequisites = {
11*2066Szelenkov@nginx.com        'modules': {
12*2066Szelenkov@nginx.com            'python': lambda v: version.parse(v) >= version.parse('3.5')
13*2066Szelenkov@nginx.com        }
141628Szelenkov@nginx.com    }
151628Szelenkov@nginx.com    load_module = 'asgi'
161628Szelenkov@nginx.com
171873So.canty@f5.com    def setup_cookies(self, prefix):
181873So.canty@f5.com        base_dir = option.test_dir + '/python/lifespan/empty'
191873So.canty@f5.com
201873So.canty@f5.com        os.chmod(base_dir, 0o777)
211873So.canty@f5.com
221873So.canty@f5.com        for name in ['startup', 'shutdown', 'version']:
231873So.canty@f5.com            path = option.test_dir + '/python/lifespan/empty/' + prefix + name
241873So.canty@f5.com            open(path, 'a').close()
251873So.canty@f5.com            os.chmod(path, 0o777)
261873So.canty@f5.com
271873So.canty@f5.com    def assert_cookies(self, prefix):
281873So.canty@f5.com        for name in ['startup', 'shutdown']:
291873So.canty@f5.com            path = option.test_dir + '/python/lifespan/empty/' + prefix + name
301873So.canty@f5.com            exists = os.path.isfile(path)
311873So.canty@f5.com            if exists:
321873So.canty@f5.com                os.remove(path)
331873So.canty@f5.com
341873So.canty@f5.com            assert not exists, name
351873So.canty@f5.com
361873So.canty@f5.com        path = option.test_dir + '/python/lifespan/empty/' + prefix + 'version'
371873So.canty@f5.com
381873So.canty@f5.com        with open(path, 'r') as f:
391873So.canty@f5.com            version = f.read()
401873So.canty@f5.com
411873So.canty@f5.com        os.remove(path)
421873So.canty@f5.com
431873So.canty@f5.com        assert version == '3.0 2.0', 'version'
441873So.canty@f5.com
451628Szelenkov@nginx.com    def test_asgi_lifespan(self):
461628Szelenkov@nginx.com        self.load('lifespan/empty')
471628Szelenkov@nginx.com
481873So.canty@f5.com        self.setup_cookies('')
491632Szelenkov@nginx.com
501628Szelenkov@nginx.com        assert self.get()['status'] == 204
511628Szelenkov@nginx.com
521654Szelenkov@nginx.com        unit_stop()
531628Szelenkov@nginx.com
541873So.canty@f5.com        self.assert_cookies('')
551628Szelenkov@nginx.com
561873So.canty@f5.com    def test_asgi_lifespan_targets(self):
571873So.canty@f5.com        assert 'success' in self.conf(
581873So.canty@f5.com            {
591873So.canty@f5.com                "listeners": {"*:7080": {"pass": "routes"}},
601873So.canty@f5.com                "routes": [
611873So.canty@f5.com                    {
621873So.canty@f5.com                        "match": {"uri": "/1"},
631873So.canty@f5.com                        "action": {"pass": "applications/targets/1"},
641873So.canty@f5.com                    },
651873So.canty@f5.com                    {
661873So.canty@f5.com                        "match": {"uri": "/2"},
671873So.canty@f5.com                        "action": {"pass": "applications/targets/2"},
681873So.canty@f5.com                    },
691873So.canty@f5.com                ],
701873So.canty@f5.com                "applications": {
711873So.canty@f5.com                    "targets": {
722055Szelenkov@nginx.com                        "type": self.get_application_type(),
731873So.canty@f5.com                        "processes": {"spare": 0},
741873So.canty@f5.com                        "working_directory": option.test_dir
751873So.canty@f5.com                        + "/python/lifespan/empty",
761873So.canty@f5.com                        "path": option.test_dir + '/python/lifespan/empty',
771873So.canty@f5.com                        "targets": {
781873So.canty@f5.com                            "1": {"module": "asgi", "callable": "application"},
791873So.canty@f5.com                            "2": {
801873So.canty@f5.com                                "module": "asgi",
811873So.canty@f5.com                                "callable": "application2",
821873So.canty@f5.com                            },
831873So.canty@f5.com                        },
841873So.canty@f5.com                    }
851873So.canty@f5.com                },
861873So.canty@f5.com            }
871873So.canty@f5.com        )
881628Szelenkov@nginx.com
891873So.canty@f5.com        self.setup_cookies('')
901873So.canty@f5.com        self.setup_cookies('app2_')
911628Szelenkov@nginx.com
921873So.canty@f5.com        assert self.get(url="/1")['status'] == 204
931873So.canty@f5.com        assert self.get(url="/2")['status'] == 204
941873So.canty@f5.com
951873So.canty@f5.com        unit_stop()
961873So.canty@f5.com
971873So.canty@f5.com        self.assert_cookies('')
981873So.canty@f5.com        self.assert_cookies('app2_')
991628Szelenkov@nginx.com
1001628Szelenkov@nginx.com    def test_asgi_lifespan_failed(self):
1011628Szelenkov@nginx.com        self.load('lifespan/failed')
1021628Szelenkov@nginx.com
1031628Szelenkov@nginx.com        assert self.get()['status'] == 503
1041628Szelenkov@nginx.com
1051628Szelenkov@nginx.com        assert (
1061628Szelenkov@nginx.com            self.wait_for_record(r'\[error\].*Application startup failed')
1071628Szelenkov@nginx.com            is not None
1081628Szelenkov@nginx.com        ), 'error message'
1091628Szelenkov@nginx.com        assert self.wait_for_record(r'Exception blah') is not None, 'exception'
1101628Szelenkov@nginx.com
1111628Szelenkov@nginx.com    def test_asgi_lifespan_error(self):
1121628Szelenkov@nginx.com        self.load('lifespan/error')
1131628Szelenkov@nginx.com
1141628Szelenkov@nginx.com        self.get()
1151628Szelenkov@nginx.com
1161628Szelenkov@nginx.com        assert self.wait_for_record(r'Exception blah') is not None, 'exception'
1171628Szelenkov@nginx.com
1181628Szelenkov@nginx.com    def test_asgi_lifespan_error_auto(self):
1191628Szelenkov@nginx.com        self.load('lifespan/error_auto')
1201628Szelenkov@nginx.com
1211628Szelenkov@nginx.com        self.get()
1221628Szelenkov@nginx.com
1231628Szelenkov@nginx.com        assert self.wait_for_record(r'AssertionError') is not None, 'assertion'
124