9 @config = mock("config")
10 @config.stubs(:identity).returns("test")
11 @config.stubs(:configured).returns(true)
12 @config.stubs(:topicsep).returns(".")
13 @config.stubs(:topicprefix).returns("/topic/")
15 @stats = mock("stats")
18 ::Time.stubs(:now).returns(@time)
20 MCollective::Log.stubs(:debug).returns(true)
22 MCollective::PluginManager << {:type => "global_stats", :class => @stats}
23 MCollective::Config.stubs("instance").returns(@config)
24 MCollective::Util.stubs("empty_filter?").returns(false)
29 describe "#should_process_msg?" do
30 it "should correctly validate messages" do
32 m.stubs(:expected_msgid).returns("rspec")
34 @plugin.should_process_msg?(m, "rspec").should == true
37 @plugin.should_process_msg?(m, "fail").should == true
38 }.to raise_error(MsgDoesNotMatchRequestID)
41 it "should not test messages without expected_msgid" do
43 m.stubs(:expected_msgid).returns(nil)
45 @plugin.should_process_msg?(m, "rspec").should == true
49 describe "#validate_filter?" do
50 it "should pass on empty filter" do
51 MCollective::Util.stubs("empty_filter?").returns(true)
53 @stats.stubs(:passed).once
54 @stats.stubs(:filtered).never
55 @stats.stubs(:passed).never
57 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
59 @plugin.validate_filter?({}).should == true
62 it "should pass for known classes" do
63 MCollective::Util.stubs("has_cf_class?").with("foo").returns(true)
65 @stats.stubs(:passed).once
66 @stats.stubs(:filtered).never
68 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
69 MCollective::Log.expects(:debug).with("Passing based on configuration management class foo").once
71 @plugin.validate_filter?({"cf_class" => ["foo"]}).should == true
74 it "should fail for unknown classes" do
75 MCollective::Util.stubs("has_cf_class?").with("foo").returns(false)
77 @stats.stubs(:filtered).once
78 @stats.stubs(:passed).never
80 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
81 MCollective::Log.expects(:debug).with("Failing based on configuration management class foo").once
83 @plugin.validate_filter?({"cf_class" => ["foo"]}).should == false
86 it "should pass for known agents" do
87 MCollective::Util.stubs("has_agent?").with("foo").returns(true)
89 @stats.stubs(:passed).once
90 @stats.stubs(:filtered).never
92 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
93 MCollective::Log.expects(:debug).with("Passing based on agent foo").once
95 @plugin.validate_filter?({"agent" => ["foo"]}).should == true
98 it "should fail for unknown agents" do
99 MCollective::Util.stubs("has_agent?").with("foo").returns(false)
101 @stats.stubs(:filtered).once
102 @stats.stubs(:passed).never
104 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
105 MCollective::Log.expects(:debug).with("Failing based on agent foo").once
107 @plugin.validate_filter?({"agent" => ["foo"]}).should == false
110 it "should pass for known facts" do
111 MCollective::Util.stubs("has_fact?").with("fact", "value", "operator").returns(true)
113 @stats.stubs(:passed).once
114 @stats.stubs(:filtered).never
116 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
117 MCollective::Log.expects(:debug).with("Passing based on fact fact operator value").once
119 @plugin.validate_filter?({"fact" => [{:fact => "fact", :operator => "operator", :value => "value"}]}).should == true
122 it "should fail for unknown facts" do
123 MCollective::Util.stubs("has_fact?").with("fact", "value", "operator").returns(false)
125 @stats.stubs(:filtered).once
126 @stats.stubs(:passed).never
128 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
129 MCollective::Log.expects(:debug).with("Failing based on fact fact operator value").once
131 @plugin.validate_filter?({"fact" => [{:fact => "fact", :operator => "operator", :value => "value"}]}).should == false
134 it "should pass for known identity" do
135 MCollective::Util.stubs("has_identity?").with("test").returns(true)
137 @stats.stubs(:passed).once
138 @stats.stubs(:filtered).never
140 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
141 MCollective::Log.expects(:debug).with("Passing based on identity").once
143 @plugin.validate_filter?({"identity" => ["test"]}).should == true
146 it "should fail for known identity" do
147 MCollective::Util.stubs("has_identity?").with("test").returns(false)
149 @stats.stubs(:passed).never
150 @stats.stubs(:filtered).once
152 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
153 MCollective::Log.expects(:debug).with("Failed based on identity").once
155 @plugin.validate_filter?({"identity" => ["test"]}).should == false
158 it "should treat multiple identity filters correctly" do
159 MCollective::Util.stubs("has_identity?").with("foo").returns(false)
160 MCollective::Util.stubs("has_identity?").with("bar").returns(true)
162 @stats.stubs(:passed).once
163 @stats.stubs(:filtered).never
165 MCollective::Log.expects(:debug).with("Message passed the filter checks").once
166 MCollective::Log.expects(:debug).with("Passing based on identity").once
168 @plugin.validate_filter?({"identity" => ["foo", "bar"]}).should == true
171 it "should fail if no identity matches are found" do
172 MCollective::Util.stubs("has_identity?").with("foo").returns(false)
173 MCollective::Util.stubs("has_identity?").with("bar").returns(false)
175 @stats.stubs(:passed).never
176 @stats.stubs(:filtered).once
178 MCollective::Log.expects(:debug).with("Message failed the filter checks").once
179 MCollective::Log.expects(:debug).with("Failed based on identity").once
181 @plugin.validate_filter?({"identity" => ["foo", "bar"]}).should == false
185 describe "#create_reply" do
186 it "should return correct data" do
187 expected = {:senderid => "test",
188 :requestid => "reqid",
189 :senderagent => "agent",
190 :msgtime => @time.to_i,
193 @plugin.create_reply("reqid", "agent", "body").should == expected
197 describe "#create_request" do
198 it "should return correct data" do
199 expected = {:body => "body",
201 :requestid => "reqid",
202 :callerid => "uid=#{Process.uid}",
203 :agent => "discovery",
204 :collective => "mcollective",
207 :msgtime => @time.to_i}
209 @plugin.create_request("reqid", "filter", "body", :server, "discovery", "mcollective", 20).should == expected
212 it "should set the callerid when appropriate" do
213 expected = {:body => "body",
215 :requestid => "reqid",
216 :agent => "discovery",
217 :collective => "mcollective",
219 :callerid => "callerid",
221 :msgtime => @time.to_i}
223 @plugin.stubs(:callerid).returns("callerid")
224 @plugin.create_request("reqid", "filter", "body", :client, "discovery", "mcollective").should == expected
228 describe "#valid_callerid?" do
229 it "should not pass invalid callerids" do
230 @plugin.valid_callerid?("foo-bar").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
234 @plugin.valid_callerid?("foo=bar|baz").should == false
237 it "should pass valid callerids" do
238 @plugin.valid_callerid?("cert=foo-bar").should == true
239 @plugin.valid_callerid?("uid=foo.bar").should == true
240 @plugin.valid_callerid?("uid=foo.bar.123").should == true
244 describe "#callerid" do
245 it "should return a unix UID based callerid" do
246 @plugin.callerid.should == "uid=#{Process.uid}"
250 describe "#validrequest?" do
251 it "should log an error when not implemented" do
252 MCollective::Log.expects(:error).with("validrequest? is not implemented in MCollective::Security::Base")
253 @plugin.validrequest?(nil)
257 describe "#encoderequest" do
258 it "should log an error when not implemented" do
259 MCollective::Log.expects(:error).with("encoderequest is not implemented in MCollective::Security::Base")
260 @plugin.encoderequest(nil, nil, nil)
264 describe "#encodereply" do
265 it "should log an error when not implemented" do
266 MCollective::Log.expects(:error).with("encodereply is not implemented in MCollective::Security::Base")
267 @plugin.encodereply(nil, nil, nil)
271 describe "#decodemsg" do
272 it "should log an error when not implemented" do
273 MCollective::Log.expects(:error).with("decodemsg is not implemented in MCollective::Security::Base")
274 @plugin.decodemsg(nil)