Skip to content

Commit

Permalink
Add a test for config generation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjstone committed Aug 28, 2024
1 parent 6f51e00 commit 5c5c689
Showing 1 changed file with 170 additions and 3 deletions.
173 changes: 170 additions & 3 deletions nexus/types/src/deployment/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct ClickhouseClusterConfig {

impl ClickhouseClusterConfig {
/// Create an intitial deployment for the first blueprint
pub fn new<'a>(
pub fn new(
cluster_name: String,
all_blueprint_zones: &BTreeMap<SledUuid, BlueprintZonesConfig>,
) -> ClickhouseClusterConfig {
Expand Down Expand Up @@ -133,9 +133,8 @@ impl ClickhouseClusterConfig {
//
// All keepers are new so start numbering the ids from 1
let mut keepers = BTreeMap::new();
let mut max_used_keeper_id = raft_servers.len() as u64 + 1;
let max_used_keeper_id = raft_servers.len() as u64;
for kz in keeper_zones {
max_used_keeper_id += 1;
let keeper_config = KeeperConfig {
logger: LogConfig {
level: LogLevel::Trace,
Expand Down Expand Up @@ -206,3 +205,171 @@ fn all_in_service_clickhouse_zones(

(server_zones, keeper_zones)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::deployment::blueprint_zone_type;
use crate::deployment::BlueprintZoneType;
use nexus_sled_agent_shared::inventory::OmicronZoneDataset;
use omicron_common::api::external::Generation;
use std::collections::BTreeSet;
use std::net::{Ipv6Addr, SocketAddrV6};

/// Create a few in service `ClickhouseKeeper` and `ClickhouseServer` zones.
pub fn gen_in_service_clickhouse_zones(
) -> BTreeMap<SledUuid, BlueprintZonesConfig> {
(0..3u64)
.map(|i| {
let mut zones = vec![];

let sled_id = SledUuid::new_v4();
let keeper_zone = BlueprintZoneConfig {
disposition: BlueprintZoneDisposition::InService,
id: OmicronZoneUuid::new_v4(),
underlay_address: Ipv6Addr::new(
0,
0,
0,
0,
0,
0,
0,
i as u16 + 1,
),
filesystem_pool: None,
zone_type: BlueprintZoneType::ClickhouseKeeper(
blueprint_zone_type::ClickhouseKeeper {
address: SocketAddrV6::new(
Ipv6Addr::new(
0,
0,
0,
0,
0,
0,
0,
i as u16 + 1,
),
CLICKHOUSE_KEEPER_PORT,
0,
0,
),
dataset: OmicronZoneDataset {
pool_name: format!("oxp_{}", Uuid::new_v4())
.parse()
.expect("bad name"),
},
},
),
};
// Each sled getgs a keeper zone
zones.push(keeper_zone);

// Only 2 sleds get clickhouse server zones
if i <= 1 {
let server_zone = BlueprintZoneConfig {
disposition: BlueprintZoneDisposition::InService,
id: OmicronZoneUuid::new_v4(),
underlay_address: Ipv6Addr::new(
0,
0,
0,
0,
0,
0,
0,
i as u16 + 10,
),
filesystem_pool: None,
zone_type: BlueprintZoneType::ClickhouseServer(
blueprint_zone_type::ClickhouseServer {
address: SocketAddrV6::new(
Ipv6Addr::new(
0,
0,
0,
0,
0,
0,
0,
i as u16 + 10,
),
CLICKHOUSE_HTTP_PORT,
0,
0,
),
dataset: OmicronZoneDataset {
pool_name: format!(
"oxp_{}",
Uuid::new_v4()
)
.parse()
.expect("bad name"),
},
},
),
};
zones.push(server_zone);
}

(
sled_id,
BlueprintZonesConfig {
generation: Generation::new(),
zones,
},
)
})
.collect()
}

#[test]
fn test_new_clickhouse_cluster_config() {
let cluster_name = "test-cluster".to_string();
let all_blueprint_zones = gen_in_service_clickhouse_zones();
let config = ClickhouseClusterConfig::new(
cluster_name.clone(),
&all_blueprint_zones,
);

assert_eq!(config.generation, 1);
assert_eq!(config.max_used_server_id, 2);
assert_eq!(config.max_used_keeper_id, 3);
assert_eq!(config.cluster_name, cluster_name);

println!("{:#?}", config);

// Ensure we have 3 valid keeper configs
for zones_config in all_blueprint_zones.values() {
let keeper_bp_zone_config = zones_config.zones.first().unwrap();
let keeper_config =
config.keepers.get(&keeper_bp_zone_config.id).unwrap();
assert!(keeper_bp_zone_config.zone_type.is_clickhouse_keeper());
assert_eq!(
keeper_bp_zone_config.underlay_address.to_string(),
keeper_config.listen_host
);
assert_eq!(keeper_config.raft_config.servers.len(), 3);
}

// Ensure that we have 2 valid clickhouse server configs
for zones_config in all_blueprint_zones.values() {
let Some(server_bp_zone_config) = zones_config.zones.get(1) else {
// We only have 2 clickhouse server configs
continue;
};
let server_config =
config.servers.get(&server_bp_zone_config.id).unwrap();
assert!(server_bp_zone_config.zone_type.is_clickhouse_server());
assert_eq!(
server_bp_zone_config.underlay_address.to_string(),
server_config.listen_host
);
assert_eq!(server_config.remote_servers.replicas.len(), 2);
assert_eq!(server_config.keepers.nodes.len(), 3);
}

// TODO: Verify some properties about the configs such as distinct ids, etc..
}
}

0 comments on commit 5c5c689

Please sign in to comment.