Skip to content

Commit 6b040e0

Browse files
committed
refactor: cover shape checks from 13-assert
1 parent 58c0f87 commit 6b040e0

3 files changed

Lines changed: 452 additions & 0 deletions

File tree

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,80 @@ pub fn build_pdb(
3737
fn max_unavailable_servers() -> u16 {
3838
1
3939
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use stackable_operator::k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;
44+
45+
use super::*;
46+
use crate::zk_controller::test_support::{minimal_zk, validated_cluster};
47+
48+
/// By default PDBs are enabled and default to `maxUnavailable: 1` for the server role.
49+
#[test]
50+
fn pdb_enabled_by_default_with_max_unavailable_one() {
51+
let validated = validated_cluster(&minimal_zk(
52+
r#"
53+
apiVersion: zookeeper.stackable.tech/v1alpha1
54+
kind: ZookeeperCluster
55+
metadata:
56+
name: simple-zookeeper
57+
spec:
58+
image:
59+
productVersion: "3.9.5"
60+
servers:
61+
roleGroups:
62+
default:
63+
replicas: 3
64+
"#,
65+
));
66+
let role_config = validated.role_config.as_ref().expect("role config present");
67+
68+
let pdb = build_pdb(&role_config.pdb, &validated, &ZookeeperRole::Server)
69+
.expect("PDB enabled by default");
70+
let spec = pdb.spec.as_ref().unwrap();
71+
assert_eq!(spec.max_unavailable, Some(IntOrString::Int(1)));
72+
73+
let match_labels = spec
74+
.selector
75+
.as_ref()
76+
.unwrap()
77+
.match_labels
78+
.as_ref()
79+
.unwrap();
80+
assert_eq!(
81+
match_labels
82+
.get("app.kubernetes.io/component")
83+
.map(String::as_str),
84+
Some("server")
85+
);
86+
}
87+
88+
/// Disabling the PDB via `roleConfig` produces no `PodDisruptionBudget`.
89+
#[test]
90+
fn pdb_disabled_returns_none() {
91+
let validated = validated_cluster(&minimal_zk(
92+
r#"
93+
apiVersion: zookeeper.stackable.tech/v1alpha1
94+
kind: ZookeeperCluster
95+
metadata:
96+
name: simple-zookeeper
97+
spec:
98+
image:
99+
productVersion: "3.9.5"
100+
servers:
101+
roleConfig:
102+
podDisruptionBudget:
103+
enabled: false
104+
roleGroups:
105+
default:
106+
replicas: 3
107+
"#,
108+
));
109+
let role_config = validated.role_config.as_ref().expect("role config present");
110+
111+
assert!(
112+
build_pdb(&role_config.pdb, &validated, &ZookeeperRole::Server).is_none(),
113+
"PDB should be None when disabled"
114+
);
115+
}
116+
}

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,125 @@ pub(crate) fn build_server_rolegroup_metrics_service(
115115
status: None,
116116
}
117117
}
118+
119+
#[cfg(test)]
120+
mod tests {
121+
use std::str::FromStr;
122+
123+
use super::*;
124+
use crate::{
125+
crd::ZookeeperRole,
126+
zk_controller::test_support::{minimal_zk, validated_cluster},
127+
};
128+
129+
const DEFAULT_ZK: &str = r#"
130+
apiVersion: zookeeper.stackable.tech/v1alpha1
131+
kind: ZookeeperCluster
132+
metadata:
133+
name: simple-zookeeper
134+
spec:
135+
image:
136+
productVersion: "3.9.5"
137+
servers:
138+
roleGroups:
139+
default:
140+
replicas: 3
141+
"#;
142+
143+
fn port(service: &Service, name: &str) -> i32 {
144+
service
145+
.spec
146+
.as_ref()
147+
.unwrap()
148+
.ports
149+
.as_ref()
150+
.unwrap()
151+
.iter()
152+
.find(|p| p.name.as_deref() == Some(name))
153+
.unwrap_or_else(|| panic!("missing service port {name}"))
154+
.port
155+
}
156+
157+
#[test]
158+
fn headless_service_shape() {
159+
let validated = validated_cluster(&minimal_zk(DEFAULT_ZK));
160+
let rg = RoleGroupName::from_str("default").expect("valid role group name");
161+
let service = build_server_rolegroup_headless_service(&validated, &rg);
162+
163+
assert_eq!(
164+
service.metadata.name.as_deref(),
165+
Some("simple-zookeeper-server-default-headless")
166+
);
167+
168+
let spec = service.spec.as_ref().unwrap();
169+
assert_eq!(spec.type_.as_deref(), Some("ClusterIP"));
170+
assert_eq!(spec.cluster_ip.as_deref(), Some("None"));
171+
assert_eq!(spec.publish_not_ready_addresses, Some(true));
172+
173+
// Only the leader/election ports (independent of the client TLS matrix).
174+
assert_eq!(port(&service, ZOOKEEPER_LEADER_PORT_NAME), 2888);
175+
assert_eq!(port(&service, ZOOKEEPER_ELECTION_PORT_NAME), 3888);
176+
assert_eq!(spec.ports.as_ref().unwrap().len(), 2);
177+
178+
let selector = spec.selector.as_ref().unwrap();
179+
assert_eq!(
180+
selector
181+
.get("app.kubernetes.io/component")
182+
.map(String::as_str),
183+
Some("server")
184+
);
185+
assert_eq!(
186+
selector
187+
.get("app.kubernetes.io/role-group")
188+
.map(String::as_str),
189+
Some("default")
190+
);
191+
}
192+
193+
#[test]
194+
fn metrics_service_shape_and_prometheus_annotations() {
195+
let validated = validated_cluster(&minimal_zk(DEFAULT_ZK));
196+
let rg = RoleGroupName::from_str("default").expect("valid role group name");
197+
let rg_config = validated.role_group_configs[&ZookeeperRole::Server][&rg].clone();
198+
let service = build_server_rolegroup_metrics_service(&validated, &rg, &rg_config);
199+
200+
assert_eq!(
201+
service.metadata.name.as_deref(),
202+
Some("simple-zookeeper-server-default-metrics")
203+
);
204+
205+
// Prometheus scrape annotations.
206+
let annotations = service.metadata.annotations.as_ref().unwrap();
207+
assert_eq!(
208+
annotations.get("prometheus.io/path").map(String::as_str),
209+
Some("/metrics")
210+
);
211+
assert_eq!(
212+
annotations.get("prometheus.io/port").map(String::as_str),
213+
Some("7000")
214+
);
215+
assert_eq!(
216+
annotations.get("prometheus.io/scheme").map(String::as_str),
217+
Some("http")
218+
);
219+
assert_eq!(
220+
annotations.get("prometheus.io/scrape").map(String::as_str),
221+
Some("true")
222+
);
223+
// ... and the scrape label.
224+
assert_eq!(
225+
service
226+
.metadata
227+
.labels
228+
.as_ref()
229+
.unwrap()
230+
.get("prometheus.io/scrape")
231+
.map(String::as_str),
232+
Some("true")
233+
);
234+
235+
// Legacy JMX port plus the Prometheus http port.
236+
assert_eq!(port(&service, JMX_METRICS_PORT_NAME), 9505);
237+
assert_eq!(port(&service, METRICS_PROVIDER_HTTP_PORT_NAME), 7000);
238+
}
239+
}

0 commit comments

Comments
 (0)