Files
Bedrock/crates/clusters/tests/deployment_config.rs
T

126 lines
4.1 KiB
Rust

use std::collections::BTreeMap;
use clusters::{Address, Cluster, ConfigError, DeploymentConfig, SqlDriver, Tls};
#[test]
fn address_debug_formats_the_inner_string() {
let value = "bedrock.sqlite3";
assert_eq!(format!("{:?}", Address::from(value)), format!("{value:?}"));
}
#[test]
fn named_cluster_requires_matching_addresses() {
let mut config = DeploymentConfig {
cluster: Some("regional".into()),
clusters: BTreeMap::from([(
"regional".into(),
Cluster {
driver: SqlDriver::Postgres,
primary: "postgres://user:secret@primary/bedrock".into(),
replicas: vec!["postgresql://reader:secret@replica/bedrock".into()],
tls: Tls::VerifyFull,
max_connections: 5,
},
)]),
..DeploymentConfig::default()
};
let resolved = config.resolve().unwrap();
let resolved = resolved.documents().unwrap();
assert!(
resolved
.primary
.connection_string()
.ends_with("sslmode=verify-full")
);
assert!(
resolved.replicas[0]
.connection_string()
.ends_with("sslmode=verify-full")
);
config.clusters.get_mut("regional").unwrap().replicas[0] =
"mysql://user:secret@replica/bedrock".into();
assert!(matches!(
config.resolve(),
Err(ConfigError::Url("postgres"))
));
}
#[test]
fn resolves_named_connections_and_round_trips_yaml() {
use clusters::ConfigFormat;
let config = DeploymentConfig::parse(
include_str!("../../../deploy/multi-storage.yaml"),
ConfigFormat::Yaml,
)
.unwrap();
let snapshot = config.resolve().unwrap();
assert_eq!(snapshot.config_version, "example-1");
assert_eq!(snapshot.documents().unwrap().driver, SqlDriver::Postgres);
assert_eq!(snapshot.sql["legacy"].driver, SqlDriver::Mysql);
assert_eq!(
snapshot.object_storage["attachments"].bucket,
"bedrock-attachments"
);
assert_eq!(snapshot.documents().unwrap().max_connections, 5);
let serialized = config.to_yaml().unwrap();
let roundtrip = DeploymentConfig::parse(&serialized, ConfigFormat::Yaml)
.unwrap()
.resolve()
.unwrap();
assert_eq!(roundtrip.sql["legacy"].driver, SqlDriver::Mysql);
}
#[test]
fn imports_catalog_atomically_and_preserves_role_references() {
use clusters::{ConfigFormat, Driver, ImportFormat, ObjectStoreDriver, Resource};
let mut config =
DeploymentConfig::parse("config_version = 'test'", ConfigFormat::Toml).unwrap();
config
.import_resources(
include_str!("../../../deploy/kubernetes-catalog.yaml"),
ImportFormat::KubernetesConfigMap,
)
.unwrap();
config.sql.insert("documents".into(), "pg-main".into());
let before = config.resource("attachments").unwrap().driver();
assert_eq!(before, Driver::ObjectStore(ObjectStoreDriver::S3));
assert!(matches!(
config.remove_resource("pg-main"),
Err(ConfigError::ResourceInUse)
));
let inventory = r#"{
"bedrock_resources": {
"value": {
"another-bucket": {"kind":"object_store","driver":"s3","bucket":"other","region":"us-east-1"},
"attachments": {"kind":"object_store","driver":"s3","bucket":"duplicate","region":"us-east-1"}
}
}
}"#;
assert!(
config
.import_resources(inventory, ImportFormat::TerraformOutputs)
.is_err()
);
assert!(config.resource("another-bucket").is_none());
let Resource::ObjectStore(mut updated) = config.remove_resource("attachments").unwrap() else {
panic!("expected S3 configuration");
};
updated.bucket = "updated".into();
config
.insert_resource("attachments".into(), Resource::ObjectStore(updated.clone()))
.unwrap();
updated.bucket = "replaced".into();
config
.update_resource("attachments", Resource::ObjectStore(updated))
.unwrap();
assert_eq!(
config.resolve().unwrap().object_storage["attachments"].bucket,
"replaced"
);
}