xref: /unit/test/unit/applications/lang/node.py (revision 1876:f85b85094541)
11019Szelenkov@nginx.comimport shutil
21472Svbart@nginx.comfrom urllib.parse import quote
31477Szelenkov@nginx.com
41019Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
51730Szelenkov@nginx.comfrom unit.option import option
61735Szelenkov@nginx.comfrom unit.utils import public_dir
71019Szelenkov@nginx.com
81019Szelenkov@nginx.com
91019Szelenkov@nginx.comclass TestApplicationNode(TestApplicationProto):
101865So.canty@f5.com    application_type = "node"
111865So.canty@f5.com    es_modules = False
121865So.canty@f5.com
131657Szelenkov@nginx.com    def prepare_env(self, script):
141019Szelenkov@nginx.com        # copy application
151019Szelenkov@nginx.com        shutil.copytree(
161654Szelenkov@nginx.com            option.test_dir + '/node/' + script, option.temp_dir + '/node'
171019Szelenkov@nginx.com        )
181019Szelenkov@nginx.com
191293St.nateldemoura@f5.com        # copy modules
201293St.nateldemoura@f5.com        shutil.copytree(
211621Szelenkov@nginx.com            option.current_dir + '/node/node_modules',
221654Szelenkov@nginx.com            option.temp_dir + '/node/node_modules',
231019Szelenkov@nginx.com        )
241019Szelenkov@nginx.com
251654Szelenkov@nginx.com        public_dir(option.temp_dir + '/node')
261293St.nateldemoura@f5.com
271657Szelenkov@nginx.com    def load(self, script, name='app.js', **kwargs):
281657Szelenkov@nginx.com        self.prepare_env(script)
291657Szelenkov@nginx.com
301865So.canty@f5.com        if self.es_modules:
311865So.canty@f5.com            arguments = [
321865So.canty@f5.com                "node",
331865So.canty@f5.com                "--loader",
34*1876So.canty@f5.com                "unit-http/loader.mjs",
351865So.canty@f5.com                "--require",
36*1876So.canty@f5.com                "unit-http/loader",
371865So.canty@f5.com                name,
381865So.canty@f5.com            ]
391865So.canty@f5.com
401865So.canty@f5.com        else:
41*1876So.canty@f5.com            arguments = ["node", "--require", "unit-http/loader", name]
421865So.canty@f5.com
431050Szelenkov@nginx.com        self._load_conf(
441019Szelenkov@nginx.com            {
451472Svbart@nginx.com                "listeners": {
461472Svbart@nginx.com                    "*:7080": {"pass": "applications/" + quote(script, '')}
471472Svbart@nginx.com                },
481019Szelenkov@nginx.com                "applications": {
491019Szelenkov@nginx.com                    script: {
501019Szelenkov@nginx.com                        "type": "external",
511019Szelenkov@nginx.com                        "processes": {"spare": 0},
521654Szelenkov@nginx.com                        "working_directory": option.temp_dir + '/node',
531865So.canty@f5.com                        "executable": '/usr/bin/env',
541865So.canty@f5.com                        "arguments": arguments,
551019Szelenkov@nginx.com                    }
561019Szelenkov@nginx.com                },
571303St.nateldemoura@f5.com            },
581303St.nateldemoura@f5.com            **kwargs
591019Szelenkov@nginx.com        )
60