0477bb0837b825739ae0be71ca39642c63bb7228
[packages/precise/mcollective.git] / spec / unit / pluginpackager_spec.rb
1 #!/usr/bin/enn rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe PluginPackager do
7     describe "#load_packagers" do
8       it "should load all PluginPackager plugins" do
9         PluginManager.expects(:find_and_load).with("pluginpackager")
10         PluginPackager.load_packagers
11       end
12     end
13
14     describe "#[]" do
15       it "should return the correct class" do
16         PluginPackager.expects(:const_get).with("Foo").returns(:foo)
17         result = PluginPackager["Foo"]
18         result.should == :foo
19       end
20
21       it "should do something else" do
22         expect{
23           PluginPackager["Bar"]
24         }.to raise_error(NameError, 'uninitialized constant MCollective::PluginPackager::Bar')
25       end
26     end
27
28     describe "#get_metadata" do
29       it "should raise an exception if the ddl file can't be loaded" do
30         DDL.expects(:new).with("package", :foo, false)
31         File.stubs(:join)
32         Dir.stubs(:glob).returns('')
33         expect{
34           PluginPackager.get_metadata("/tmp", "foo")
35         }.to raise_error(RuntimeError)
36       end
37
38       it "should load the ddl file and return the metadata" do
39         ddl = mock
40         DDL.expects(:new).with("package", :foo, false).returns(ddl)
41         File.stubs(:join)
42         Dir.stubs(:glob).returns(["foo.ddl"])
43         File.expects(:read).with("foo.ddl").returns("foo_ddl")
44         ddl.expects(:instance_eval).with("foo_ddl")
45         ddl.expects(:meta).returns("metadata")
46         ddl.expects(:requirements).returns({:mcollective => 1})
47
48         meta, requirements = PluginPackager.get_metadata("/tmp", "foo")
49         meta.should == "metadata"
50         requirements.should == 1
51       end
52     end
53
54     describe "#check_dir_present" do
55       it "should return true if the directory is present and not empty" do
56         File.expects(:directory?).with("/tmp").returns(true)
57         File.expects(:join).with("/tmp", "*")
58         Dir.expects(:glob).returns([1])
59         result = PluginPackager.check_dir_present("/tmp")
60         result.should == true
61       end
62
63       it "should return false if the directory is not present" do
64         File.expects(:directory?).with("/tmp").returns(false)
65         result = PluginPackager.check_dir_present("/tmp")
66         result.should == false
67       end
68
69       it "should return false if the direcotry is present but empty" do
70         File.expects(:directory?).with("/tmp").returns(true)
71         File.expects(:join).with("/tmp", "*")
72         Dir.expects(:glob).returns([])
73         result = PluginPackager.check_dir_present("/tmp")
74         result.should == false
75       end
76     end
77
78     describe "#do_quietly?" do
79       it "should call the block parameter if verbose is true" do
80         result = PluginPackager.do_quietly?(true) {:success}
81         result.should == :success
82       end
83
84       it "should call the block parameter quietly if verbose is false" do
85         std_out = Tempfile.new("mc_pluginpackager_spec")
86         File.expects(:new).with("/dev/null", "w").returns(std_out)
87         PluginPackager.do_quietly?(false) {puts "success"}
88         std_out.rewind
89         std_out.read.should == "success\n"
90         std_out.close
91         std_out.unlink
92       end
93
94       it "should raise an exception and reset stdout if the block raises an execption" do
95         expect{
96           PluginPackager.do_quietly?(false) {raise Exception, "exception"}
97         }.to raise_error(Exception, "exception")
98       end
99     end
100
101     describe "#build_tool?" do
102       it "should return true if the given build tool is present on the system" do
103         File.expects(:join).returns("foo")
104         File.expects(:exists?).with("foo").returns(true)
105         result = PluginPackager.build_tool?("foo")
106         result.should == true
107       end
108
109       it "should return false if the given build tool is not present on the system" do
110         File.stubs(:join).returns("foo")
111         File.stubs(:exists?).with("foo").returns(false)
112         result = PluginPackager.build_tool?("foo")
113         result.should == false
114       end
115     end
116
117     describe "#safe_system" do
118       it "should not raise any exceptions if a command ran" do
119         PluginPackager.expects(:system).with("foo").returns(true)
120         lambda{PluginPackager.safe_system("foo")}.should_not raise_error
121       end
122
123       it "should raise a RuntimeError if command cannot be run" do
124         PluginPackager.expects(:system).with("foo").returns(false)
125         expect{
126           PluginPackager.safe_system("foo")
127         }.to raise_error(RuntimeError, "Failed: foo")
128       end
129     end
130   end
131 end