ci: Allow threads to send ssh commands

The goal here is to decouple the Guest instance from the ssh connection
to send some commands to the guest. The reason being to allow ssh
commands to be issued from a different thread, which can be useful to
wait for the end of a command with a thread.join().

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2019-09-06 16:34:19 -07:00 committed by Samuel Ortiz
parent 40fc6c3f0f
commit a9b2207bcf

View File

@ -577,6 +577,44 @@ mod tests {
(child, virtiofsd_socket_path)
}
fn ssh_command_ip(command: &str, ip: &str) -> Result<String, Error> {
let mut s = String::new();
let mut counter = 0;
loop {
match (|| -> Result<(), Error> {
let tcp =
TcpStream::connect(format!("{}:22", ip)).map_err(|_| Error::Connection)?;
let mut sess = Session::new().unwrap();
sess.handshake(&tcp).map_err(|_| Error::Connection)?;
sess.userauth_password("cloud", "cloud123")
.map_err(|_| Error::Authentication)?;
assert!(sess.authenticated());
let mut channel = sess.channel_session().map_err(|_| Error::Command)?;
channel.exec(command).map_err(|_| Error::Command)?;
// Intentionally ignore these results here as their failure
// does not precipitate a repeat
let _ = channel.read_to_string(&mut s);
let _ = channel.close();
let _ = channel.wait_close();
Ok(())
})() {
Ok(_) => break,
Err(e) => {
counter += 1;
if counter >= 6 {
return Err(e);
}
}
};
thread::sleep(std::time::Duration::new(10 * counter, 0));
}
Ok(s)
}
#[derive(Debug)]
enum Error {
Connection,
@ -636,54 +674,16 @@ mod tests {
)
}
fn ssh_command_ip(&self, command: &str, ip: &str) -> Result<String, Error> {
let mut s = String::new();
let mut counter = 0;
loop {
match (|| -> Result<(), Error> {
let tcp =
TcpStream::connect(format!("{}:22", ip)).map_err(|_| Error::Connection)?;
let mut sess = Session::new().unwrap();
sess.handshake(&tcp).map_err(|_| Error::Connection)?;
sess.userauth_password("cloud", "cloud123")
.map_err(|_| Error::Authentication)?;
assert!(sess.authenticated());
let mut channel = sess.channel_session().map_err(|_| Error::Command)?;
channel.exec(command).map_err(|_| Error::Command)?;
// Intentionally ignore these results here as their failure
// does not precipitate a repeat
let _ = channel.read_to_string(&mut s);
let _ = channel.close();
let _ = channel.wait_close();
Ok(())
})() {
Ok(_) => break,
Err(e) => {
counter += 1;
if counter >= 6 {
return Err(e);
}
}
};
thread::sleep(std::time::Duration::new(10 * counter, 0));
}
Ok(s)
}
fn ssh_command(&self, command: &str) -> Result<String, Error> {
self.ssh_command_ip(command, &self.network.guest_ip)
ssh_command_ip(command, &self.network.guest_ip)
}
fn ssh_command_l1(&self, command: &str) -> Result<String, Error> {
self.ssh_command_ip(command, &self.network.guest_ip)
ssh_command_ip(command, &self.network.guest_ip)
}
fn ssh_command_l2(&self, command: &str) -> Result<String, Error> {
self.ssh_command_ip(command, &self.network.l2_guest_ip)
ssh_command_ip(command, &self.network.l2_guest_ip)
}
fn get_cpu_count(&self) -> Result<u32, Error> {
@ -2225,5 +2225,4 @@ mod tests {
Ok(())
});
}
}