mcollective-client 2.2.4 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mcollective-client might be problematic. Click here for more details.

Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mcollective/application.rb +25 -34
  3. data/lib/mcollective/client.rb +91 -33
  4. data/lib/mcollective/config.rb +42 -43
  5. data/lib/mcollective/data/base.rb +1 -1
  6. data/lib/mcollective/data/result.rb +6 -2
  7. data/lib/mcollective/ddl/agentddl.rb +28 -1
  8. data/lib/mcollective/ddl/base.rb +8 -6
  9. data/lib/mcollective/log.rb +11 -3
  10. data/lib/mcollective/logger/file_logger.rb +4 -4
  11. data/lib/mcollective/matcher.rb +9 -1
  12. data/lib/mcollective/message.rb +14 -23
  13. data/lib/mcollective/optionparser.rb +9 -1
  14. data/lib/mcollective/pluginpackager.rb +24 -3
  15. data/lib/mcollective/pluginpackager/agent_definition.rb +12 -12
  16. data/lib/mcollective/pluginpackager/standard_definition.rb +12 -12
  17. data/lib/mcollective/rpc/agent.rb +15 -12
  18. data/lib/mcollective/rpc/client.rb +67 -31
  19. data/lib/mcollective/rpc/helpers.rb +7 -1
  20. data/lib/mcollective/rpc/reply.rb +3 -1
  21. data/lib/mcollective/shell.rb +45 -8
  22. data/lib/mcollective/util.rb +37 -1
  23. data/lib/mcollective/windows_daemon.rb +14 -3
  24. data/spec/spec_helper.rb +2 -0
  25. data/spec/unit/application_spec.rb +45 -26
  26. data/spec/unit/cache_spec.rb +3 -3
  27. data/spec/unit/client_spec.rb +269 -24
  28. data/spec/unit/config_spec.rb +89 -26
  29. data/spec/unit/data/base_spec.rb +1 -0
  30. data/spec/unit/data/result_spec.rb +19 -1
  31. data/spec/unit/data_spec.rb +3 -0
  32. data/spec/unit/ddl/agentddl_spec.rb +32 -0
  33. data/spec/unit/ddl/base_spec.rb +4 -0
  34. data/spec/unit/ddl/dataddl_spec.rb +1 -1
  35. data/spec/unit/log_spec.rb +44 -27
  36. data/spec/unit/logger/base_spec.rb +1 -1
  37. data/spec/unit/matcher_spec.rb +14 -0
  38. data/spec/unit/message_spec.rb +24 -0
  39. data/spec/unit/optionparser_spec.rb +99 -0
  40. data/spec/unit/pluginpackager/agent_definition_spec.rb +48 -17
  41. data/spec/unit/pluginpackager/standard_definition_spec.rb +44 -20
  42. data/spec/unit/pluginpackager_spec.rb +31 -7
  43. data/spec/unit/plugins/mcollective/agent/rpcutil_spec.rb +176 -0
  44. data/spec/unit/plugins/mcollective/application/plugin_spec.rb +81 -0
  45. data/spec/unit/plugins/mcollective/audit/logfile_spec.rb +44 -0
  46. data/spec/unit/plugins/mcollective/connector/activemq_spec.rb +118 -27
  47. data/spec/unit/plugins/mcollective/connector/rabbitmq_spec.rb +168 -34
  48. data/spec/unit/plugins/mcollective/data/agent_data_spec.rb +1 -0
  49. data/spec/unit/plugins/mcollective/data/fstat_data_spec.rb +1 -0
  50. data/spec/unit/plugins/mcollective/discovery/flatfile_spec.rb +10 -0
  51. data/spec/unit/plugins/mcollective/discovery/stdin_spec.rb +65 -0
  52. data/spec/unit/plugins/mcollective/facts/yaml_facts_spec.rb +65 -0
  53. data/spec/unit/plugins/mcollective/packagers/debpackage_packager_spec.rb +240 -219
  54. data/spec/unit/plugins/mcollective/packagers/modulepackage_packager_spec.rb +209 -0
  55. data/spec/unit/plugins/mcollective/packagers/rpmpackage_packager_spec.rb +223 -109
  56. data/spec/unit/rpc/actionrunner_spec.rb +2 -1
  57. data/spec/unit/rpc/agent_spec.rb +130 -1
  58. data/spec/unit/rpc/client_spec.rb +169 -3
  59. data/spec/unit/security/base_spec.rb +0 -1
  60. data/spec/unit/shell_spec.rb +76 -3
  61. data/spec/unit/util_spec.rb +69 -1
  62. data/spec/unit/windows_daemon_spec.rb +30 -9
  63. metadata +104 -90
  64. data/spec/unit/plugins/mcollective/connector/stomp/eventlogger_spec.rb +0 -34
  65. data/spec/unit/plugins/mcollective/connector/stomp_spec.rb +0 -424
  66. data/spec/unit/plugins/mcollective/validator/any_validator_spec.rb +0 -15
