Skip to content

Commit

Permalink
10265 - When supplying discovery data to RPC::Client#discover always …
Browse files Browse the repository at this point in the history
…call reset

Improve test isolation by allowing mock STDERR and STDOUT in the client
  • Loading branch information
ripienaar committed Oct 25, 2011
1 parent 5c40c6f commit 1f90fc2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
22 changes: 18 additions & 4 deletions lib/mcollective/rpc/client.rb
Expand Up @@ -79,8 +79,22 @@ def initialize(agent, flags = {})
@ddl = nil
end

STDERR.sync = true
STDOUT.sync = true
# allows stderr and stdout to be overridden for testing
# but also for web apps that might not want a bunch of stuff
# generated to actual file handles
if initial_options[:stderr]
@stderr = initial_options[:stderr]
else
@stderr = STDERR
@stderr.sync = true
end

if initial_options[:stderr]
@stdout = initial_options[:stderr]
else
@stdout = STDOUT
@stdout.sync = true
end
end

# Disconnects cleanly from the middleware
Expand Down Expand Up @@ -393,7 +407,7 @@ def discover(flags={})
unless @discovered_agents
@stats.time_discovery :start

STDERR.print("Determining the amount of hosts matching filter for #{discovery_timeout} seconds .... ") if verbose
@stderr.print("Determining the amount of hosts matching filter for #{discovery_timeout} seconds .... ") if verbose

# if the requested limit is a pure number and not a percent
# and if we're configured to use the first found hosts as the
Expand All @@ -406,7 +420,7 @@ def discover(flags={})
end

@force_direct_request = false
STDERR.puts(@discovered_agents.size) if verbose
@stderr.puts(@discovered_agents.size) if verbose

@stats.time_discovery :end
end
Expand Down
15 changes: 9 additions & 6 deletions spec/unit/rpc/client_spec.rb
Expand Up @@ -13,6 +13,9 @@ module RPC
@client.stubs("options=")
@client.stubs(:collective).returns("mcollective")

@stderr = stub
@stdout = stub

Config.any_instance.stubs(:loadconfig).with("/nonexisting").returns(true)
MCollective::Client.expects(:new).returns(@client)
end
Expand Down Expand Up @@ -86,19 +89,19 @@ module RPC

it "should print status to stderr if in verbose mode" do
@client.expects(:discover).with({'identity' => [], 'compound' => [], 'fact' => [], 'agent' => ['foo'], 'cf_class' => []}, 2).returns(["foo"])
client = Client.new("foo", {:options => {:filter => Util.empty_filter, :config => "/nonexisting", :verbose => true, :disctimeout => 2}})
@stderr.expects(:print).with("Determining the amount of hosts matching filter for 2 seconds .... ")
@stderr.expects(:puts).with(1)

STDERR.expects(:print).with("Determining the amount of hosts matching filter for 2 seconds .... ")
STDERR.expects(:puts).with(1)
client = Client.new("foo", {:options => {:filter => Util.empty_filter, :config => "/nonexisting", :verbose => true, :disctimeout => 2, :stderr => @stderr, :stdout => @stdout}})
client.discover
end

it "should not print status to stderr if in verbose mode" do
@client.expects(:discover).with({'identity' => [], 'compound' => [], 'fact' => [], 'agent' => ['foo'], 'cf_class' => []}, 2).returns(["foo"])
client = Client.new("foo", {:options => {:filter => Util.empty_filter, :config => "/nonexisting", :verbose => false, :disctimeout => 2}})
client = Client.new("foo", {:options => {:filter => Util.empty_filter, :config => "/nonexisting", :verbose => false, :disctimeout => 2, :stderr => @stderr, :stdout => @stdout}})

STDERR.expects(:print).never
STDERR.expects(:puts).never
@stderr.expects(:print).never
@stderr.expects(:puts).never

client.discover
end
Expand Down

0 comments on commit 1f90fc2

Please sign in to comment.