Skip to content

Commit 8b58c2c

Browse files
authored
Merge pull request #1917 from mintlayer/podman_runner_improvements
Podman runner - ability to restart after stopping
2 parents 7ac9bba + afa1d9e commit 8b58c2c

File tree

1 file changed

+47
-50
lines changed

1 file changed

+47
-50
lines changed

api-server/storage-test-suite/src/podman.rs

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ pub struct Podman {
4343
env: Vec<(String, String)>,
4444
port_mappings: Vec<(Option<u16>, u16)>,
4545
container: Container,
46-
stopped: bool,
46+
// `None` means that the container hasn't been created yet.
47+
is_running: Option<bool>,
4748
}
4849

4950
impl Podman {
@@ -57,7 +58,7 @@ impl Podman {
5758
env: Vec::new(),
5859
port_mappings: Vec::new(),
5960
container,
60-
stopped: false,
61+
is_running: None,
6162
}
6263
}
6364

@@ -76,7 +77,6 @@ impl Podman {
7677
let mut command = std::process::Command::new("podman");
7778
command.arg("run");
7879
command.arg("--detach");
79-
command.arg("--rm");
8080
command.arg("--name");
8181
command.arg(&self.name);
8282
for (key, value) in &self.env {
@@ -91,18 +91,8 @@ impl Podman {
9191
};
9292
}
9393
command.arg(self.container.container_name());
94-
logging::log::debug!(
95-
"Podman run command args: {:?}",
96-
command.get_args().map(|s| s.to_string_lossy()).collect::<Vec<_>>().join(" ")
97-
);
98-
let output = command.output().unwrap();
99-
assert!(
100-
output.status.success(),
101-
"Failed to run podman command: {:?}\n{}",
102-
command,
103-
String::from_utf8_lossy(&output.stderr)
104-
);
105-
self.stopped = false;
94+
Self::run_command(command);
95+
self.is_running = Some(true);
10696
}
10797

10898
pub fn get_port_mapping(&self, container_port: u16) -> Option<u16> {
@@ -111,17 +101,7 @@ impl Podman {
111101
command.arg(&self.name);
112102
command.arg(format!("{}", container_port));
113103

114-
let output = command.output().unwrap();
115-
logging::log::debug!(
116-
"Podman ports command args: {:?}",
117-
command.get_args().map(|s| s.to_string_lossy()).collect::<Vec<_>>().join(" ")
118-
);
119-
assert!(
120-
output.status.success(),
121-
"Failed to run podman command: {:?}\n{}",
122-
command,
123-
String::from_utf8_lossy(&output.stderr)
124-
);
104+
let output = Self::run_command(command);
125105
let stdout = String::from_utf8(output.stdout).unwrap();
126106
let line = stdout.lines().next()?;
127107
let parts = line.split(':').collect::<Vec<&str>>();
@@ -138,36 +118,28 @@ impl Podman {
138118
let mut command = std::process::Command::new("podman");
139119
command.arg("stop");
140120
command.arg(&self.name);
141-
let output = command.output().unwrap();
142-
logging::log::debug!(
143-
"Podman stop command args: {:?}",
144-
command.get_args().map(|s| s.to_string_lossy()).collect::<Vec<_>>().join(" ")
145-
);
121+
Self::run_command(command);
122+
self.is_running = Some(false);
123+
}
124+
125+
pub fn restart(&mut self) {
146126
assert!(
147-
output.status.success(),
148-
"Failed to run podman command: {:?}\n{}",
149-
command,
150-
String::from_utf8_lossy(&output.stderr)
127+
self.is_running == Some(false),
128+
"The container must have been created and stopped before it can be restarted"
151129
);
152-
self.stopped = true;
130+
let mut command = std::process::Command::new("podman");
131+
command.arg("start");
132+
command.arg(&self.name);
133+
Self::run_command(command);
134+
self.is_running = Some(true);
153135
}
154136

155137
/// Uses the command `podman logs` to print the logs of the container.
156138
pub fn print_logs(&mut self) {
157139
let mut command = std::process::Command::new("podman");
158140
command.arg("logs");
159141
command.arg(&self.name);
160-
let output = command.output().unwrap();
161-
logging::log::debug!(
162-
"Podman logs command args: {:?}",
163-
command.get_args().map(|s| s.to_string_lossy()).collect::<Vec<_>>().join(" ")
164-
);
165-
assert!(
166-
output.status.success(),
167-
"Failed to run podman command: {:?}\n{}",
168-
command,
169-
String::from_utf8_lossy(&output.stderr)
170-
);
142+
let output = Self::run_command(command);
171143

172144
{
173145
let mut logs = String::new();
@@ -192,10 +164,35 @@ impl Podman {
192164
}
193165
}
194166

167+
fn run_command(mut command: std::process::Command) -> std::process::Output {
168+
let output = command.output().unwrap();
169+
logging::log::debug!(
170+
"Podman command args: {:?}",
171+
command.get_args().map(|s| s.to_string_lossy()).collect::<Vec<_>>().join(" ")
172+
);
173+
assert!(
174+
output.status.success(),
175+
"Failed to run podman command: {:?}\n{}",
176+
command,
177+
String::from_utf8_lossy(&output.stderr)
178+
);
179+
output
180+
}
181+
182+
fn remove_container(&mut self) {
183+
let mut command = std::process::Command::new("podman");
184+
command.arg("rm");
185+
command.arg(&self.name);
186+
Self::run_command(command);
187+
}
188+
195189
fn destructor(&mut self) {
196-
self.print_logs();
197-
if !self.stopped {
198-
self.stop();
190+
if let Some(is_running) = self.is_running {
191+
self.print_logs();
192+
if is_running {
193+
self.stop();
194+
}
195+
self.remove_container();
199196
}
200197
}
201198

0 commit comments

Comments
 (0)