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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Fix pytest container networking issues #1856

Merged

Conversation

polarathene
Copy link
Contributor

This PR contributes fixes I described here when investigating test failures from running pytest via the supported Docker container method (my preference).

I've scoped changes out to individual commits as usual, with commit messages adding context. The final commit does a little housekeeping with white-space so it might be a cleaner diff if you disable white-space when viewing.


Roughly the changes are:

  • Proper detection of running pytest via container (pytest.sh) by using ENV instead of the deprecated internal file check that Docker no longer supports. We only need it for running tests and control the pytest Dockerfile, seems the right way to approach it 馃憤
  • Supporting host networking mode instead of bridged networks. This was mostly for troubleshooting, previously it failed as the early setup removes all running containers unless they match the pytest hostname with the container ID, however host network mode will use the hosts $HOSTNAME / /etc/hostname, not the containers ID, thus the pytest container would accidentally remove itself. We now match by setting a container name in pytest.sh to compare against instead.
  • By default the pytest container should connect to other container networks (specifically the nginx-proxy one, typically a bridge network with name based on the docker-compose file name). This wasn't happening because of a missing arg to request networks with their associated containers, and once that was corrected, several other parts were found to not be working properly, such as avoiding connecting to a network that was already connected, or avoiding none network (different from passing None).

Behaviour of the pytest container is now at parity with running it locally on the host.

Fixes #1615

The original `/.dockerenv` approach is no longer valid, and context wise we're only using this for the test suite, so using an ENV in that container is a better solution.
DRY and clearer that we're referring to the pytest container.
When the container runs with host networking instead of the default bridge, the `$HOSTNAME` / `/etc/hostname` reflects that of the host instead of the container ID , which causes the pytest container to get removed accidentally.

Using a container name instead we can more reliably target the container to avoid removing it, should we need to run with host networking instead.
This is not compatible or required, since host networking is no longer isolated to container networks only.
The `network` object would never be in a list of network names (strings), and without `greedy=True` arg as the `docker-py` API docs note, the containers will not be part of the results, thus always returning an empty list which was not intended..

Now the network will properly match the current networks for pytest container, avoiding duplicate connect attempts, and the network list result will actually have containers to count when filtering by length.
A test on raw IP addresses doesn't reach the existing IPv6 skip logic, added that to avoid a test failing when only IPv4 is available (eg: standard docker container networks).

Additionally some other tests set the `none` network and connecting to this fails as it's not allowed? Preventing that from happening resolves the final failing tests within containerized pytest.
Noticed some trailing white-space. Removed for consistency with the rest of the file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WSL: Trying to run the unit tests locally, many fail (very slowly) because connections time out.
2 participants