10 ddl.stubs(:meta).returns({})
11 ddl.stubs(:action).returns([])
12 ddl.stubs(:validate_rpc_request).returns(true)
13 DDL.stubs(:new).returns(ddl)
20 describe "#handlemsg" do
22 Reply.any_instance.stubs(:initialize_data)
24 @agent.stubs(:respond_to?).with("rspec_action_action").returns(true)
25 @agent.stubs(:respond_to?).with("authorization_hook").returns(false)
26 @agent.stubs(:rspec_action_action).returns(nil)
28 @msg = {:msgtime => 1356006671,
29 :senderid => "example.com",
30 :requestid => "55f8abe1442328321667877a08bdc586",
31 :body => {:agent => "rspec_agent",
32 :action => "rspec_action",
34 :caller => "cert=rspec"}
37 it "should or validate the incoming request" do
38 exception = DDLValidationError.new(:RSPEC, "Failed to validate", :error)
39 Request.any_instance.expects(:validate!).raises(exception)
41 reply = @agent.handlemsg(@msg, DDL.new)
43 reply[:statuscode].should == 4
44 reply[:statusmsg].should == "Failed to validate"
47 it "should call the authorization hook if set" do
48 @agent.expects(:respond_to?).with("authorization_hook").returns(true)
49 @agent.expects(:authorization_hook).raises("authorization denied")
52 reply = @agent.handlemsg(@msg, DDL.new)
54 reply[:statuscode].should == 5
55 reply[:statusmsg].should == "authorization denied"
58 it "should audit the request" do
59 @agent.expects(:audit_request)
61 reply = @agent.handlemsg(@msg, DDL.new)
62 reply[:statuscode].should == 0
65 it "should call the before_processing_hook" do
66 @agent.expects(:before_processing_hook)
68 reply = @agent.handlemsg(@msg, DDL.new)
69 reply[:statuscode].should == 0
72 it "should fail if the action does not exist" do
73 @agent.expects(:respond_to?).with("rspec_action_action").returns(false)
74 reply = @agent.handlemsg(@msg, DDL.new)
75 reply[:statuscode].should == 2
78 it "should call the action correctly" do
79 @agent.expects(:rspec_action_action)
80 reply = @agent.handlemsg(@msg, DDL.new)
81 reply[:statuscode].should == 0
84 it "should handle RPC Aborted errors" do
85 @agent.expects(:rspec_action_action).raises(RPCAborted, "rspec test")
86 reply = @agent.handlemsg(@msg, DDL.new)
87 reply[:statuscode].should == 1
88 reply[:statusmsg].should == "rspec test"
91 it "should handle Unknown Action errors" do
92 @agent.stubs(:respond_to?).with("rspec_action_action").returns(false)
93 reply = @agent.handlemsg(@msg, DDL.new)
94 reply[:statuscode].should == 2
95 reply[:statusmsg].should == "Unknown action 'rspec_action' for agent 'rspec_agent'"
98 it "should handle Missing Data errors" do
99 @agent.expects(:rspec_action_action).raises(MissingRPCData, "rspec test")
100 reply = @agent.handlemsg(@msg, DDL.new)
101 reply[:statuscode].should == 3
102 reply[:statusmsg].should == "rspec test"
105 it "should handle Invalid Data errors" do
106 @agent.expects(:rspec_action_action).raises(InvalidRPCData, "rspec test")
107 reply = @agent.handlemsg(@msg, DDL.new)
108 reply[:statuscode].should == 4
109 reply[:statusmsg].should == "rspec test"
112 it "should handle unknown errors" do
113 @agent.expects(:rspec_action_action).raises(UnknownRPCError, "rspec test")
114 Log.expects(:error).twice
116 reply = @agent.handlemsg(@msg, DDL.new)
117 reply[:statuscode].should == 5
118 reply[:statusmsg].should == "rspec test"
121 it "should handle arbitrary exceptions" do
122 @agent.expects(:rspec_action_action).raises(Exception, "rspec test")
123 Log.expects(:error).twice
125 reply = @agent.handlemsg(@msg, DDL.new)
126 reply[:statuscode].should == 5
127 reply[:statusmsg].should == "rspec test"
130 it "should call the after_processing_hook" do
131 @agent.expects(:after_processing_hook)
132 reply = @agent.handlemsg(@msg, DDL.new)
135 it "should respond if required" do
136 Request.any_instance.expects(:should_respond?).returns(true)
137 Reply.any_instance.expects(:to_hash).returns({})
138 @agent.handlemsg(@msg, DDL.new).should == {}
141 it "should not respond when not required" do
142 Request.any_instance.expects(:should_respond?).returns(false)
143 Reply.any_instance.expects(:to_hash).never
144 @agent.handlemsg(@msg, DDL.new).should == nil
149 it "should be deprecated" do
150 Agent.expects(:log_code).with(:PLMC34, is_a(String), :warn, has_value(regexp_matches(/agent_spec.rb/)))
151 Agent.metadata("foo")
155 describe "#load_ddl" do
156 it "should load the correct DDL" do
158 ddl.stubs(:meta).returns({:timeout => 5})
160 DDL.expects(:new).with("agent", :agent).returns(ddl)
162 Agent.new.timeout.should == 5
165 it "should fail if the DDL isn't loaded" do
166 DDL.expects(:new).raises("failed to load")
167 expect { Agent.new }.to raise_code(:PLMC24)
170 it "should default to 10 second timeout" do
172 ddl.stubs(:meta).returns({})
174 DDL.expects(:new).with("agent", :agent).returns(ddl)
176 Agent.new.timeout.should == 10
183 @status.stubs(:exitstatus).returns(0)
185 @shell.stubs(:runcommand)
186 @shell.stubs(:status).returns(@status)
189 it "should accept stderr and stdout and force them to be strings" do
190 Shell.expects(:new).with("rspec", {:stderr => "", :stdout => ""}).returns(@shell)
191 @agent.send(:run, "rspec", {:stderr => :err, :stdout => :out})
192 @agent.reply[:err].should == ""
193 @agent.reply[:out].should == ""
196 it "should accept existing variables for stdout and stderr and fail if they dont support <<" do
197 @agent.reply[:err] = "err"
198 @agent.reply[:out] = "out"
200 Shell.expects(:new).with("rspec", {:stderr => "err", :stdout => "out"}).returns(@shell)
201 @agent.send(:run, "rspec", {:stderr => @agent.reply[:err], :stdout => @agent.reply[:out]})
202 @agent.reply[:err].should == "err"
203 @agent.reply[:out].should == "out"
205 @agent.reply.expects("fail!").with("stderr should support << while calling run(rspec)").raises("stderr fail")
206 expect { @agent.send(:run, "rspec", {:stderr => nil, :stdout => ""}) }.to raise_error("stderr fail")
208 @agent.reply.expects("fail!").with("stdout should support << while calling run(rspec)").raises("stdout fail")
209 expect { @agent.send(:run, "rspec", {:stderr => "", :stdout => nil}) }.to raise_error("stdout fail")
212 it "should set stdin, cwd and environment if supplied" do
213 Shell.expects(:new).with("rspec", {:stdin => "stdin", :cwd => "cwd", :environment => "env"}).returns(@shell)
214 @agent.send(:run, "rspec", {:stdin => "stdin", :cwd => "cwd", :environment => "env"})
217 it "should ignore unknown options" do
218 Shell.expects(:new).with("rspec", {}).returns(@shell)
219 @agent.send(:run, "rspec", {:rspec => "rspec"})
222 it "should chomp strings if configured to do so" do
223 Shell.expects(:new).with("rspec", {:stderr => 'err', :stdout => 'out'}).returns(@shell)
225 @agent.reply[:err] = "err"
226 @agent.reply[:out] = "out"
228 @agent.reply[:err].expects("chomp!")
229 @agent.reply[:out].expects("chomp!")
231 @agent.send(:run, "rspec", {:chomp => true, :stdout => @agent.reply[:out], :stderr => @agent.reply[:err]})
234 it "should return the exitstatus" do
235 Shell.expects(:new).with("rspec", {}).returns(@shell)
236 @agent.send(:run, "rspec", {}).should == 0
239 it "should handle nil from the shell handler" do
240 @shell.expects(:status).returns(nil)
241 Shell.expects(:new).with("rspec", {}).returns(@shell)
242 @agent.send(:run, "rspec", {}).should == -1
246 describe "#validate" do
247 it "should detect missing data" do
249 expect { @agent.send(:validate, :foo, String) }.to raise_error(MissingRPCData, "please supply a foo argument")
252 it "should catch validation errors and turn them into use case specific ones" do
253 @agent.request = {:input_key => "should be a number"}
254 Validator.expects(:validate).raises(ValidatorError, "input_key should be a number")
255 expect { @agent.send(:validate, :input_key, :number) }.to raise_error("Input input_key did not pass validation: input_key should be a number")