Update mcollective.init according to OSCI-855
[packages/precise/mcollective.git] / spec / unit / pluginmanager_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe PluginManager do
7     before do
8       class MCollective::Foo; end
9
10       PluginManager.pluginlist.each {|p| PluginManager.delete p}
11     end
12
13     describe "#<<" do
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
17       end
18
19       it "should store a plugin instance" do
20         f = MCollective::Foo.new
21
22         PluginManager << {:type => "foo", :class => f}
23         PluginManager.instance_variable_get("@plugins")["foo"][:instance].object_id.should == f.object_id
24       end
25
26       it "should detect duplicate plugins" do
27         PluginManager << {:type => "foo", :class => "MCollective::Foo"}
28
29         expect {
30           PluginManager << {:type => "foo", :class => "MCollective::Foo"}
31         }.to raise_error("Plugin foo already loaded")
32       end
33
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
37       end
38
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
42       end
43     end
44
45     describe "#delete" do
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
51       end
52     end
53
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
59       end
60     end
61
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}
66
67         PluginManager.pluginlist.sort.should == ["bar", "foo"]
68       end
69     end
70
71     describe "#[]" do
72       it "should detect if the requested plugin does not exist" do
73         expect {
74           PluginManager["foo"]
75         }.to raise_error("No plugin foo defined")
76       end
77
78       it "should create new instances on demand" do
79         PluginManager << {:type => "foo", :class => "MCollective::Foo"}
80         PluginManager["foo"].class.should == MCollective::Foo
81       end
82
83       it "should return the cached instance" do
84         f = MCollective::Foo.new
85
86         PluginManager << {:type => "foo", :class => f}
87         PluginManager["foo"].object_id.should == f.object_id
88       end
89
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
93       end
94     end
95
96     describe "#find" do
97       before do
98         @config.stubs(:libdir).returns(["/libdir/"])
99         Config.stubs(:instance).returns(@config)
100       end
101
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"]
107       end
108
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"]
114       end
115
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"]
124       end
125     end
126
127     describe "#find_and_load" do
128       before do
129         @config.stubs(:libdir).returns(["/libdir/"])
130         Config.stubs(:instance).returns(@config)
131         PluginManager.expects(:loadclass).with("MCollective::Test::Testplugin", true)
132       end
133
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")
137       end
138
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/)}
142       end
143     end
144
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")
149       end
150
151       it "should raise errors for load errors" do
152         PluginManager.stubs(:load).raises("load failure")
153         Log.expects(:error)
154         expect { PluginManager.loadclass("foo") }.to raise_error(/load failure/)
155       end
156
157       it "should support squashing load errors" do
158         PluginManager.stubs(:load).raises("load failure")
159         Log.expects(:error)
160         PluginManager.loadclass("foo", true)
161       end
162     end
163
164     describe "#grep" do
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}
168
169         PluginManager.grep(/oo/).should == ["foo"]
170       end
171     end
172   end
173 end