diff options
author | Max Romanov <max.romanov@nginx.com> | 2022-01-10 16:07:31 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@nginx.com> | 2022-01-10 16:07:31 +0300 |
commit | 1297e8a16a2fb0ccc195c0dd14b678bb2d7fc9fc (patch) | |
tree | a1ea5a3eb322bd8f3e8c6f222dcb7c076e11339c | |
parent | 818a78d82cd9aeb6c7429ef97cd1f39f9053b909 (diff) | |
download | unit-1297e8a16a2fb0ccc195c0dd14b678bb2d7fc9fc.tar.gz unit-1297e8a16a2fb0ccc195c0dd14b678bb2d7fc9fc.tar.bz2 |
Tests: using modules in Go.
Diffstat (limited to '')
-rw-r--r-- | auto/modules/go | 3 | ||||
-rw-r--r-- | go/go.mod | 1 | ||||
-rw-r--r-- | test/conftest.py | 4 | ||||
-rw-r--r-- | test/unit/applications/lang/go.py | 43 | ||||
-rw-r--r-- | test/unit/check/go.py | 36 |
5 files changed, 41 insertions, 46 deletions
diff --git a/auto/modules/go b/auto/modules/go index 7324ffbe..a8596bf3 100644 --- a/auto/modules/go +++ b/auto/modules/go @@ -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} diff --git a/go/go.mod b/go/go.mod new file mode 100644 index 00000000..2c0431db --- /dev/null +++ b/go/go.mod @@ -0,0 +1 @@ +module unit.nginx.org/go diff --git a/test/conftest.py b/test/conftest.py index 689c857a..966bb8f5 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -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']) diff --git a/test/unit/applications/lang/go.py b/test/unit/applications/lang/go.py index 367059e6..67501e9c 100644 --- a/test/unit/applications/lang/go.py +++ b/test/unit/applications/lang/go.py @@ -1,4 +1,5 @@ import os +import shutil import subprocess from unit.applications.proto import TestApplicationProto @@ -6,14 +7,25 @@ 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 = [ @@ -24,23 +36,32 @@ class TestApplicationGo(TestApplicationProto): '-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 @@ -61,7 +82,7 @@ class TestApplicationGo(TestApplicationProto): 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}}, diff --git a/test/unit/check/go.py b/test/unit/check/go.py index cc17f0fe..3d9d13e7 100644 --- a/test/unit/check/go.py +++ b/test/unit/check/go.py @@ -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 |