@@ -6,304 +6,325 @@ module MCollective
6
6
  module PluginPackager
7
7
  describe DebpackagePackager, :unless => MCollective::Util.windows? do
8
8
 
9
- let(:maketmpdir) do
10
- tmpdir = Dir.mktmpdir("mc-test")
11
- @tmpdirs << tmpdir
12
- tmpdir
9
+ let(:plugin) do
10
+ plugin = mock
11
+ plugin.stubs(:mcname).returns('mcollective')
12
+ plugin.stubs(:metadata).returns({ :name => 'rspec', :version => '1.0'})
13
+ plugin.stubs(:target_path).returns('/rspec')
14
+ plugin
13
15
  end
14
16
 
15
- before :all do
16
- @tmpdirs = []
17
+ let(:packager) do
18
+ p = DebpackagePackager.new(plugin)
19
+ p.instance_variable_set(:@plugin, plugin)
20
+ p.instance_variable_set(:@tmpdir, 'rspec_tmp')
21
+ p.instance_variable_set(:@build_dir, 'rspec_build')
22
+ p.instance_variable_set(:@libdir, 'rspec_libdir')
23
+ p
17
24
  end
18
25
 
19
- before :each do
20
- PluginPackager.stubs(:build_tool?).with("debuild").returns(true)
21
- @plugin = mock()
22
- @plugin.stubs(:mcname).returns("mcollective")
26
+ let(:data) do
27
+ {:files => ['/rspec/agent/file.rb', '/rspec/agent/file.ddl', '/rspec/application/file.rb'],
28
+ :dependencies => [{:name => 'dep1', :version => nil, :revision => nil},
29
+ {:name => 'dep2', :version => '1.1', :revision => nil},
30
+ {:name => 'dep3', :version => '1.1', :revision => 2}],
31
+ :plugindependency => {:name => 'mcollective-rspec-common'}}
23
32
  end
24
33
 
25
- after :all do
26
- @tmpdirs.each{|tmpdir| FileUtils.rm_rf tmpdir if File.directory? tmpdir}
34
+ before :each do
35
+ PluginPackager.stubs(:command_available?).returns(true)
27
36
  end
28
37
 
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")
38
+ describe '#initialize' do
39
+ it 'should set the instance variables' do
40
+ new_packager = DebpackagePackager.new(plugin)
41
+ new_packager.instance_variable_get(:@plugin).should == plugin
42
+ new_packager.instance_variable_get(:@verbose).should == false
43
+ new_packager.instance_variable_get(:@libdir).should == '/usr/share/mcollective/plugins/mcollective/'
44
+ new_packager.instance_variable_get(:@signature).should == nil
45
+ new_packager.instance_variable_get(:@package_name).should == 'mcollective-rspec'
35
46
  end
36
47
 
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
48
+ it 'should fail if debuild is not present on the system' do
49
+ PluginPackager.stubs(:command_available?).with('debuild').returns(false)
50
+ expect{
51
+ DebpackagePackager.new(plugin)
52
+ }.to raise_error("Cannot build package. 'debuild' is not present on the system.")
42
53
  end
43
54
  end
44
55
 
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)
56
+ describe '#create_packages' do
57
+ it 'should run through the complete build process' do
58
+ build_dir = 'mc-tmp/mcollective-rspec_1.0'
59
+ tmpdir = 'mc-tmp'
60
+ packager.stubs(:puts)
61
+ Dir.expects(:mktmpdir).with('mcollective_packager').returns(tmpdir)
62
+ Dir.expects(:mkdir).with(build_dir)
63
+ packager.expects(:create_debian_dir)
64
+ plugin.stubs(:packagedata).returns({:agent => data})
65
+ packager.expects(:prepare_tmpdirs).with(data)
66
+ packager.expects(:create_install_file).with(:agent, data)
67
+ packager.expects(:create_pre_and_post_install).with(:agent)
68
+ packager.expects(:create_debian_files)
69
+ packager.expects(:create_tar)
70
+ packager.expects(:run_build)
71
+ packager.expects(:move_packages)
72
+ packager.expects(:cleanup_tmpdirs)
73
+ packager.create_packages
57
74
  end
58
75
 
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"
76
+ it 'should clean up tmpdirs if keep_artifacts is false' do
77
+ packager.stubs(:puts)
78
+ Dir.stubs(:mktmpdir).raises('error')
79
+ packager.expects(:cleanup_tmpdirs)
80
+ expect{
81
+ packager.create_packages
82
+ }.to raise_error('error')
65
83
  end
66
84
 
67
- it "Should create the build dir" do
68
- Dir.expects(:mkdir).with("/tmp/mcollective-test_plugin-test_1")
69
- @packager.create_packages
85
+ it 'should keep the build artifacts if keep_artifacts is true' do
86
+ packager.instance_variable_set(:@keep_artifacts, true)
87
+ packager.stubs(:puts)
88
+ Dir.stubs(:mktmpdir).raises('error')
89
+ packager.expects(:cleanup_tmpdirs).never
90
+ expect{
91
+ packager.create_packages
92
+ }.to raise_error('error')
70
93
  end
94
+ end
71
95
 
72
- it "should create packages" do
73
- @packager.expects(:create_package)
74
- @packager.create_packages
96
+ describe '#create_debian_files' do
97
+ it 'should create all the debian build files' do
98
+ ['control', 'Makefile', 'compat', 'rules', 'copyright', 'changelog'].each do |f|
99
+ packager.expects(:create_file).with(f)
100
+ end
101
+ packager.send(:create_debian_files)
75
102
  end
76
103
  end
77
104
 
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")
105
+ describe '#run_build' do
106
+ it 'should build the packages' do
107
+ FileUtils.expects(:cd).with('rspec_build').yields
108
+ PluginPackager.stubs(:do_quietly).with(false).yields
109
+ PluginPackager.expects(:safe_system).with('debuild --no-lintian -i -us -uc')
110
+ packager.send(:run_build)
85
111
  end
86
112
 
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
113
+ it 'should build the package and sign it with the set signature' do
114
+ packager.instance_variable_set(:@signature, '0x1234')
115
+ FileUtils.expects(:cd).with('rspec_build').yields
116
+ PluginPackager.stubs(:do_quietly).with(false).yields
117
+ PluginPackager.expects(:safe_system).with('debuild --no-lintian -i -k0x1234')
118
+ packager.send(:run_build)
107
119
  end
108
120
 
109
- it "should add a signature if one is given" do
110
- packager = DebpackagePackager.new(@plugin, nil, "test", true)
111
121
 
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
122
+ it 'should sign with the exported gpg key' do
123
+ packager.instance_variable_set(:@signature, true)
124
+ FileUtils.expects(:cd).with('rspec_build').yields
125
+ PluginPackager.stubs(:do_quietly).with(false).yields
126
+ PluginPackager.expects(:safe_system).with('debuild --no-lintian -i')
127
+ packager.send(:run_build)
129
128
  end
130
129
  end
131
130
 
132
- describe "#create_preandpost_install" do
133
- before :each do
134
- @packager = DebpackagePackager.new(@plugin)
131
+ describe '#build_dependency_string' do
132
+ it 'should create the correct dependency string' do
133
+ PluginPackager.expects(:filter_dependencies).with('debian', data[:dependencies]).returns(data[:dependencies])
134
+ result = packager.send(:build_dependency_string, data)
135
+ result.should == 'dep1, dep2 (>=1.1), dep3 (>=1.1-2), mcollective-rspec-common (= ${binary:Version})'
135
136
  end
