xref: /unit/test/unit/applications/lang/go.py (revision 1880:00dc6b211906)
1import os
2import subprocess
3
4from unit.applications.proto import TestApplicationProto
5from unit.option import option
6
7
8class TestApplicationGo(TestApplicationProto):
9    def prepare_env(self, script, name, static=False):
10        if not os.path.exists(option.temp_dir + '/go'):
11            os.mkdir(option.temp_dir + '/go')
12
13        env = os.environ.copy()
14        env['GOPATH'] = option.current_dir + '/build/go'
15        env['GOCACHE'] = option.cache_dir + '/go'
16        env['GO111MODULE'] = 'auto'
17
18        if static:
19            args = [
20                'go',
21                'build',
22                '-tags',
23                'netgo',
24                '-ldflags',
25                '-extldflags "-static"',
26                '-o',
27                option.temp_dir + '/go/' + name,
28                option.test_dir + '/go/' + script + '/' + name + '.go',
29            ]
30        else:
31            args = [
32                'go',
33                'build',
34                '-o',
35                option.temp_dir + '/go/' + name,
36                option.test_dir + '/go/' + script + '/' + name + '.go',
37            ]
38
39        if option.detailed:
40            print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
41
42        try:
43            process = subprocess.Popen(args, env=env)
44            process.communicate()
45
46        except KeyboardInterrupt:
47            raise
48
49        except:
50            return None
51
52        return process
53
54    def load(self, script, name='app', **kwargs):
55        static_build = False
56
57        wdir = option.test_dir + "/go/" + script
58        executable = option.temp_dir + "/go/" + name
59
60        if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
61            wdir = "/go/"
62            executable = "/go/" + name
63            static_build = True
64
65        self.prepare_env(script, name, static=static_build)
66
67        conf = {
68            "listeners": {"*:7080": {"pass": "applications/" + script}},
69            "applications": {
70                script: {
71                    "type": "external",
72                    "processes": {"spare": 0},
73                    "working_directory": wdir,
74                    "executable": executable,
75                },
76            },
77        }
78
79        self._load_conf(conf, **kwargs)
80