9 @expected = {:discoverytime => 0,
13 :noresponsefrom => [],
17 :starttime => 1300031826.0,
19 :aggregate_summary => [],
20 :aggregate_failures => [],
21 :discovered_nodes => []}
26 describe "#initialize" do
27 it "should reset stats on creation" do
28 Stats.any_instance.stubs(:reset).returns(true).once
34 it "should initialize data correctly" do
35 Time.stubs(:now).returns(Time.at(1300031826))
38 @expected.keys.each do |k|
39 @expected[k].should == s.send(k)
44 describe "#to_hash" do
45 it "should return correct stats" do
46 Time.stubs(:now).returns(Time.at(1300031826))
49 s.to_hash.should == @expected
54 it "should return stat values" do
55 Time.stubs(:now).returns(Time.at(1300031826))
58 @expected.keys.each do |k|
59 @expected[k].should == s[k]
63 it "should return nil for unknown values" do
64 @stats["foo"].should == nil
69 it "should increment stats" do
71 @stats[:okcount].should == 1
76 it "should increment stats" do
78 @stats.failcount.should == 1
82 describe "#time_discovery" do
83 it "should set start time correctly" do
84 Time.stubs(:now).returns(Time.at(1300031826))
86 @stats.time_discovery(:start)
88 @stats.instance_variable_get("@discovery_start").should == 1300031826.0
91 it "should record the difference correctly" do
92 Time.stubs(:now).returns(Time.at(1300031826))
93 @stats.time_discovery(:start)
95 Time.stubs(:now).returns(Time.at(1300031827))
96 @stats.time_discovery(:end)
98 @stats.discoverytime.should == 1.0
101 it "should handle unknown actions and set discovery time to 0" do
102 Time.stubs(:now).returns(Time.at(1300031826))
103 @stats.time_discovery(:start)
105 Time.stubs(:now).returns(Time.at(1300031827))
106 @stats.time_discovery(:stop)
108 @stats.discoverytime.should == 0
113 describe "#client_stats=" do
114 it "should store stats correctly" do
116 keys = [:noresponsefrom, :responses, :starttime, :blocktime, :totaltime, :discoverytime]
117 keys.each {|k| data[k] = k}
119 @stats.client_stats = data
122 @stats[k].should == data[k]
126 it "should not store discovery time if it was already stored" do
128 keys = [:noresponsefrom, :responses, :starttime, :blocktime, :totaltime, :discoverytime]
129 keys.each {|k| data[k] = k}
131 Time.stubs(:now).returns(Time.at(1300031826))
132 @stats.time_discovery(:start)
134 Time.stubs(:now).returns(Time.at(1300031827))
135 @stats.time_discovery(:end)
137 dtime = @stats.discoverytime
139 @stats.client_stats = data
141 @stats.discoverytime.should == dtime
145 describe "#time_block_execution" do
146 it "should set start time correctly" do
147 Time.stubs(:now).returns(Time.at(1300031826))
149 @stats.time_block_execution(:start)
151 @stats.instance_variable_get("@block_start").should == 1300031826.0
154 it "should record the difference correctly" do
155 Time.stubs(:now).returns(Time.at(1300031826))
156 @stats.time_block_execution(:start)
158 Time.stubs(:now).returns(Time.at(1300031827))
159 @stats.time_block_execution(:end)
161 @stats.blocktime.should == 1
164 it "should handle unknown actions and set discovery time to 0" do
165 Time.stubs(:now).returns(Time.at(1300031826))
166 @stats.time_block_execution(:start)
168 Time.stubs(:now).returns(Time.at(1300031827))
169 @stats.time_block_execution(:stop)
171 @stats.blocktime.should == 0
175 describe "#discovered_agents" do
176 it "should set discovered_nodes" do
177 nodes = ["one", "two"]
178 @stats.discovered_agents(nodes)
179 @stats.discovered_nodes.should == nodes
182 it "should set discovered count" do
183 nodes = ["one", "two"]
184 @stats.discovered_agents(nodes)
185 @stats.discovered.should == 2
189 describe "#finish_request" do
190 it "should calculate totaltime correctly" do
191 Time.stubs(:now).returns(Time.at(1300031824))
192 @stats.time_discovery(:start)
194 Time.stubs(:now).returns(Time.at(1300031825))
195 @stats.time_discovery(:end)
197 Time.stubs(:now).returns(Time.at(1300031826))
198 @stats.time_block_execution(:start)
200 Time.stubs(:now).returns(Time.at(1300031827))
201 @stats.time_block_execution(:end)
203 @stats.discovered_agents(["one", "two", "three"])
204 @stats.node_responded("one")
205 @stats.node_responded("two")
207 @stats.finish_request
209 @stats.totaltime.should == 2
212 it "should calculate no responses correctly" do
213 Time.stubs(:now).returns(Time.at(1300031824))
214 @stats.time_discovery(:start)
216 Time.stubs(:now).returns(Time.at(1300031825))
217 @stats.time_discovery(:end)
219 Time.stubs(:now).returns(Time.at(1300031826))
220 @stats.time_block_execution(:start)
222 Time.stubs(:now).returns(Time.at(1300031827))
223 @stats.time_block_execution(:end)
225 @stats.discovered_agents(["one", "two", "three"])
226 @stats.node_responded("one")
227 @stats.node_responded("two")
229 @stats.finish_request
231 @stats.noresponsefrom.should == ["three"]
234 it "should recover from failure correctly" do
235 Time.stubs(:now).returns(Time.at(1300031824))
236 @stats.time_discovery(:start)
238 Time.stubs(:now).returns(Time.at(1300031825))
239 @stats.time_discovery(:end)
241 Time.stubs(:now).returns(Time.at(1300031826))
242 @stats.time_block_execution(:start)
244 Time.stubs(:now).returns(Time.at(1300031827))
245 @stats.time_block_execution(:end)
247 # cause the .each to raise an exception
248 @stats.instance_variable_set("@responsesfrom", nil)
249 @stats.finish_request
251 @stats.noresponsefrom.should == []
252 @stats.totaltime.should == 0
256 describe "#node_responded" do
257 it "should append to the list of nodes" do
258 @stats.node_responded "foo"
259 @stats.responsesfrom.should == ["foo"]
262 it "should create a new array if adding fails" do
263 # cause the << to fail
264 @stats.instance_variable_set("@responsesfrom", nil)
266 @stats.node_responded "foo"
267 @stats.responsesfrom.should == ["foo"]
271 describe "#no_response_report" do
272 it "should create an empty report if all nodes responded" do
273 @stats.discovered_agents ["foo"]
274 @stats.node_responded "foo"
275 @stats.finish_request
277 @stats.no_response_report.should == ""
280 it "should list all nodes that did not respond" do
281 @stats.discovered_agents ["foo", "bar"]
282 @stats.finish_request
284 @stats.no_response_report.should match(Regexp.new(/No response from.+bar\s+foo/m))
288 describe "#text_for_aggregates" do
289 let(:aggregate){mock()}
292 aggregate.stubs(:result).returns({:output => "success"})
293 aggregate.stubs(:action).returns("action")
296 it "should create the correct output text for aggregate functions" do
297 @stats.aggregate_summary = [aggregate]
298 aggregate.stubs(:is_a?).returns(true)
299 @stats.text_for_aggregates.should =~ /Summary of.*/
302 it "should display an error message for a failed statup hook" do
303 @stats.aggregate_failures = [{:name => "rspec", :type => :startup}]
304 @stats.text_for_aggregates.should =~ /exception raised while processing startup hook/
307 it "should display an error message for an unspecified output" do
308 @stats.aggregate_failures = [{:name => "rspec", :type => :create}]
309 @stats.text_for_aggregates.should =~ /unspecified output 'rspec' for the action/
312 it "should display an error message for a failed process_result" do
313 @stats.aggregate_failures = [{:name => "rspec", :type => :process_result}]
314 @stats.text_for_aggregates.should =~ /exception raised while processing result data/
317 it "should display an error message for a failed summarize" do
318 @stats.aggregate_failures = [{:name => "rspec", :type => :summarize}]
319 @stats.text_for_aggregates.should =~ /exception raised while summarizing/