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

feat(ingest): list referenced env variables in recipe #6043

Merged
merged 1 commit into from
Sep 27, 2022
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
23 changes: 17 additions & 6 deletions metadata-ingestion/src/datahub/configuration/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import pathlib
import re
import sys
from typing import Any, Dict, Union
import unittest.mock
from typing import Any, Dict, Set, Union

from expandvars import UnboundVariable, expandvars

Expand Down Expand Up @@ -51,6 +52,19 @@ def resolve_env_variables(config: dict) -> dict:
return new_dict


def list_referenced_env_variables(config: dict) -> Set[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this function being used anywhere (other than the test). So what's the purpose of this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to fetch secrets from an external secret store rather than env vars, we'll need to be able to list the required variables. This function will be used by the acryl executor.

# This is a bit of a hack, but expandvars does a bunch of escaping
# and other logic that we don't want to duplicate here.

with unittest.mock.patch("expandvars.getenv") as mock_getenv:
mock_getenv.return_value = "mocked_value"

resolve_env_variables(config)

calls = mock_getenv.mock_calls
return set([call[1][0] for call in calls])


def load_config_file(
config_file: Union[pathlib.Path, str],
squirrel_original_config: bool = False,
Expand All @@ -63,8 +77,7 @@ def load_config_file(
config_mech = YamlConfigurationMechanism()
raw_config_file = sys.stdin.read()
else:
if isinstance(config_file, str):
config_file = pathlib.Path(config_file)
config_file = pathlib.Path(config_file)
if not config_file.is_file():
raise ConfigurationError(f"Cannot open config file {config_file}")

Expand All @@ -74,9 +87,7 @@ def load_config_file(
config_mech = TomlConfigurationMechanism()
else:
raise ConfigurationError(
"Only .toml and .yml are supported. Cannot process file type {}".format(
config_file.suffix
)
f"Only .toml and .yml are supported. Cannot process file type {config_file.suffix}"
)

raw_config_file = config_file.read_text()
Expand Down
110 changes: 70 additions & 40 deletions metadata-ingestion/tests/unit/config/test_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@

import expandvars
import pytest
import yaml

from datahub.configuration.common import ConfigurationError
from datahub.configuration.config_loader import load_config_file
from datahub.configuration.config_loader import (
list_referenced_env_variables,
load_config_file,
)


@pytest.mark.parametrize(
"filename,golden_config,env,error_type",
"filename,golden_config,env,referenced_env_vars",
[
(
# Basic YAML load
"tests/unit/config/basic.yml",
{"foo": "bar", "nested": {"array": ["one", "two"], "hi": "hello"}},
{},
None,
set(),
),
(
# Basic TOML load
Expand All @@ -38,35 +42,7 @@
"VAR1": "stuff1",
"VAR2": "stuff2",
},
None,
),
(
# Variable expansion error
"tests/unit/config/bad_variable_expansion.yml",
None,
{},
expandvars.ParameterNullOrNotSet,
),
(
# Missing file
"tests/unit/config/this_file_does_not_exist.yml",
None,
{},
ConfigurationError,
),
(
# Unknown extension
"tests/unit/config/bad_extension.whatevenisthis",
None,
{},
ConfigurationError,
),
(
# Variable expansion error
"tests/unit/config/bad_complex_variable_expansion.yml",
None,
{},
expandvars.MissingClosingBrace,
set(["VAR1", "UNSET_VAR3", "VAR2"]),
),
(
"tests/unit/config/complex_variable_expansion.yml",
Expand Down Expand Up @@ -121,17 +97,71 @@
"VAR10": "stuff10",
"VAR11": "stuff11",
},
None,
set(
[
"VAR1",
"VAR2",
"VAR3",
"VAR4",
"VAR5",
"VAR6",
"VAR7",
"VAR8",
"VAR9",
"VAR10",
# VAR11 is escaped and hence not referenced
"VARNONEXISTENT",
]
),
),
],
)
def test_load_success(pytestconfig, filename, golden_config, env, referenced_env_vars):
filepath = pytestconfig.rootpath / filename

if referenced_env_vars is not None:
raw_config = yaml.safe_load(filepath.read_text())
assert list_referenced_env_variables(raw_config) == referenced_env_vars

with mock.patch.dict(os.environ, env):
loaded_config = load_config_file(filepath)
assert loaded_config == golden_config

# TODO check referenced env vars


@pytest.mark.parametrize(
"filename,env,error_type",
[
(
# Variable expansion error
"tests/unit/config/bad_variable_expansion.yml",
{},
expandvars.ParameterNullOrNotSet,
),
(
# Missing file
"tests/unit/config/this_file_does_not_exist.yml",
{},
ConfigurationError,
),
(
# Unknown extension
"tests/unit/config/bad_extension.whatevenisthis",
{},
ConfigurationError,
),
(
# Variable expansion error
"tests/unit/config/bad_complex_variable_expansion.yml",
{},
expandvars.MissingClosingBrace,
),
],
)
def test_load(pytestconfig, filename, golden_config, env, error_type):
def test_load_error(pytestconfig, filename, env, error_type):
filepath = pytestconfig.rootpath / filename

with mock.patch.dict(os.environ, env):
if error_type:
with pytest.raises(error_type):
_ = load_config_file(filepath)
else:
loaded_config = load_config_file(filepath)
assert loaded_config == golden_config
with pytest.raises(error_type):
_ = load_config_file(filepath)