Skip to content

Commit 6276387

Browse files
committed
Improve tests
1 parent 08b25fa commit 6276387

File tree

1 file changed

+46
-14
lines changed
  • crates/stackable-operator/src/builder/pod

1 file changed

+46
-14
lines changed

crates/stackable-operator/src/builder/pod/probe.rs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ use crate::time::Duration;
77

88
#[derive(Debug)]
99
pub struct ProbeBuilder<Action, Period> {
10+
// Mandatory field
1011
action: Action,
1112
period: Period,
1213

14+
// Fields with defaults
1315
success_threshold: i32,
1416
failure_threshold: i32,
1517
timeout: Duration,
@@ -22,7 +24,7 @@ impl Default for ProbeBuilder<(), ()> {
2224
Self {
2325
action: (),
2426
period: (),
25-
// The following values match the Kubernetes default
27+
// The following values match the Kubernetes defaults
2628
success_threshold: 1,
2729
failure_threshold: 1,
2830
timeout: Duration::from_secs(1),
@@ -64,10 +66,10 @@ impl<Period> ProbeBuilder<(), Period> {
6466
pub fn with_http_get_action_helper(
6567
self,
6668
port: u16,
67-
path: Option<impl Into<String>>,
69+
path: Option<String>,
6870
) -> ProbeBuilder<ProbeAction, Period> {
6971
self.with_http_get_action(HTTPGetAction {
70-
path: path.map(Into::into),
72+
path,
7173
port: IntOrString::Int(port.into()),
7274
..Default::default()
7375
})
@@ -138,12 +140,6 @@ impl ProbeBuilder<ProbeAction, ()> {
138140
}
139141
}
140142

141-
// success_threshold: i32,
142-
// failure_threshold: i32,
143-
// timeout: Duration,
144-
// initial_delay: Duration,
145-
// termination_grace_period: Duration,
146-
147143
impl ProbeBuilder<ProbeAction, Duration> {
148144
/// How often the probe must succeed before being considered successful.
149145
pub fn with_success_threshold(mut self, success_threshold: i32) -> Self {
@@ -169,6 +165,21 @@ impl ProbeBuilder<ProbeAction, Duration> {
169165
self
170166
}
171167

168+
pub fn with_timeout(mut self, timeout: Duration) -> Self {
169+
self.timeout = timeout;
170+
self
171+
}
172+
173+
pub fn with_initial_delay(mut self, initial_delay: Duration) -> Self {
174+
self.initial_delay = initial_delay;
175+
self
176+
}
177+
178+
pub fn with_termination_grace_period(mut self, termination_grace_period: Duration) -> Self {
179+
self.termination_grace_period = termination_grace_period;
180+
self
181+
}
182+
172183
/// The duration the probe needs to fail before being considered failed.
173184
///
174185
/// This internally calculates the needed failure threshold based on the period and passes that
@@ -231,11 +242,32 @@ mod tests {
231242
use super::*;
232243

233244
#[test]
234-
fn test_probe_builder() {
245+
fn test_probe_builder_minimal() {
246+
let probe = ProbeBuilder::default()
247+
.with_http_get_action_helper(8080, None)
248+
.with_period(Duration::from_secs(10))
249+
.build();
250+
251+
assert_eq!(
252+
probe.http_get,
253+
Some(HTTPGetAction {
254+
port: IntOrString::Int(8080),
255+
..Default::default()
256+
})
257+
);
258+
assert_eq!(probe.period_seconds, Some(10));
259+
}
260+
261+
#[test]
262+
fn test_probe_builder_complex() {
235263
let probe = ProbeBuilder::default()
236264
.with_exec_action_helper(["sleep", "1"])
237265
.with_period(Duration::from_secs(5))
266+
.with_success_threshold(2)
238267
.with_failure_threshold_duration(Duration::from_secs(33))
268+
.with_timeout(Duration::from_secs(3))
269+
.with_initial_delay(Duration::from_secs(7))
270+
.with_termination_grace_period(Duration::from_secs(4))
239271
.build();
240272

241273
assert_eq!(
@@ -247,12 +279,12 @@ mod tests {
247279
failure_threshold: Some(7),
248280
grpc: None,
249281
http_get: None,
250-
initial_delay_seconds: Some(0),
282+
initial_delay_seconds: Some(7),
251283
period_seconds: Some(5),
252-
success_threshold: Some(1),
284+
success_threshold: Some(2),
253285
tcp_socket: None,
254-
termination_grace_period_seconds: Some(0),
255-
timeout_seconds: Some(1),
286+
termination_grace_period_seconds: Some(4),
287+
timeout_seconds: Some(3),
256288
}
257289
);
258290
}

0 commit comments

Comments
 (0)