9 :aggregate => [{:function => :func, :args=>[:foo, {:format => "%s"}]}],
10 :action => "test_action",
11 :output => {:foo => nil, :bar => nil}
15 describe '#create_functions' do
18 it "should load all the functions with a format if defined" do
19 function.expects(:new).with(:foo, {}, "%s", 'test_action')
20 Aggregate.any_instance.expects(:contains_output?).returns(true)
21 Aggregate.any_instance.expects(:load_function).once.returns(function)
25 it "should load all the functions without a format if it isn't defined" do
26 function.expects(:new).with(:foo, {}, nil, 'test_action')
27 Aggregate.any_instance.expects(:load_function).once.returns(function)
28 ddl[:aggregate].first[:args][1][:format] = nil
32 it "should not summarize functions where the output is not specified in the ddl" do
33 invalid_ddl = { :aggregate => [{:function => :func, :args=>[:foo], :format => "%s"}, {:function => :func, :args=>[:fail], :format => "%s"}],
34 :action => "test_action",
35 :output => {:foo => nil, :bar => nil}}
37 function.stubs(:new).returns("function")
38 Aggregate.any_instance.stubs(:load_function).returns(function)
41 aggregate = Aggregate.new(invalid_ddl)
42 aggregate.functions.should == ["function"]
43 aggregate.failed.should == [{:type=>:create, :name=>:fail}]
46 it "should pass additional arguments if specified in the ddl" do
47 function.expects(:new).with(:foo, {:extra => "extra"}, "%s", 'test_action')
48 Aggregate.any_instance.expects(:load_function).once.returns(function)
49 ddl[:aggregate].first[:args][1][:extra] = "extra"
53 it "should not summarize functions if the startup hook raises an exception" do
54 function.stubs(:new).raises("rspec")
55 Aggregate.any_instance.expects(:load_function).returns(function)
58 aggregate = Aggregate.new(ddl)
59 aggregate.failed.should == [{:type=>:startup, :name =>:foo }]
63 describe '#contains_ouput?' do
65 Aggregate.any_instance.stubs(:create_functions)
66 @aggregate = Aggregate.new(ddl)
69 it "should return false if the ddl output does not include the function's input" do
70 result = @aggregate.contains_output?(:baz)
71 result.should == false
74 it "should return true if the ddl output includes the function's input" do
75 result = @aggregate.contains_output?(:foo)
80 describe '#call_functions' do
81 let(:aggregate){ Aggregate.new(ddl)}
82 let(:result){ RPC::Result.new("rspec", "rspec", :sender => "rspec", :statuscode => 0, :statusmsg => "rspec", :data => {:test => :result})}
86 Aggregate.any_instance.stubs(:create_functions)
89 it "should call all of the functions" do
90 function.expects(:process_result).with(:result, result).once
91 function.expects(:output_name).returns(:test)
92 aggregate.functions = [function]
94 aggregate.call_functions(result)
97 it "should not fail if 'process_result' method raises an exception" do
98 aggregate.functions = [function]
99 function.stubs(:output_name).returns(:test)
100 function.stubs(:process_result).raises("Failed")
103 aggregate.call_functions(result)
104 aggregate.failed.should == [:name => :test, :type => :process_result]
107 it "should not fail if 'summarize' method raises en exception" do
108 function.stubs(:summarize).raises("Failed")
109 function.stubs(:output_name).returns("rspec")
110 aggregate.functions = [function]
113 result = aggregate.summarize
117 describe '#summarize' do
118 it "should return the ordered function results" do
119 Aggregate.any_instance.stubs(:create_functions)
120 aggregate = Aggregate.new(ddl)
123 func1.expects(:summarize).returns(func1)
124 func1.stubs(:result).returns(:output => 5)
127 func2.expects(:summarize).returns(func2)
128 func2.stubs(:result).returns(:output => 2)
130 aggregate.functions = [func1, func2]
132 result = aggregate.summarize
133 result.should == [func2, func1]
136 it "should not summarise data that raises an exception" do
137 Aggregate.any_instance.stubs(:create_functions)
138 aggregate = Aggregate.new(ddl)
140 func.stubs(:summarize).raises("rspec")
141 func.stubs(:output_name).returns("rspec")
142 aggregate.functions = [func]
146 aggregate.failed.should == [{:name => "rspec", :type => :summarize}]
150 describe '#load_function' do
152 Aggregate.any_instance.stubs(:create_functions)
153 @aggregate = Aggregate.new(ddl)
156 it "should return a class object if it can be loaded" do
157 PluginManager.expects(:loadclass).with("MCollective::Aggregate::Test")
158 Aggregate.expects(:const_get).with("Test")
159 function = @aggregate.load_function("test")
162 it "should raise an exception if the class object cannot be loaded" do
163 PluginManager.expects(:loadclass).with("MCollective::Aggregate::Test")
165 function = @aggregate.load_function("test")
166 }.to raise_error("Aggregate function file 'test.rb' cannot be loaded")