9 @config = mock("config")
10 @config.stubs(:identity).returns("test")
11 @config.stubs(:configured).returns(true)
12 @config.stubs(:topicsep).returns(".")
14 @stats = mock("stats")
17 ::Time.stubs(:now).returns(@time)
19 MCollective::Log.stubs(:debug).returns(true)
21 MCollective::PluginManager << {:type => "global_stats", :class => @stats}
22 MCollective::Config.stubs("instance").returns(@config)
23 MCollective::Util.stubs("empty_filter?").returns(false)
28 describe "#should_process_msg?" do
29 it "should correctly validate messages" do
31 m.stubs(:expected_msgid).returns("rspec")
33 @plugin.should_process_msg?(m, "rspec").should == true
36 @plugin.should_process_msg?(m, "fail").should == true
37 }.to raise_error(MsgDoesNotMatchRequestID)
40 it "should not test messages without expected_msgid" do
42 m.stubs(:expected_msgid).returns(nil)
44 @plugin.should_process_msg?(m, "rspec").should == true
48 describe "#validate_filter?" do
49 it "should pass on empty filter" do
50 MCollective::Util.stubs("empty_filter?").returns(true)
52 @stats.stubs(:passed).once
53 @stats.stubs(:filtered).never
54 @stats.stubs(:passed).never
56 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
58 @plugin.validate_filter?({}).should == true
61 it "should pass for known classes" do
62 MCollective::Util.stubs("has_cf_class?").with("foo").returns(true)
64 @stats.stubs(:passed).once
65 @stats.stubs(:filtered).never
67 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
68 MCollective::Log.expects(:debug).with("Passing based on configuration management class foo").once
70 @plugin.validate_filter?({"cf_class" => ["foo"]}).should == true
73 it "should fail for unknown classes" do
74 MCollective::Util.stubs("has_cf_class?").with("foo").returns(false)
76 @stats.stubs(:filtered).once
77 @stats.stubs(:passed).never
79 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
80 MCollective::Log.expects(:debug).with("Failing based on configuration management class foo").once
82 @plugin.validate_filter?({"cf_class" => ["foo"]}).should == false
85 it "should pass for known agents" do
86 MCollective::Util.stubs("has_agent?").with("foo").returns(true)
88 @stats.stubs(:passed).once
89 @stats.stubs(:filtered).never
91 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
92 MCollective::Log.expects(:debug).with("Passing based on agent foo").once
94 @plugin.validate_filter?({"agent" => ["foo"]}).should == true
97 it "should fail for unknown agents" do
98 MCollective::Util.stubs("has_agent?").with("foo").returns(false)
100 @stats.stubs(:filtered).once
101 @stats.stubs(:passed).never
103 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
104 MCollective::Log.expects(:debug).with("Failing based on agent foo").once
106 @plugin.validate_filter?({"agent" => ["foo"]}).should == false
109 it "should pass for known facts" do
110 MCollective::Util.stubs("has_fact?").with("fact", "value", "operator").returns(true)
112 @stats.stubs(:passed).once
113 @stats.stubs(:filtered).never
115 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
116 MCollective::Log.expects(:debug).with("Passing based on fact fact operator value").once
118 @plugin.validate_filter?({"fact" => [{:fact => "fact", :operator => "operator", :value => "value"}]}).should == true
121 it "should fail for unknown facts" do
122 MCollective::Util.stubs("has_fact?").with("fact", "value", "operator").returns(false)
124 @stats.stubs(:filtered).once
125 @stats.stubs(:passed).never
127 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
128 MCollective::Log.expects(:debug).with("Failing based on fact fact operator value").once
130 @plugin.validate_filter?({"fact" => [{:fact => "fact", :operator => "operator", :value => "value"}]}).should == false
133 it "should pass for known identity" do
134 MCollective::Util.stubs("has_identity?").with("test").returns(true)
136 @stats.stubs(:passed).once
137 @stats.stubs(:filtered).never
139 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
140 MCollective::Log.expects(:debug).with("Passing based on identity").once
142 @plugin.validate_filter?({"identity" => ["test"]}).should == true
145 it "should fail for known identity" do
146 MCollective::Util.stubs("has_identity?").with("test").returns(false)
148 @stats.stubs(:passed).never
149 @stats.stubs(:filtered).once
151 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
152 MCollective::Log.expects(:debug).with("Failed based on identity").once
154 @plugin.validate_filter?({"identity" => ["test"]}).should == false
157 it "should treat multiple identity filters correctly" do
158 MCollective::Util.stubs("has_identity?").with("foo").returns(false)
159 MCollective::Util.stubs("has_identity?").with("bar").returns(true)
161 @stats.stubs(:passed).once
162 @stats.stubs(:filtered).never
164 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
165 MCollective::Log.expects(:debug).with("Passing based on identity").once
167 @plugin.validate_filter?({"identity" => ["foo", "bar"]}).should == true
170 it "should fail if no identity matches are found" do
171 MCollective::Util.stubs("has_identity?").with("foo").returns(false)
172 MCollective::Util.stubs("has_identity?").with("bar").returns(false)
174 @stats.stubs(:passed).never
175 @stats.stubs(:filtered).once
177 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
178 MCollective::Log.expects(:debug).with("Failed based on identity").once
180 @plugin.validate_filter?({"identity" => ["foo", "bar"]}).should == false
184 describe "#create_reply" do
185 it "should return correct data" do
186 expected = {:senderid => "test",
187 :requestid => "reqid",
188 :senderagent => "agent",
189 :msgtime => @time.to_i,
192 @plugin.create_reply("reqid", "agent", "body").should == expected
196 describe "#create_request" do
197 it "should return correct data" do
198 expected = {:body => "body",
200 :requestid => "reqid",
201 :callerid => "uid=#{Process.uid}",
202 :agent => "discovery",
203 :collective => "mcollective",
206 :msgtime => @time.to_i}
208 @plugin.create_request("reqid", "filter", "body", :server, "discovery", "mcollective", 20).should == expected
211 it "should set the callerid when appropriate" do
212 expected = {:body => "body",
214 :requestid => "reqid",
215 :agent => "discovery",
216 :collective => "mcollective",
218 :callerid => "callerid",
220 :msgtime => @time.to_i}
222 @plugin.stubs(:callerid).returns("callerid")
223 @plugin.create_request("reqid", "filter", "body", :client, "discovery", "mcollective").should == expected
227 describe "#valid_callerid?" do
228 it "should not pass invalid callerids" do
229 @plugin.valid_callerid?("foo-bar").should == false
230 @plugin.valid_callerid?("foo=bar=baz").should == false
231 @plugin.valid_callerid?('foo=bar\baz').should == false
232 @plugin.valid_callerid?("foo=bar/baz").should == false
233 @plugin.valid_callerid?("foo=bar|baz").should == false
236 it "should pass valid callerids" do
237 @plugin.valid_callerid?("cert=foo-bar").should == true
238 @plugin.valid_callerid?("uid=foo.bar").should == true
239 @plugin.valid_callerid?("uid=foo.bar.123").should == true
243 describe "#callerid" do
244 it "should return a unix UID based callerid" do
245 @plugin.callerid.should == "uid=#{Process.uid}"
249 describe "#validrequest?" do
250 it "should log an error when not implemented" do
251 MCollective::Log.expects(:error).with("validrequest? is not implemented in MCollective::Security::Base")
252 @plugin.validrequest?(nil)
256 describe "#encoderequest" do
257 it "should log an error when not implemented" do
258 MCollective::Log.expects(:error).with("encoderequest is not implemented in MCollective::Security::Base")
259 @plugin.encoderequest(nil, nil, nil)
263 describe "#encodereply" do
264 it "should log an error when not implemented" do
265 MCollective::Log.expects(:error).with("encodereply is not implemented in MCollective::Security::Base")
266 @plugin.encodereply(nil, nil, nil)
270 describe "#decodemsg" do
271 it "should log an error when not implemented" do
272 MCollective::Log.expects(:error).with("decodemsg is not implemented in MCollective::Security::Base")
273 @plugin.decodemsg(nil)