From e1086e6b073195056f29c9c3944cde4bb66bdd0e Mon Sep 17 00:00:00 2001 From: Julio Date: Wed, 15 Apr 2020 04:56:25 +0200 Subject: [PATCH 1/4] Prevent autover from failing when used alongside async frameworks --- .travis.yml | 2 +- autover/version.py | 2 +- conda.recipe/meta.yaml | 1 + examples/PkgBundle/version.py | 2 +- examples/pkg_bundle/version.py | 2 +- tests/test_asgi_server.py | 6 ++++++ tox.ini | 8 ++++++-- 7 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 tests/test_asgi_server.py diff --git a/.travis.yml b/.travis.yml index 9b778771..182def1c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,7 +44,7 @@ jobs: - stage: test python: 3.6 - install: pip install tox + install: pip install tox gunicorn uvicorn starlette script: tox -e py${TRAVIS_PYTHON_VERSION//./} - stage: test diff --git a/autover/version.py b/autover/version.py index f367defc..d57663ff 100644 --- a/autover/version.py +++ b/autover/version.py @@ -117,7 +117,7 @@ def run_cmd(args, cwd=None): cwd=cwd) output, error = (str(s.decode()).strip() for s in proc.communicate()) - if proc.returncode != 0: + if proc.returncode != 0 or len(error) > 0: raise Exception(proc.returncode, error) return output diff --git a/conda.recipe/meta.yaml b/conda.recipe/meta.yaml index 9ceba99b..853fa2e0 100644 --- a/conda.recipe/meta.yaml +++ b/conda.recipe/meta.yaml @@ -26,6 +26,7 @@ test: source_files: - tests commands: + - conda install -y -c conda-forge gunicorn uvicorn starlette - nosetests -vv --nologcapture about: diff --git a/examples/PkgBundle/version.py b/examples/PkgBundle/version.py index f367defc..d57663ff 100644 --- a/examples/PkgBundle/version.py +++ b/examples/PkgBundle/version.py @@ -117,7 +117,7 @@ def run_cmd(args, cwd=None): cwd=cwd) output, error = (str(s.decode()).strip() for s in proc.communicate()) - if proc.returncode != 0: + if proc.returncode != 0 or len(error) > 0: raise Exception(proc.returncode, error) return output diff --git a/examples/pkg_bundle/version.py b/examples/pkg_bundle/version.py index f367defc..d57663ff 100644 --- a/examples/pkg_bundle/version.py +++ b/examples/pkg_bundle/version.py @@ -117,7 +117,7 @@ def run_cmd(args, cwd=None): cwd=cwd) output, error = (str(s.decode()).strip() for s in proc.communicate()) - if proc.returncode != 0: + if proc.returncode != 0 or len(error) > 0: raise Exception(proc.returncode, error) return output diff --git a/tests/test_asgi_server.py b/tests/test_asgi_server.py new file mode 100644 index 00000000..dbc313f5 --- /dev/null +++ b/tests/test_asgi_server.py @@ -0,0 +1,6 @@ +import autover +from starlette.applications import Starlette + +print(autover.__version__) + +app = Starlette() diff --git a/tox.ini b/tox.ini index 1d98793e..641c62b4 100644 --- a/tox.ini +++ b/tox.ini @@ -3,8 +3,12 @@ envlist = py36,py27 [testenv] passenv = GIT_VERSION -deps = nose -commands = nosetests -vv --nologcapture --with-doctest --with-coverage --cover-package=autover +deps = + nose + gunicorn;python_version>"3.4" + uvicorn;python_version>"3.4" + starlette;python_version>"3.4" +commands = nosetests -vv --nologcapture --with-doctest --with-coverage --cover-package=autover --ignore-files=test_asgi_server.py [testenv:lint_checks] deps = flake8 From d0a6733af3f75dd1a5f26ef80ff6d75dcf4e1fd0 Mon Sep 17 00:00:00 2001 From: Julio Date: Wed, 15 Apr 2020 04:58:17 +0200 Subject: [PATCH 2/4] Added test for async server --- tests/testversion.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/testversion.py b/tests/testversion.py index 8af07e68..2f6a46c9 100644 --- a/tests/testversion.py +++ b/tests/testversion.py @@ -1,7 +1,11 @@ """ Unit test for autover.Version """ +import os import unittest +import subprocess +import sys +import time import pkg_resources._vendor.packaging.version as packaging_version from autover import Version @@ -75,6 +79,7 @@ def pep440(version): class TestVersion(unittest.TestCase): + maxDiff=None def git_describe_check(self, describe_tests, index): @@ -160,6 +165,31 @@ def test_git_describe_4(self): def test_git_describe_5(self): self.git_describe_check(describe_tests, 5) + #=======================================# + # Compatibility with ASGI applications # + #=======================================# + + def test_run_asgi_server(self): + if sys.version[0] == "3": + serverfile_dir = os.path.abspath(os.path.join(__file__, os.pardir)) + logfile_path = os.path.abspath(os.path.join(__file__, os.pardir, "stderr_gunicorn.log")) + subprocess.Popen(["gunicorn", "-D", "--log-level", "ERROR", + "--chdir", serverfile_dir, + "-k", "uvicorn.workers.UvicornWorker", + "--error-logfile", logfile_path, + "--bind", "unix:/tmp/gunicorn.sock", + "test_asgi_server:app"]) + time.sleep(5) + with open(logfile_path, "r") as gunicorn_log_file: + stderr_log = gunicorn_log_file.read() + + subprocess.Popen(["pkill", "-f", "gunicorn"]) + time.sleep(5) + os.remove(logfile_path) + else: + stderr_log = "" + self.assertEqual(stderr_log, "") + if __name__ == "__main__": import nose nose.runmodule() From fae9e7d07c548084fbdcaf8976ba2214c1a34190 Mon Sep 17 00:00:00 2001 From: Julio Date: Wed, 15 Apr 2020 13:12:20 +0200 Subject: [PATCH 3/4] Removed dependencies installation from .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 182def1c..9b778771 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,7 +44,7 @@ jobs: - stage: test python: 3.6 - install: pip install tox gunicorn uvicorn starlette + install: pip install tox script: tox -e py${TRAVIS_PYTHON_VERSION//./} - stage: test From 1176fdf1c93aba490494d812a610d7f9ec23dae7 Mon Sep 17 00:00:00 2001 From: Julio Date: Wed, 15 Apr 2020 18:17:20 +0200 Subject: [PATCH 4/4] Better documentation for new test --- tests/test_asgi_server.py | 20 +++++++++++++++++++- tests/testversion.py | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_asgi_server.py b/tests/test_asgi_server.py index dbc313f5..0c8599fa 100644 --- a/tests/test_asgi_server.py +++ b/tests/test_asgi_server.py @@ -1,6 +1,24 @@ import autover from starlette.applications import Starlette -print(autover.__version__) +# A simple ASGI server application +# that tries to make use of autover. +# +# Given that autover makes use of +# subprocess.Popen and the program +# workflow depends on that, there +# were some corner cases where +# autover would just crash if we +# tried to get its __version__, +# therefore crashing the whole +# ASGI application. +# +# This test application is used +# with Gunicorn in the unit tests +# (testversion.py), just to check +# if the ASGI application crashes +# or not. + +_ = autover.__version__ app = Starlette() diff --git a/tests/testversion.py b/tests/testversion.py index 2f6a46c9..5118eb6a 100644 --- a/tests/testversion.py +++ b/tests/testversion.py @@ -170,6 +170,7 @@ def test_git_describe_5(self): #=======================================# def test_run_asgi_server(self): + """test_run_asgi_server (An ASGI app that uses autover and is launched with Gunicorn should not crash)""" if sys.version[0] == "3": serverfile_dir = os.path.abspath(os.path.join(__file__, os.pardir)) logfile_path = os.path.abspath(os.path.join(__file__, os.pardir, "stderr_gunicorn.log"))