How to get uwsgi to exit with the return code of any failed subprocess

I am writing some integration tests that use a Python application running uwsgi.
To test an aspect of this, I run the uwsgi queue manager, which requires the master process to run .
If pytest has a failed test, it returns a non-zero exit code, which is great.
Without the main process, the entire uwsgi process also returns this exit code, so our continuous integration server responds accordingly.
However, when the main process is started, it always exits with a zero exit code - regardless of failed tests.

I need it to pass the first non-zero subprocess exit code, if any.

Note. I am not interested in mocking this - I need to test this work.

I created a Dockerized Minimal, Complete and Verified Example that illustrates my problem:

Dockerfile:

FROM python:3.6.4-slim-stretch

WORKDIR /srv

RUN apt-get update \
    && apt-get install -y build-essential \
    && pip install uwsgi pytest

COPY test_app.py /srv/

CMD ['/bin/bash']

test_app.py:

import pytest

def test_this():
    assert 1==0

Given the above 2 files in the directory, the return code is shown below if I run this test failure under uwsgi without the main process:

$ docker build -t=test .
$ docker run test uwsgi --chdir /srv --pyrun /usr/local/bin/pytest
...
============================= test session starts ==============================
platform linux -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /srv, inifile:
collected 1 item

test_app.py F                                                            [100%]

=================================== FAILURES ===================================
__________________________________ test_this ___________________________________

    def test_this():
>       assert 1==0
E       assert 1 == 0

test_app.py:4: AssertionError
=========================== 1 failed in 0.05 seconds ===========================
$ echo $?
1

Note: you can see that the return code from this process (last line) is not zero


Now, changing nothing but starting uwsgi with the main process, we get the following result:

$ docker run test uwsgi --set master=true --chdir /srv --pyrun /usr/local/bin/pytest
...
============================= test session starts ==============================
platform linux -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /srv, inifile:
collected 1 item

test_app.py F                                                            [100%]

=================================== FAILURES ===================================
__________________________________ test_this ___________________________________

    def test_this():
>       assert 1==0
E       assert 1 == 0

test_app.py:4: AssertionError
=========================== 1 failed in 0.05 seconds ===========================
worker 1 buried after 0 seconds
goodbye to uWSGI.
$ echo $?
0

Note: this time, the return code from this process (last line) is zero - even if the test failed

How can I get uwsgi to forward the exit code from the failure process to the wizard?

+4
source share
1

, . , .

( Docker):

Dockerfile

FROM python:3.6.4-slim-stretch

WORKDIR /srv

RUN apt-get update \
    && apt-get install -y build-essential \
    && pip install uwsgi pytest

COPY test_app.py test run_tests.py /srv/

CMD ['/bin/bash']

#!/bin/bash
uwsgi --set master=true --chdir /srv --pyrun /srv/run_tests.py
exit $(cat /tmp/test_results)

run_tests.py

#!/usr/bin/python

import re
import subprocess
import sys

from pytest import main


def write_result(retcode):

    path = r'/tmp/test_results'
    with open(path, 'w') as f:
        f.write(str(retcode))


def run():

    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    retcode = 1
    try:
        retcode = main()
    finally:
        write_result(retcode)

    sys.exit(retcode)


if __name__ == '__main__':
    run()

, pytest run_tests.py, . bash script: test, uwsgi, , script .

:

$ docker build -t=test .
$ docker run test /srv/test
...
============================= test session starts ==============================
platform linux -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /srv, inifile:
collected 1 item

test_app.py F                                                            [100%]

=================================== FAILURES ===================================
__________________________________ test_this ___________________________________

    def test_this():
>       assert 1==0
E       assert 1 == 0

test_app.py:4: AssertionError
=========================== 1 failed in 0.05 seconds ===========================
worker 1 buried after 0 seconds
goodbye to uWSGI.
$ echo $?
1
0

Source: https://habr.com/ru/post/1692114/


All Articles