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

Question: how to form a URI with expose? #187

Closed
sajith opened this issue Jun 13, 2024 · 3 comments
Closed

Question: how to form a URI with expose? #187

sajith opened this issue Jun 13, 2024 · 3 comments

Comments

@sajith
Copy link

sajith commented Jun 13, 2024

I have an application that expects a MONGODB_CONNSTRING environment variable, and used a tox.ini like below:

[testenv]
deps =
    [test]

commands =
    pytest

setenv =
    MONGODB_CONNSTRING = mongodb://guest:guest@localhost:27017/

[docker:mongo]
image = mongo:7.0.11
 
ports =
    27017:27017/tcp

In the pre-5.0.0 days of tox-docker, the above tox.ini worked just fine in most cases. Now that ports is deprecated, I am having a hard time with the new expose thing. This does not work:

[testenv]
deps =
    [test]

commands =
    pytest

setenv:
    MONGODB_CONNSTRING = mongodb://guest:guest@localhost:{env:MONGO_PORT}/

[docker:mongo]
image = mongo:7.0.11

expose =
    MONGO_PORT=27017/tcp

The above does not work because tox expands MONGODB_CONNSTRING to mongodb://guest:guest@localhost:/. Tox does not raise an error here unlike what the documentation says.

How can I form a MONGODB_CONNSTRING here? Refactoring the application to use MONGO_PORT would be an inconvenience, since that code's "owner" is not me. Not everyone in the team uses tox-docker, so refactoring in that manner would be a hard-sell.

Going back to older versions of tox-docker is also an inconvenience, because of this error:

"docker.errors.DockerException: Error while fetching server API version: Not supported URL scheme http+docker"

The above is because of this docker-py issue. Now I have to pin requests at 2.31.0, which is a hassle, and may not be always possible.

@dcrosta
Copy link
Member

dcrosta commented Jun 13, 2024

Your best bet here is probably to write a wrapper script that sets the MONGODB_CONNSTRING env var before invoking your tests/your application. That wrapper can be the place that knows about tox-docker's env vars. Something like this might work:

#!/bin/bash

export MONGODB_CONNSTRING=mongodb://guest:guest@localhost:${MONGO_PORT}/
exec $@

@sajith
Copy link
Author

sajith commented Jun 14, 2024

@dcrosta Thank you. I prefer going down the refactoring route, I think. That feels less "workaround-y", for lack of a better word. Please feel free to close this issue if you need to. :-)

Do you think tox not expanding {env:MONGO_PORT} in the above instance is an issue that should be reported? Perhaps it is reading comprehension failure from my part, but from the documentation it is not clear to me that environment variable substitution is not supposed to work under setenv. In fact setenv's description links directly to the notes about environment variable substitutions.

sajith added a commit to atlanticwave-sdx/sdx-controller that referenced this issue Jun 14, 2024
tox-docker 5.0.0 has deprecated `ports`; we need to use `expose`
instead.  This is a good change; with `expose` the services we use
when testing should not conflict with any pre-existing services.

We could have pinned to an older version of tox-docker, but older
tox-docker sometimes errors out if the right version of
requests (<=2.31.0) is not available.

docker/docker-py#3256

A fallout of this change is that we can't use MONGODB_CONNSTRING
anymore, since forming a URI using environment variables from tox
appears to be rather awkward.

tox-dev/tox-docker#187
@dcrosta
Copy link
Member

dcrosta commented Jun 14, 2024

I agree with your reading of the docs, and that does seem like a bug, yeah. I'd report it to https://github.com/tox-dev/tox

@dcrosta dcrosta closed this as completed Jun 14, 2024
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

No branches or pull requests

2 participants