From 067a0a5d0d02cf12930b61a61c5dc855115ac2da Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 18 Apr 2017 13:33:19 -0700 Subject: [PATCH] communicator/ssh: Do not pass empty data to registered blocks Prevent sending empty data strings to defined blocks handling stderr and stdout output. These can occur when the garbage marker is identified and collected data pruned, but no remaining data is left to send. Fixes #8259 --- plugins/communicators/ssh/communicator.rb | 4 +-- .../communicators/ssh/communicator_test.rb | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/plugins/communicators/ssh/communicator.rb b/plugins/communicators/ssh/communicator.rb index 2b0c2db7b..a8abadead 100644 --- a/plugins/communicators/ssh/communicator.rb +++ b/plugins/communicators/ssh/communicator.rb @@ -519,7 +519,7 @@ module VagrantPlugins end end - if block_given? && marker_found + if block_given? && marker_found && !data.empty? yield :stdout, data end end @@ -540,7 +540,7 @@ module VagrantPlugins end end - if block_given? && marker_found + if block_given? && marker_found && !data.empty? yield :stderr, data end end diff --git a/test/unit/plugins/communicators/ssh/communicator_test.rb b/test/unit/plugins/communicators/ssh/communicator_test.rb index 6a9c94fe0..a0998c6dc 100644 --- a/test/unit/plugins/communicators/ssh/communicator_test.rb +++ b/test/unit/plugins/communicators/ssh/communicator_test.rb @@ -168,6 +168,24 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do end end + context "with no command output" do + let(:command_stdout_data) do + "#{command_garbage_marker}" + end + + it "does not send empty stdout data string" do + empty = true + expect( + communicator.execute("ls /") do |type, data| + if type == :stdout && data.empty? + empty = false + end + end + ).to eq(0) + expect(empty).to be(true) + end + end + context "with garbage content prepended to command stderr output" do let(:command_stderr_data) do "Line of garbage\nMore garbage\n#{command_garbage_marker}bin\ntmp\n" @@ -186,6 +204,24 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do end end + context "with no command output on stderr" do + let(:command_stderr_data) do + "#{command_garbage_marker}" + end + + it "does not send empty stderr data string" do + empty = true + expect( + communicator.execute("ls /") do |type, data| + if type == :stderr && data.empty? + empty = false + end + end + ).to eq(0) + expect(empty).to be(true) + end + end + context "with pty enabled" do before do expect(ssh).to receive(:pty).and_return true