xref: /unit/test/unit/applications/lang/python.py (revision 1984:06514cd08a35)
11527Szelenkov@nginx.comimport os
21490St.nateldemoura@f5.comimport shutil
31635Szelenkov@nginx.comfrom urllib.parse import quote
41635Szelenkov@nginx.com
51019Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
61730Szelenkov@nginx.comfrom unit.option import option
71019Szelenkov@nginx.com
81019Szelenkov@nginx.com
91019Szelenkov@nginx.comclass TestApplicationPython(TestApplicationProto):
101116Szelenkov@nginx.com    application_type = "python"
111610Smax.romanov@nginx.com    load_module = "wsgi"
121116Szelenkov@nginx.com
131610Smax.romanov@nginx.com    def load(self, script, name=None, module=None, **kwargs):
141019Szelenkov@nginx.com        if name is None:
151019Szelenkov@nginx.com            name = script
161019Szelenkov@nginx.com
171610Smax.romanov@nginx.com        if module is None:
181610Smax.romanov@nginx.com            module = self.load_module
191610Smax.romanov@nginx.com
201490St.nateldemoura@f5.com        if script[0] == '/':
211490St.nateldemoura@f5.com            script_path = script
221490St.nateldemoura@f5.com        else:
231596Szelenkov@nginx.com            script_path = option.test_dir + '/python/' + script
241490St.nateldemoura@f5.com
251490St.nateldemoura@f5.com        if kwargs.get('isolation') and kwargs['isolation'].get('rootfs'):
261490St.nateldemoura@f5.com            rootfs = kwargs['isolation']['rootfs']
271490St.nateldemoura@f5.com
281490St.nateldemoura@f5.com            if not os.path.exists(rootfs + '/app/python/'):
291490St.nateldemoura@f5.com                os.makedirs(rootfs + '/app/python/')
301490St.nateldemoura@f5.com
311490St.nateldemoura@f5.com            if not os.path.exists(rootfs + '/app/python/' + name):
321490St.nateldemoura@f5.com                shutil.copytree(script_path, rootfs + '/app/python/' + name)
331490St.nateldemoura@f5.com
341490St.nateldemoura@f5.com            script_path = '/app/python/' + name
351019Szelenkov@nginx.com
361695Smax.romanov@nginx.com        app = {
371695Smax.romanov@nginx.com            "type": self.get_application_type(),
381695Smax.romanov@nginx.com            "processes": kwargs.pop('processes', {"spare": 0}),
391695Smax.romanov@nginx.com            "path": script_path,
401695Smax.romanov@nginx.com            "working_directory": script_path,
411695Smax.romanov@nginx.com            "module": module,
421695Smax.romanov@nginx.com        }
431695Smax.romanov@nginx.com
441873So.canty@f5.com        for attr in (
451873So.canty@f5.com            'callable',
46*1926Smax.romanov@nginx.com            'environment',
471873So.canty@f5.com            'home',
481873So.canty@f5.com            'limits',
491873So.canty@f5.com            'path',
501873So.canty@f5.com            'protocol',
511873So.canty@f5.com            'targets',
521873So.canty@f5.com            'threads',
531873So.canty@f5.com        ):
541695Smax.romanov@nginx.com            if attr in kwargs:
551695Smax.romanov@nginx.com                app[attr] = kwargs.pop(attr)
561695Smax.romanov@nginx.com
571050Szelenkov@nginx.com        self._load_conf(
581019Szelenkov@nginx.com            {
591610Smax.romanov@nginx.com                "listeners": {
601610Smax.romanov@nginx.com                    "*:7080": {"pass": "applications/" + quote(name, '')}
611610Smax.romanov@nginx.com                },
621695Smax.romanov@nginx.com                "applications": {name: app},
631303St.nateldemoura@f5.com            },
641303St.nateldemoura@f5.com            **kwargs
651019Szelenkov@nginx.com        )
66