xref: /unit/test/unit/applications/lang/python.py (revision 1926:6e85d6c0b8bb)
1import os
2import shutil
3from urllib.parse import quote
4
5import pytest
6from unit.applications.proto import TestApplicationProto
7from unit.option import option
8
9
10class TestApplicationPython(TestApplicationProto):
11    application_type = "python"
12    load_module = "wsgi"
13
14    def load(self, script, name=None, module=None, **kwargs):
15        if name is None:
16            name = script
17
18        if module is None:
19            module = self.load_module
20
21        if script[0] == '/':
22            script_path = script
23        else:
24            script_path = option.test_dir + '/python/' + script
25
26        if kwargs.get('isolation') and kwargs['isolation'].get('rootfs'):
27            rootfs = kwargs['isolation']['rootfs']
28
29            if not os.path.exists(rootfs + '/app/python/'):
30                os.makedirs(rootfs + '/app/python/')
31
32            if not os.path.exists(rootfs + '/app/python/' + name):
33                shutil.copytree(script_path, rootfs + '/app/python/' + name)
34
35            script_path = '/app/python/' + name
36
37        app = {
38            "type": self.get_application_type(),
39            "processes": kwargs.pop('processes', {"spare": 0}),
40            "path": script_path,
41            "working_directory": script_path,
42            "module": module,
43        }
44
45        for attr in (
46            'callable',
47            'environment',
48            'home',
49            'limits',
50            'path',
51            'protocol',
52            'targets',
53            'threads',
54        ):
55            if attr in kwargs:
56                app[attr] = kwargs.pop(attr)
57
58        self._load_conf(
59            {
60                "listeners": {
61                    "*:7080": {"pass": "applications/" + quote(name, '')}
62                },
63                "applications": {name: app},
64            },
65            **kwargs
66        )
67