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

Accommodations for running tests in containers that don't match the host OS #1834

Merged
merged 3 commits into from
Dec 16, 2024

Conversation

flyinghyrax
Copy link
Contributor

@flyinghyrax flyinghyrax commented Dec 15, 2024

I have been working on running the tests within a container to better match the CI environment and more easily test with multiple Python versions. I've kept my Docker/Docker Compose configuration in a sibling folder but needed to make a handful of changes to run the tests successfully.

  1. The tests didn't have a way to configure the endpoint URL used for S3 plugin tests, so I added a check for an environment variable to allow overriding localhost:9000 with something else.
  2. There was a newline handling issue with a filesystem storage plugin test that only occurs when sys.platform doesn't match the platform the project source files were checked out of source control on. My host is Windows, so a sample file had \r\n endings, but when running the test in a Linux container the test expects \n.
  3. I had problems getting pytest to discover test files, but adding some explicit file discovery settings fixed it.

Each of the above is in its own commit with some additional description in the commit log messages.

Since these changes only modify the development environment I'm not sure if they need a corresponding changelog entry.

@flyinghyrax flyinghyrax changed the title Accommodations for running tests in containers that don'accommodations Accommodations for running tests in containers that don't match the host OS Dec 15, 2024
Copy link

codecov bot commented Dec 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.15%. Comparing base (4d020e8) to head (8f03968).
Report is 182 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1834      +/-   ##
==========================================
+ Coverage   79.69%   86.15%   +6.45%     
==========================================
  Files          31       33       +2     
  Lines        4324     4384      +60     
  Branches      780      465     -315     
==========================================
+ Hits         3446     3777     +331     
+ Misses        721      428     -293     
- Partials      157      179      +22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

The S3 endpoint URL used in the unit tests was hard-coded to localhost.
When executing the tests in a container, a minio sibling container can't
be accessed via localhost. This allows setting the endpoint URL via an
environment variable, and adds 'BANDERSNATCH_*' to the environment
variable passthrough list for Tox.

...noticing that Tox was the reason the environment variable was being
ignored took a very long time.
Previously the test_get_hash test used shutil.copy to duplicate a sample file
from the project directory into a temp directory for the test.

If the project folder is cloned on Windows then bind-mounted into
a Linux container, with the test run in the container, the file
would have Windows line endings while the test would expect unix
line endings based on sys.platform within the container.

Instead of copying the file, this change writes the file from a string
value using Python's universal newlines support, so the written file
will have the same line ending as the platform executing the test.

This change trips mypy errors in the pre-commit checks. The errors show
when running mypy in the console or via pre-commit, but the MyPy plugin
for VS Code *doesn't* and will make the *opposite complaint* if you try
to change the file to fix the errors shown in pre-commit. I don't know
what to do about that.
Pytest would fail to discover any test files when run in a container
with the project folder mounted. Adding these helped, and doesn't
change anything when tests are run 'normally' as far as I can tell.

I know what each does, but I don't know why the problem was happening
in the container specifically, or why these fix it. This is a "web search
the error message" -> "find reasonable option on StackOverflow" fix.
@flyinghyrax flyinghyrax force-pushed the container-testing-accommodations branch from 2644fee to 8f03968 Compare December 15, 2024 00:35
Copy link
Contributor

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All seems sane. Thanks for making the simple PR and not putting it in with lots of changes to fix our async problems! Really appreciate that.

@cooperlees cooperlees added the skip news Skip CI check for news/changelog label Dec 16, 2024
@cooperlees cooperlees merged commit 27caae5 into pypa:main Dec 16, 2024
11 of 12 checks passed
@flyinghyrax flyinghyrax deleted the container-testing-accommodations branch December 24, 2024 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news Skip CI check for news/changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants