Skip to content

Commit

Permalink
Tests: using modules in Go.
Browse files Browse the repository at this point in the history
  • Loading branch information
mar0x committed Jan 10, 2022
1 parent 818a78d commit 1297e8a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 46 deletions.
3 changes: 2 additions & 1 deletion auto/modules/go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ install: ${NXT_GO}-install
${NXT_GO}:

${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:
install -d \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}
Expand Down
1 change: 1 addition & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module unit.nginx.org/go
4 changes: 1 addition & 3 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ def pytest_sessionstart(session):
# discover modules from check

option.available['modules']['openssl'] = check_openssl(unit['unitd'])
option.available['modules']['go'] = check_go(
option.current_dir, unit['temp_dir'], option.test_dir
)
option.available['modules']['go'] = check_go()
option.available['modules']['node'] = check_node(option.current_dir)
option.available['modules']['regex'] = check_regex(unit['unitd'])

Expand Down
43 changes: 32 additions & 11 deletions test/unit/applications/lang/go.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import os
import shutil
import subprocess

from unit.applications.proto import TestApplicationProto
from unit.option import option


class TestApplicationGo(TestApplicationProto):
def prepare_env(self, script, name, static=False):
if not os.path.exists(option.temp_dir + '/go'):
os.mkdir(option.temp_dir + '/go')
@staticmethod
def prepare_env(script, name='app', static=False):
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['GOPATH'] = option.current_dir + '/build/go'
env['GOCACHE'] = option.cache_dir + '/go'
env['GO111MODULE'] = 'auto'
env['GOCACHE'] = cache_dir

shutil.copy2(
option.test_dir + '/go/' + script + '/' + name + '.go',
temp_dir)

if static:
args = [
Expand All @@ -24,23 +36,32 @@ def prepare_env(self, script, name, static=False):
'-ldflags',
'-extldflags "-static"',
'-o',
option.temp_dir + '/go/' + name,
option.test_dir + '/go/' + script + '/' + name + '.go',
temp_dir + name,
temp_dir + name + '.go',
]
else:
args = [
'go',
'build',
'-o',
option.temp_dir + '/go/' + name,
option.test_dir + '/go/' + script + '/' + name + '.go',
temp_dir + name,
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:
print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))

try:
process = subprocess.run(args, env=env)
process = subprocess.run(args, env=env, cwd=temp_dir)

except KeyboardInterrupt:
raise
Expand All @@ -61,7 +82,7 @@ def load(self, script, name='app', **kwargs):
executable = "/go/" + name
static_build = True

self.prepare_env(script, name, static=static_build)
TestApplicationGo.prepare_env(script, name, static=static_build)

conf = {
"listeners": {"*:7080": {"pass": "applications/" + script}},
Expand Down
36 changes: 5 additions & 31 deletions test/unit/check/go.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
import os
import subprocess
from unit.applications.lang.go import TestApplicationGo


def check_go(current_dir, temp_dir, test_dir):
if not os.path.exists(temp_dir + '/go'):
os.mkdir(temp_dir + '/go')
def check_go():
process = TestApplicationGo.prepare_env('empty')

env = os.environ.copy()
env['GOPATH'] = current_dir + '/build/go'
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
if process != None and process.returncode == 0:
return True

0 comments on commit 1297e8a

Please sign in to comment.