Tests: using modules in Go.

This commit is contained in:
Max Romanov
2022-01-10 16:07:31 +03:00
parent 818a78d82c
commit 1297e8a16a
5 changed files with 41 additions and 46 deletions

View File

@@ -111,7 +111,8 @@ install: ${NXT_GO}-install
${NXT_GO}: ${NXT_GO}:
${NXT_GO}-install: ${NXT_GO}-install-src ${NXT_GO}-install-env ${NXT_GO}-install: ${NXT_GO}-install-src ${NXT_GO}-install-env
GOPATH=\$(DESTDIR)\$(GOPATH) GO111MODULE=auto ${NXT_GO} build ${NXT_GO_PKG} cd \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG} && \
GOPATH=\$(DESTDIR)\$(GOPATH) ${NXT_GO} build ${NXT_GO_PKG}
${NXT_GO}-install-src: ${NXT_GO}-install-src:
install -d \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG} install -d \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}

1
go/go.mod Normal file
View File

@@ -0,0 +1 @@
module unit.nginx.org/go

View File

@@ -203,9 +203,7 @@ def pytest_sessionstart(session):
# discover modules from check # discover modules from check
option.available['modules']['openssl'] = check_openssl(unit['unitd']) option.available['modules']['openssl'] = check_openssl(unit['unitd'])
option.available['modules']['go'] = check_go( option.available['modules']['go'] = check_go()
option.current_dir, unit['temp_dir'], option.test_dir
)
option.available['modules']['node'] = check_node(option.current_dir) option.available['modules']['node'] = check_node(option.current_dir)
option.available['modules']['regex'] = check_regex(unit['unitd']) option.available['modules']['regex'] = check_regex(unit['unitd'])

View File

@@ -1,4 +1,5 @@
import os import os
import shutil
import subprocess import subprocess
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
@@ -6,14 +7,25 @@ from unit.option import option
class TestApplicationGo(TestApplicationProto): class TestApplicationGo(TestApplicationProto):
def prepare_env(self, script, name, static=False): @staticmethod
if not os.path.exists(option.temp_dir + '/go'): def prepare_env(script, name='app', static=False):
os.mkdir(option.temp_dir + '/go') temp_dir = option.temp_dir + '/go/'
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
cache_dir = option.cache_dir + '/go-build'
if not os.path.exists(cache_dir):
os.mkdir(cache_dir)
env = os.environ.copy() env = os.environ.copy()
env['GOPATH'] = option.current_dir + '/build/go' env['GOPATH'] = option.current_dir + '/build/go'
env['GOCACHE'] = option.cache_dir + '/go' env['GOCACHE'] = cache_dir
env['GO111MODULE'] = 'auto'
shutil.copy2(
option.test_dir + '/go/' + script + '/' + name + '.go',
temp_dir)
if static: if static:
args = [ args = [
@@ -24,23 +36,32 @@ class TestApplicationGo(TestApplicationProto):
'-ldflags', '-ldflags',
'-extldflags "-static"', '-extldflags "-static"',
'-o', '-o',
option.temp_dir + '/go/' + name, temp_dir + name,
option.test_dir + '/go/' + script + '/' + name + '.go', temp_dir + name + '.go',
] ]
else: else:
args = [ args = [
'go', 'go',
'build', 'build',
'-o', '-o',
option.temp_dir + '/go/' + name, temp_dir + name,
option.test_dir + '/go/' + script + '/' + name + '.go', temp_dir + name + '.go',
] ]
replace_path = option.current_dir + '/build/go/src/unit.nginx.org/go'
with open(temp_dir + 'go.mod', 'w') as f:
f.write(
f"""module test/app
require unit.nginx.org/go v0.0.0
replace unit.nginx.org/go => {replace_path}
""")
if option.detailed: if option.detailed:
print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args)) print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
try: try:
process = subprocess.run(args, env=env) process = subprocess.run(args, env=env, cwd=temp_dir)
except KeyboardInterrupt: except KeyboardInterrupt:
raise raise
@@ -61,7 +82,7 @@ class TestApplicationGo(TestApplicationProto):
executable = "/go/" + name executable = "/go/" + name
static_build = True static_build = True
self.prepare_env(script, name, static=static_build) TestApplicationGo.prepare_env(script, name, static=static_build)
conf = { conf = {
"listeners": {"*:7080": {"pass": "applications/" + script}}, "listeners": {"*:7080": {"pass": "applications/" + script}},

View File

@@ -1,34 +1,8 @@
import os from unit.applications.lang.go import TestApplicationGo
import subprocess
def check_go(current_dir, temp_dir, test_dir): def check_go():
if not os.path.exists(temp_dir + '/go'): process = TestApplicationGo.prepare_env('empty')
os.mkdir(temp_dir + '/go')
env = os.environ.copy() if process != None and process.returncode == 0:
env['GOPATH'] = current_dir + '/build/go' return True
env['GO111MODULE'] = 'auto'
try:
process = subprocess.run(
[
'go',
'build',
'-o',
temp_dir + '/go/app',
test_dir + '/go/empty/app.go',
],
env=env,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
)
if process.returncode == 0:
return True
except KeyboardInterrupt:
raise
except subprocess.CalledProcessError:
return None