232 lines
6.6 KiB
Rust
232 lines
6.6 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use clusters::{
|
|
Address, ConfigError, DOCUMENTS, DeploymentConfig, Resource, SqlCluster, 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 {
|
|
bindings: BTreeMap::from([(DOCUMENTS.into(), "regional".into())]),
|
|
resources: BTreeMap::from([(
|
|
"regional".into(),
|
|
Resource::Sql(SqlCluster {
|
|
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.binding(DOCUMENTS).unwrap().as_sql().unwrap();
|
|
assert!(
|
|
resolved
|
|
.primary
|
|
.connection_string()
|
|
.ends_with("sslmode=verify-full")
|
|
);
|
|
assert!(
|
|
resolved.replicas[0]
|
|
.connection_string()
|
|
.ends_with("sslmode=verify-full")
|
|
);
|
|
let Some(Resource::Sql(cluster)) = config.resources.get_mut("regional") else {
|
|
panic!("expected a SQL resource");
|
|
};
|
|
cluster.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
|
|
.binding(DOCUMENTS)
|
|
.unwrap()
|
|
.as_sql()
|
|
.unwrap()
|
|
.driver,
|
|
SqlDriver::Postgres
|
|
);
|
|
assert_eq!(
|
|
snapshot.binding("legacy").unwrap().as_sql().unwrap().driver,
|
|
SqlDriver::Mysql
|
|
);
|
|
assert_eq!(
|
|
snapshot
|
|
.binding("attachments")
|
|
.unwrap()
|
|
.as_object_store()
|
|
.unwrap()
|
|
.bucket,
|
|
"bedrock-attachments"
|
|
);
|
|
assert_eq!(
|
|
snapshot
|
|
.binding(DOCUMENTS)
|
|
.unwrap()
|
|
.as_sql()
|
|
.unwrap()
|
|
.max_connections,
|
|
5
|
|
);
|
|
|
|
let serialized = config.to_yaml().unwrap();
|
|
let roundtrip = DeploymentConfig::parse(&serialized, ConfigFormat::Yaml)
|
|
.unwrap()
|
|
.resolve()
|
|
.unwrap();
|
|
assert_eq!(
|
|
roundtrip
|
|
.binding("legacy")
|
|
.unwrap()
|
|
.as_sql()
|
|
.unwrap()
|
|
.driver,
|
|
SqlDriver::Mysql
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn imports_catalog_atomically_and_preserves_role_references() {
|
|
use clusters::{ConfigFormat, Driver, ImportFormat, ObjectStoreDriver};
|
|
|
|
let mut config =
|
|
DeploymentConfig::parse("config_version = 'test'", ConfigFormat::Toml).unwrap();
|
|
config
|
|
.import_resources(
|
|
include_str!("../../../deploy/kubernetes-catalog.yaml"),
|
|
ImportFormat::KubernetesConfigMap,
|
|
)
|
|
.unwrap();
|
|
config.bindings.insert(DOCUMENTS.into(), "pg-main".into());
|
|
config.bindings.insert("files".into(), "attachments".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());
|
|
assert!(matches!(
|
|
config.remove_resource("attachments"),
|
|
Err(ConfigError::ResourceInUse)
|
|
));
|
|
config.bindings.remove("files");
|
|
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()
|
|
.resource("attachments")
|
|
.unwrap()
|
|
.as_object_store()
|
|
.unwrap()
|
|
.bucket,
|
|
"replaced"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_configuration_is_normalized_at_load() {
|
|
use clusters::ConfigFormat;
|
|
|
|
let config = DeploymentConfig::parse(
|
|
r#"{
|
|
"cluster":"east",
|
|
"clusters":{"east":{"driver":"postgres","primary":"postgres://u@primary/db"}},
|
|
"object_storage":{"files":{"driver":"s3","bucket":"files","region":"us-east-1"}}
|
|
}"#,
|
|
ConfigFormat::Json,
|
|
)
|
|
.unwrap();
|
|
let snapshot = config.resolve().unwrap();
|
|
assert_eq!(
|
|
snapshot
|
|
.binding(DOCUMENTS)
|
|
.unwrap()
|
|
.as_sql()
|
|
.unwrap()
|
|
.driver,
|
|
SqlDriver::Postgres
|
|
);
|
|
assert_eq!(
|
|
snapshot
|
|
.resource("files")
|
|
.unwrap()
|
|
.as_object_store()
|
|
.unwrap()
|
|
.bucket,
|
|
"files"
|
|
);
|
|
|
|
let config =
|
|
DeploymentConfig::parse("sqlite_path: custom.sqlite3", ConfigFormat::Yaml).unwrap();
|
|
let snapshot = config.resolve().unwrap();
|
|
let connection = snapshot.binding(DOCUMENTS).unwrap().as_sql().unwrap();
|
|
assert!(
|
|
connection
|
|
.primary
|
|
.connection_string()
|
|
.ends_with("/custom.sqlite3?mode=rwc")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn missing_binding_target_is_rejected_for_any_role() {
|
|
let mut config = DeploymentConfig::default();
|
|
config
|
|
.bindings
|
|
.insert("attachments".into(), "missing".into());
|
|
assert!(
|
|
matches!(config.resolve(), Err(ConfigError::UnknownBinding { role, target })
|
|
if role == "attachments" && target == "missing")
|
|
);
|
|
}
|