-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-core: detect non vdk_ prefixed environment values for config (#874)
* vdk-core: detect non vdk_ prefixed environment values for config Currently user need to set VDK_ + config name as environment variable so VDK can recognize it. The idea was to avoid conflicts with similarly named env variables. But this proved to be very error prone where users tend to forget or get confused on where vdk_ prefix is needed and where it is not. This is solved by allowing non-prefixed env variable to be set. For for configuration "team" Now if "VDK_TEAM" is set as environment variable is set it is used (as before) If it is not set but "TEAM" is set as env variable, it is now used. Prefixed variables do have higher priority. Testing Done: unit test Signed-off-by: Antoni Ivanov <[email protected]>
- Loading branch information
1 parent
4c1a580
commit 8bc6a96
Showing
2 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
projects/vdk-core/tests/vdk/internal/builtin_plugins/config/test_vdk_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2021 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import os | ||
from unittest import mock | ||
|
||
from vdk.internal.builtin_plugins.config.vdk_config import EnvironmentVarsConfigPlugin | ||
from vdk.internal.core.config import ConfigurationBuilder | ||
|
||
|
||
@mock.patch.dict( | ||
os.environ, | ||
{"VDK_OPTION": "high", "OPTION": "low", "no_prefixed_option": "value"}, | ||
) | ||
def test_vdk_config_env_variable_vdk_prefix_prio(): | ||
envVarsConfig = EnvironmentVarsConfigPlugin() | ||
configuration_builder = ConfigurationBuilder() | ||
configuration_builder.add("option", "") | ||
configuration_builder.add("no_prefixed_option", "") | ||
|
||
envVarsConfig.vdk_configure(configuration_builder) | ||
|
||
assert configuration_builder.build().get_value("option") == "high" | ||
assert configuration_builder.build().get_value("no_prefixed_option") == "value" | ||
|
||
|
||
@mock.patch.dict( | ||
os.environ, | ||
{"VDK_OPTION_WITH_DOTS": "value1", "no_prefix_option_dots": "value2"}, | ||
) | ||
def test_vdk_config_env_variable_dots_replaced(): | ||
envVarsConfig = EnvironmentVarsConfigPlugin() | ||
configuration_builder = ConfigurationBuilder() | ||
configuration_builder.add("option.with.dots", "") | ||
configuration_builder.add("no.prefix.option.dots", "") | ||
|
||
envVarsConfig.vdk_configure(configuration_builder) | ||
|
||
assert configuration_builder.build().get_value("option.with.dots") == "value1" | ||
assert configuration_builder.build().get_value("no.prefix.option.dots") == "value2" | ||
|
||
|
||
@mock.patch.dict( | ||
os.environ, | ||
{"VDK_OPTION_WITH_DASH": "value1", "no_prefix_option_dash": "value2"}, | ||
) | ||
def test_vdk_config_env_variable_dash_replaced(): | ||
envVarsConfig = EnvironmentVarsConfigPlugin() | ||
configuration_builder = ConfigurationBuilder() | ||
configuration_builder.add("option-with-dash", "") | ||
configuration_builder.add("no-prefix-option-dash", "") | ||
|
||
envVarsConfig.vdk_configure(configuration_builder) | ||
|
||
assert configuration_builder.build().get_value("option-with-dash") == "value1" | ||
assert configuration_builder.build().get_value("no-prefix-option-dash") == "value2" |