Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log output is not displayed when using a scope other than 'function' #29

Open
RmStorm opened this issue Aug 26, 2019 · 3 comments
Open
Labels
bug Something isn't working

Comments

@RmStorm
Copy link
Contributor

RmStorm commented Aug 26, 2019

If a user is running multiple tests against one fixture such as a module or class scoped conatiner fixture and the --verbose flag is used the logs are not necessarily displayed! It seems like they are only displayed when the last test to be run with the fixture fails, triggering the printing of the logs.

I think this behaviour should be documented or solved in some way. @todofixthis what do you think? I stumbled over this issue while testing the behaviour in #28. Otherwise it is not related to that PR since this 'bug' has been there since introducing the dynamically scoped fixture.

@todofixthis todofixthis added the bug Something isn't working label Aug 26, 2019
@todofixthis
Copy link
Member

todofixthis commented Aug 26, 2019

🤔 aye, that's troubling. I wonder if there's a clean way to address this — can it output only the logs that were generated during a particular test?

I wonder if we can achieve this by providing a since or tail kwarg to Container.logs()?

https://github.com/docker/docker-py/blob/master/docker/models/containers.py#L286-L290

@RmStorm
Copy link
Contributor Author

RmStorm commented Aug 26, 2019

'since' is already used to only display the logs since the start of the fixture. I think the issue is that the parts of the code before the yield are run once before the first test using the fixture and the parts behind yield are run once only after the final test using the fixture has finished.

Instead of doing the logging in the fixture after yield I think we need to look into using different hooks supplied by pytest. But I'm not sure what this would exactly look like.

@evandrocoan
Copy link

evandrocoan commented Oct 3, 2021

I would like to see the logs in realtime, instead of only after the container stopped. Then, and I adding a dedicated thread to print logs in realtime:

@pytest.fixture(scope="function")
def fixed_function_scoped_container_getter(function_scoped_container_getter, request):
    def callback(service_name):
        container = function_scoped_container_getter.get(service_name)
        if request.config.getoption("--verbose"):
            def threaded_function():
                for event in container.logs(stream=True):
                    print(event.decode("utf-8", errors='replace'), end='')
            thread = threading.Thread(target=threaded_function, daemon=True)
            thread.start()
        return container
    return callback

I am removing out the code this code because it would print out the log doubled:

if request.config.getoption("--verbose"):
for container in sorted(containers, key=lambda c: c.name):
header = "Logs from {name}:".format(name=container.name)
print(header, '\n', "=" * len(header))
print(container.logs(since=now).decode("utf-8", errors="replace")
or "(no logs)", '\n')

For this case log output is not displayed when using a scope other than 'function', if a dedicated thread was running, printing the log as I did, it should solve the problem and it would bring the comfort of seeing the log in realtime.

Related to docker logging: docker/compose#2227 - Improvements to docker-compose logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants