summaryrefslogtreecommitdiffhomepage
path: root/test/test_asgi_targets.py
blob: a0eb1f84ca9d0392e407023cfba22a20e5ad5470 (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
from distutils.version import LooseVersion

import pytest

from unit.applications.lang.python import TestApplicationPython
from unit.option import option


class TestASGITargets(TestApplicationPython):
    prerequisites = {
        'modules': {'python': lambda v: LooseVersion(v) >= LooseVersion('3.5')}
    }
    load_module = 'asgi'

    @pytest.fixture(autouse=True)
    def setup_method_fixture(self):
        assert 'success' in self.conf(
            {
                "listeners": {"*:7080": {"pass": "routes"}},
                "routes": [
                    {
                        "match": {"uri": "/1"},
                        "action": {"pass": "applications/targets/1"},
                    },
                    {
                        "match": {"uri": "/2"},
                        "action": {"pass": "applications/targets/2"},
                    },
                ],
                "applications": {
                    "targets": {
                        "type": "python",
                        "processes": {"spare": 0},
                        "working_directory": option.test_dir
                        + "/python/targets/",
                        "path": option.test_dir + '/python/targets/',
                        "protocol": "asgi",
                        "targets": {
                            "1": {
                                "module": "asgi",
                                "callable": "application_200",
                            },
                            "2": {
                                "module": "asgi",
                                "callable": "application_201",
                            },
                        },
                    }
                },
            }
        )

    def conf_targets(self, targets):
        assert 'success' in self.conf(targets, 'applications/targets/targets')

    def test_asgi_targets(self):
        assert self.get(url='/1')['status'] == 200
        assert self.get(url='/2')['status'] == 201

    def test_asgi_targets_legacy(self):
        self.conf_targets(
            {
                "1": {"module": "asgi", "callable": "legacy_application_200"},
                "2": {"module": "asgi", "callable": "legacy_application_201"},
            }
        )

        assert self.get(url='/1')['status'] == 200
        assert self.get(url='/2')['status'] == 201

    def test_asgi_targets_mix(self):
        self.conf_targets(
            {
                "1": {"module": "asgi", "callable": "application_200"},
                "2": {"module": "asgi", "callable": "legacy_application_201"},
            }
        )

        assert self.get(url='/1')['status'] == 200
        assert self.get(url='/2')['status'] == 201

    def test_asgi_targets_broken(self, skip_alert):
        skip_alert(r'Python failed to get "blah" from module')

        self.conf_targets(
            {
                "1": {"module": "asgi", "callable": "application_200"},
                "2": {"module": "asgi", "callable": "blah"},
            }
        )

        assert self.get(url='/1')['status'] != 200