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

fix(core): running testcontainer inside container #714

Merged

Conversation

CarliJoy
Copy link
Contributor

@CarliJoy CarliJoy commented Oct 11, 2024

closes #475

One step closer to solve #517

Replaces #622

According to the data collected this should fix issues running testcontainers inside a container in almost all cases.

There is still an issue when running it within docker desktop, that is probably easily solved which checking for the existence of host.docker.internal.

But I have to recollect the data to ensure this, so this will be added at later point in time, as with with setting -e TESTCONTAINERS_HOST_OVERRIDE=host.docker.internal an easy workaround exists as well.

@CarliJoy CarliJoy changed the title Fix running testcontainer inside container fix(core): running testcontainer inside container Oct 11, 2024
Copy link

codecov bot commented Oct 11, 2024

Codecov Report

Attention: Patch coverage is 95.83333% with 3 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@f958cf9). Learn more about missing BASE report.

Files with missing lines Patch % Lines
core/testcontainers/core/container.py 86.66% 1 Missing and 1 partial ⚠️
core/testcontainers/core/docker_client.py 96.15% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #714   +/-   ##
=======================================
  Coverage        ?   85.62%           
=======================================
  Files           ?       12           
  Lines           ?      654           
  Branches        ?      102           
=======================================
  Hits            ?      560           
  Misses          ?       72           
  Partials        ?       22           

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

@CarliJoy CarliJoy force-pushed the fix_running_inside_container branch 3 times, most recently from d80b3b1 to 341bd03 Compare October 18, 2024 20:53
required to finally figure out how github actions actually work
@CarliJoy
Copy link
Contributor Author

I am happy to announce that I was able to add some DinD and DooD directly into the test suite that work even within the github action here.

I think this is a good starting point to tackle more issues arising from running testonctainer within a container itself.

Following the discussion with @alexanderankin on #720 I am aware that testing non core thing inside the core test is not really wanted.
But I don't know of another method that wouldn't increase the running time by a multitude.

Comment on lines +12 to +19
def pytest_configure(config: pytest.Config) -> None:
"""
Add configuration for custom pytest markers.
"""
config.addinivalue_line(
"markers",
"inside_docker_check: test used to validate DinD/DooD are working as expected",
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this just for filtering in/out dind/dod tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is to filter out tests that run within the test_dind and test_dood tests.
Inside this tests now the docker container is build and executed in a dind and dood environment.
This way we have proper test that dind and dood actually works.

I left the old make test-dind (which is not correct because it's acutall DooD) untouched.
It would actually be better to remove it not only because of the wrong name.
It also doesn't succeed because some things as some auth tests seem to have troubles with TLS settings.
I don't think the complete test suit has to pass inside a dind/dood env.
In the end these test are integration tests and should be treated as such.

Copy link
Collaborator

Choose a reason for hiding this comment

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

lets remove the things that don't make sense

Copy link
Collaborator

Choose a reason for hiding this comment

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

perhaps in a separate PR maybe to make the history clearer

Copy link
Collaborator

@alexanderankin alexanderankin left a comment

Choose a reason for hiding this comment

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

This is great! I still have a couple things I want to figure out before merging because its a lot. in particular I don't like the breaking change with the returning int instead of string because if I'm not mixing it up there is a method somewhere in there that returns str but actually returns int or str but i dont want to do that without a version bump (4.9 or just v5).

@CarliJoy
Copy link
Contributor Author

CarliJoy commented Oct 23, 2024

in particular I don't like the breaking change with the returning int instead of string because if I'm not mixing it up there is a method somewhere in there that returns str but actually returns int or str but i dont want to do that without a version bump (4.9 or just v5).

To be frank, that doesn't matter as all, as long the packages is not marked as typed (#305), it is not considered by type checkers from end users anyway.
At least this is true for mypy.
A port can be only int so I am quite sure this is only a mistake/bug.
And I have seen other packages fix such mistakes/bug in typing without introducing a new major number.

Comment on lines 131 to +135
def get_container_host_ip(self) -> str:
# infer from docker host
host = self.get_docker_client().host()
if not host:
return "localhost"
# see https://github.com/testcontainers/testcontainers-python/issues/415
if host == "localnpipe" and system() == "Windows":
return "localhost"

# # check testcontainers itself runs inside docker container
# if inside_container() and not os.getenv("DOCKER_HOST") and not host.startswith("http://"):
# # If newly spawned container's gateway IP address from the docker
# # "bridge" network is equal to detected host address, we should use
# # container IP address, otherwise fall back to detected host
# # address. Even it's inside container, we need to double check,
# # because docker host might be set to docker:dind, usually in CI/CD environment
# gateway_ip = self.get_docker_client().gateway_ip(self._container.id)

# if gateway_ip == host:
# return self.get_docker_client().bridge_ip(self._container.id)
# return gateway_ip
return host
connection_mode: ConnectionMode
connection_mode = self.get_docker_client().get_connection_mode()
if connection_mode == ConnectionMode.docker_host:
return self.get_docker_client().host()
Copy link
Contributor Author

@CarliJoy CarliJoy Oct 23, 2024

Choose a reason for hiding this comment

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

Btw: one could argue that this is incorrect.

self.get_docker_client().host() does return a DNS name instead of an IP especially localhost.
Indeed for some containers this has a negative effect: I tried to create a MariaDB container that failed. Using localhost instead of 127.0.0.1 it was trying to connect through the unix socket.

But this is the old behaviour so I kept it the way it is.
We might want to change this in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah yes, as it says "container_ip" in the method name but it returns a domain name instead, hm.

@alexanderankin alexanderankin merged commit 85a6666 into testcontainers:main Oct 23, 2024
19 checks passed
alexanderankin pushed a commit that referenced this pull request Oct 24, 2024
they both have been replaced by proper `pytest` tests

follow up of #714
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.

Bug: in DinD, Since 4.0.0 postgres.get_connection_url() within another container gives broken connection url
2 participants