Skip to content

Commit 596a46a

Browse files
committed
refactor: dry cluster definitions and move helper code in own file
1 parent 29f71f8 commit 596a46a

12 files changed

Lines changed: 158 additions & 257 deletions

File tree

rust/operator-binary/src/crd/affinity.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ mod tests {
3535
},
3636
};
3737

38-
use crate::zk_controller::test_support::{
39-
minimal_zk, server_rolegroup_config, validated_cluster,
40-
};
38+
use crate::test_support::{minimal_zk, server_rolegroup_config, validated_cluster};
4139

4240
#[test]
4341
fn test_affinity_defaults() {

rust/operator-binary/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ use crate::{
4040
};
4141

4242
pub mod crd;
43+
#[cfg(test)]
44+
mod test_support;
4345
mod webhooks;
4446
mod zk_controller;
4547
mod znode_controller;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//! Shared helpers for building validated test clusters from minimal YAML fixtures.
2+
//!
3+
//! Lives at the crate root because it is used across `zk_controller`, `znode_controller` and
4+
//! `crd` test modules, not just one of them.
5+
6+
use std::str::FromStr;
7+
8+
use stackable_operator::{
9+
cli::OperatorEnvironmentOptions, commons::networking::DomainName,
10+
utils::cluster_info::KubernetesClusterInfo, v2::types::operator::RoleGroupName,
11+
};
12+
13+
use crate::{
14+
crd::{ZookeeperRole, authentication::DereferencedAuthenticationClasses, v1alpha1},
15+
zk_controller::{
16+
dereference::DereferencedObjects,
17+
validate::{ValidatedCluster, ZookeeperRoleGroupConfig, validate},
18+
},
19+
};
20+
21+
/// Parses a minimal `ZookeeperCluster` test fixture, defaulting `namespace`/`uid` so the
22+
/// validate step can build a [`ValidatedCluster`].
23+
pub fn minimal_zk(yaml: &str) -> v1alpha1::ZookeeperCluster {
24+
let mut zk: v1alpha1::ZookeeperCluster =
25+
serde_yaml::from_str(yaml).expect("invalid test ZookeeperCluster YAML");
26+
zk.metadata
27+
.namespace
28+
.get_or_insert_with(|| "default".to_owned());
29+
zk.metadata
30+
.uid
31+
.get_or_insert_with(|| "c27b3971-ca72-42c1-80a4-abdfc1db0ddd".to_owned());
32+
zk
33+
}
34+
35+
/// Minimal valid `ZookeeperCluster` YAML: a single `default` server role group with `replicas`
36+
/// servers and nothing else.
37+
///
38+
/// Use this (or [`minimal_zk_default`]) for tests that just need *a* valid cluster and don't
39+
/// care about the spec. Keep an inline fixture when the spec detail is the thing under test.
40+
pub fn minimal_zk_yaml(replicas: u16) -> String {
41+
format!(
42+
r#"
43+
apiVersion: zookeeper.stackable.tech/v1alpha1
44+
kind: ZookeeperCluster
45+
metadata:
46+
name: simple-zookeeper
47+
spec:
48+
image:
49+
productVersion: "3.9.5"
50+
servers:
51+
roleGroups:
52+
default:
53+
replicas: {replicas}
54+
"#
55+
)
56+
}
57+
58+
/// Parsed counterpart of [`minimal_zk_yaml`].
59+
pub fn minimal_zk_default(replicas: u16) -> v1alpha1::ZookeeperCluster {
60+
minimal_zk(&minimal_zk_yaml(replicas))
61+
}
62+
63+
pub fn cluster_info() -> KubernetesClusterInfo {
64+
KubernetesClusterInfo {
65+
cluster_domain: DomainName::try_from("cluster.local").expect("valid domain"),
66+
}
67+
}
68+
69+
fn operator_environment() -> OperatorEnvironmentOptions {
70+
OperatorEnvironmentOptions {
71+
operator_namespace: "stackable-operators".to_owned(),
72+
operator_service_name: "zookeeper-operator".to_owned(),
73+
image_repository: "oci.example.org".to_owned(),
74+
}
75+
}
76+
77+
/// Runs the real validate step against a minimal (auth-free) fixture, returning the result so
78+
/// tests can assert on validation errors.
79+
pub fn try_validate(
80+
zk: &v1alpha1::ZookeeperCluster,
81+
) -> Result<ValidatedCluster, crate::zk_controller::validate::Error> {
82+
try_validate_with_auth(zk, DereferencedAuthenticationClasses::new_for_tests())
83+
}
84+
85+
/// Runs the real validate step with caller-supplied (dereferenced) AuthenticationClasses, so
86+
/// tests can exercise the client-mTLS matrix.
87+
pub fn try_validate_with_auth(
88+
zk: &v1alpha1::ZookeeperCluster,
89+
authentication_classes: DereferencedAuthenticationClasses,
90+
) -> Result<ValidatedCluster, crate::zk_controller::validate::Error> {
91+
validate(
92+
zk,
93+
&DereferencedObjects {
94+
authentication_classes,
95+
},
96+
&operator_environment(),
97+
)
98+
}
99+
100+
/// Runs the real validate step against a minimal (auth-free) fixture.
101+
pub fn validated_cluster(zk: &v1alpha1::ZookeeperCluster) -> ValidatedCluster {
102+
try_validate(zk).expect("validate should succeed for the test fixture")
103+
}
104+
105+
/// Runs the real validate step with a single TLS client-auth `AuthenticationClass`.
106+
pub fn validated_cluster_with_client_auth(zk: &v1alpha1::ZookeeperCluster) -> ValidatedCluster {
107+
try_validate_with_auth(
108+
zk,
109+
DereferencedAuthenticationClasses::new_for_tests_with_tls_client_auth(),
110+
)
111+
.expect("validate should succeed for the test fixture")
112+
}
113+
114+
/// Looks up the validated, merged config of the named `server` role group together with its
115+
/// parsed [`RoleGroupName`] — the standard `(name, config)` inputs to the
116+
/// `build_server_rolegroup_*` functions. Panics if the group does not exist.
117+
pub fn server_rolegroup_config<'a>(
118+
validated: &'a ValidatedCluster,
119+
role_group: &str,
120+
) -> (RoleGroupName, &'a ZookeeperRoleGroupConfig) {
121+
let role_group_name = RoleGroupName::from_str(role_group).expect("valid role group name");
122+
let config = validated
123+
.role_group_configs
124+
.get(&ZookeeperRole::Server)
125+
.and_then(|groups| groups.get(&role_group_name))
126+
.unwrap_or_else(|| panic!("server role group {role_group:?} should exist"));
127+
(role_group_name, config)
128+
}

