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

Only read env variables on __init__ #157

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions nomad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def __init__( # pylint: disable=too-many-arguments
host: str = "127.0.0.1",
secure: bool = False,
port: int = 4646,
address: Optional[str] = os.getenv("NOMAD_ADDR", None),
address: Optional[str] = None,
user_agent: Optional[str] = None,
namespace: Optional[str] = os.getenv("NOMAD_NAMESPACE", None),
token: Optional[str] = os.getenv("NOMAD_TOKEN", None),
namespace: Optional[str] = None,
token: Optional[str] = None,
timeout: int = 5,
region: Optional[str] = os.getenv("NOMAD_REGION", None),
region: Optional[str] = None,
version: str = "v1",
verify: bool = False,
cert: tuple = (os.getenv("NOMAD_CLIENT_CERT", None), os.getenv("NOMAD_CLIENT_KEY", None)),
cert: tuple = (),
session: requests.Session = None,
):
"""Nomad api client
Expand Down Expand Up @@ -57,19 +57,21 @@ def __init__( # pylint: disable=too-many-arguments
- nomad.api.exceptions.URLNotFoundNomadException
- nomad.api.exceptions.URLNotAuthorizedNomadException
"""
cert = cert or (os.getenv("NOMAD_CLIENT_CERT", None), os.getenv("NOMAD_CLIENT_KEY", None))

self.host = host
self.secure = secure
self.port = port
self.address = address
self.address = address or os.getenv("NOMAD_ADDR", None)
self.user_agent = user_agent
self.region = region
self.region = region or os.getenv("NOMAD_REGION", None)
self.timeout = timeout
self.version = version
self.token = token
self.token = token or os.getenv("NOMAD_TOKEN", None)
self.verify = verify
self.cert = cert if all(cert) else ()
self.session = session
self.__namespace = namespace
self.__namespace = namespace or os.getenv("NOMAD_NAMESPACE", None)

self.requester_settings = {
"address": self.address,
Expand Down
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if [ "${1-}" == "init" ]; then
source .venv/bin/activate
fi

NOMAD_VERSION=`nomad --version | awk '{print $2}' | cut -c2-`
NOMAD_VERSION=`nomad --version | awk 'NR==1{print $2}' | cut -c2-`

echo "Run Nomad in dev mode"
nomad agent -dev -node pynomad1 --acl-enabled &> nomad.log &
Expand Down
7 changes: 7 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,10 @@ def test_use_custom_user_agent():
n.jobs.get_jobs()

assert responses.calls[0].request.headers["User-Agent"] == custom_agent_name

def test_env_variables():
# This ensures that the env variables are only read upon initialization of Nomad() instance,
# and not before.
with mock.patch.dict(os.environ, {"NOMAD_ADDR": "https://foo"}):
n = nomad.Nomad()
assert n.address == os.environ["NOMAD_ADDR"]