File size: 6,520 Bytes
afa0cbf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | //! Checks retained providers against managed policy without reloading session configuration.
use super::*;
use anyhow::Result;
use codex_config::CloudConfigBundleLoadError;
use codex_config::CloudConfigBundleLoadErrorCode;
use codex_config::ThreadConfigContext;
use codex_config::ThreadConfigLoadError;
use codex_config::ThreadConfigLoadErrorCode;
use codex_config::ThreadConfigLoaderFuture;
use codex_config::ThreadConfigSource;
use codex_config::test_support::CloudConfigBundleFixture;
use pretty_assertions::assert_eq;
use tempfile::tempdir;
use test_case::test_case;
#[test_case(AMAZON_BEDROCK_PROVIDER_ID; "bedrock")]
#[test_case(AMAZON_BEDROCK_RUNTIME_PROVIDER_ID; "bedrock_runtime")]
#[tokio::test]
async fn provider_requirements_resolve_bedrock_overrides(provider_id: &str) -> Result<()> {
let home = tempdir()?;
let requirements = format!(
"model_provider = '{provider_id}'\n[model_providers.{provider_id}.aws]\nregion = 'us-west-2'"
);
let manager = ConfigManager::new_for_tests(
home.path().to_path_buf(),
Vec::new(),
LoaderOverrides::without_managed_config_for_tests(),
CloudConfigBundleFixture::loader_with_enterprise_requirement(&requirements),
);
let current = manager.load_latest_config(/*fallback_cwd*/ None).await?;
manager.check_thread_model_provider(¤t).await?;
let changed = ConfigManager::new_for_tests(
home.path().to_path_buf(),
Vec::new(),
LoaderOverrides::without_managed_config_for_tests(),
CloudConfigBundleFixture::loader_with_enterprise_requirement(
requirements.replace("us-west-2", "us-east-1"),
),
);
assert_eq!(
changed
.check_thread_model_provider(¤t)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::PermissionDenied,
);
Ok(())
}
struct UnavailableThreadConfig;
impl ThreadConfigLoader for UnavailableThreadConfig {
fn load(
&self,
_context: ThreadConfigContext,
) -> ThreadConfigLoaderFuture<'_, Vec<ThreadConfigSource>> {
Box::pin(async {
Err(ThreadConfigLoadError::new(
ThreadConfigLoadErrorCode::RequestFailed,
/*status_code*/ None,
"thread config service unavailable",
))
})
}
}
#[tokio::test]
async fn provider_requirements_do_not_reload_thread_config() -> Result<()> {
let home = tempdir()?;
let mut manager = ConfigManager::without_managed_config_for_tests(home.path().to_path_buf());
let current = manager.load_latest_config(/*fallback_cwd*/ None).await?;
manager.thread_config_loader = Arc::new(UnavailableThreadConfig);
assert!(
manager
.load_latest_config(/*fallback_cwd*/ None)
.await
.is_err()
);
manager.check_thread_model_provider(¤t).await?;
manager.cloud_config_bundle = Arc::new(RwLock::new(
CloudConfigBundleFixture::loader_with_enterprise_requirement("model_provider = 'other'"),
));
assert_eq!(
manager
.check_thread_model_provider(¤t)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::PermissionDenied,
);
Ok(())
}
#[tokio::test]
async fn provider_requirements_ignore_system_defaults_but_reject_requirement_changes() -> Result<()>
{
let home = tempdir()?;
let system_config_path = home.path().join("system-config.toml");
let requirements_path = home.path().join("requirements.toml");
let managed_config_path = home.path().join("managed-config.toml");
let mut overrides = LoaderOverrides::without_managed_config_for_tests();
overrides.system_config_path = Some(system_config_path.clone());
overrides.system_requirements_path = Some(requirements_path.clone());
overrides.managed_config_path = Some(managed_config_path.clone());
let manager = ConfigManager::new_for_tests(
home.path().to_path_buf(),
Vec::new(),
overrides,
CloudConfigBundleLoader::default(),
);
let current = manager.load_latest_config(/*fallback_cwd*/ None).await?;
std::fs::write(&system_config_path, "invalid toml !!!")?;
manager.check_thread_model_provider(¤t).await?;
std::fs::write(&requirements_path, "model_provider = 'other'")?;
assert_eq!(
manager
.check_thread_model_provider(¤t)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::PermissionDenied,
);
std::fs::write(&requirements_path, "invalid toml !!!")?;
assert_eq!(
manager
.check_thread_model_provider(¤t)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::InvalidData,
);
std::fs::write(
&requirements_path,
"[model_providers.gateway]\nbase_url = 'https://example.test'",
)?;
assert_eq!(
manager
.check_thread_model_provider(¤t)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::InvalidData,
);
std::fs::remove_file(requirements_path)?;
std::fs::write(managed_config_path, "invalid toml !!!")?;
assert_eq!(
manager
.check_thread_model_provider(¤t)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::InvalidData,
);
Ok(())
}
#[tokio::test]
async fn provider_requirement_load_errors_reject_input() -> Result<()> {
let home = tempdir()?;
let previous = ConfigManager::without_managed_config_for_tests(home.path().to_path_buf())
.load_latest_config(/*fallback_cwd*/ None)
.await?;
for loader in [
CloudConfigBundleFixture::loader_with_enterprise_requirement("invalid toml !!!"),
CloudConfigBundleLoader::new(async {
Err(CloudConfigBundleLoadError::new(
CloudConfigBundleLoadErrorCode::RequestFailed,
/*status_code*/ None,
"policy unavailable",
))
}),
] {
let manager = ConfigManager::new_for_tests(
home.path().to_path_buf(),
Vec::new(),
LoaderOverrides::without_managed_config_for_tests(),
loader,
);
assert!(
manager
.check_thread_model_provider(&previous)
.await
.is_err()
);
}
Ok(())
}
|