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

vdk-core: lowercase env variables are inferred as configuration #751

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2021 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import logging
import os
import pathlib
from os import getenv
from typing import Optional
Expand Down Expand Up @@ -131,8 +132,10 @@ def vdk_configure(self, config_builder: ConfigurationBuilder) -> None:
log.debug(
f"Founds config keys: {config_keys}. Will check if environment variable is set for any"
)
upper_cased_env = {k.upper(): v for k, v in os.environ.items()}
for key in config_keys:
value = getenv(VDK_ + key.replace(".", "_").upper())
normalized_key = VDK_ + key.replace(".", "_").upper()
value = upper_cased_env.get(normalized_key)
if value is not None:
log.debug(f"Found environment variable for key {key}")
config_builder.set_value(key, value)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2021 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
from vdk.api.job_input import IJobInput


def run(job_input: IJobInput):
print("hi")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[vdk]
attempt_id=attempt-id-from-config-ini
other_config=other-config-from-config-ini
33 changes: 33 additions & 0 deletions projects/vdk-core/tests/functional/run/test_run_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,36 @@ def run_job(self, context: JobContext) -> Optional[ExecutionResult]:
result: Result = runner.invoke(["-v", "INFO", "run", util.job_path("simple-job")])
cli_assert_equal(0, result)
assert debug_log_message not in result.output


def test_run_config_variables_are_set():
class DebugConfigLog:
def __init__(self):
self.config = None

@hookimpl(tryfirst=True)
def run_job(self, context: JobContext) -> Optional[ExecutionResult]:
self.config = context.core_context.configuration
return None # continue with next hook impl.

with mock.patch.dict(
os.environ,
{
"VDK_ATTEMPT_ID": "env-attempt-id-overrides-config-ini",
"vdk_execution_id": "lower-case-env-exec-id",
},
):
config_log = DebugConfigLog()
runner = CliEntryBasedTestRunner(config_log)
result: Result = runner.invoke(["run", util.job_path("job-with-config-ini")])
cli_assert_equal(0, result)

assert (
config_log.config.get_value("attempt_id")
== "env-attempt-id-overrides-config-ini"
)
assert config_log.config.get_value("execution_id") == "lower-case-env-exec-id"
assert (
config_log.config.get_value("other_config")
== "other-config-from-config-ini"
)