Skip to content

Commit

Permalink
Only read env variables on __init__ (#157)
Browse files Browse the repository at this point in the history
* run_tests.sh: Handle multi-line nomad --version output

* Nomad(): Evaluate env variables on __init__() call, not before

---------

Co-authored-by: Damien Couture <dcouture@veritionfund.com>
  • Loading branch information
atwam and Damien Couture authored Apr 22, 2024
1 parent 7239fec commit 35556fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
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"]

0 comments on commit 35556fa

Please sign in to comment.