Deleted Added
1import os
2import shutil
3import subprocess
4
5from unit.applications.proto import TestApplicationProto
6from unit.option import option
7
8
9class TestApplicationGo(TestApplicationProto):
10 @staticmethod
11 def prepare_env(script, name='app', static=False):
12 temp_dir = option.temp_dir + '/go/'
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
22 env = os.environ.copy()
23 env['GOPATH'] = option.current_dir + '/build/go'
24 env['GOCACHE'] = cache_dir
25
26 shutil.copy2(
27 option.test_dir + '/go/' + script + '/' + name + '.go',
28 temp_dir)
29
30 if static:
31 args = [
32 'go',
33 'build',
34 '-tags',
35 'netgo',
36 '-ldflags',
37 '-extldflags "-static"',
38 '-o',
39 temp_dir + name,
40 temp_dir + name + '.go',
41 ]
42 else:
43 args = [
44 'go',
45 'build',
46 '-o',
47 temp_dir + name,
48 temp_dir + name + '.go',
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
60 if option.detailed:
61 print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
62
63 try:
64 process = subprocess.run(args, env=env, cwd=temp_dir)
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
85 TestApplicationGo.prepare_env(script, name, static=static_build)
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)