137
+ end
136
138
 
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")
139
+ describe '#create_install_file' do
140
+ it 'should create the .install file in the correct location' do
141
+ file = mock
142
+ file.expects(:puts).with('rspec_libdir/agent/file.rb rspec_libdir/agent')
143
+ file.expects(:puts).with('rspec_libdir/agent/file.ddl rspec_libdir/agent')
144
+ file.expects(:puts).with('rspec_libdir/application/file.rb rspec_libdir/application')
145
+ File.expects(:open).with('rspec_build/debian/mcollective-rspec-agent.install', 'w').yields(file)
146
+ packager.send(:create_install_file, :agent, data)
143
147
  end
144
148
 
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
+ it 'should write a message and raise an error if we do not have permission' do
150
+ File.expects(:open).with('rspec_build/debian/mcollective-rspec-agent.install', 'w').raises(Errno::EACCES)
151
+ packager.expects(:puts).with("Could not create install file 'rspec_build/debian/mcollective-rspec-agent.install'. Permission denied")
149
152
  expect{
150
- @packager.create_preandpost_install
151
- }.to raise_error(RuntimeError, "post-install script 'myscript' not found")
153
+ packager.send(:create_install_file, :agent, data)
154
+ }.to raise_error(Errno::EACCES)
152
155
  end
153
156
 
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
157
+ it 'should write a message and raise an error if we cannot create the install file' do
158
+ File.expects(:open).with('rspec_build/debian/mcollective-rspec-agent.install', 'w').raises('error')
159
+ packager.expects(:puts).with("Could not create install file 'rspec_build/debian/mcollective-rspec-agent.install'.")
160
+ expect{
161
+ packager.send(:create_install_file, :agent, data)
162
+ }.to raise_error('error')
163
163
  end
164
164
  end
165
165
 
166
- describe "#create_install" do
167
- before :each do
168
- @packager = DebpackagePackager.new(@plugin)
169
- @plugin.stubs(:path).returns("")
166
+ describe '#move_packages' do
167
+ it 'should move the source package and debs to the cdw' do
168
+ files = ['rspec.deb', 'rspec.diff.gz', 'rspec.orig.tar.gz', 'rspec.changes']
169
+ Dir.stubs(:glob).returns(files)
170
+ FileUtils.expects(:cp).with(files, '.')
171
+ packager.send(:move_packages)
170
172
  end
171
173
 
172
- it "should raise an exception if the install file can't be created" do
173
- File.expects(:join).raises("test error")
174
+ it 'should log an error and raise an error if the files cannot be moved' do
175
+ files = ['rspec.deb', 'rspec.diff.gz', 'rspec.orig.tar.gz', 'rspec.changes']
176
+ Dir.stubs(:glob).returns(files)
177
+ FileUtils.expects(:cp).with(files, '.').raises('error')
178
+ packager.expects(:puts).with('Could not copy packages to working directory.')
174
179
  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"
180
+ packager.send(:move_packages)
181
+ }.to raise_error('error')
188
182
  end
189
183
  end
190
184
 
191
- describe "#move_packages" do
192
- before :each do
193
- @plugin = mock()
185
+ describe '#create_pre_and_post_install' do
186
+ it 'should create the pre-install file' do
187
+ plugin.stubs(:preinstall).returns('rspec-preinstall')
188
+ plugin.stubs(:postinstall).returns(nil)
189
+ File.expects(:exists?).with('rspec-preinstall').returns(true)
190
+ FileUtils.expects(:cp).with('rspec-preinstall', 'rspec_build/debian/mcollective-rspec-agent.preinst')
191
+ packager.send(:create_pre_and_post_install, :agent)
194
192
  end
195
193
 
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
194
+ it 'should create the post-install file' do
195
+ plugin.stubs(:preinstall).returns(nil)
196
+ plugin.stubs(:postinstall).returns('rspec-postinstall')
197
+ File.expects(:exists?).with('rspec-postinstall').returns(true)
198
+ FileUtils.expects(:cp).with('rspec-postinstall', 'rspec_build/debian/mcollective-rspec-agent.postinst')
199
+ packager.send(:create_pre_and_post_install, :agent)
202
200
  end
