diff options
author | Wez Furlong <wez@wezfurlong.org> | 2019-07-23 23:51:24 -0700 |
---|---|---|
committer | Wez Furlong <wez@wezfurlong.org> | 2019-07-29 08:55:06 -0700 |
commit | dfcba59b6a77d9a730ed8d6b6728d8812ece424b (patch) | |
tree | a60942b1ef1d25a947c871b42d1509ab7bbd7da2 /tests | |
parent | 9efb2d664971682278b37eb40152c27f6e231645 (diff) | |
download | ssh2-rs-dfcba59b6a77d9a730ed8d6b6728d8812ece424b.zip |
Make channel tests more robust to stderr output
At the start of feb 2019 the tests started to fail on macos on Travis. No
changes were made to the repo, so the problem must be environmental.
It turns out that the server was emitting stderr output, and since the
tests were not consuming stderr, this made `channel.eof()` always
return false.
The resolution is to drain both stdout and stderr in the tests before
making assertions with `channel.eof()`.
I've also updated the doc comment on `channel.eof()` to reflect this,
as it is a bit of a sharp edge.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/all/channel.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/tests/all/channel.rs b/tests/all/channel.rs index 2a9dfdf..9053cf0 100644 --- a/tests/all/channel.rs +++ b/tests/all/channel.rs @@ -1,15 +1,33 @@ +use ssh2::Channel; use std::io::prelude::*; use std::net::{TcpListener, TcpStream}; use std::thread; +/// Consume all available stdout and stderr data. +/// It is important to read both if you are using +/// channel.eof() to make assertions that the stream +/// is complete +fn consume_stdio(channel: &mut Channel) -> (String, String) { + let mut stdout = String::new(); + channel.read_to_string(&mut stdout).unwrap(); + + let mut stderr = String::new(); + channel.stderr().read_to_string(&mut stderr).unwrap(); + + (stdout, stderr) +} + #[test] fn smoke() { let sess = ::authed_session(); let mut channel = sess.channel_session().unwrap(); channel.flush().unwrap(); channel.exec("true").unwrap(); + consume_stdio(&mut channel); + channel.wait_eof().unwrap(); assert!(channel.eof()); + channel.close().unwrap(); channel.wait_close().unwrap(); assert_eq!(channel.exit_status().unwrap(), 0); @@ -22,8 +40,11 @@ fn bad_smoke() { let mut channel = sess.channel_session().unwrap(); channel.flush().unwrap(); channel.exec("false").unwrap(); + consume_stdio(&mut channel); + channel.wait_eof().unwrap(); assert!(channel.eof()); + channel.close().unwrap(); channel.wait_close().unwrap(); assert_eq!(channel.exit_status().unwrap(), 1); @@ -35,8 +56,8 @@ fn reading_data() { let sess = ::authed_session(); let mut channel = sess.channel_session().unwrap(); channel.exec("echo foo").unwrap(); - let mut output = String::new(); - channel.read_to_string(&mut output).unwrap(); + + let (output, _) = consume_stdio(&mut channel); assert_eq!(output, "foo\n"); } @@ -46,8 +67,8 @@ fn writing_data() { let mut channel = sess.channel_session().unwrap(); channel.exec("read foo && echo $foo").unwrap(); channel.write_all(b"foo\n").unwrap(); - let mut output = String::new(); - channel.read_to_string(&mut output).unwrap(); + + let (output, _) = consume_stdio(&mut channel); assert_eq!(output, "foo\n"); } |