xref: /unit/test/unit/applications/lang/go.py (revision 2073:bc6ad31ce286)
11019Szelenkov@nginx.comimport os
22062Smax.romanov@nginx.comimport shutil
31388Szelenkov@nginx.comimport subprocess
41477Szelenkov@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 TestApplicationGo(TestApplicationProto):
102062Smax.romanov@nginx.com    @staticmethod
112062Smax.romanov@nginx.com    def prepare_env(script, name='app', static=False):
122062Smax.romanov@nginx.com        temp_dir = option.temp_dir + '/go/'
132062Smax.romanov@nginx.com
142062Smax.romanov@nginx.com        if not os.path.exists(temp_dir):
152062Smax.romanov@nginx.com            os.mkdir(temp_dir)
162062Smax.romanov@nginx.com
172062Smax.romanov@nginx.com        cache_dir = option.cache_dir + '/go-build'
182062Smax.romanov@nginx.com
192062Smax.romanov@nginx.com        if not os.path.exists(cache_dir):
202062Smax.romanov@nginx.com            os.mkdir(cache_dir)
211019Szelenkov@nginx.com
221019Szelenkov@nginx.com        env = os.environ.copy()
231621Szelenkov@nginx.com        env['GOPATH'] = option.current_dir + '/build/go'
242062Smax.romanov@nginx.com        env['GOCACHE'] = cache_dir
252062Smax.romanov@nginx.com
262062Smax.romanov@nginx.com        shutil.copy2(
27*2073Szelenkov@nginx.com            option.test_dir + '/go/' + script + '/' + name + '.go', temp_dir
28*2073Szelenkov@nginx.com        )
291164Szelenkov@nginx.com
301490St.nateldemoura@f5.com        if static:
311490St.nateldemoura@f5.com            args = [
321490St.nateldemoura@f5.com                'go',
331490St.nateldemoura@f5.com                'build',
341490St.nateldemoura@f5.com                '-tags',
351490St.nateldemoura@f5.com                'netgo',
361490St.nateldemoura@f5.com                '-ldflags',
371490St.nateldemoura@f5.com                '-extldflags "-static"',
381490St.nateldemoura@f5.com                '-o',
392062Smax.romanov@nginx.com                temp_dir + name,
402062Smax.romanov@nginx.com                temp_dir + name + '.go',
411490St.nateldemoura@f5.com            ]
421490St.nateldemoura@f5.com        else:
431490St.nateldemoura@f5.com            args = [
441490St.nateldemoura@f5.com                'go',
451490St.nateldemoura@f5.com                'build',
461490St.nateldemoura@f5.com                '-o',
472062Smax.romanov@nginx.com                temp_dir + name,
482062Smax.romanov@nginx.com                temp_dir + name + '.go',
491490St.nateldemoura@f5.com            ]
501490St.nateldemoura@f5.com
512062Smax.romanov@nginx.com        replace_path = option.current_dir + '/build/go/src/unit.nginx.org/go'
522062Smax.romanov@nginx.com
532062Smax.romanov@nginx.com        with open(temp_dir + 'go.mod', 'w') as f:
542062Smax.romanov@nginx.com            f.write(
552062Smax.romanov@nginx.com                f"""module test/app
562062Smax.romanov@nginx.comrequire unit.nginx.org/go v0.0.0
572062Smax.romanov@nginx.comreplace unit.nginx.org/go => {replace_path}
58*2073Szelenkov@nginx.com"""
59*2073Szelenkov@nginx.com            )
602062Smax.romanov@nginx.com
611706Smax.romanov@nginx.com        if option.detailed:
621706Smax.romanov@nginx.com            print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
631706Smax.romanov@nginx.com
641208Szelenkov@nginx.com        try:
652062Smax.romanov@nginx.com            process = subprocess.run(args, env=env, cwd=temp_dir)
661208Szelenkov@nginx.com
671706Smax.romanov@nginx.com        except KeyboardInterrupt:
681706Smax.romanov@nginx.com            raise
691706Smax.romanov@nginx.com
702004Szelenkov@nginx.com        except subprocess.CalledProcessError:
711208Szelenkov@nginx.com            return None
721019Szelenkov@nginx.com
731164Szelenkov@nginx.com        return process
741164Szelenkov@nginx.com
751303St.nateldemoura@f5.com    def load(self, script, name='app', **kwargs):
761490St.nateldemoura@f5.com        static_build = False
771490St.nateldemoura@f5.com
781596Szelenkov@nginx.com        wdir = option.test_dir + "/go/" + script
791654Szelenkov@nginx.com        executable = option.temp_dir + "/go/" + name
801490St.nateldemoura@f5.com
811490St.nateldemoura@f5.com        if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
821490St.nateldemoura@f5.com            wdir = "/go/"
831490St.nateldemoura@f5.com            executable = "/go/" + name
841490St.nateldemoura@f5.com            static_build = True
851164Szelenkov@nginx.com
862062Smax.romanov@nginx.com        TestApplicationGo.prepare_env(script, name, static=static_build)
871490St.nateldemoura@f5.com
881490St.nateldemoura@f5.com        conf = {
891490St.nateldemoura@f5.com            "listeners": {"*:7080": {"pass": "applications/" + script}},
901490St.nateldemoura@f5.com            "applications": {
911490St.nateldemoura@f5.com                script: {
921490St.nateldemoura@f5.com                    "type": "external",
931490St.nateldemoura@f5.com                    "processes": {"spare": 0},
941490St.nateldemoura@f5.com                    "working_directory": wdir,
951490St.nateldemoura@f5.com                    "executable": executable,
961019Szelenkov@nginx.com                },
971303St.nateldemoura@f5.com            },
981490St.nateldemoura@f5.com        }
991490St.nateldemoura@f5.com
1001490St.nateldemoura@f5.com        self._load_conf(conf, **kwargs)
101