|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use bollard_stubs::models::HealthConfig; |
| 4 | + |
| 5 | +/// Represents a custom health check configuration for a container. |
| 6 | +/// |
| 7 | +/// This mirrors the options available in Docker's `HEALTHCHECK` instruction, |
| 8 | +/// allowing users to define custom health checks at runtime. |
| 9 | +/// |
| 10 | +/// # Example |
| 11 | +/// |
| 12 | +/// ```rust,no_run |
| 13 | +/// use std::time::Duration; |
| 14 | +/// use testcontainers::core::Healthcheck; |
| 15 | +/// |
| 16 | +/// let healthcheck = Healthcheck::cmd_shell("mysqladmin ping -h localhost -u root -proot") |
| 17 | +/// .with_interval(Duration::from_secs(2)) |
| 18 | +/// .with_timeout(Duration::from_secs(1)) |
| 19 | +/// .with_retries(5) |
| 20 | +/// .with_start_period(Duration::from_secs(10)); |
| 21 | +/// ``` |
| 22 | +#[derive(Debug, Clone)] |
| 23 | +pub struct Healthcheck { |
| 24 | + /// The test command to run. |
| 25 | + test: Vec<String>, |
| 26 | + /// The time to wait between health checks. |
| 27 | + interval: Option<Duration>, |
| 28 | + /// The time to wait before considering the health check failed. |
| 29 | + timeout: Option<Duration>, |
| 30 | + /// The number of consecutive failures needed to consider a container as unhealthy. |
| 31 | + retries: Option<u32>, |
| 32 | + /// Start period for the container to initialize before starting health-retries countdown. |
| 33 | + start_period: Option<Duration>, |
| 34 | + /// The time to wait between health checks during the start period. |
| 35 | + start_interval: Option<Duration>, |
| 36 | +} |
| 37 | + |
| 38 | +impl Healthcheck { |
| 39 | + /// Creates a new `Healthcheck` that disables the health check for the container. |
| 40 | + /// |
| 41 | + /// This is equivalent to `HEALTHCHECK NONE` in a Dockerfile. |
| 42 | + pub fn none() -> Self { |
| 43 | + Self { |
| 44 | + test: vec!["NONE".to_string()], |
| 45 | + interval: None, |
| 46 | + timeout: None, |
| 47 | + retries: None, |
| 48 | + start_period: None, |
| 49 | + start_interval: None, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Creates a new `Healthcheck` with the specified shell command. |
| 54 | + /// |
| 55 | + /// This is equivalent to `HEALTHCHECK CMD-SHELL <command>` in the Docker API. |
| 56 | + pub fn cmd_shell(command: impl Into<String>) -> Self { |
| 57 | + Self { |
| 58 | + test: vec!["CMD-SHELL".to_string(), command.into()], |
| 59 | + interval: None, |
| 60 | + timeout: None, |
| 61 | + retries: None, |
| 62 | + start_period: None, |
| 63 | + start_interval: None, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// Creates a new `Healthcheck` with the specified command and arguments. |
| 68 | + /// |
| 69 | + /// This is equivalent to `HEALTHCHECK CMD ["<command>", "<arg1>", ...]` in the Docker API. |
| 70 | + /// The command can be any iterator that yields string-like items. |
| 71 | + pub fn cmd<I, S>(command: I) -> Self |
| 72 | + where |
| 73 | + I: IntoIterator<Item = S>, |
| 74 | + S: AsRef<str>, |
| 75 | + { |
| 76 | + let mut test = vec!["CMD".to_string()]; |
| 77 | + test.extend(command.into_iter().map(|s| s.as_ref().to_owned())); |
| 78 | + Self { |
| 79 | + test, |
| 80 | + interval: None, |
| 81 | + timeout: None, |
| 82 | + retries: None, |
| 83 | + start_period: None, |
| 84 | + start_interval: None, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Creates an empty healthcheck configuration to customize an image's existing healthcheck. |
| 89 | + /// |
| 90 | + /// This keeps the original healthcheck command from the image, but allows overriding |
| 91 | + /// other parameters like `interval` or `retries`. In the Docker API, this is achieved |
| 92 | + /// by sending an empty `test` field along with the other desired values. |
| 93 | + pub fn empty() -> Self { |
| 94 | + Self { |
| 95 | + test: vec![], |
| 96 | + interval: None, |
| 97 | + timeout: None, |
| 98 | + retries: None, |
| 99 | + start_period: None, |
| 100 | + start_interval: None, |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// Sets the interval between health checks. |
| 105 | + /// |
| 106 | + /// Passing `None` will clear the value and use the Docker default. |
| 107 | + pub fn with_interval(mut self, interval: impl Into<Option<Duration>>) -> Self { |
| 108 | + self.interval = interval.into(); |
| 109 | + self |
| 110 | + } |
| 111 | + |
| 112 | + /// Sets the timeout for each health check. |
| 113 | + /// |
| 114 | + /// Passing `None` will clear the value and use the Docker default. |
| 115 | + pub fn with_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self { |
| 116 | + self.timeout = timeout.into(); |
| 117 | + self |
| 118 | + } |
| 119 | + |
| 120 | + /// Sets the number of consecutive failures needed to consider the container unhealthy. |
| 121 | + /// |
| 122 | + /// Passing `None` will clear the value and use the Docker default. |
| 123 | + pub fn with_retries(mut self, retries: impl Into<Option<u32>>) -> Self { |
| 124 | + self.retries = retries.into(); |
| 125 | + self |
| 126 | + } |
| 127 | + |
| 128 | + /// Sets the start period for the container to initialize before starting health checks. |
| 129 | + /// |
| 130 | + /// Passing `None` will clear the value and use the Docker default. |
| 131 | + pub fn with_start_period(mut self, start_period: impl Into<Option<Duration>>) -> Self { |
| 132 | + self.start_period = start_period.into(); |
| 133 | + self |
| 134 | + } |
| 135 | + |
| 136 | + /// Sets the interval between health checks during the start period. |
| 137 | + /// |
| 138 | + /// Passing `None` will clear the value and use the Docker default. |
| 139 | + pub fn with_start_interval(mut self, interval: impl Into<Option<Duration>>) -> Self { |
| 140 | + self.start_interval = interval.into(); |
| 141 | + self |
| 142 | + } |
| 143 | + |
| 144 | + /// Returns the test command as a vector of strings. |
| 145 | + pub fn test(&self) -> &[String] { |
| 146 | + &self.test |
| 147 | + } |
| 148 | + |
| 149 | + /// Returns the interval between health checks. |
| 150 | + pub fn interval(&self) -> Option<Duration> { |
| 151 | + self.interval |
| 152 | + } |
| 153 | + |
| 154 | + /// Returns the timeout for each health check. |
| 155 | + pub fn timeout(&self) -> Option<Duration> { |
| 156 | + self.timeout |
| 157 | + } |
| 158 | + |
| 159 | + /// Returns the number of retries before considering the container unhealthy. |
| 160 | + pub fn retries(&self) -> Option<u32> { |
| 161 | + self.retries |
| 162 | + } |
| 163 | + |
| 164 | + /// Returns the start period before health checks begin. |
| 165 | + pub fn start_period(&self) -> Option<Duration> { |
| 166 | + self.start_period |
| 167 | + } |
| 168 | + |
| 169 | + /// Returns the interval between health checks during the start period. |
| 170 | + pub fn start_interval(&self) -> Option<Duration> { |
| 171 | + self.start_interval |
| 172 | + } |
| 173 | + |
| 174 | + /// Converts this `Healthcheck` into a bollard `HealthConfig` for use with Docker API. |
| 175 | + pub(crate) fn into_health_config(self) -> HealthConfig { |
| 176 | + // Helper to convert Duration to i64 nanoseconds, capping at i64::MAX. |
| 177 | + // Docker interprets 0 as the default value (e.g., 30s for interval). |
| 178 | + // A negative value would disable the healthcheck, but our `Duration` type ensures it's always non-negative. |
| 179 | + let to_nanos = |d: Duration| -> i64 { d.as_nanos().try_into().unwrap_or(i64::MAX) }; |
| 180 | + |
| 181 | + HealthConfig { |
| 182 | + test: Some(self.test), |
| 183 | + interval: self.interval.map(to_nanos), |
| 184 | + timeout: self.timeout.map(to_nanos), |
| 185 | + retries: self.retries.map(|r| r as i64), |
| 186 | + start_period: self.start_period.map(to_nanos), |
| 187 | + start_interval: self.start_interval.map(to_nanos), |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments