kaizendorks / pymongo_inmemory

A mongo mocking library with an ephemeral MongoDB running in memory.
MIT License
40 stars 13 forks source link

Attempts to connect to instance running on localhost:27017 #35

Closed ItsKarma closed 3 years ago

ItsKarma commented 3 years ago

Describe the bug If running a live mongodb instance at localhost:27017, pymongo_inmemory attempts to connects to it and fails operations.

To Reproduce Steps to reproduce the behavior:

  1. Run mongo in a docker container port mapping 27017: docker run -itd -p 27017:27017 -v db-data:/data/db mongo:4
  2. Run any test that should connect to pymongo_inmemory.
  3. See connection to docker container running at localhost:27017 (from docker logs)
    {"t":{"$date":"2021-02-21T02:17:01.379+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.17.0.1:63446","connectionId":7,"connectionCount":3}}
  4. Run mongo in a docker container port mapping 27018: docker run -itd -p 27018:27017 -v db-data:/data/db mongo:4
  5. Run any test that should connect to pymongo_inmemory.
  6. Tests successfully connect to pymongo_inmemory, docker logs are empty besides init.

Expected behavior I would expect pytest to connect to pymongo_inmemory instead of the running mongo docker container. I suspect the port checking that pymongo_inmemory does, is incompatible with Docker, or is simply not working. I have not tested this running mongo locally outside of a docker container, but that is not applicable to my use case.

This does not block me from using pymongo_inmemory, but is inconvenient. I either need to run mongo on a non-default port, or I have to shut down the running container which I use for active development, prior to running tests.

Logs I don't think there are any logs to provide.

Screenshots N/A

Context:

ItsKarma commented 3 years ago

I am running pytest inside a docker container. I was originally running my tests against the live mongo container. This live container was a part of a different docker "network" so my pytest container did not have direct access via the docker network. To get around this I was adding --network="host" to my pytest container'sdocker run command. I believe this caused pymongo_inmemory to not see that my host was using port: 27017, because within the pytest container where pymongo_inmemory was running, there was no port 27017. However, when attempting to connect to port 27017, because of the --network=host flag, it was actually connecting to the host port 27017 which was the live mongo container.

To resolve this, I removed the --network=host flag, as I no longer need to reach out to the live mongo container when using pymongo_inmemory.

Thanks for the great project!

ekarademir commented 3 years ago

Hi Matt,

Thanks for using pymongo_inmemory!

Even though you have closed your issue, I'll have a look and see if we can provide a better solution.

Best,

Ertugrul