go.py (2004:ca4c5855f139) go.py (2062:ede07f84045b)
1import os
1import os
2import shutil
2import subprocess
3
4from unit.applications.proto import TestApplicationProto
5from unit.option import option
6
7
8class TestApplicationGo(TestApplicationProto):
3import subprocess
4
5from unit.applications.proto import TestApplicationProto
6from unit.option import option
7
8
9class 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')
10 @staticmethod
11 def prepare_env(script, name='app', static=False):
12 temp_dir = option.temp_dir + '/go/'
12
13
14 if not os.path.exists(temp_dir):
15 os.mkdir(temp_dir)
16
17 cache_dir = option.cache_dir + '/go-build'
18
19 if not os.path.exists(cache_dir):
20 os.mkdir(cache_dir)
21
13 env = os.environ.copy()
14 env['GOPATH'] = option.current_dir + '/build/go'
22 env = os.environ.copy()
23 env['GOPATH'] = option.current_dir + '/build/go'
15 env['GOCACHE'] = option.cache_dir + '/go'
16 env['GO111MODULE'] = 'auto'
24 env['GOCACHE'] = cache_dir
17
25
26 shutil.copy2(
27 option.test_dir + '/go/' + script + '/' + name + '.go',
28 temp_dir)
29
18 if static:
19 args = [
20 'go',
21 'build',
22 '-tags',
23 'netgo',
24 '-ldflags',
25 '-extldflags "-static"',
26 '-o',
30 if static:
31 args = [
32 'go',
33 'build',
34 '-tags',
35 'netgo',
36 '-ldflags',
37 '-extldflags "-static"',
38 '-o',
27 option.temp_dir + '/go/' + name,
28 option.test_dir + '/go/' + script + '/' + name + '.go',
39 temp_dir + name,
40 temp_dir + name + '.go',
29 ]
30 else:
31 args = [
32 'go',
33 'build',
34 '-o',
41 ]
42 else:
43 args = [
44 'go',
45 'build',
46 '-o',
35 option.temp_dir + '/go/' + name,
36 option.test_dir + '/go/' + script + '/' + name + '.go',
47 temp_dir + name,
48 temp_dir + name + '.go',
37 ]
38
49 ]
50
51 replace_path = option.current_dir + '/build/go/src/unit.nginx.org/go'
52
53 with open(temp_dir + 'go.mod', 'w') as f:
54 f.write(
55 f"""module test/app
56require unit.nginx.org/go v0.0.0
57replace unit.nginx.org/go => {replace_path}
58""")
59
39 if option.detailed:
40 print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
41
42 try:
60 if option.detailed:
61 print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
62
63 try:
43 process = subprocess.run(args, env=env)
64 process = subprocess.run(args, env=env, cwd=temp_dir)
44
45 except KeyboardInterrupt:
46 raise
47
48 except subprocess.CalledProcessError:
49 return None
50
51 return process

--- 4 unchanged lines hidden (view full) ---

56 wdir = option.test_dir + "/go/" + script
57 executable = option.temp_dir + "/go/" + name
58
59 if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
60 wdir = "/go/"
61 executable = "/go/" + name
62 static_build = True
63
65
66 except KeyboardInterrupt:
67 raise
68
69 except subprocess.CalledProcessError:
70 return None
71
72 return process

--- 4 unchanged lines hidden (view full) ---

77 wdir = option.test_dir + "/go/" + script
78 executable = option.temp_dir + "/go/" + name
79
80 if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
81 wdir = "/go/"
82 executable = "/go/" + name
83 static_build = True
84
64 self.prepare_env(script, name, static=static_build)
85 TestApplicationGo.prepare_env(script, name, static=static_build)
65
66 conf = {
67 "listeners": {"*:7080": {"pass": "applications/" + script}},
68 "applications": {
69 script: {
70 "type": "external",
71 "processes": {"spare": 0},
72 "working_directory": wdir,
73 "executable": executable,
74 },
75 },
76 }
77
78 self._load_conf(conf, **kwargs)
86
87 conf = {
88 "listeners": {"*:7080": {"pass": "applications/" + script}},
89 "applications": {
90 script: {
91 "type": "external",
92 "processes": {"spare": 0},
93 "working_directory": wdir,
94 "executable": executable,
95 },
96 },
97 }
98
99 self._load_conf(conf, **kwargs)