Skip to content

Commit

Permalink
feat: update env var validation
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybayblade committed Oct 3, 2023
1 parent ff3705c commit 4c1d897
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
36 changes: 26 additions & 10 deletions autonomy/analyse/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)
from aea.crypto.base import LedgerApi
from aea.helpers.cid import to_v0
from aea.helpers.env_vars import ENV_VARIABLE_RE, apply_env_variables
from aea.helpers.env_vars import apply_env_variables
from aea.helpers.logging import setup_logger
from jsonschema.exceptions import ValidationError as SchemaValidationError
from jsonschema.validators import Draft4Validator
Expand Down Expand Up @@ -184,6 +184,9 @@
ABCI = "abci"
LEDGER = "ledger"

ENV_VAR_RE = re.compile(
r"^\$\{(?P<name>[A-Z_0-9]+)?:?(?P<type>bool|int|float|str|list|dict)?:?(?P<value>.+)?\}$"
)
REQUIRED_PROPERTY_RE = re.compile(r"'(.*)' is a required property")
PROPERTY_NOT_ALLOWED_RE = re.compile(
r"Additional properties are not allowed \((('(.*)'(,)? )+)(was|were) unexpected\)"
Expand Down Expand Up @@ -540,19 +543,32 @@ def validate_override_env_vars(
else:
json_path_str = ".".join(_json_path)
try:
re_result = ENV_VARIABLE_RE.match(value)
re_result = ENV_VAR_RE.match(value)
if re_result is None:
errors.append(
f"`{json_path_str}` needs to be defined as a environment variable"
f"`{json_path_str}` needs environment variable defined in following format "
"${ENV_VAR_NAME:DATA_TYPE:DEFAULT_VALUE}"
)
continue
if validate_env_var_name:
_, env_var_name, *_ = re_result.groups()
if env_var_name is None:
errors.append(
f"`{json_path_str}` needs environment variable defined in following format "
"${ENV_VAR_NAME:DATA_TYPE:DEFAULT_VALUE}"
)

result = re_result.groupdict()
if validate_env_var_name and result.get("name") is None:
errors.append(
f"Enviroment variable template for `{json_path_str}` does not have variable name defined"
)

if result.get("type") is None:
errors.append(

Check warning on line 561 in autonomy/analyse/service.py

View check run for this annotation

Codecov / codecov/patch

autonomy/analyse/service.py#L561

Added line #L561 was not covered by tests
f"Enviroment variable template for `{json_path_str}` does not have type defined"
)
continue

Check warning on line 564 in autonomy/analyse/service.py

View check run for this annotation

Codecov / codecov/patch

autonomy/analyse/service.py#L564

Added line #L564 was not covered by tests

if result.get("value") is None:
errors.append(

Check warning on line 567 in autonomy/analyse/service.py

View check run for this annotation

Codecov / codecov/patch

autonomy/analyse/service.py#L567

Added line #L567 was not covered by tests
f"Enviroment variable template for `{json_path_str}` does not have default value defined"
)
continue

Check warning on line 570 in autonomy/analyse/service.py

View check run for this annotation

Codecov / codecov/patch

autonomy/analyse/service.py#L570

Added line #L570 was not covered by tests

apply_env_variables(
data={key: value}, env_variables=os.environ.copy()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,8 @@ def test_agent_var_validation(self) -> None:

assert result.exit_code == 1, result.stdout
assert (
"(skill, valory/abci_skill:0.1.0) envrionment variable validation failed with following error"
"\n\t- `models.params.args.message` needs to be defined as a environment variable"
"(skill, valory/abci_skill:0.1.0) envrionment variable validation failed with following error\n\t- "
"`models.params.args.message` needs environment variable defined in following format ${ENV_VAR_NAME:DATA_TYPE:DEFAULT_VALUE}\n"
in result.stderr
), result.stdout

Expand Down

0 comments on commit 4c1d897

Please sign in to comment.