rust/operator-binary/src/zk_controller.rs

Lines changed: 7 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
};
4646

4747
pub(crate) mod build;
48-
mod dereference;
48+
pub(crate) mod dereference;
4949
pub(crate) mod validate;
5050

5151
pub const ZK_CONTROLLER_NAME: &str = "zookeepercluster";
@@ -390,138 +390,24 @@ pub fn error_policy(
390390
}
391391
}
392392

393-
/// Shared helpers for building validated test clusters from minimal YAML fixtures.
394-
#[cfg(test)]
395-
pub(crate) mod test_support {
396-
use std::str::FromStr;
397-
398-
use stackable_operator::{
399-
cli::OperatorEnvironmentOptions, commons::networking::DomainName,
400-
utils::cluster_info::KubernetesClusterInfo, v2::types::operator::RoleGroupName,
401-
};
402-
403-
use crate::{
404-
crd::{ZookeeperRole, authentication::DereferencedAuthenticationClasses, v1alpha1},
405-
zk_controller::{
406-
dereference::DereferencedObjects,
407-
validate::{ValidatedCluster, ZookeeperRoleGroupConfig, validate},
408-
},
409-
};
410-
411-
/// Parses a minimal `ZookeeperCluster` test fixture, defaulting `namespace`/`uid` so the
412-
/// validate step can build a [`ValidatedCluster`].
413-
pub fn minimal_zk(yaml: &str) -> v1alpha1::ZookeeperCluster {
414-
let mut zk: v1alpha1::ZookeeperCluster =
415-
serde_yaml::from_str(yaml).expect("invalid test ZookeeperCluster YAML");
416-
zk.metadata
417-
.namespace
418-
.get_or_insert_with(|| "default".to_owned());
419-
zk.metadata
420-
.uid
421-
.get_or_insert_with(|| "c27b3971-ca72-42c1-80a4-abdfc1db0ddd".to_owned());
422-
zk
423-
}
424-
425-
pub fn cluster_info() -> KubernetesClusterInfo {
426-
KubernetesClusterInfo {
427-
cluster_domain: DomainName::try_from("cluster.local").expect("valid domain"),
428-
}
429-
}
430-
431-
fn operator_environment() -> OperatorEnvironmentOptions {
432-
OperatorEnvironmentOptions {
433-
operator_namespace: "stackable-operators".to_owned(),
434-
operator_service_name: "zookeeper-operator".to_owned(),
435-
image_repository: "oci.example.org".to_owned(),
436-
}
437-
}
438-
439-
/// Runs the real validate step against a minimal (auth-free) fixture, returning the result so
440-
/// tests can assert on validation errors.
441-
pub fn try_validate(
442-
zk: &v1alpha1::ZookeeperCluster,
443-
) -> Result<ValidatedCluster, super::validate::Error> {
444-
try_validate_with_auth(zk, DereferencedAuthenticationClasses::new_for_tests())
445-
}
446-
447-
/// Runs the real validate step with caller-supplied (dereferenced) AuthenticationClasses, so
448-
/// tests can exercise the client-mTLS matrix.
449-
pub fn try_validate_with_auth(
450-
zk: &v1alpha1::ZookeeperCluster,
451-
authentication_classes: DereferencedAuthenticationClasses,
452-
) -> Result<ValidatedCluster, super::validate::Error> {
453-
validate(
454-
zk,
455-
&DereferencedObjects {
456-
authentication_classes,
457-
},
458-
&operator_environment(),
459-
)
460-
}
461-
462-
/// Runs the real validate step against a minimal (auth-free) fixture.
463-
pub fn validated_cluster(zk: &v1alpha1::ZookeeperCluster) -> ValidatedCluster {
464-
try_validate(zk).expect("validate should succeed for the test fixture")
465-
}
466-
467-
/// Runs the real validate step with a single TLS client-auth `AuthenticationClass`.
468-
pub fn validated_cluster_with_client_auth(zk: &v1alpha1::ZookeeperCluster) -> ValidatedCluster {
469-
try_validate_with_auth(
470-
zk,
471-
DereferencedAuthenticationClasses::new_for_tests_with_tls_client_auth(),
472-
)
473-
.expect("validate should succeed for the test fixture")
474-
}
475-
476-
/// Looks up the validated, merged config of the named `server` role group together with its
477-
/// parsed [`RoleGroupName`] — the standard `(name, config)` inputs to the
478-
/// `build_server_rolegroup_*` functions. Panics if the group does not exist.
479-
pub fn server_rolegroup_config<'a>(
480-
validated: &'a ValidatedCluster,
481-
role_group: &str,
482-
) -> (RoleGroupName, &'a ZookeeperRoleGroupConfig) {
483-
let role_group_name = RoleGroupName::from_str(role_group).expect("valid role group name");
484-
let config = validated
485-
.role_group_configs
486-
.get(&ZookeeperRole::Server)
487-
.and_then(|groups| groups.get(&role_group_name))
488-
.unwrap_or_else(|| panic!("server role group {role_group:?} should exist"));
489-
(role_group_name, config)
490-
}
491-
}
492-
493393
#[cfg(test)]
494394
mod tests {
495395
use std::collections::{BTreeMap, BTreeSet};
496396

497397
use stackable_operator::k8s_openapi::api::core::v1::ConfigMap;
498398

499399
use super::*;
500-
use crate::zk_controller::{
501-
build::properties::zoo_cfg,
400+
use crate::{
502401
test_support::{
503-
cluster_info, minimal_zk, server_rolegroup_config, validated_cluster,
402+
cluster_info, minimal_zk, minimal_zk_yaml, server_rolegroup_config, validated_cluster,
504403
validated_cluster_with_client_auth,
505404
},
506-
validate::ValidatedCluster,
405+
zk_controller::{build::properties::zoo_cfg, validate::ValidatedCluster},
507406
};
508407

509408
#[test]
510409
fn test_default_config() {
511-
let zookeeper_yaml = r#"
512-
apiVersion: zookeeper.stackable.tech/v1alpha1
513-
kind: ZookeeperCluster
514-
metadata:
515-
name: simple-zookeeper
516-
spec:
517-
image:
518-
productVersion: "3.9.5"
519-
servers:
520-
roleGroups:
521-
default:
522-
replicas: 3
523-
"#;
524-
let cm = build_config_map(zookeeper_yaml).data.unwrap();
410+
let cm = build_config_map(&minimal_zk_yaml(3)).data.unwrap();
525411
let config = cm.get("zoo.cfg").unwrap();
526412
assert!(config.contains(
527413
"authProvider.x509=org.apache.zookeeper.server.auth.X509AuthenticationProvider"
@@ -581,20 +467,7 @@ mod tests {
581467
// This test (together with the TLS x client-auth matrix tests below) is the source of truth
582468
// for the rendered `zoo.cfg` / `security.properties`; the kuttl smoke keeps only the
583469
// live-cluster readiness/status checks.
584-
let zookeeper_yaml = r#"
585-
apiVersion: zookeeper.stackable.tech/v1alpha1
586-
kind: ZookeeperCluster
587-
metadata:
588-
name: simple-zookeeper
589-
spec:
590-
image:
591-
productVersion: "3.9.5"
592-
servers:
593-
roleGroups:
594-
default:
595-
replicas: 3
596-
"#;
597-
let cm = build_config_map(zookeeper_yaml).data.unwrap();
470+
let cm = build_config_map(&minimal_zk_yaml(3)).data.unwrap();
598471

599472
// `security.properties` is fully operator-injected; assert it byte-for-byte.
600473
assert_eq!(
@@ -708,20 +581,7 @@ mod tests {
708581
fn test_custom_log_config_omits_logback() {
709582
// Automatic logging renders `logback.xml` into the ConfigMap; a custom log ConfigMap
710583
// suppresses it (the `Custom` arm of `build_logback_config`).
711-
let automatic_yaml = r#"
712-
apiVersion: zookeeper.stackable.tech/v1alpha1
713-
kind: ZookeeperCluster
714-
metadata:
715-
name: simple-zookeeper
716-
spec:
717-
image:
718-
productVersion: "3.9.5"
719-
servers:
720-
roleGroups:
721-
default:
722-
replicas: 3
723-
"#;
724-
let automatic = build_config_map(automatic_yaml).data.unwrap();
584+
let automatic = build_config_map(&minimal_zk_yaml(3)).data.unwrap();
725585
assert!(automatic.contains_key("logback.xml"));
726586

727587
let custom_yaml = r#"

rust/operator-binary/src/zk_controller/build/jvm.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ mod tests {
8484
use super::*;
8585
use crate::{
8686
crd::v1alpha1::ZookeeperCluster,
87-
zk_controller::test_support::{minimal_zk, server_rolegroup_config, validated_cluster},
87+
test_support::{
88+
minimal_zk, minimal_zk_default, server_rolegroup_config, validated_cluster,
89+
},
8890
};
8991

9092
/// The validated, merged config for the `default` server role group.
@@ -96,20 +98,7 @@ mod tests {
9698

9799
#[test]
98100
fn test_construct_jvm_arguments_defaults() {
99-
let input = r#"
100-
apiVersion: zookeeper.stackable.tech/v1alpha1
101-
kind: ZookeeperCluster
102-
metadata:
103-
name: simple-zookeeper
104-
spec:
105-
image:
106-
productVersion: "3.9.5"
107-
servers:
108-
roleGroups:
109-
default:
110-
replicas: 1
111-
"#;
112-
let zk = minimal_zk(input);
101+
let zk = minimal_zk_default(1);
113102
let rg = server_default(&zk);
114103
let non_heap_jvm_args = construct_non_heap_jvm_args(&rg);
115104
let zk_server_heap_env =

rust/operator-binary/src/zk_controller/build/properties/zoo_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl ValidatedCluster {
178178
#[cfg(test)]
179179
mod tests {
180180
use super::*;
181-
use crate::zk_controller::test_support::{
181+
use crate::test_support::{
182182
cluster_info, minimal_zk, server_rolegroup_config, validated_cluster,
183183
};
184184

rust/operator-binary/src/zk_controller/build/resource/discovery.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,8 @@ mod tests {
226226

227227
use super::*;
228228
use crate::{
229-
zk_controller::{
230-
ZK_CONTROLLER_NAME,
231-
test_support::{minimal_zk, validated_cluster},
232-
},
229+
test_support::{minimal_zk, validated_cluster},
230+
zk_controller::ZK_CONTROLLER_NAME,
233231
znode_controller::{
234232
ZNODE_CONTROLLER_NAME,
235233
validate::test_support::{minimal_znode, validated_znode},

0 commit comments

Comments
 (0)