Update mcollective.init according to OSCI-855
[packages/precise/mcollective.git] / spec / unit / ddl / agentddl_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module DDL
7     describe AgentDDL do
8       before :each do
9         Cache.delete!(:ddl) rescue nil
10         @ddl = DDL.new("rspec", :agent, false)
11         @ddl.metadata(:name => "name", :description => "description", :author => "author", :license => "license", :version => "version", :url => "url", :timeout => "timeout")
12       end
13
14       describe "#input" do
15         it "should validate that an :optional property is set" do
16           expect { @ddl.input(:x, {:y => 1}) }.to raise_error("Input needs a :optional property")
17         end
18       end
19
20       describe "#set_default_input_arguments" do
21         before do
22           @ddl.action(:test, :description => "rspec")
23           @ddl.instance_variable_set("@current_entity", :test)
24
25           @ddl.input(:optional, :prompt => "prompt", :description => "descr",
26                      :type => :string, :optional => true, :validation => "",
27                      :maxlength => 1, :default => "default")
28           @ddl.input(:required, :prompt => "prompt", :description => "descr",
29                      :type => :string, :optional => false, :validation => "",
30                      :maxlength => 1, :default => "default")
31         end
32
33         it "should correctly add default arguments to required inputs" do
34           args = {}
35
36           @ddl.set_default_input_arguments(:test, args)
37
38           args.should == {:required => "default"}
39         end
40
41         it "should not override any existing arguments" do
42           args = {:required => "specified"}
43
44           @ddl.set_default_input_arguments(:test, args)
45
46           args.should == {:required => "specified"}
47         end
48       end
49
50       describe "#validate_rpc_request" do
51         it "should ensure the action is known" do
52           @ddl.action(:test, :description => "rspec")
53
54           expect {
55             @ddl.validate_rpc_request(:fail, {})
56           }.to raise_code(:PLMC29, :action => :fail, :plugin => "rspec")
57
58           @ddl.validate_rpc_request(:test, {})
59         end
60
61         it "should check all required arguments are present" do
62           @ddl.action(:test, :description => "rspec")
63           @ddl.instance_variable_set("@current_entity", :test)
64           @ddl.input(:optional, :prompt => "prompt", :description => "descr",
65                      :type => :string, :optional => true, :validation => "",
66                      :maxlength => 1)
67           @ddl.input(:required, :prompt => "prompt", :description => "descr",
68                      :type => :string, :optional => false, :validation => "",
69                      :maxlength => 1)
70
71           @ddl.stubs(:validate_input_argument).returns(true)
72
73           expect {
74             @ddl.validate_rpc_request(:test, {})
75           }.to raise_code(:PLMC30, :action => :test, :key => :required)
76
77           @ddl.validate_rpc_request(:test, {:required => "f"}).should == true
78         end
79
80         it "should input validate every supplied key" do
81           @ddl.action(:test, :description => "rspec")
82           @ddl.instance_variable_set("@current_entity", :test)
83           @ddl.input(:optional, :prompt => "prompt", :description => "descr",
84                      :type => :string, :optional => true, :validation => "",
85                      :maxlength => 1)
86           @ddl.input(:required, :prompt => "prompt", :description => "descr",
87                      :type => :string, :optional => false, :validation => "",
88                      :maxlength => 1)
89
90
91           @ddl.expects(:validate_input_argument).with(@ddl.entities[:test][:input], :required, "f")
92           @ddl.expects(:validate_input_argument).with(@ddl.entities[:test][:input], :optional, "f")
93
94           @ddl.validate_rpc_request(:test, {:required => "f", :optional => "f"}).should == true
95         end
96       end
97
98       describe "#is_function" do
99         before :each do
100           PluginManager.expects(:find).with("aggregate").returns(["plugin"])
101         end
102
103         it "should return true if the aggregate function is present" do
104           @ddl.is_function?("plugin").should == true
105         end
106
107         it "should return false if the aggregate function is not present" do
108           @ddl.is_function?("no_plugin").should == false
109         end
110       end
111
112       describe "#method_missing" do
113         it "should call super if the aggregate plugin isn't present" do
114           expect{
115             @ddl.test
116           }.to raise_error(NoMethodError)
117         end
118
119         it "should call super if @process_aggregate_function is false" do
120           expect{
121             result = @ddl.test(:value)
122           }.to raise_error(NoMethodError)
123         end
124
125         it "should return the function hash" do
126           Config.instance.mode = :client
127           @ddl.instance_variable_set(:@process_aggregate_functions, true)
128           result = @ddl.method_missing(:test_function, :rspec)
129           result.should == {:args => [:rspec], :function => :test_function }
130         end
131       end
132
133       describe "#actions" do
134         it "should return the correct list of actions" do
135           @ddl.action(:test1, :description => "rspec")
136           @ddl.action(:test2, :description => "rspec")
137
138           @ddl.actions.sort.should == [:test1, :test2]
139         end
140       end
141
142       describe "#action_interface" do
143         it "should return the correct interface" do
144           @ddl.action(:test1, :description => "rspec")
145           @ddl.action_interface(:test1).should == {:description=>"rspec", :output=>{}, :input=>{}, :action=>:test1, :display=>:failed}
146         end
147       end
148
149       describe "#display" do
150         it "should ensure a valid display property is set" do
151           @ddl.action(:test, :description => "rspec")
152           @ddl.instance_variable_set("@current_entity", :test)
153
154           [:ok, :failed, :flatten, :always].each do |display|
155             @ddl.display(display)
156
157             action = @ddl.action_interface(:test)
158             action[:display].should == display
159           end
160
161           expect {
162             @ddl.display(:foo)
163           }.to raise_error(/Display preference foo is not valid/)
164         end
165       end
166
167       describe "#summarize" do
168         before :each do
169           @block_result = nil
170           @block = Proc.new{@block_result = :success}
171         end
172
173         after :each do
174           @block_result = nil
175         end
176
177         it "should call the block parameter if config mode is not server" do
178           Config.instance.mode = :client
179           result = @ddl.summarize(&@block)
180           @block_result.should == :success
181         end
182
183         it "should not call the block parameter if config mode is server" do
184           Config.instance.mode = :server
185           result = @ddl.summarize(&@block)
186           @block_result.should == nil
187         end
188       end
189
190       describe "#aggregate" do
191         it "should raise an exception if aggregate format isn't a hash" do
192           expect{
193             @ddl.aggregate(:foo, :format)
194           }.to raise_code(:PLMC28)
195         end
196
197         it "should raise an exception if format hash does not include a :format key" do
198           expect{
199             @ddl.aggregate(:foo, {})
200           }.to raise_code(:PLMC27)
201         end
202
203         it "should raise an exception if aggregate function is not a hash" do
204           expect{
205             @ddl.aggregate(:foo)
206           }.to raise_code(:PLMC26)
207         end
208
209         it "should raise an exception if function hash does not include a :args key" do
210           expect{
211             @ddl.stubs(:entities).returns({nil => {:action => :foo}})
212             @ddl.aggregate({})
213           }.to raise_code(:PLMC25, :action => :foo)
214         end
215
216         it "should correctly add an aggregate function to the function array" do
217           @ddl.stubs(:entities).returns({nil => {:aggregate => nil}})
218           @ddl.aggregate({:function => :foo, :args => [:bar]})
219           @ddl.entities.should == {nil => {:aggregate => [{:function => :foo, :args => [:bar]}]}}
220         end
221       end
222
223       describe "#action" do
224         it "should ensure a description is set" do
225           expect {
226             @ddl.action("act", {})
227           }.to raise_error("Action needs a :description property")
228         end
229
230         it "should create a default action structure" do
231           @ddl.action("act", :description => "rspec")
232
233           action = @ddl.action_interface("act")
234
235           action.class.should == Hash
236           action[:action].should == "act"
237           action[:input].should == {}
238           action[:output].should == {}
239           action[:display].should == :failed
240           action[:description].should == "rspec"
241         end
242
243         it "should call a block if one is given and set the correct action name" do
244           @ddl.action("act", :description => "rspec") { @ddl.instance_variable_get("@current_entity").should == "act" }
245         end
246       end
247     end
248   end
249 end