|
| 1 | +""" |
| 2 | +Tests for api_key ${VAR} environment variable substitution in configuration. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | + |
| 9 | +from openevolve.config import Config, LLMModelConfig, _resolve_env_var |
| 10 | + |
| 11 | + |
| 12 | +class TestEnvVarSubstitution(unittest.TestCase): |
| 13 | + """Tests for ${VAR} environment variable substitution in api_key fields""" |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + """Set up test environment variables""" |
| 17 | + self.test_env_var = "TEST_OPENEVOLVE_API_KEY" |
| 18 | + self.test_api_key = "test-secret-key-12345" |
| 19 | + os.environ[self.test_env_var] = self.test_api_key |
| 20 | + |
| 21 | + def tearDown(self): |
| 22 | + """Clean up test environment variables""" |
| 23 | + if self.test_env_var in os.environ: |
| 24 | + del os.environ[self.test_env_var] |
| 25 | + |
| 26 | + def test_resolve_env_var_with_match(self): |
| 27 | + """Test that _resolve_env_var resolves ${VAR} syntax""" |
| 28 | + result = _resolve_env_var(f"${{{self.test_env_var}}}") |
| 29 | + self.assertEqual(result, self.test_api_key) |
| 30 | + |
| 31 | + def test_resolve_env_var_no_match(self): |
| 32 | + """Test that strings without ${VAR} are returned unchanged""" |
| 33 | + result = _resolve_env_var("regular-api-key") |
| 34 | + self.assertEqual(result, "regular-api-key") |
| 35 | + |
| 36 | + def test_resolve_env_var_none(self): |
| 37 | + """Test that None is returned unchanged""" |
| 38 | + result = _resolve_env_var(None) |
| 39 | + self.assertIsNone(result) |
| 40 | + |
| 41 | + def test_resolve_env_var_missing_var(self): |
| 42 | + """Test that missing environment variable raises ValueError""" |
| 43 | + with self.assertRaises(ValueError) as context: |
| 44 | + _resolve_env_var("${NONEXISTENT_ENV_VAR_12345}") |
| 45 | + |
| 46 | + self.assertIn("NONEXISTENT_ENV_VAR_12345", str(context.exception)) |
| 47 | + self.assertIn("is not set", str(context.exception)) |
| 48 | + |
| 49 | + def test_api_key_env_var_in_model_config(self): |
| 50 | + """Test that api_key ${VAR} works in LLMModelConfig""" |
| 51 | + model_config = LLMModelConfig(name="test-model", api_key=f"${{{self.test_env_var}}}") |
| 52 | + |
| 53 | + self.assertEqual(model_config.api_key, self.test_api_key) |
| 54 | + |
| 55 | + def test_api_key_direct_value(self): |
| 56 | + """Test that direct api_key value still works""" |
| 57 | + direct_key = "direct-api-key-value" |
| 58 | + model_config = LLMModelConfig(name="test-model", api_key=direct_key) |
| 59 | + |
| 60 | + self.assertEqual(model_config.api_key, direct_key) |
| 61 | + |
| 62 | + def test_api_key_none(self): |
| 63 | + """Test that api_key can be None""" |
| 64 | + model_config = LLMModelConfig(name="test-model", api_key=None) |
| 65 | + |
| 66 | + self.assertIsNone(model_config.api_key) |
| 67 | + |
| 68 | + def test_api_key_env_var_in_llm_config(self): |
| 69 | + """Test that api_key ${VAR} works at LLM config level""" |
| 70 | + yaml_config = { |
| 71 | + "log_level": "INFO", |
| 72 | + "llm": { |
| 73 | + "api_base": "https://api.openai.com/v1", |
| 74 | + "api_key": f"${{{self.test_env_var}}}", |
| 75 | + "models": [{"name": "test-model", "weight": 1.0}], |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + config = Config.from_dict(yaml_config) |
| 80 | + |
| 81 | + self.assertEqual(config.llm.api_key, self.test_api_key) |
| 82 | + # Models should inherit the resolved api_key |
| 83 | + self.assertEqual(config.llm.models[0].api_key, self.test_api_key) |
| 84 | + |
| 85 | + def test_api_key_env_var_per_model(self): |
| 86 | + """Test that api_key ${VAR} can be specified per model""" |
| 87 | + # Set up a second env var for testing |
| 88 | + second_env_var = "TEST_OPENEVOLVE_API_KEY_2" |
| 89 | + second_api_key = "second-secret-key-67890" |
| 90 | + os.environ[second_env_var] = second_api_key |
| 91 | + |
| 92 | + try: |
| 93 | + yaml_config = { |
| 94 | + "log_level": "INFO", |
| 95 | + "llm": { |
| 96 | + "api_base": "https://api.openai.com/v1", |
| 97 | + "models": [ |
| 98 | + { |
| 99 | + "name": "model-1", |
| 100 | + "weight": 1.0, |
| 101 | + "api_key": f"${{{self.test_env_var}}}", |
| 102 | + }, |
| 103 | + { |
| 104 | + "name": "model-2", |
| 105 | + "weight": 0.5, |
| 106 | + "api_key": f"${{{second_env_var}}}", |
| 107 | + }, |
| 108 | + ], |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + config = Config.from_dict(yaml_config) |
| 113 | + |
| 114 | + self.assertEqual(config.llm.models[0].api_key, self.test_api_key) |
| 115 | + self.assertEqual(config.llm.models[1].api_key, second_api_key) |
| 116 | + finally: |
| 117 | + if second_env_var in os.environ: |
| 118 | + del os.environ[second_env_var] |
| 119 | + |
| 120 | + def test_api_key_env_var_in_evaluator_models(self): |
| 121 | + """Test that api_key ${VAR} works in evaluator_models""" |
| 122 | + yaml_config = { |
| 123 | + "log_level": "INFO", |
| 124 | + "llm": { |
| 125 | + "api_base": "https://api.openai.com/v1", |
| 126 | + "models": [{"name": "evolution-model", "weight": 1.0, "api_key": "direct-key"}], |
| 127 | + "evaluator_models": [ |
| 128 | + { |
| 129 | + "name": "evaluator-model", |
| 130 | + "weight": 1.0, |
| 131 | + "api_key": f"${{{self.test_env_var}}}", |
| 132 | + } |
| 133 | + ], |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + config = Config.from_dict(yaml_config) |
| 138 | + |
| 139 | + self.assertEqual(config.llm.evaluator_models[0].api_key, self.test_api_key) |
| 140 | + |
| 141 | + def test_yaml_file_loading_with_env_var(self): |
| 142 | + """Test loading api_key ${VAR} from actual YAML file""" |
| 143 | + yaml_content = f""" |
| 144 | +log_level: INFO |
| 145 | +llm: |
| 146 | + api_base: https://api.openai.com/v1 |
| 147 | + api_key: ${{{self.test_env_var}}} |
| 148 | + models: |
| 149 | + - name: test-model |
| 150 | + weight: 1.0 |
| 151 | +""" |
| 152 | + |
| 153 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: |
| 154 | + f.write(yaml_content) |
| 155 | + f.flush() |
| 156 | + |
| 157 | + try: |
| 158 | + config = Config.from_yaml(f.name) |
| 159 | + self.assertEqual(config.llm.api_key, self.test_api_key) |
| 160 | + finally: |
| 161 | + os.unlink(f.name) |
| 162 | + |
| 163 | + def test_mixed_api_key_sources(self): |
| 164 | + """Test mixing direct api_key and ${VAR} in same config""" |
| 165 | + yaml_config = { |
| 166 | + "log_level": "INFO", |
| 167 | + "llm": { |
| 168 | + "api_base": "https://api.openai.com/v1", |
| 169 | + "api_key": "llm-level-direct-key", |
| 170 | + "models": [ |
| 171 | + { |
| 172 | + "name": "model-with-env", |
| 173 | + "weight": 1.0, |
| 174 | + "api_key": f"${{{self.test_env_var}}}", |
| 175 | + }, |
| 176 | + { |
| 177 | + "name": "model-with-direct", |
| 178 | + "weight": 0.5, |
| 179 | + "api_key": "model-direct-key", |
| 180 | + }, |
| 181 | + ], |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + config = Config.from_dict(yaml_config) |
| 186 | + |
| 187 | + self.assertEqual(config.llm.api_key, "llm-level-direct-key") |
| 188 | + self.assertEqual(config.llm.models[0].api_key, self.test_api_key) |
| 189 | + self.assertEqual(config.llm.models[1].api_key, "model-direct-key") |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + unittest.main() |
0 commit comments