backup-agoddard 3.0.27

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

Potentially problematic release.


This version of backup-agoddard might be problematic. Click here for more details.

Files changed (190) hide show
  1. data/.gitignore +8 -0
  2. data/.travis.yml +10 -0
  3. data/Gemfile +28 -0
  4. data/Guardfile +23 -0
  5. data/LICENSE.md +24 -0
  6. data/README.md +478 -0
  7. data/backup.gemspec +32 -0
  8. data/bin/backup +11 -0
  9. data/lib/backup.rb +133 -0
  10. data/lib/backup/archive.rb +117 -0
  11. data/lib/backup/binder.rb +22 -0
  12. data/lib/backup/cleaner.rb +121 -0
  13. data/lib/backup/cli/helpers.rb +93 -0
  14. data/lib/backup/cli/utility.rb +255 -0
  15. data/lib/backup/compressor/base.rb +35 -0
  16. data/lib/backup/compressor/bzip2.rb +50 -0
  17. data/lib/backup/compressor/custom.rb +53 -0
  18. data/lib/backup/compressor/gzip.rb +50 -0
  19. data/lib/backup/compressor/lzma.rb +52 -0
  20. data/lib/backup/compressor/pbzip2.rb +59 -0
  21. data/lib/backup/config.rb +174 -0
  22. data/lib/backup/configuration.rb +33 -0
  23. data/lib/backup/configuration/helpers.rb +130 -0
  24. data/lib/backup/configuration/store.rb +24 -0
  25. data/lib/backup/database/base.rb +53 -0
  26. data/lib/backup/database/mongodb.rb +230 -0
  27. data/lib/backup/database/mysql.rb +160 -0
  28. data/lib/backup/database/postgresql.rb +144 -0
  29. data/lib/backup/database/redis.rb +136 -0
  30. data/lib/backup/database/riak.rb +67 -0
  31. data/lib/backup/dependency.rb +108 -0
  32. data/lib/backup/encryptor/base.rb +29 -0
  33. data/lib/backup/encryptor/gpg.rb +760 -0
  34. data/lib/backup/encryptor/open_ssl.rb +72 -0
  35. data/lib/backup/errors.rb +124 -0
  36. data/lib/backup/hooks.rb +68 -0
  37. data/lib/backup/logger.rb +152 -0
  38. data/lib/backup/model.rb +409 -0
  39. data/lib/backup/notifier/base.rb +81 -0
  40. data/lib/backup/notifier/campfire.rb +155 -0
  41. data/lib/backup/notifier/hipchat.rb +93 -0
  42. data/lib/backup/notifier/mail.rb +206 -0
  43. data/lib/backup/notifier/prowl.rb +65 -0
  44. data/lib/backup/notifier/pushover.rb +88 -0
  45. data/lib/backup/notifier/twitter.rb +70 -0
  46. data/lib/backup/package.rb +47 -0
  47. data/lib/backup/packager.rb +100 -0
  48. data/lib/backup/pipeline.rb +110 -0
  49. data/lib/backup/splitter.rb +75 -0
  50. data/lib/backup/storage/base.rb +99 -0
  51. data/lib/backup/storage/cloudfiles.rb +87 -0
  52. data/lib/backup/storage/cycler.rb +117 -0
  53. data/lib/backup/storage/dropbox.rb +178 -0
  54. data/lib/backup/storage/ftp.rb +119 -0
  55. data/lib/backup/storage/local.rb +82 -0
  56. data/lib/backup/storage/ninefold.rb +116 -0
  57. data/lib/backup/storage/rsync.rb +149 -0
  58. data/lib/backup/storage/s3.rb +94 -0
  59. data/lib/backup/storage/scp.rb +99 -0
  60. data/lib/backup/storage/sftp.rb +108 -0
  61. data/lib/backup/syncer/base.rb +46 -0
  62. data/lib/backup/syncer/cloud/base.rb +247 -0
  63. data/lib/backup/syncer/cloud/cloud_files.rb +78 -0
  64. data/lib/backup/syncer/cloud/s3.rb +68 -0
  65. data/lib/backup/syncer/rsync/base.rb +49 -0
  66. data/lib/backup/syncer/rsync/local.rb +55 -0
  67. data/lib/backup/syncer/rsync/pull.rb +36 -0
  68. data/lib/backup/syncer/rsync/push.rb +116 -0
  69. data/lib/backup/template.rb +46 -0
  70. data/lib/backup/version.rb +43 -0
  71. data/spec-live/.gitignore +6 -0
  72. data/spec-live/README +7 -0
  73. data/spec-live/backups/config.rb +83 -0
  74. data/spec-live/backups/config.yml.template +46 -0
  75. data/spec-live/backups/models.rb +184 -0
  76. data/spec-live/compressor/custom_spec.rb +30 -0
  77. data/spec-live/compressor/gzip_spec.rb +30 -0
  78. data/spec-live/encryptor/gpg_keys.rb +239 -0
  79. data/spec-live/encryptor/gpg_spec.rb +287 -0
  80. data/spec-live/notifier/mail_spec.rb +121 -0
  81. data/spec-live/spec_helper.rb +151 -0
  82. data/spec-live/storage/dropbox_spec.rb +151 -0
  83. data/spec-live/storage/local_spec.rb +83 -0
  84. data/spec-live/storage/scp_spec.rb +193 -0
  85. data/spec-live/syncer/cloud/s3_spec.rb +124 -0
  86. data/spec/archive_spec.rb +335 -0
  87. data/spec/cleaner_spec.rb +312 -0
  88. data/spec/cli/helpers_spec.rb +301 -0
  89. data/spec/cli/utility_spec.rb +411 -0
  90. data/spec/compressor/base_spec.rb +52 -0
  91. data/spec/compressor/bzip2_spec.rb +217 -0
  92. data/spec/compressor/custom_spec.rb +106 -0
  93. data/spec/compressor/gzip_spec.rb +217 -0
  94. data/spec/compressor/lzma_spec.rb +123 -0
  95. data/spec/compressor/pbzip2_spec.rb +165 -0
  96. data/spec/config_spec.rb +321 -0
  97. data/spec/configuration/helpers_spec.rb +247 -0
  98. data/spec/configuration/store_spec.rb +39 -0
  99. data/spec/configuration_spec.rb +62 -0
  100. data/spec/database/base_spec.rb +63 -0
  101. data/spec/database/mongodb_spec.rb +510 -0
  102. data/spec/database/mysql_spec.rb +411 -0
  103. data/spec/database/postgresql_spec.rb +353 -0
  104. data/spec/database/redis_spec.rb +334 -0
  105. data/spec/database/riak_spec.rb +176 -0
  106. data/spec/dependency_spec.rb +51 -0
  107. data/spec/encryptor/base_spec.rb +40 -0
  108. data/spec/encryptor/gpg_spec.rb +909 -0
  109. data/spec/encryptor/open_ssl_spec.rb +148 -0
  110. data/spec/errors_spec.rb +306 -0
  111. data/spec/hooks_spec.rb +35 -0
  112. data/spec/logger_spec.rb +367 -0
  113. data/spec/model_spec.rb +694 -0
  114. data/spec/notifier/base_spec.rb +104 -0
  115. data/spec/notifier/campfire_spec.rb +217 -0
  116. data/spec/notifier/hipchat_spec.rb +211 -0
  117. data/spec/notifier/mail_spec.rb +316 -0
  118. data/spec/notifier/prowl_spec.rb +138 -0
  119. data/spec/notifier/pushover_spec.rb +123 -0
  120. data/spec/notifier/twitter_spec.rb +153 -0
  121. data/spec/package_spec.rb +61 -0
  122. data/spec/packager_spec.rb +213 -0
  123. data/spec/pipeline_spec.rb +259 -0
  124. data/spec/spec_helper.rb +60 -0
  125. data/spec/splitter_spec.rb +120 -0
  126. data/spec/storage/base_spec.rb +166 -0
  127. data/spec/storage/cloudfiles_spec.rb +254 -0
  128. data/spec/storage/cycler_spec.rb +247 -0
  129. data/spec/storage/dropbox_spec.rb +480 -0
  130. data/spec/storage/ftp_spec.rb +271 -0
  131. data/spec/storage/local_spec.rb +259 -0
  132. data/spec/storage/ninefold_spec.rb +343 -0
  133. data/spec/storage/rsync_spec.rb +362 -0
  134. data/spec/storage/s3_spec.rb +245 -0
  135. data/spec/storage/scp_spec.rb +233 -0
  136. data/spec/storage/sftp_spec.rb +244 -0
  137. data/spec/syncer/base_spec.rb +109 -0
  138. data/spec/syncer/cloud/base_spec.rb +515 -0
  139. data/spec/syncer/cloud/cloud_files_spec.rb +181 -0
  140. data/spec/syncer/cloud/s3_spec.rb +174 -0
  141. data/spec/syncer/rsync/base_spec.rb +98 -0
  142. data/spec/syncer/rsync/local_spec.rb +149 -0
  143. data/spec/syncer/rsync/pull_spec.rb +98 -0
  144. data/spec/syncer/rsync/push_spec.rb +333 -0
  145. data/spec/version_spec.rb +21 -0
  146. data/templates/cli/utility/archive +25 -0
  147. data/templates/cli/utility/compressor/bzip2 +4 -0
  148. data/templates/cli/utility/compressor/custom +11 -0
  149. data/templates/cli/utility/compressor/gzip +4 -0
  150. data/templates/cli/utility/compressor/lzma +10 -0
  151. data/templates/cli/utility/compressor/pbzip2 +10 -0
  152. data/templates/cli/utility/config +32 -0
  153. data/templates/cli/utility/database/mongodb +18 -0
  154. data/templates/cli/utility/database/mysql +21 -0
  155. data/templates/cli/utility/database/postgresql +17 -0
  156. data/templates/cli/utility/database/redis +16 -0
  157. data/templates/cli/utility/database/riak +11 -0
  158. data/templates/cli/utility/encryptor/gpg +27 -0
  159. data/templates/cli/utility/encryptor/openssl +9 -0
  160. data/templates/cli/utility/model.erb +23 -0
  161. data/templates/cli/utility/notifier/campfire +12 -0
  162. data/templates/cli/utility/notifier/hipchat +15 -0
  163. data/templates/cli/utility/notifier/mail +22 -0
  164. data/templates/cli/utility/notifier/prowl +11 -0
  165. data/templates/cli/utility/notifier/pushover +11 -0
  166. data/templates/cli/utility/notifier/twitter +13 -0
  167. data/templates/cli/utility/splitter +7 -0
  168. data/templates/cli/utility/storage/cloud_files +22 -0
  169. data/templates/cli/utility/storage/dropbox +20 -0
  170. data/templates/cli/utility/storage/ftp +12 -0
  171. data/templates/cli/utility/storage/local +7 -0
  172. data/templates/cli/utility/storage/ninefold +9 -0
  173. data/templates/cli/utility/storage/rsync +11 -0
  174. data/templates/cli/utility/storage/s3 +19 -0
  175. data/templates/cli/utility/storage/scp +11 -0
  176. data/templates/cli/utility/storage/sftp +11 -0
  177. data/templates/cli/utility/syncer/cloud_files +46 -0
  178. data/templates/cli/utility/syncer/rsync_local +12 -0
  179. data/templates/cli/utility/syncer/rsync_pull +17 -0
  180. data/templates/cli/utility/syncer/rsync_push +17 -0
  181. data/templates/cli/utility/syncer/s3 +43 -0
  182. data/templates/general/links +11 -0
  183. data/templates/general/version.erb +2 -0
  184. data/templates/notifier/mail/failure.erb +9 -0
  185. data/templates/notifier/mail/success.erb +7 -0
  186. data/templates/notifier/mail/warning.erb +9 -0
  187. data/templates/storage/dropbox/authorization_url.erb +6 -0
  188. data/templates/storage/dropbox/authorized.erb +4 -0
  189. data/templates/storage/dropbox/cache_file_written.erb +10 -0
  190. metadata +277 -0
