|
14 | 14 |
|
15 | 15 | import pytest |
16 | 16 | import os |
| 17 | +from pathlib import Path |
17 | 18 | from agentkit.platform.configuration import VolcConfiguration |
18 | 19 |
|
19 | 20 |
|
@@ -91,6 +92,101 @@ def test_creds_vefaas_fallback( |
91 | 92 | assert creds.secret_key == "vefaas_sk" |
92 | 93 | assert creds.session_token == "vefaas_token" |
93 | 94 |
|
| 95 | + def test_creds_dotenv_fallback_from_cwd( |
| 96 | + self, clean_env, mock_global_config, monkeypatch, tmp_path, mocker |
| 97 | + ): |
| 98 | + """Test fallback to .env in current working directory when other sources are missing.""" |
| 99 | + # Ensure VeFaaS IAM check fails |
| 100 | + mocker.patch("pathlib.Path.exists", return_value=False) |
| 101 | + |
| 102 | + monkeypatch.chdir(tmp_path) |
| 103 | + (tmp_path / ".env").write_text( |
| 104 | + "VOLCENGINE_ACCESS_KEY=AK_FROM_DOTENV\nVOLCENGINE_SECRET_KEY=SK_FROM_DOTENV\n", |
| 105 | + encoding="utf-8", |
| 106 | + ) |
| 107 | + |
| 108 | + config = VolcConfiguration() |
| 109 | + creds = config.get_service_credentials("agentkit") |
| 110 | + |
| 111 | + assert creds.access_key == "AK_FROM_DOTENV" |
| 112 | + assert creds.secret_key == "SK_FROM_DOTENV" |
| 113 | + |
| 114 | + def test_creds_dotenv_does_not_override_global_env( |
| 115 | + self, clean_env, mock_global_config, monkeypatch, tmp_path |
| 116 | + ): |
| 117 | + """Test that .env fallback never overrides real process environment variables.""" |
| 118 | + os.environ["VOLCENGINE_ACCESS_KEY"] = "AK_FROM_ENV" |
| 119 | + os.environ["VOLCENGINE_SECRET_KEY"] = "SK_FROM_ENV" |
| 120 | + |
| 121 | + monkeypatch.chdir(tmp_path) |
| 122 | + (tmp_path / ".env").write_text( |
| 123 | + "VOLCENGINE_ACCESS_KEY=AK_FROM_DOTENV\nVOLCENGINE_SECRET_KEY=SK_FROM_DOTENV\n", |
| 124 | + encoding="utf-8", |
| 125 | + ) |
| 126 | + |
| 127 | + config = VolcConfiguration() |
| 128 | + creds = config.get_service_credentials("agentkit") |
| 129 | + |
| 130 | + assert creds.access_key == "AK_FROM_ENV" |
| 131 | + assert creds.secret_key == "SK_FROM_ENV" |
| 132 | + |
| 133 | + def test_creds_dotenv_does_not_override_global_config( |
| 134 | + self, clean_env, mock_global_config, monkeypatch, tmp_path, mocker |
| 135 | + ): |
| 136 | + """Test that .env fallback never overrides ~/.agentkit/config.yaml credentials.""" |
| 137 | + # Ensure VeFaaS IAM check fails |
| 138 | + mocker.patch("pathlib.Path.exists", return_value=False) |
| 139 | + |
| 140 | + mock_global_config.update( |
| 141 | + {"volcengine": {"access_key": "AK_FROM_CFG", "secret_key": "SK_FROM_CFG"}} |
| 142 | + ) |
| 143 | + |
| 144 | + monkeypatch.chdir(tmp_path) |
| 145 | + (tmp_path / ".env").write_text( |
| 146 | + "VOLCENGINE_ACCESS_KEY=AK_FROM_DOTENV\nVOLCENGINE_SECRET_KEY=SK_FROM_DOTENV\n", |
| 147 | + encoding="utf-8", |
| 148 | + ) |
| 149 | + |
| 150 | + config = VolcConfiguration() |
| 151 | + creds = config.get_service_credentials("agentkit") |
| 152 | + |
| 153 | + assert creds.access_key == "AK_FROM_CFG" |
| 154 | + assert creds.secret_key == "SK_FROM_CFG" |
| 155 | + |
| 156 | + def test_creds_dotenv_partial_is_ignored( |
| 157 | + self, clean_env, mock_global_config, monkeypatch, tmp_path, mocker |
| 158 | + ): |
| 159 | + """Test that partial .env credentials are ignored and lookup continues.""" |
| 160 | + # Ensure VeFaaS IAM check fails |
| 161 | + mocker.patch("pathlib.Path.exists", return_value=False) |
| 162 | + |
| 163 | + monkeypatch.chdir(tmp_path) |
| 164 | + (tmp_path / ".env").write_text( |
| 165 | + "VOLCENGINE_ACCESS_KEY=AK_ONLY\n", |
| 166 | + encoding="utf-8", |
| 167 | + ) |
| 168 | + |
| 169 | + config = VolcConfiguration() |
| 170 | + with pytest.raises(ValueError, match="Volcengine credentials not found"): |
| 171 | + config.get_service_credentials("agentkit") |
| 172 | + |
| 173 | + def test_creds_vefaas_takes_priority_over_dotenv( |
| 174 | + self, clean_env, mock_global_config, mock_vefaas_file, monkeypatch, tmp_path |
| 175 | + ): |
| 176 | + """Test that VeFaaS IAM credentials take priority over .env fallback.""" |
| 177 | + monkeypatch.chdir(tmp_path) |
| 178 | + (tmp_path / ".env").write_text( |
| 179 | + "VOLCENGINE_ACCESS_KEY=AK_FROM_DOTENV\nVOLCENGINE_SECRET_KEY=SK_FROM_DOTENV\n", |
| 180 | + encoding="utf-8", |
| 181 | + ) |
| 182 | + |
| 183 | + config = VolcConfiguration() |
| 184 | + creds = config.get_service_credentials("agentkit") |
| 185 | + |
| 186 | + assert creds.access_key == "vefaas_ak" |
| 187 | + assert creds.secret_key == "vefaas_sk" |
| 188 | + assert creds.session_token == "vefaas_token" |
| 189 | + |
94 | 190 | def test_creds_missing_error(self, clean_env, mock_global_config, mocker): |
95 | 191 | """Test error raised when no credentials found.""" |
96 | 192 | mocker.patch("pathlib.Path.exists", return_value=False) |
|
0 commit comments