203
201
 
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")
202
+ it 'should fail if a pre-install script is defined but the file does not exist' do
203
+ plugin.stubs(:preinstall).returns('rspec-preinstall')
204
+ File.expects(:exists?).with('rspec-preinstall').returns(false)
205
+ packager.expects(:puts).with("pre-install script 'rspec-preinstall' not found.")
207
206
  expect{
208
- @packager.move_packages
209
- }.to raise_error(RuntimeError, "Could not copy packages to working directory: 'error'")
207
+ packager.send(:create_pre_and_post_install, :agent)
208
+ }.to raise_error(Errno::ENOENT, 'No such file or directory - rspec-preinstall')
210
209
  end
211
- end
212
210
 
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")
211
+ it 'should fail if a post-install script is defined but the file does not exist' do
212
+ plugin.stubs(:preinstall).returns(nil)
213
+ plugin.stubs(:postinstall).returns('rspec-postinstall')
214
+ File.expects(:exists?).with('rspec-postinstall').returns(false)
215
+ packager.expects(:puts).with("post-install script 'rspec-postinstall' not found.")
220
216
  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
217
+ packager.send(:create_pre_and_post_install, :agent)
218
+ }.to raise_error(Errno::ENOENT, 'No such file or directory - rspec-postinstall')
234
219
  end
235
220
  end
236
221
 
237
- describe "#create_file" do
238
- before :each do
239
- @packager = DebpackagePackager.new(@plugin)
222
+ describe '#create_tar' do
223
+ it 'should create the tarball' do
224
+ PluginPackager.stubs(:do_quietly?).yields
225
+ Dir.stubs(:chdir).with('rspec_tmp').yields
226
+ PluginPackager.expects(:safe_system).with('tar -Pcvzf rspec_tmp/mcollective-rspec_1.0.orig.tar.gz mcollective-rspec_1.0')
227
+ packager.send(:create_tar)
240
228
  end
241
229
 
242
- it "should raise an exception if the file can't be created" do
243
- File.expects(:dirname).raises("test error")
230
+ it 'should log an error and raise an exception if it cannot create the tarball' do
231
+ PluginPackager.stubs(:do_quietly?).yields
232
+ Dir.stubs(:chdir).with('rspec_tmp').yields
233
+ PluginPackager.expects(:safe_system).raises('error')
234
+ packager.expects(:puts).with("Could not create tarball - mcollective-rspec_1.0.orig.tar.gz")
244
235
  expect{
245
- @packager.create_file("test")
246
- }.to raise_error(RuntimeError, "could not create test file - test error")
236
+ packager.send(:create_tar)
237
+ }.to raise_error('error')
247
238
  end
239
+ end
248
240
 
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"
241
+ describe '#create_file' do
242
+ it 'should create the named file in the build dir' do
243
+ file = mock
244
+ erb_content = mock
245
+ File.stubs(:read).returns('<%= "file content" %>')
246
+ ERB.expects(:new).with('<%= "file content" %>', nil, '-').returns(erb_content)
247
+ File.stubs(:open).with('rspec_build/debian/rspec', 'w').yields(file)
248
+ erb_content.expects(:result).returns('file content')
249
+ file.expects(:puts).with('file content')
250
+ packager.send(:create_file, 'rspec')
251
+ end
252
+
253
+ it 'should log an error and raise if it cannot create the file' do
254
+ File.stubs(:read).returns('<%= "file content" %>')
255
+ ERB.stubs(:new).with('<%= "file content" %>', nil, '-').raises('error')
256
+ packager.expects(:puts).with("Could not create file - 'rspec'")
257
+ expect{
258
+ packager.send(:create_file, 'rspec')
259
+ }.to raise_error('error')
259
260
  end
260
261
  end
261
262
 
