xref: /unit/test/test_asgi_targets.py (revision 2066:242192963d93)
11873So.canty@f5.comimport pytest
2*2066Szelenkov@nginx.comfrom packaging import version
31873So.canty@f5.comfrom unit.applications.lang.python import TestApplicationPython
41873So.canty@f5.comfrom unit.option import option
51873So.canty@f5.com
61873So.canty@f5.com
71873So.canty@f5.comclass TestASGITargets(TestApplicationPython):
81873So.canty@f5.com    prerequisites = {
9*2066Szelenkov@nginx.com        'modules': {
10*2066Szelenkov@nginx.com            'python': lambda v: version.parse(v) >= version.parse('3.5')
11*2066Szelenkov@nginx.com        }
121873So.canty@f5.com    }
131873So.canty@f5.com    load_module = 'asgi'
141873So.canty@f5.com
151873So.canty@f5.com    @pytest.fixture(autouse=True)
161873So.canty@f5.com    def setup_method_fixture(self):
171873So.canty@f5.com        assert 'success' in self.conf(
181873So.canty@f5.com            {
191873So.canty@f5.com                "listeners": {"*:7080": {"pass": "routes"}},
201873So.canty@f5.com                "routes": [
211873So.canty@f5.com                    {
221873So.canty@f5.com                        "match": {"uri": "/1"},
231873So.canty@f5.com                        "action": {"pass": "applications/targets/1"},
241873So.canty@f5.com                    },
251873So.canty@f5.com                    {
261873So.canty@f5.com                        "match": {"uri": "/2"},
271873So.canty@f5.com                        "action": {"pass": "applications/targets/2"},
281873So.canty@f5.com                    },
291873So.canty@f5.com                ],
301873So.canty@f5.com                "applications": {
311873So.canty@f5.com                    "targets": {
322055Szelenkov@nginx.com                        "type": self.get_application_type(),
331873So.canty@f5.com                        "processes": {"spare": 0},
341873So.canty@f5.com                        "working_directory": option.test_dir
351873So.canty@f5.com                        + "/python/targets/",
361873So.canty@f5.com                        "path": option.test_dir + '/python/targets/',
371873So.canty@f5.com                        "protocol": "asgi",
381873So.canty@f5.com                        "targets": {
391873So.canty@f5.com                            "1": {
401873So.canty@f5.com                                "module": "asgi",
411873So.canty@f5.com                                "callable": "application_200",
421873So.canty@f5.com                            },
431873So.canty@f5.com                            "2": {
441873So.canty@f5.com                                "module": "asgi",
451873So.canty@f5.com                                "callable": "application_201",
461873So.canty@f5.com                            },
471873So.canty@f5.com                        },
481873So.canty@f5.com                    }
491873So.canty@f5.com                },
501873So.canty@f5.com            }
511873So.canty@f5.com        )
521873So.canty@f5.com
531873So.canty@f5.com    def conf_targets(self, targets):
541873So.canty@f5.com        assert 'success' in self.conf(targets, 'applications/targets/targets')
551873So.canty@f5.com
561873So.canty@f5.com    def test_asgi_targets(self):
571873So.canty@f5.com        assert self.get(url='/1')['status'] == 200
581873So.canty@f5.com        assert self.get(url='/2')['status'] == 201
591873So.canty@f5.com
601873So.canty@f5.com    def test_asgi_targets_legacy(self):
611873So.canty@f5.com        self.conf_targets(
621873So.canty@f5.com            {
631873So.canty@f5.com                "1": {"module": "asgi", "callable": "legacy_application_200"},
641873So.canty@f5.com                "2": {"module": "asgi", "callable": "legacy_application_201"},
651873So.canty@f5.com            }
661873So.canty@f5.com        )
671873So.canty@f5.com
681873So.canty@f5.com        assert self.get(url='/1')['status'] == 200
691873So.canty@f5.com        assert self.get(url='/2')['status'] == 201
701873So.canty@f5.com
711873So.canty@f5.com    def test_asgi_targets_mix(self):
721873So.canty@f5.com        self.conf_targets(
731873So.canty@f5.com            {
741873So.canty@f5.com                "1": {"module": "asgi", "callable": "application_200"},
751873So.canty@f5.com                "2": {"module": "asgi", "callable": "legacy_application_201"},
761873So.canty@f5.com            }
771873So.canty@f5.com        )
781873So.canty@f5.com
791873So.canty@f5.com        assert self.get(url='/1')['status'] == 200
801873So.canty@f5.com        assert self.get(url='/2')['status'] == 201
811873So.canty@f5.com
821873So.canty@f5.com    def test_asgi_targets_broken(self, skip_alert):
831873So.canty@f5.com        skip_alert(r'Python failed to get "blah" from module')
841873So.canty@f5.com
851873So.canty@f5.com        self.conf_targets(
861873So.canty@f5.com            {
871873So.canty@f5.com                "1": {"module": "asgi", "callable": "application_200"},
881873So.canty@f5.com                "2": {"module": "asgi", "callable": "blah"},
891873So.canty@f5.com            }
901873So.canty@f5.com        )
911873So.canty@f5.com
921873So.canty@f5.com        assert self.get(url='/1')['status'] != 200
93