diff --git a/nomad/__init__.py b/nomad/__init__.py index fdb4f16..2371b61 100644 --- a/nomad/__init__.py +++ b/nomad/__init__.py @@ -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 @@ -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, diff --git a/run_tests.sh b/run_tests.sh index 0318587..eea22cf 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -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 & diff --git a/tests/test_base.py b/tests/test_base.py index d1501a0..df8f44a 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -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"]