mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-10-03 19:57:35 +00:00
When using bash style substitution env variable in distribution template, we are processing the string and convert it to the type associated with the provider's config class. This allows us to return the proper type. This is crucial for api key since they are not strings anymore but SecretStr. If the key is unset we will get an empty string which will result in a Pydantic error like: ``` ERROR 2025-09-25 21:40:44,565 __main__:527 core::server: Error creating app: 1 validation error for AnthropicConfig api_key Input should be a valid string For further information visit https://errors.pydantic.dev/2.11/v/string_type ``` Signed-off-by: Sébastien Han <seb@redhat.com>
102 lines
3.3 KiB
Python
102 lines
3.3 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the terms described in the LICENSE file in
|
|
# the root directory of this source tree.
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
|
# Wrapper for backward compatibility in tests
|
|
def replace_env_vars_compat(config, path=""):
|
|
return replace_env_vars_compat(config, path, None, None)
|
|
|
|
|
|
@pytest.fixture
|
|
def setup_env_vars():
|
|
# Clear any existing environment variables we'll use in tests
|
|
for var in ["TEST_VAR", "EMPTY_VAR", "ZERO_VAR"]:
|
|
if var in os.environ:
|
|
del os.environ[var]
|
|
|
|
# Set up test environment variables
|
|
os.environ["TEST_VAR"] = "test_value"
|
|
os.environ["EMPTY_VAR"] = ""
|
|
os.environ["ZERO_VAR"] = "0"
|
|
|
|
yield
|
|
|
|
# Cleanup after test
|
|
for var in ["TEST_VAR", "EMPTY_VAR", "ZERO_VAR"]:
|
|
if var in os.environ:
|
|
del os.environ[var]
|
|
|
|
|
|
def test_simple_replacement(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.TEST_VAR}") == "test_value"
|
|
|
|
|
|
def test_default_value_when_not_set(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.NOT_SET:=default}") == "default"
|
|
|
|
|
|
def test_default_value_when_set(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.TEST_VAR:=default}") == "test_value"
|
|
|
|
|
|
def test_default_value_when_empty(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.EMPTY_VAR:=default}") == "default"
|
|
|
|
|
|
def test_none_value_when_empty(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.EMPTY_VAR:=}") is None
|
|
|
|
|
|
def test_value_when_set(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.TEST_VAR:=}") == "test_value"
|
|
|
|
|
|
def test_empty_var_no_default(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.EMPTY_VAR_NO_DEFAULT:+}") is None
|
|
|
|
|
|
def test_conditional_value_when_set(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.TEST_VAR:+conditional}") == "conditional"
|
|
|
|
|
|
def test_conditional_value_when_not_set(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.NOT_SET:+conditional}") is None
|
|
|
|
|
|
def test_conditional_value_when_empty(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.EMPTY_VAR:+conditional}") is None
|
|
|
|
|
|
def test_conditional_value_with_zero(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.ZERO_VAR:+conditional}") == "conditional"
|
|
|
|
|
|
def test_mixed_syntax(setup_env_vars):
|
|
assert replace_env_vars_compat("${env.TEST_VAR:=default} and ${env.NOT_SET:+conditional}") == "test_value and "
|
|
assert (
|
|
replace_env_vars_compat("${env.NOT_SET:=default} and ${env.TEST_VAR:+conditional}") == "default and conditional"
|
|
)
|
|
|
|
|
|
def test_nested_structures(setup_env_vars):
|
|
data = {
|
|
"key1": "${env.TEST_VAR:=default}",
|
|
"key2": ["${env.NOT_SET:=default}", "${env.TEST_VAR:+conditional}"],
|
|
"key3": {"nested": "${env.NOT_SET:+conditional}"},
|
|
}
|
|
expected = {"key1": "test_value", "key2": ["default", "conditional"], "key3": {"nested": None}}
|
|
assert replace_env_vars_compat(data) == expected
|
|
|
|
|
|
def test_explicit_strings_preserved(setup_env_vars):
|
|
# Explicit strings that look like numbers/booleans should remain strings
|
|
data = {"port": "8080", "enabled": "true", "count": "123", "ratio": "3.14"}
|
|
expected = {"port": "8080", "enabled": "true", "count": "123", "ratio": "3.14"}
|
|
assert replace_env_vars_compat(data) == expected
|