3 require File.dirname(__FILE__) + '/../../../../../plugins/mcollective/pluginpackager/debpackage_packager.rb'
7 describe DebpackagePackager do
10 tmpdir = Dir.mktmpdir("mc-test")
20 PluginPackager.stubs(:build_tool?).with("debuild").returns(true)
22 @plugin.stubs(:mcname).returns("mcollective")
26 @tmpdirs.each{|tmpdir| FileUtils.rm_rf tmpdir if File.directory? tmpdir}
29 describe "#initialize" do
30 it "should raise an exception if debuild isn't present" do
31 PluginPackager.expects(:build_tool?).with("debuild").returns(false)
33 DebpackagePackager.new("plugin")
34 }.to raise_error(RuntimeError, "package 'debuild' is not installed")
37 it "should set the correct libdir and verbose value" do
38 PluginPackager.expects(:build_tool?).with("debuild").returns(true)
39 packager = DebpackagePackager.new("plugin", nil, nil, true)
40 packager.libdir.should == "/usr/share/mcollective/plugins/mcollective/"
41 packager.verbose.should == true
45 describe "#create_packages" do
47 @packager = DebpackagePackager.new(@plugin)
48 @plugin.stubs(:packagedata).returns({:test => {:files => ["test.rb"]}})
49 @plugin.stubs(:metadata).returns({:name => "test_plugin", :version => "1"})
50 @plugin.stubs(:iteration).returns("1")
51 @packager.stubs(:prepare_tmpdirs)
52 @packager.stubs(:create_package)
53 @packager.stubs(:move_packages)
54 @packager.stubs(:cleanup_tmpdirs)
55 Dir.stubs(:mktmpdir).with("mcollective_packager").returns("/tmp")
59 it "should set the package instance variables" do
60 @packager.create_packages
61 @packager.current_package_type.should == :test
62 @packager.current_package_data.should == {:files => ["test.rb"]}
63 @packager.current_package_shortname.should == "mcollective-test_plugin-test"
64 @packager.current_package_fullname.should == "mcollective-test_plugin-test_1-1"
67 it "Should create the build dir" do
68 Dir.expects(:mkdir).with("/tmp/mcollective-test_plugin-test_1")
69 @packager.create_packages
72 it "should create packages" do
73 @packager.expects(:create_package)
74 @packager.create_packages
78 describe "#create_package" do
79 it "should raise an exception if the package cannot be created" do
80 packager = DebpackagePackager.new(@plugin)
81 packager.stubs(:create_file).raises("test exception")
83 packager.create_package
84 }.to raise_error(RuntimeError, "Could not build package - test exception")
87 it "should correctly create a package" do
88 packager = DebpackagePackager.new(@plugin, nil, nil, true)
90 packager.expects(:create_file).with("control")
91 packager.expects(:create_file).with("Makefile")
92 packager.expects(:create_file).with("compat")
93 packager.expects(:create_file).with("rules")
94 packager.expects(:create_file).with("copyright")
95 packager.expects(:create_file).with("changelog")
96 packager.expects(:create_tar)
97 packager.expects(:create_install)
98 packager.expects(:create_preandpost_install)
100 packager.build_dir = "/tmp"
101 packager.tmpdir = "/tmp"
102 packager.current_package_fullname = "test"
103 PluginPackager.expects(:safe_system).with("debuild -i -us -uc")
104 packager.expects(:puts).with("Created package test")
106 packager.create_package
109 it "should add a signature if one is given" do
110 packager = DebpackagePackager.new(@plugin, nil, "test", true)
112 packager.expects(:create_file).with("control")
113 packager.expects(:create_file).with("Makefile")
114 packager.expects(:create_file).with("compat")
115 packager.expects(:create_file).with("rules")
116 packager.expects(:create_file).with("copyright")
117 packager.expects(:create_file).with("changelog")
118 packager.expects(:create_tar)
119 packager.expects(:create_install)
120 packager.expects(:create_preandpost_install)
122 packager.build_dir = "/tmp"
123 packager.tmpdir = "/tmp"
124 packager.current_package_fullname = "test"
125 PluginPackager.expects(:safe_system).with("debuild -i -ktest")
126 packager.expects(:puts).with("Created package test")
128 packager.create_package
132 describe "#create_preandpost_install" do
134 @packager = DebpackagePackager.new(@plugin)
137 it "should raise an exception if preinstall is not null and preinstall script isn't present" do
138 @plugin.stubs(:preinstall).returns("myscript")
139 File.expects(:exists?).with("myscript").returns(false)
141 @packager.create_preandpost_install
142 }.to raise_error(RuntimeError, "pre-install script 'myscript' not found")
145 it "should raise an exception if postinstall is not null and postinstall script isn't present" do
146 @plugin.stubs(:preinstall).returns(nil)
147 @plugin.stubs(:postinstall).returns("myscript")
148 File.expects(:exists?).with("myscript").returns(false)
150 @packager.create_preandpost_install
151 }.to raise_error(RuntimeError, "post-install script 'myscript' not found")
154 it "should copy the preinstall and postinstall scripts to the correct directory with the correct name" do
155 @plugin.stubs(:postinstall).returns("myscript")
156 @plugin.stubs(:preinstall).returns("myscript")
157 @packager.build_dir = "/tmp/"
158 @packager.current_package_shortname = "test"
159 File.expects(:exists?).with("myscript").twice.returns(true)
160 FileUtils.expects(:cp).with("myscript", "/tmp/debian/test.preinst")
161 FileUtils.expects(:cp).with("myscript", "/tmp/debian/test.postinst")
162 @packager.create_preandpost_install
166 describe "#create_install" do
168 @packager = DebpackagePackager.new(@plugin)
169 @plugin.stubs(:path).returns("")
172 it "should raise an exception if the install file can't be created" do
173 File.expects(:join).raises("test error")
175 @packager.create_install
176 }.to raise_error(RuntimeError, "Could not create install file - test error")
179 it "should copy the package install file to the correct location" do
181 Dir.mkdir(File.join(tmpdir, "debian"))
182 @packager.build_dir = tmpdir
183 @packager.current_package_shortname = "test"
184 @packager.current_package_data = {:files => ["foo.rb"]}
185 @packager.create_install
186 install_file = File.read("#{tmpdir}/debian/test.install")
187 install_file.should == "/usr/share/mcollective/plugins/mcollective/foo.rb /usr/share/mcollective/plugins/mcollective\n"
191 describe "#move_packages" do
196 it "should move the packages to the working directory" do
199 FileUtils.expects(:cp)
200 @packager = DebpackagePackager.new(@plugin)
201 @packager.move_packages
204 it "should raise an error if the packages could not be moved" do
205 @packager = DebpackagePackager.new(@plugin)
206 File.expects(:join).raises("error")
208 @packager.move_packages
209 }.to raise_error(RuntimeError, "Could not copy packages to working directory: 'error'")
213 describe "#create_tar" do
215 @packager = DebpackagePackager.new(@plugin, nil, true)
218 it "should raise an exception if the tarball can't be built" do
219 PluginPackager.expects(:do_quietly?).raises("test error")
222 }.to raise_error(RuntimeError, "Could not create tarball - test error")
225 it "should create a tarball containing the package files" do
226 @packager.tmpdir = "/tmp"
227 @packager.build_dir = "/build_dir"
228 @packager.current_package_shortname = "test"
229 @plugin.stubs(:metadata).returns(@plugin)
230 @plugin.stubs(:[]).with(:version).returns("1")
231 @plugin.stubs(:iteration).returns("1")
232 PluginPackager.expects(:safe_system).with("tar -Pcvzf /tmp/test_1.orig.tar.gz test_1")
237 describe "#create_file" do
239 @packager = DebpackagePackager.new(@plugin)
242 it "should raise an exception if the file can't be created" do
243 File.expects(:dirname).raises("test error")
245 @packager.create_file("test")
246 }.to raise_error(RuntimeError, "could not create test file - test error")
249 it "should place a build file in the debian directory" do
251 Dir.mkdir(File.join(tmpdir, "debian"))
252 @packager.build_dir = tmpdir
253 File.expects(:read).returns("testfile")
254 @packager.create_file("testfile")
256 result = File.read(File.join(tmpdir, "debian", "testfile"))
257 result.stubs(:result)
258 result.should == "testfile\n"
262 describe "#prepare_tmpdirs" do
264 @tmpfile = Tempfile.new("mc-file").path
269 FileUtils.rm(@tmpfile)
274 it "should create the correct tmp dirs and copy package contents to correct dir" do
275 packager = DebpackagePackager.new(@plugin)
277 packager.build_dir = tmpdir
278 @plugin.stubs(:target_path).returns("")
280 packager.prepare_tmpdirs({:files => [@tmpfile]})
281 File.directory?(tmpdir).should == true
282 File.directory?(File.join(tmpdir, "debian")).should == true
283 File.exists?(File.join(tmpdir, packager.libdir, "tmp", File.basename(@tmpfile))).should == true
287 describe "#cleanup_tmpdirs" do
293 @packager = DebpackagePackager.new(@plugin)
296 it "should cleanup temp directories" do
297 @packager.tmpdir = @tmpdir
298 @packager.cleanup_tmpdirs
299 File.directory?(@tmpdir).should == false
302 it "should not delete any directories if @tmpdir isn't present" do
303 @packager = DebpackagePackager.new(@plugin)
304 @packager.tmpdir = rand.to_s
305 FileUtils.expects(:rm_r).never
306 @packager.cleanup_tmpdirs