8 tmpfile = Tempfile.new("mc_agents_spec")
12 @tmpdir = FileUtils.mkdir_p(path)
13 @tmpdir = @tmpdir[0] if @tmpdir.is_a?(Array) # ruby 1.9.2
15 @agentsdir = File.join([@tmpdir, "mcollective", "agent"])
16 FileUtils.mkdir_p(@agentsdir)
25 FileUtils.rm_r(@tmpdir)
28 describe "#initialize" do
29 it "should fail if configuration has not been loaded" do
30 Config.instance.expects(:configured).returns(false)
34 }.to raise_error("Configuration has not been loaded, can't load agents")
37 it "should load agents" do
38 Config.instance.expects(:configured).returns(true)
39 Agents.any_instance.expects(:loadagents).once
46 it "should delete and unsubscribe all loaded agents" do
47 Config.instance.expects(:configured).returns(true).at_least_once
48 Config.instance.expects(:libdir).returns([@tmpdir])
49 PluginManager.expects(:delete).with("foo_agent").once
50 Util.expects(:make_subscriptions).with("foo", :broadcast).returns("foo_target")
51 Util.expects(:unsubscribe).with("foo_target")
53 a = Agents.new({"foo" => 1})
57 describe "#loadagents" do
59 Config.instance.stubs(:configured).returns(true)
60 Config.instance.stubs(:libdir).returns([@tmpdir])
61 Agents.any_instance.stubs("clear!").returns(true)
64 it "should delete all existing agents" do
65 Agents.any_instance.expects("clear!").once
69 it "should attempt to load agents from all libdirs" do
70 Config.instance.expects(:libdir).returns(["/nonexisting", "/nonexisting"])
71 File.expects("directory?").with("/nonexisting/mcollective/agent").twice
76 it "should load found agents" do
77 Agents.any_instance.expects("loadagent").with("test").once
79 FileUtils.touch(File.join([@agentsdir, "test.rb"]))
84 it "should load each agent unless already loaded" do
85 Agents.any_instance.expects("loadagent").with("test").never
87 FileUtils.touch(File.join([@agentsdir, "test.rb"]))
89 PluginManager << {:type => "test_agent", :class => String.new}
94 describe "#loadagent" do
96 FileUtils.touch(File.join([@agentsdir, "test.rb"]))
97 Config.instance.stubs(:configured).returns(true)
98 Config.instance.stubs(:libdir).returns([@tmpdir])
99 Agents.any_instance.stubs("clear!").returns(true)
100 PluginManager.stubs(:loadclass).returns(true)
101 PluginManager.stubs("[]").with("test_agent").returns(true)
102 Util.stubs(:make_subscriptions).with("test", :broadcast).returns([{:agent => "test", :type => :broadcast, :collective => "test"}])
103 Util.stubs(:subscribe).with([{:agent => "test", :type => :broadcast, :collective => "test"}]).returns(true)
104 Agents.stubs(:findagentfile).returns(File.join([@agentsdir, "test.rb"]))
105 Agents.any_instance.stubs("activate_agent?").returns(true)
110 it "should return false if the agent file is missing" do
111 Agents.any_instance.expects(:findagentfile).returns(false).once
112 @a.loadagent("test").should == false
115 it "should delete the agent before loading again" do
116 PluginManager.expects(:delete).with("test_agent").twice
120 it "should load the agent class from disk" do
121 PluginManager.expects(:loadclass).with("MCollective::Agent::Test")
125 it "should check if the agent should be activated" do
126 Agents.any_instance.expects(:findagentfile).with("test").returns(File.join([@agentsdir, "test.rb"]))
127 Agents.any_instance.expects("activate_agent?").with("test").returns(true)
128 @a.loadagent("test").should == true
131 it "should set discovery and registration to be single instance plugins" do
132 PluginManager.expects("<<").with({:type => "registration_agent", :class => "MCollective::Agent::Registration", :single_instance => true}).once
133 PluginManager.expects("<<").with({:type => "discovery_agent", :class => "MCollective::Agent::Discovery", :single_instance => true}).once
134 Agents.any_instance.expects("activate_agent?").with("registration").returns(true)
135 Agents.any_instance.expects("activate_agent?").with("discovery").returns(true)
137 PluginManager.expects(:loadclass).with("MCollective::Agent::Registration").returns(true).once
138 PluginManager.expects(:loadclass).with("MCollective::Agent::Discovery").returns(true).once
140 FileUtils.touch(File.join([@agentsdir, "registration.rb"]))
141 FileUtils.touch(File.join([@agentsdir, "discovery.rb"]))
143 @a.loadagent("registration")
144 @a.loadagent("discovery")
147 it "should add general plugins as multiple instance plugins" do
148 PluginManager.expects("<<").with({:type => "test_agent", :class => "MCollective::Agent::Test", :single_instance => false}).once
152 it "should add the agent to the plugin manager and subscribe" do
153 PluginManager.expects("<<").with({:type => "foo_agent", :class => "MCollective::Agent::Foo", :single_instance => false})
154 Util.stubs(:make_subscriptions).with("foo", :broadcast).returns([{:agent => "foo", :type => :broadcast, :collective => "test"}])
155 Util.expects("subscribe").with([{:type => :broadcast, :agent => 'foo', :collective => 'test'}]).returns(true)
156 Agents.any_instance.expects(:findagentfile).with("foo").returns(File.join([@agentsdir, "foo.rb"]))
157 FileUtils.touch(File.join([@agentsdir, "foo.rb"]))
158 Agents.any_instance.expects("activate_agent?").with("foo").returns(true)
159 PluginManager.stubs("[]").with("foo_agent").returns(true)
164 it "should check if an agent is loadable and remove it from the list if not" do
165 PluginManager.expects("<<").with({:type => "foo_agent", :class => "MCollective::Agent::Foo", :single_instance => false})
166 Agents.any_instance.expects(:findagentfile).with("foo").returns(File.join([@agentsdir, "foo.rb"]))
167 FileUtils.touch(File.join([@agentsdir, "foo.rb"]))
168 Agents.any_instance.expects("activate_agent?").with("foo").returns(true)
169 PluginManager.stubs("[]").with("foo_agent").raises("rspec")
171 Log.expects(:error).once.with("Loading agent foo failed: rspec")
173 @a.loadagent("foo").should == false
174 Agents.agentlist.include?("foo").should == false
177 it "should add the agent to the agent list" do
178 Agents.agentlist.should == ["test"]
181 it "should return true on success" do
182 @a.loadagent("test").should == true
185 it "should handle load exceptions" do
186 Agents.any_instance.expects(:findagentfile).with("foo").returns(File.join([@agentsdir, "foo.rb"]))
187 Log.expects(:error).with(regexp_matches(/Loading agent foo failed/))
188 @a.loadagent("foo").should == false
191 it "should delete plugins that failed to load" do
192 Agents.any_instance.expects(:findagentfile).with("foo").returns(File.join([@agentsdir, "foo.rb"]))
193 PluginManager.expects(:delete).with("foo_agent").twice
195 @a.loadagent("foo").should == false
199 describe "#class_for_agent" do
200 it "should return the correct class" do
201 Config.instance.stubs(:configured).returns(true)
202 Agents.any_instance.stubs(:loadagents).returns(true)
203 Agents.new.class_for_agent("foo").should == "MCollective::Agent::Foo"
207 describe "#activate_agent?" do
209 Config.instance.stubs(:configured).returns(true)
210 Agents.any_instance.stubs(:loadagents).returns(true)
213 module MCollective::Agent;end
214 class MCollective::Agent::Test; end
217 it "should check if the correct class has an activation method" do
218 Agent::Test.expects("respond_to?").with("activate?").once
220 @a.activate_agent?("test")
223 it "should call the activation method" do
224 Agent::Test.expects("activate?").returns(true).once
225 @a.activate_agent?("test")
228 it "should log a debug message and return true if the class has no activation method" do
229 Agent::Test.expects("respond_to?").with("activate?").returns(false).once
230 Log.expects(:debug).with("MCollective::Agent::Test does not have an activate? method, activating as default")
232 @a.activate_agent?("test").should == true
235 it "should handle exceptions in the activation as false" do
236 Agent::Test.expects("activate?").raises(RuntimeError)
237 @a.activate_agent?("test").should == false
241 describe "#findagentfile" do
243 Config.instance.stubs(:configured).returns(true)
244 Config.instance.stubs(:libdir).returns([@tmpdir])
245 Agents.any_instance.stubs(:loadagents).returns(true)
249 it "should support multiple libdirs" do
250 Config.instance.expects(:libdir).returns([@tmpdir, @tmpdir]).once
251 File.expects("exist?").returns(false).twice
252 @a.findagentfile("test")
255 it "should look for the correct filename in the libdir" do
256 File.expects("exist?").with(File.join([@tmpdir, "mcollective", "agent", "test.rb"])).returns(false).once
257 @a.findagentfile("test")
260 it "should return the full path if the agent is found" do
261 agentfile = File.join([@tmpdir, "mcollective", "agent", "test.rb"])
262 File.expects("exist?").with(agentfile).returns(true).once
263 @a.findagentfile("test").should == agentfile
266 it "should return false if no agent is found" do
267 @a.findagentfile("foo").should == false
271 describe "#include?" do
272 it "should correctly report the plugin state" do
273 Config.instance.stubs(:configured).returns(true)
274 Config.instance.stubs(:libdir).returns([@tmpdir])
275 Agents.any_instance.stubs(:loadagents).returns(true)
276 PluginManager.expects("include?").with("test_agent").returns(true)
280 @a.include?("test").should == true
284 describe "#agentlist" do
285 it "should return the correct agent list" do
286 Config.instance.stubs(:configured).returns(true)
287 Config.instance.stubs(:libdir).returns([@tmpdir])
288 Agents.any_instance.stubs(:loadagents).returns(true)
290 @a = Agents.new("test" => true)
291 Agents.agentlist.should == ["test"]