262
- describe "#prepare_tmpdirs" do
263
- before :each do
264
- @tmpfile = Tempfile.new("mc-file").path
263
+ describe '#prepare_tmpdirs' do
264
+ it 'should create the target directories and copy the files' do
265
+ FileUtils.expects(:mkdir_p).with('rspec_build/rspec_libdir/agent').twice
266
+ FileUtils.expects(:mkdir_p).with('rspec_build/rspec_libdir/application')
267
+ FileUtils.expects(:cp_r).with('/rspec/agent/file.rb', 'rspec_build/rspec_libdir/agent')
268
+ FileUtils.expects(:cp_r).with('/rspec/agent/file.ddl', 'rspec_build/rspec_libdir/agent')
269
+ FileUtils.expects(:cp_r).with('/rspec/application/file.rb', 'rspec_build/rspec_libdir/application')
270
+ packager.send(:prepare_tmpdirs, data)
265
271
  end
266
272
 
267
- after :each do
268
- begin
269
- FileUtils.rm(@tmpfile)
270
- rescue Exception
271
- end
273
+ it 'should log an error and raise if permissions means the dir cannot be created' do
274
+ FileUtils.stubs(:mkdir_p).with('rspec_build/rspec_libdir/agent').raises(Errno::EACCES)
275
+ packager.expects(:puts).with("Could not create directory 'rspec_build/rspec_libdir/agent'. Permission denied")
276
+ expect{
277
+ packager.send(:prepare_tmpdirs, data)
278
+ }.to raise_error(Errno::EACCES)
272
279
  end
273
280
 
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("")
281
+ it 'should log an error and raise if the file does not exist' do
282
+ FileUtils.stubs(:mkdir_p)
283
+ FileUtils.expects(:cp_r).with('/rspec/agent/file.rb', 'rspec_build/rspec_libdir/agent').raises(Errno::ENOENT)
284
+ packager.expects(:puts).with("Could not copy file '/rspec/agent/file.rb' to 'rspec_build/rspec_libdir/agent'. File does not exist")
285
+ expect{
286
+ packager.send(:prepare_tmpdirs, data)
287
+ }.to raise_error(Errno::ENOENT)
288
+ end
279
289
 
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
290
+ it 'should log an error and raise for any other exception' do
291
+ File.stubs(:expand_path).raises('error')
292
+ packager.expects(:puts).with('Could not prepare build directory')
293
+ expect{
294
+ packager.send(:prepare_tmpdirs, data)
295
+ }.to raise_error('error')
284
296
  end
285
297
  end
286
298
 
287
- describe "#cleanup_tmpdirs" do
288
- before :all do
289
- @tmpdir = maketmpdir
299
+ describe '#create_debian_dir' do
300
+ it 'should create the debian dir in the build dir' do
301
+ FileUtils.expects(:mkdir_p).with('rspec_build/debian')
302
+ packager.send(:create_debian_dir)
290
303
  end
291
304
 
292
- before :each do
293
- @packager = DebpackagePackager.new(@plugin)
305
+ it 'should log an error and raise an exception if the dir cannot be created' do
306
+ FileUtils.expects(:mkdir_p).with('rspec_build/debian').raises('error')
307
+ packager.expects(:puts).with("Could not create directory 'rspec_build/debian'")
308
+ expect{
309
+ packager.send(:create_debian_dir)
310
+ }.to raise_error('error')
294
311
  end
312
+ end
295
313
 
296
- it "should cleanup temp directories" do
297
- @packager.tmpdir = @tmpdir
298
- @packager.cleanup_tmpdirs
299
- File.directory?(@tmpdir).should == false
314
+ describe '#cleanup_tmpdirs' do
315
+ it 'should remove the temporary build directory' do
316
+ File.stubs(:directory?).with('rspec_tmp').returns(true)
317
+ FileUtils.expects(:rm_r).with('rspec_tmp')
318
+ packager.send(:cleanup_tmpdirs)
300
319
  end
301
320
 
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
321
+ it 'should log an error and raise an exception if the directory could not be removed' do
322
+ File.stubs(:directory?).with('rspec_tmp').returns(true)
323
+ FileUtils.expects(:rm_r).with('rspec_tmp').raises('error')
324
+ packager.expects(:puts).with("Could not remove temporary build directory - 'rspec_tmp'")
325
+ expect{
326
+ packager.send(:cleanup_tmpdirs)
327
+ }.to raise_error('error')
307
328
  end
308
329
  end
309
330
  end