@@ -0,0 +1,259 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../spec_helper.rb', __FILE__)
4
+
5
+ describe 'Backup::Pipeline' do
6
+ let(:pipeline) { Backup::Pipeline.new }
7
+
8
+ describe '#initialize' do
9
+ it 'should create a new pipeline' do
10
+ pipeline.instance_variable_get(:@commands).should == []
11
+ pipeline.errors.should == []
12
+ pipeline.stderr.should == ''
13
+ end
14
+ end
15
+
16
+ describe '#<<' do
17
+ it 'should add a command string to @commands' do
18
+ pipeline << 'a command string'
19
+ pipeline.instance_variable_get(:@commands).should == ['a command string']
20
+ end
21
+ end
22
+
23
+ describe '#run' do
24
+ let(:stdout) { mock }
25
+ let(:stderr) { mock }
26
+
27
+ before do
28
+ pipeline.expects(:pipeline).returns('foo')
29
+ # stub CLI::Helpers#command_name so it simply returns what it's passed
30
+ pipeline.class.send(:define_method, :command_name, lambda {|arg| arg } )
31
+ end
32
+
33
+ context 'when pipeline command is successfully executed' do
34
+ before do
35
+ Open4.expects(:popen4).with('foo').yields(nil, nil, stdout, stderr)
36
+ end
37
+
38
+ context 'when all commands within the pipeline are successful' do
39
+ before do
40
+ stdout.expects(:read).returns("0|0:1|0:\n")
41
+ end
42
+
43
+ context 'when commands output no stderr messages' do
44
+ before do
45
+ stderr.expects(:read).returns('')
46
+ pipeline.stubs(:stderr_messages).returns(false)
47
+ end
48
+
49
+ it 'should process the returned stdout/stderr and report no errors' do
50
+ Backup::Logger.expects(:warn).never
51
+
52
+ pipeline.run
53
+ pipeline.stderr.should == ''
54
+ pipeline.errors.should == []
55
+ end
56
+ end
57
+
58
+ context 'when successful commands output messages on stderr' do
59
+ before do
60
+ stderr.expects(:read).returns("stderr output\n")
61
+ pipeline.stubs(:stderr_messages).returns('stderr_messages_output')
62
+ end
63
+
64
+ it 'should log a warning with the stderr messages' do
65
+ Backup::Logger.expects(:warn).with('stderr_messages_output')
66
+
67
+ pipeline.run
68
+ pipeline.stderr.should == 'stderr output'
69
+ pipeline.errors.should == []
70
+ end
71
+ end
72
+ end # context 'when all commands within the pipeline are successful'
73
+
74
+ context 'when commands within the pipeline are not successful' do
75
+ before do
76
+ pipeline.instance_variable_set(:@commands, ['first', 'second', 'third'])
77
+ stderr.expects(:read).returns("stderr output\n")
78
+ pipeline.stubs(:stderr_messages).returns('success? should be false')
79
+ end
80
+
81
+ context 'when the commands return in sequence' do
82
+ before do
83
+ stdout.expects(:read).returns("0|0:1|1:2|0:\n")
84
+ end
85
+
86
+ it 'should set @errors and @stderr without logging warnings' do
87
+ Backup::Logger.expects(:warn).never
88
+
89
+ pipeline.run
90
+ pipeline.stderr.should == 'stderr output'
91
+ pipeline.errors.count.should be(1)
92
+ pipeline.errors.first.should be_a_kind_of SystemCallError
93
+ pipeline.errors.first.errno.should be(1)
94
+ pipeline.errors.first.message.should match(
95
+ "'second' returned exit code: 1"
96
+ )
97
+ end
98
+ end # context 'when the commands return in sequence'
99
+
100
+ context 'when the commands return out of sequence' do
101
+ before do
102
+ stdout.expects(:read).returns("1|1:2|0:0|0:\n")
103
+ end
104
+
105
+ it 'should properly associate the exitstatus for each command' do
106
+ Backup::Logger.expects(:warn).never
107
+
108
+ pipeline.run
109
+ pipeline.stderr.should == 'stderr output'
110
+ pipeline.errors.count.should be(1)
111
+ pipeline.errors.first.should be_a_kind_of SystemCallError
112
+ pipeline.errors.first.errno.should be(1)
113
+ pipeline.errors.first.message.should match(
114
+ "'second' returned exit code: 1"
115
+ )
116
+ end
117
+ end # context 'when the commands return out of sequence'
118
+
119
+ context 'when multiple commands fail (out of sequence)' do
120
+ before do
121
+ stdout.expects(:read).returns("1|1:2|0:0|3:\n")
122
+ end
123
+
124
+ it 'should properly associate the exitstatus for each command' do
125
+ Backup::Logger.expects(:warn).never
126
+
127
+ pipeline.run
128
+ pipeline.stderr.should == 'stderr output'
129
+ pipeline.errors.count.should be(2)
130
+ pipeline.errors.each {|err| err.should be_a_kind_of SystemCallError }
131
+ pipeline.errors[0].errno.should be(3)
132
+ pipeline.errors[0].message.should match(
133
+ "'first' returned exit code: 3"
134
+ )
135
+ pipeline.errors[1].errno.should be(1)
136
+ pipeline.errors[1].message.should match(
137
+ "'second' returned exit code: 1"
138
+ )
139
+ end
140
+ end # context 'when the commands return (out of sequence)'
141
+
142
+ end # context 'when commands within the pipeline are not successful'
143
+ end # context 'when pipeline command is successfully executed'
144
+
145
+ context 'when pipeline command fails to execute' do
146
+ before do
147
+ Open4.expects(:popen4).with('foo').raises('exec failed')
148
+ end
149
+
150
+ it 'should raise an error' do
151
+ expect do
152
+ pipeline.run
153
+ end.to raise_error(
154
+ Backup::Errors::Pipeline::ExecutionError,
155
+ "Pipeline::ExecutionError: RuntimeError: exec failed"
156
+ )
157
+ end
158
+ end # context 'when pipeline command fails to execute'
159
+
160
+ end # describe '#run'
161
+
162
+ describe '#success?' do
163
+ it 'returns true when @errors is empty' do
164
+ pipeline.success?.should be_true
165
+ end
166
+
167
+ it 'returns false when @errors is not empty' do
168
+ pipeline.instance_variable_set(:@errors, ['foo'])
169
+ pipeline.success?.should be_false
170
+ end
171
+ end # describe '#success?'
172
+
173
+ describe '#error_messages' do
174
+ before do
175
+ pipeline.instance_variable_set(
176
+ :@errors, [
177
+ StandardError.new('standard error'),
178
+ RuntimeError.new('runtime error')
179
+ ]
180
+ )
181
+ end
182
+
183
+ context 'when #stderr_messages has messages' do
184
+ before do
185
+ pipeline.expects(:stderr_messages).returns('stderr messages')
186
+ end
187
+
188
+ it 'should output #stderr_messages and formatted system error messages' do
189
+ pipeline.error_messages.should == 'stderr messages' +
190
+ "The following system errors were returned:\n" +
191
+ "Error: StandardError: standard error\n" +
192
+ "Error: RuntimeError: runtime error"
193
+ end
194
+ end
195
+
196
+ context 'when #stderr_messages has no messages' do
197
+ before do
198
+ pipeline.expects(:stderr_messages).returns(false)
199
+ end
200
+
201
+ it 'should only output the formatted system error messages' do
202
+ pipeline.error_messages.should ==
203
+ "The following system errors were returned:\n" +
204
+ "Error: StandardError: standard error\n" +
205
+ "Error: RuntimeError: runtime error"
206
+ end
207
+ end
208
+ end # describe '#error_messages'
209
+
210
+ describe '#pipeline' do
211
+ context 'when there are multiple system commands to execute' do
212
+ before do
213
+ pipeline.instance_variable_set(:@commands, %w{ one two three })
214
+ end
215
+
216
+ it 'should build a pipeline with redirected/collected exit codes' do
217
+ pipeline.send(:pipeline).should ==
218
+ '{ { one 2>&4 ; echo "0|$?:" >&3 ; } | ' +
219
+ '{ two 2>&4 ; echo "1|$?:" >&3 ; } | ' +
220
+ '{ three 2>&4 ; echo "2|$?:" >&3 ; } } 3>&1 1>&2 4>&2'
221
+ end
222
+ end
223
+
224
+ context 'when there is only one system command to execute' do
225
+ before do
226
+ pipeline.instance_variable_set(:@commands, ['foo'])
227
+ end
228
+
229
+ it 'should build the command line in the same manner, but without pipes' do
230
+ pipeline.send(:pipeline).should ==
231
+ '{ { foo 2>&4 ; echo "0|$?:" >&3 ; } } 3>&1 1>&2 4>&2'
232
+ end
233
+ end
234
+ end # describe '#pipeline'
235
+
236
+ describe '#stderr_message' do
237
+ context 'when @stderr has messages' do
238
+ before do
239
+ pipeline.instance_variable_set(:@stderr, "stderr message\n output")
240
+ end
241
+
242
+ it 'should return a formatted message with the @stderr messages' do
243
+ pipeline.send(:stderr_messages).should ==
244
+ " Pipeline STDERR Messages:\n" +
245
+ " (Note: may be interleaved if multiple commands returned error messages)\n" +
246
+ "\n" +
247
+ " stderr message\n" +
248
+ " output\n"
249
+ end
250
+ end
251
+
252
+ context 'when @stderr is empty' do
253
+ it 'should return false' do
254
+ pipeline.send(:stderr_messages).should be_false
255
+ end
256
+ end
257
+ end # describe '#stderr_message'
258
+
259
+ end #describe 'Backup::Pipeline'
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # Use Bundler
5
+ require 'rubygems' if RUBY_VERSION < '1.9'
6
+ require 'bundler/setup'
7
+
8
+ ##
9
+ # Load Backup
10
+ require 'backup'
11
+
12
+ require 'timecop'
13
+
14
+ module Backup::ExampleHelpers
15
+ # ripped from MiniTest :)
16
+ # RSpec doesn't have a method for this? Am I missing something?
17
+ def capture_io
18
+ require 'stringio'
19
+
20
+ orig_stdout, orig_stderr = $stdout, $stderr
21
+ captured_stdout, captured_stderr = StringIO.new, StringIO.new
22
+ $stdout, $stderr = captured_stdout, captured_stderr
23
+
24
+ yield
25
+
26
+ return captured_stdout.string, captured_stderr.string
27
+ ensure
28
+ $stdout = orig_stdout
29
+ $stderr = orig_stderr
30
+ end
31
+ end
32
+
33
+ require 'rspec/autorun'
34
+ RSpec.configure do |config|
35
+ ##
36
+ # Use Mocha to mock with RSpec
37
+ config.mock_with :mocha
38
+
39
+ ##
40
+ # Example Helpers
41
+ config.include Backup::ExampleHelpers
42
+
43
+ ##
44
+ # Actions to perform before each example
45
+ config.before(:each) do
46
+ FileUtils.collect_method(:noop).each do |method|
47
+ FileUtils.stubs(method).raises("Unexpected call to FileUtils.#{ method }")
48
+ end
49
+
50
+ Open4.stubs(:popen4).raises('Unexpected call to Open4::popen4()')
51
+
52
+ [:message, :error, :warn, :normal, :silent].each do |method|
53
+ Backup::Logger.stubs(method).raises("Unexpected call to Backup::Logger.#{ method }")
54
+ end
55
+ end
56
+ end
57
+
58
+ unless @_put_ruby_version
59
+ puts @_put_ruby_version = "\n\nRuby version: #{ RUBY_DESCRIPTION }\n\n"
60
+ end
@@ -0,0 +1,120 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../spec_helper.rb', __FILE__)
4
+
5
+ describe Backup::Splitter do
6
+ let(:model) { Backup::Model.new(:test_trigger, 'test label') }
7
+ let(:splitter) { Backup::Splitter.new(model, 250) }
8
+ let(:package) { mock }
9
+
10
+ describe '#initialize' do
11
+ it 'should set instance variables' do
12
+ splitter.instance_variable_get(:@model).should be(model)
13
+ splitter.instance_variable_get(:@chunk_size).should be(250)
14
+ end
15
+ end
16
+
17
+ describe '#split_with' do
18
+ it 'should yield the split command, performing before/after methods' do
19
+ s = sequence ''
20
+ given_block = mock
21
+ block = lambda {|arg| given_block.got(arg) }
22
+ splitter.instance_variable_set(:@split_command, 'split command')
23
+
24
+ splitter.expects(:before_packaging).in_sequence(s)
25
+ given_block.expects(:got).in_sequence(s).with('split command')
26
+ splitter.expects(:after_packaging).in_sequence(s)
27
+
28
+ splitter.split_with(&block)
29
+ end
30
+ end
31
+
32
+ # Note: using a 'M' suffix for the byte size is not OSX compatible
33
+ describe '#before_packaging' do
34
+ before do
35
+ model.instance_variable_set(:@package, package)
36
+ splitter.expects(:utility).with(:split).returns('split')
37
+ package.expects(:basename).returns('base_filename')
38
+ end
39
+
40
+ it 'should set @package and @split_command' do
41
+ Backup::Logger.expects(:message).with(
42
+ 'Splitter configured with a chunk size of 250MB.'
43
+ )
44
+ splitter.send(:before_packaging)
45
+
46
+ splitter.instance_variable_get(:@package).should be(package)
47
+
48
+ split_suffix = File.join(Backup::Config.tmp_path, 'base_filename-')
49
+ splitter.instance_variable_get(:@split_command).should ==
50
+ "split -b 250m - '#{ split_suffix }'"
51
+ end
52
+ end
53
+
54
+ describe '#after_packaging' do
55
+ before do
56
+ splitter.instance_variable_set(:@package, package)
57
+ end
58
+
59
+ context 'when splitting occurred during packaging' do
60
+ before do
61
+ splitter.expects(:chunk_suffixes).returns(['aa', 'ab'])
62
+ end
63
+
64
+ it 'should set the chunk_suffixes for the package' do
65
+ package.expects(:chunk_suffixes=).with(['aa', 'ab'])
66
+ splitter.send(:after_packaging)
67
+ end
68
+ end
69
+
70
+ context 'when splitting did not occur during packaging' do
71
+ before do
72
+ splitter.expects(:chunk_suffixes).returns(['aa'])
73
+ package.expects(:basename).twice.returns('base_filename')
74
+ end
75
+
76
+ it 'should remove the suffix from the only package file' do
77
+ package.expects(:chunk_suffixes=).never
78
+ FileUtils.expects(:mv).with(
79
+ File.join(Backup::Config.tmp_path, 'base_filename-aa'),
80
+ File.join(Backup::Config.tmp_path, 'base_filename')
81
+ )
82
+ splitter.send(:after_packaging)
83
+ end
84
+ end
85
+ end # describe '#after_packaging'
86
+
87
+ describe '#chunk_suffixes' do
88
+ before do
89
+ splitter.expects(:chunks).returns(
90
+ ['/path/to/file.tar-aa', '/path/to/file.tar-ab']
91
+ )
92
+ end
93
+
94
+ it 'should return an array of chunk suffixes' do
95
+ splitter.send(:chunk_suffixes).should == ['aa', 'ab']
96
+ end
97
+ end
98
+
99
+ describe '#chunks' do
100
+ before do
101
+ splitter.instance_variable_set(:@package, package)
102
+ package.expects(:basename).returns('base_filename')
103
+ FileUtils.unstub(:touch)
104
+ end
105
+
106
+ it 'should return a sorted array of chunked file paths' do
107
+ Dir.mktmpdir do |dir|
108
+ Backup::Config.expects(:tmp_path).returns(dir)
109
+ FileUtils.touch(File.join(dir, 'base_filename-aa'))
110
+ FileUtils.touch(File.join(dir, 'base_filename-ab'))
111
+
112
+ splitter.send(:chunks).should == [
113
+ File.join(dir, 'base_filename-aa'),
114
+ File.join(dir, 'base_filename-ab')
115
+ ]
116
+ end
117
+ end
118
+ end
119
+
120
+ end
@@ -0,0 +1,166 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../../spec_helper.rb', __FILE__)
4
+
5
+ describe Backup::Storage::Base do
6
+ let(:model) { Backup::Model.new(:test_trigger, 'test label') }
7
+ let(:package) { mock }
8
+ let(:storage) { Backup::Storage::Base.new(model) }
9
+
10
+ it 'should include Configuration::Helpers' do
11
+ Backup::Storage::Base.
12
+ include?(Backup::Configuration::Helpers).should be_true
13
+ end
14
+
15
+ describe '#initialize' do
16
+ after { Backup::Storage::Base.clear_defaults! }
17
+
18
+ it 'should load pre-configured defaults' do
19
+ Backup::Storage::Base.any_instance.expects(:load_defaults!)
20
+ storage
21
+ end
22
+
23
+ it 'should set a reference to the model' do
24
+ storage.instance_variable_get(:@model).should == model
25
+ end
26
+
27
+ it 'should set a reference to the storage_id' do
28
+ storage = Backup::Storage::Base.new(model, 'test_id')
29
+ storage.storage_id.should == 'test_id'
30
+ end
31
+
32
+ it 'should not require the storage_id' do
33
+ storage.instance_variable_defined?(:@storage_id).should be_true
34
+ storage.storage_id.should be_nil
35
+ end
36
+
37
+ context 'when no pre-configured defaults have been set' do
38
+ it 'should set default values' do
39
+ storage.keep.should be_nil
40
+ storage.storage_id.should be_nil
41
+ end
42
+ end # context 'when no pre-configured defaults have been set'
43
+
44
+ context 'when pre-configured defaults have been set' do
45
+ before do
46
+ Backup::Storage::Base.defaults do |s|
47
+ s.keep = 5
48
+ end
49
+ end
50
+
51
+ it 'should use pre-configured defaults' do
52
+ storage.keep.should be(5)
53
+ end
54
+ end # context 'when pre-configured defaults have been set'
55
+ end # describe '#initialize'
56
+
57
+ describe '#perform!' do
58
+ before do
59
+ model.instance_variable_set(:@package, package)
60
+ end
61
+
62
+ it 'should call #transfer!, then #cycle!' do
63
+ s = sequence ''
64
+ storage.expects(:transfer!).in_sequence(s)
65
+ storage.expects(:cycle!).in_sequence(s)
66
+ storage.perform!
67
+ storage.instance_variable_get(:@package).should be(package)
68
+ end
69
+ end
70
+
71
+ describe '#storage_name' do
72
+ context 'when given a storage_id' do
73
+ before { storage.storage_id = 'storage id' }
74
+ it 'should return a log-friendly name with the storage_id' do
75
+ storage.send(:storage_name).should == 'Storage::Base (storage id)'
76
+ end
77
+ end
78
+
79
+ context 'when not given a storage_id' do
80
+ it 'should return a log-friendly name without a storage_id' do
81
+ storage.send(:storage_name).should == 'Storage::Base'
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '#local_path' do
87
+ it 'should return the configured tmp_path' do
88
+ storage.send(:local_path).should == Backup::Config.tmp_path
89
+ end
90
+ end
91
+
92
+ describe '#remote_path_for' do
93
+ before do
94
+ package.expects(:trigger).returns('test_trigger')
95
+ package.expects(:time).returns('backup_time')
96
+ storage.expects(:path).returns('base/remote/path')
97
+ end
98
+
99
+ it 'should return the remote_path for the given package' do
100
+ storage.send(:remote_path_for, package).should ==
101
+ File.join('base/remote/path', 'test_trigger', 'backup_time')
102
+ end
103
+ end
104
+
105
+ describe '#files_to_transfer_for' do
106
+ let(:given_block) { mock }
107
+ before do
108
+ package.stubs(:filenames).returns(
109
+ ['2011.12.31.11.00.02.backup.tar.enc-aa',
110
+ '2011.12.31.11.00.02.backup.tar.enc-ab']
111
+ )
112
+ end
113
+
114
+ it 'should yield the full filename and the filename without the timestamp' do
115
+ given_block.expects(:got).with(
116
+ '2011.12.31.11.00.02.backup.tar.enc-aa', 'backup.tar.enc-aa'
117
+ )
118
+ given_block.expects(:got).with(
119
+ '2011.12.31.11.00.02.backup.tar.enc-ab', 'backup.tar.enc-ab'
120
+ )
121
+ storage.send(:files_to_transfer_for, package) do |local_file, remote_file|
122
+ given_block.got(local_file, remote_file)
123
+ end
124
+ end
125
+
126
+ it 'should have an alias method called #transferred_files_for' do
127
+ storage.method(:transferred_files_for).should ==
128
+ storage.method(:files_to_transfer_for)
129
+ end
130
+ end
131
+
132
+ describe '#cycle!' do
133
+ before do
134
+ storage.stubs(:storage_name).returns('Storage Name')
135
+ storage.instance_variable_set(:@package, package)
136
+ end
137
+
138
+ context 'when keep is set and > 0' do
139
+ before { storage.keep = 1 }
140
+ it 'should cycle' do
141
+ s = sequence ''
142
+ Backup::Logger.expects(:message).in_sequence(s).
143
+ with('Storage Name: Cycling Started...')
144
+ Backup::Storage::Cycler.expects(:cycle!).in_sequence(s).
145
+ with(storage, package)
146
+ Backup::Logger.expects(:message).in_sequence(s).
147
+ with('Storage Name: Cycling Complete!')
148
+
149
+ storage.send(:cycle!)
150
+ end
151
+ end
152
+
153
+ context 'when keep is not set or == 0' do
154
+ it 'should return nil when not set' do
155
+ storage.keep = nil
156
+ storage.send(:cycle!).should be_nil
157
+ end
158
+
159
+ it 'should return nil when == 0' do
160
+ storage.keep = 0
161
+ storage.send(:cycle!).should be_nil
162
+ end
163
+ end
164
+ end
165
+
166
+ end