6 describe PluginManager do
8 class MCollective::Foo; end
10 PluginManager.pluginlist.each {|p| PluginManager.delete p}
14 it "should store a plugin by name" do
15 PluginManager << {:type => "foo", :class => "MCollective::Foo"}
16 PluginManager.instance_variable_get("@plugins").include?("foo").should == true
19 it "should store a plugin instance" do
20 f = MCollective::Foo.new
22 PluginManager << {:type => "foo", :class => f}
23 PluginManager.instance_variable_get("@plugins")["foo"][:instance].object_id.should == f.object_id
26 it "should detect duplicate plugins" do
27 PluginManager << {:type => "foo", :class => "MCollective::Foo"}
30 PluginManager << {:type => "foo", :class => "MCollective::Foo"}
31 }.to raise_error("Plugin foo already loaded")
34 it "should store single instance preference correctly" do
35 PluginManager << {:type => "foo", :class => "MCollective::Foo", :single_instance => false}
36 PluginManager.instance_variable_get("@plugins")["foo"][:single].should == false
39 it "should always set single to true when supplied an instance" do
40 PluginManager << {:type => "foo", :class => MCollective::Foo.new, :single_instance => false}
41 PluginManager.instance_variable_get("@plugins")["foo"][:single].should == true
46 it "should remove plugins" do
47 PluginManager << {:type => "foo", :class => MCollective::Foo.new}
48 PluginManager.instance_variable_get("@plugins").include?("foo").should == true
49 PluginManager.delete("foo")
50 PluginManager.instance_variable_get("@plugins").include?("foo").should == false
54 describe "#include?" do
55 it "should correctly check if plugins were added" do
56 PluginManager << {:type => "foo", :class => MCollective::Foo.new}
57 PluginManager.include?("foo").should == true
58 PluginManager.include?("bar").should == false
62 describe "#pluginlist" do
63 it "should return the correct list of plugins" do
64 PluginManager << {:type => "foo", :class => MCollective::Foo.new}
65 PluginManager << {:type => "bar", :class => MCollective::Foo.new}
67 PluginManager.pluginlist.sort.should == ["bar", "foo"]
72 it "should detect if the requested plugin does not exist" do
75 }.to raise_error("No plugin foo defined")
78 it "should create new instances on demand" do
79 PluginManager << {:type => "foo", :class => "MCollective::Foo"}
80 PluginManager["foo"].class.should == MCollective::Foo
83 it "should return the cached instance" do
84 f = MCollective::Foo.new
86 PluginManager << {:type => "foo", :class => f}
87 PluginManager["foo"].object_id.should == f.object_id
90 it "should create new instances on every request if requested" do
91 PluginManager << {:type => "foo", :class => "MCollective::Foo", :single_instance => false}
92 PluginManager["foo"].object_id.should_not == PluginManager["foo"].object_id
98 @config.stubs(:libdir).returns(["/libdir/"])
99 Config.stubs(:instance).returns(@config)
102 it "should find all plugins in configured libdirs" do
103 File.expects(:join).with(["/libdir/", "mcollective", "test"]).returns("/plugindir/")
104 File.expects(:directory?).with("/plugindir/").returns(true)
105 Dir.expects(:new).with("/plugindir/").returns(["plugin.rb"])
106 PluginManager.find("test").should == ["plugin"]
109 it "should find all plugins with a given file extension" do
110 File.expects(:join).with(["/libdir/", "mcollective", "test"]).returns("/plugindir/")
111 File.expects(:directory?).with("/plugindir/").returns(true)
112 Dir.expects(:new).with("/plugindir/").returns(["plugin.ddl"])
113 PluginManager.find("test", "ddl").should == ["plugin"]
116 it "should skip libdirs that do not have the plugin type directories" do
117 @config.stubs(:libdir).returns(["/plugindir/", "/tmp/"])
118 File.expects(:join).with(["/plugindir/", "mcollective", "test"]).returns("/plugindir/")
119 File.expects(:join).with(["/tmp/", "mcollective", "test"]).returns("/tmpdir/")
120 File.expects(:directory?).with("/plugindir/").returns(true)
121 File.expects(:directory?).with("/tmpdir/").returns(false)
122 Dir.expects(:new).with("/plugindir/").returns(["plugin.ddl"])
123 PluginManager.find("test", "ddl").should == ["plugin"]
127 describe "#find_and_load" do
129 @config.stubs(:libdir).returns(["/libdir/"])
130 Config.stubs(:instance).returns(@config)
131 PluginManager.expects(:loadclass).with("MCollective::Test::Testplugin", true)
134 it "should find and load all plugins from all libdirs that match the type" do
135 PluginManager.expects(:find).with("test", ".rb").returns(["testplugin"])
136 PluginManager.find_and_load("test")
139 it "should exclude plugins who do not match criteria if block is given" do
140 PluginManager.expects(:find).with("test", ".rb").returns(["testplugin", "failplugin"])
141 PluginManager.find_and_load("test") {|plugin| plugin.match(/^test/)}
145 describe "#loadclass" do
146 it "should load the correct filename given a ruby class name" do
147 PluginManager.stubs(:load).with("mcollective/foo.rb").once
148 PluginManager.loadclass("MCollective::Foo")
151 it "should raise errors for load errors" do
152 PluginManager.stubs(:load).raises("load failure")
154 expect { PluginManager.loadclass("foo") }.to raise_error(/load failure/)
157 it "should support squashing load errors" do
158 PluginManager.stubs(:load).raises("load failure")
160 PluginManager.loadclass("foo", true)
165 it "should return matching plugins from the list" do
166 PluginManager << {:type => "foo", :class => MCollective::Foo.new}
167 PluginManager << {:type => "bar", :class => MCollective::Foo.new}
169 PluginManager.grep(/oo/).should == ["foo"]