74755d800c7deb906039172ebd08ac6da2d14da2
[packages/precise/mcollective.git] / spec / unit / plugins / mcollective / packagers / debpackage_packager_spec.rb
1 #!/usr/bin/env rspec
2 require 'spec_helper'
3 require File.dirname(__FILE__) + '/../../../../../plugins/mcollective/pluginpackager/debpackage_packager.rb'
4
5 module MCollective
6   module PluginPackager
7     describe DebpackagePackager do
8
9       let(:maketmpdir) do
10         tmpdir = Dir.mktmpdir("mc-test")
11         @tmpdirs << tmpdir
12         tmpdir
13       end
14
15       before :all do
16         @tmpdirs = []
17       end
18
19       before :each do
20         PluginPackager.stubs(:build_tool?).with("debuild").returns(true)
21         @plugin = mock()
22         @plugin.stubs(:mcname).returns("mcollective")
23       end
24
25       after :all do
26         @tmpdirs.each{|tmpdir| FileUtils.rm_rf tmpdir if File.directory? tmpdir}
27       end
28
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)
32           expect{
33             DebpackagePackager.new("plugin")
34           }.to raise_error(RuntimeError, "package 'debuild' is not installed")
35         end
36
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
42         end
43       end
44
45       describe "#create_packages" do
46         before :each 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")
56           Dir.stubs(:mkdir)
57         end
58
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"
65         end
66
67         it "Should create the build dir" do
68           Dir.expects(:mkdir).with("/tmp/mcollective-test_plugin-test_1")
69           @packager.create_packages
70         end
71
72         it "should create packages" do
73           @packager.expects(:create_package)
74           @packager.create_packages
75         end
76       end
77
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")
82           expect{
83             packager.create_package
84           }.to raise_error(RuntimeError, "Could not build package - test exception")
85         end
86
87         it "should correctly create a package" do
88           packager = DebpackagePackager.new(@plugin, nil, nil, true)
89
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)
99
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")
105
106           packager.create_package
107         end
108
109         it "should add a signature if one is given" do
110           packager = DebpackagePackager.new(@plugin, nil, "test", true)
111
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)
121
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")
127
128           packager.create_package
129         end
130       end
131
132       describe "#create_preandpost_install" do
133         before :each do
134           @packager = DebpackagePackager.new(@plugin)
135         end
136
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)
140           expect{
141             @packager.create_preandpost_install
142           }.to raise_error(RuntimeError, "pre-install script 'myscript' not found")
143         end
144
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)
149           expect{
150             @packager.create_preandpost_install
151           }.to raise_error(RuntimeError, "post-install script 'myscript' not found")
152         end
153
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
163         end
164       end
165
166       describe "#create_install" do
167         before :each do
168           @packager = DebpackagePackager.new(@plugin)
169           @plugin.stubs(:path).returns("")
170         end
171
172         it "should raise an exception if the install file can't be created" do
173           File.expects(:join).raises("test error")
174           expect{
175             @packager.create_install
176           }.to raise_error(RuntimeError, "Could not create install file - test error")
177         end
178
179         it "should copy the package install file to the correct location" do
180           tmpdir = maketmpdir
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"
188         end
189       end
190
191       describe "#move_packages" do
192         before :each do
193           @plugin = mock()
194         end
195
196         it "should move the packages to the working directory" do
197           Dir.expects(:glob)
198           File.expects(:join)
199           FileUtils.expects(:cp)
200           @packager = DebpackagePackager.new(@plugin)
201           @packager.move_packages
202         end
203
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")
207           expect{
208             @packager.move_packages
209           }.to raise_error(RuntimeError, "Could not copy packages to working directory: 'error'")
210         end
211       end
212
213       describe "#create_tar" do
214         before :each do
215           @packager = DebpackagePackager.new(@plugin, nil, true)
216         end
217
218         it "should raise an exception if the tarball can't be built" do
219           PluginPackager.expects(:do_quietly?).raises("test error")
220           expect{
221             @packager.create_tar
222           }.to raise_error(RuntimeError, "Could not create tarball - test error")
223         end
224
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")
233           @packager.create_tar
234         end
235       end
236
237       describe "#create_file" do
238         before :each do
239           @packager = DebpackagePackager.new(@plugin)
240         end
241
242         it "should raise an exception if the file can't be created" do
243           File.expects(:dirname).raises("test error")
244           expect{
245             @packager.create_file("test")
246           }.to raise_error(RuntimeError, "could not create test file - test error")
247         end
248
249         it "should place a build file in the debian directory" do
250           tmpdir = maketmpdir
251           Dir.mkdir(File.join(tmpdir, "debian"))
252           @packager.build_dir = tmpdir
253           File.expects(:read).returns("testfile")
254           @packager.create_file("testfile")
255           File.unstub(:read)
256           result = File.read(File.join(tmpdir, "debian", "testfile"))
257           result.stubs(:result)
258           result.should == "testfile\n"
259         end
260       end
261
262       describe "#prepare_tmpdirs" do
263         before :each do
264           @tmpfile = Tempfile.new("mc-file").path
265         end
266
267         after :each do
268           begin
269             FileUtils.rm(@tmpfile)
270           rescue Exception
271           end
272         end
273
274         it "should create the correct tmp dirs and copy package contents to correct dir" do
275           packager = DebpackagePackager.new(@plugin)
276           tmpdir = maketmpdir
277           packager.build_dir = tmpdir
278           @plugin.stubs(:target_path).returns("")
279
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
284         end
285       end
286
287       describe "#cleanup_tmpdirs" do
288         before :all do
289           @tmpdir = maketmpdir
290         end
291
292         before :each do
293           @packager = DebpackagePackager.new(@plugin)
294         end
295
296         it "should cleanup temp directories" do
297           @packager.tmpdir = @tmpdir
298           @packager.cleanup_tmpdirs
299           File.directory?(@tmpdir).should == false
300         end
301
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
307         end
308       end
309     end
310   end
311 end