llama-stack-mirror/tests/unit/server/test_replace_env_vars.py
Sébastien Han 9654dd9da1
refactor(env)!: enhanced environment variable substitution
This commit significantly improves the environment variable substitution
functionality in Llama Stack configuration files:
* The version field in configuration files has been changed from string
  to integer type for better type consistency across build and run
  configurations.

* The environment variable substitution system for ${env.FOO:} was fixed
  and properly returns an error

* The environment variable substitution system for ${env.FOO+} returns
  None instead of an empty strings, it better matches type annotations
  in config fields

* The system includes automatic type conversion for boolean, integer,
  and float values.

* The error messages have been enhanced to provide clearer guidance when
  environment variables are missing, including suggestions for using
  default values or conditional syntax.

* Comprehensive documentation has been added to the configuration guide
  explaining all supported syntax patterns, best practices, and runtime
  override capabilities.

* Multiple provider configurations have been updated to use the new
  conditional syntax for optional API keys, making the system more
  flexible for different deployment scenarios. The telemetry
  configuration has been improved to properly handle optional endpoints
  with appropriate validation, ensuring that required endpoints are
  specified when their corresponding sinks are enabled.

* There were many instances of ${env.NVIDIA_API_KEY:} that should have
  caused the code to fail. However, due to a bug, the distro server was
  still being started, and early validation wasn’t triggered. As a
  result, failures were likely being handled downstream by the
  providers.  I’ve maintained similar behavior by using
  ${env.NVIDIA_API_KEY:+}, though I believe this is incorrect for many
  configurations. I’ll leave it to each provider to correct it as
  needed.

* Environment variable substitution now uses the same syntax as Bash
  parameter expansion.

Signed-off-by: Sébastien Han <seb@redhat.com>
2025-06-25 15:59:04 +02:00

71 lines
2.6 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 unittest
from llama_stack.distribution.stack import replace_env_vars
class TestReplaceEnvVars(unittest.TestCase):
def setUp(self):
# 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"
def test_simple_replacement(self):
self.assertEqual(replace_env_vars("${env.TEST_VAR}"), "test_value")
def test_default_value_when_not_set(self):
self.assertEqual(replace_env_vars("${env.NOT_SET:=default}"), "default")
def test_default_value_when_set(self):
self.assertEqual(replace_env_vars("${env.TEST_VAR:=default}"), "test_value")
def test_default_value_when_empty(self):
self.assertEqual(replace_env_vars("${env.EMPTY_VAR:=default}"), "default")
def test_empty_var_no_default(self):
self.assertEqual(replace_env_vars("${env.EMPTY_VAR_NO_DEFAULT:+}"), None)
def test_conditional_value_when_set(self):
self.assertEqual(replace_env_vars("${env.TEST_VAR:+conditional}"), "conditional")
def test_conditional_value_when_not_set(self):
self.assertEqual(replace_env_vars("${env.NOT_SET:+conditional}"), None)
def test_conditional_value_when_empty(self):
self.assertEqual(replace_env_vars("${env.EMPTY_VAR:+conditional}"), None)
def test_conditional_value_with_zero(self):
self.assertEqual(replace_env_vars("${env.ZERO_VAR:+conditional}"), "conditional")
def test_mixed_syntax(self):
self.assertEqual(
replace_env_vars("${env.TEST_VAR:=default} and ${env.NOT_SET:+conditional}"), "test_value and "
)
self.assertEqual(
replace_env_vars("${env.NOT_SET:=default} and ${env.TEST_VAR:+conditional}"), "default and conditional"
)
def test_nested_structures(self):
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}}
self.assertEqual(replace_env_vars(data), expected)
if __name__ == "__main__":
unittest.main()