Skip to content

Commit 3c4125d

Browse files
committed
improve unit-test coverage for all derived values
1 parent 9f50fa5 commit 3c4125d

1 file changed

Lines changed: 96 additions & 1 deletion

File tree

rust/operator-binary/src/controller/validate.rs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,110 @@ fn inject_cluster_id(env_overrides: EnvVarSet, cluster_id: Option<&str>) -> Resu
414414
mod tests {
415415
use std::str::FromStr;
416416

417-
use stackable_operator::v2::builder::pod::container::{EnvVarName, EnvVarSet};
417+
use stackable_operator::v2::{
418+
builder::pod::container::{EnvVarName, EnvVarSet},
419+
types::operator::RoleGroupName,
420+
};
418421

419422
use super::{KAFKA_CLUSTER_ID_ENV, inject_cluster_id};
423+
use crate::{
424+
controller::test_support::{app_version_label, minimal_kafka, validated_cluster},
425+
crd::role::KafkaRole,
426+
};
420427

421428
fn cluster_id_value(env: &EnvVarSet) -> Option<String> {
422429
let name = EnvVarName::from_str(KAFKA_CLUSTER_ID_ENV).unwrap();
423430
env.get(&name).and_then(|var| var.value.clone())
424431
}
425432

433+
/// Locks every value the validate step itself derives from the minimal KRaft fixture — so a
434+
/// validation regression fails here, with a validate-shaped message, instead of surfacing as
435+
/// a confusing build-test failure downstream.
436+
///
437+
/// The merged per-role-group config (resources, affinity, logging defaults, …) is produced by
438+
/// `with_validated_config` and the config defaults, whose contracts are tested in operator-rs;
439+
/// only the values this module derives on top are re-asserted here.
440+
#[test]
441+
fn validate_ok_derives_expected_values() {
442+
let kafka = minimal_kafka(
443+
r#"
444+
apiVersion: kafka.stackable.tech/v1alpha1
445+
kind: KafkaCluster
446+
metadata:
447+
name: simple-kafka
448+
namespace: default
449+
uid: 12345678-1234-1234-1234-123456789012
450+
spec:
451+
image:
452+
productVersion: 3.9.2
453+
clusterConfig:
454+
metadataManager: kraft
455+
controllers:
456+
roleGroups:
457+
default:
458+
replicas: 3
459+
brokers:
460+
roleGroups:
461+
default:
462+
replicas: 3
463+
"#,
464+
);
465+
let cluster = validated_cluster(&kafka);
466+
467+
assert_eq!(cluster.name.to_string(), "simple-kafka");
468+
assert_eq!(cluster.namespace.to_string(), "default");
469+
assert_eq!(
470+
cluster.uid.to_string(),
471+
"12345678-1234-1234-1234-123456789012"
472+
);
473+
assert_eq!(cluster.cluster_domain.to_string(), "cluster.local");
474+
assert_eq!(
475+
cluster.image.image,
476+
format!("oci.example.org/kafka:{}", app_version_label("3.9.2"))
477+
);
478+
assert_eq!(cluster.image.product_version, "3.9.2");
479+
assert_eq!(
480+
cluster.product_version.to_string(),
481+
app_version_label("3.9.2")
482+
);
483+
484+
// KRaft mode: no ZooKeeper ConfigMap; no user-supplied broker-id map or authorization.
485+
let cluster_config = &cluster.cluster_config;
486+
assert!(cluster_config.is_kraft_mode());
487+
assert_eq!(cluster_config.zookeeper_config_map_name, None);
488+
assert_eq!(cluster_config.broker_id_pod_config_map_name, None);
489+
assert!(cluster_config.authorization_config.is_none());
490+
491+
// TLS defaults: server and internal both use the `tls` SecretClass; no Kerberos or OPA.
492+
let security = &cluster_config.kafka_security;
493+
assert!(security.tls_enabled());
494+
assert_eq!(security.tls_server_secret_class(), Some("tls"));
495+
assert_eq!(security.tls_internal_secret_class(), "tls");
496+
assert!(!security.has_kerberos_enabled());
497+
assert_eq!(security.opa_secret_class(), None);
498+
499+
// Both roles are present, with default (enabled) PDB configs.
500+
let roles: Vec<_> = cluster.role_configs.keys().collect();
501+
assert_eq!(roles, [&KafkaRole::Broker, &KafkaRole::Controller]);
502+
for role_config in cluster.role_configs.values() {
503+
assert!(role_config.pdb.enabled);
504+
assert_eq!(role_config.pdb.max_unavailable, None);
505+
}
506+
507+
// One `default` role group per role. The KRaft cluster id (derived from the cluster
508+
// name) is injected into every role group's env overrides.
509+
let default_rg = RoleGroupName::from_str("default").expect("valid role group name");
510+
for role in [KafkaRole::Broker, KafkaRole::Controller] {
511+
let role_group = &cluster.role_group_configs[&role][&default_rg];
512+
assert_eq!(role_group.replicas, Some(3));
513+
assert_eq!(
514+
cluster_id_value(&role_group.env_overrides),
515+
Some("simple-kafka".to_string())
516+
);
517+
assert_eq!(role_group.config.logging.vector_container, None);
518+
}
519+
}
520+
426521
#[test]
427522
fn injects_cluster_id_when_absent() {
428523
let env = inject_cluster_id(EnvVarSet::new(), Some("my-id")).unwrap();

0 commit comments

Comments
 (0)