backup 3.0.20 → 3.0.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/Gemfile +1 -5
  2. data/Gemfile.lock +46 -50
  3. data/README.md +54 -27
  4. data/lib/backup.rb +16 -39
  5. data/lib/backup/archive.rb +42 -18
  6. data/lib/backup/cleaner.rb +110 -25
  7. data/lib/backup/cli/helpers.rb +17 -32
  8. data/lib/backup/cli/utility.rb +46 -107
  9. data/lib/backup/compressor/base.rb +14 -2
  10. data/lib/backup/compressor/bzip2.rb +10 -24
  11. data/lib/backup/compressor/gzip.rb +10 -24
  12. data/lib/backup/compressor/lzma.rb +10 -23
  13. data/lib/backup/compressor/pbzip2.rb +12 -32
  14. data/lib/backup/config.rb +171 -0
  15. data/lib/backup/configuration/compressor/base.rb +1 -2
  16. data/lib/backup/configuration/compressor/pbzip2.rb +4 -4
  17. data/lib/backup/configuration/database/base.rb +2 -1
  18. data/lib/backup/configuration/database/mongodb.rb +8 -0
  19. data/lib/backup/configuration/database/mysql.rb +4 -0
  20. data/lib/backup/configuration/database/postgresql.rb +4 -0
  21. data/lib/backup/configuration/database/redis.rb +4 -0
  22. data/lib/backup/configuration/database/riak.rb +5 -1
  23. data/lib/backup/configuration/encryptor/base.rb +1 -2
  24. data/lib/backup/configuration/encryptor/open_ssl.rb +1 -1
  25. data/lib/backup/configuration/helpers.rb +7 -2
  26. data/lib/backup/configuration/notifier/base.rb +4 -28
  27. data/lib/backup/configuration/storage/base.rb +1 -1
  28. data/lib/backup/configuration/storage/dropbox.rb +14 -4
  29. data/lib/backup/configuration/syncer/base.rb +10 -0
  30. data/lib/backup/configuration/syncer/rsync/base.rb +28 -0
  31. data/lib/backup/configuration/syncer/rsync/local.rb +11 -0
  32. data/lib/backup/configuration/syncer/rsync/pull.rb +11 -0
  33. data/lib/backup/configuration/syncer/rsync/push.rb +31 -0
  34. data/lib/backup/configuration/syncer/s3.rb +0 -4
  35. data/lib/backup/database/base.rb +25 -7
  36. data/lib/backup/database/mongodb.rb +112 -75
  37. data/lib/backup/database/mysql.rb +54 -29
  38. data/lib/backup/database/postgresql.rb +60 -42
  39. data/lib/backup/database/redis.rb +61 -39
  40. data/lib/backup/database/riak.rb +35 -11
  41. data/lib/backup/dependency.rb +4 -5
  42. data/lib/backup/encryptor/base.rb +13 -1
  43. data/lib/backup/encryptor/gpg.rb +39 -39
  44. data/lib/backup/encryptor/open_ssl.rb +28 -38
  45. data/lib/backup/logger.rb +20 -11
  46. data/lib/backup/model.rb +206 -163
  47. data/lib/backup/notifier/base.rb +27 -25
  48. data/lib/backup/notifier/campfire.rb +7 -13
  49. data/lib/backup/notifier/hipchat.rb +28 -28
  50. data/lib/backup/notifier/mail.rb +24 -26
  51. data/lib/backup/notifier/presently.rb +10 -18
  52. data/lib/backup/notifier/prowl.rb +9 -17
  53. data/lib/backup/notifier/twitter.rb +11 -18
  54. data/lib/backup/package.rb +47 -0
  55. data/lib/backup/packager.rb +81 -16
  56. data/lib/backup/splitter.rb +48 -35
  57. data/lib/backup/storage/base.rb +44 -172
  58. data/lib/backup/storage/cloudfiles.rb +31 -46
  59. data/lib/backup/storage/cycler.rb +117 -0
  60. data/lib/backup/storage/dropbox.rb +92 -76
  61. data/lib/backup/storage/ftp.rb +30 -40
  62. data/lib/backup/storage/local.rb +44 -45
  63. data/lib/backup/storage/ninefold.rb +55 -49
  64. data/lib/backup/storage/rsync.rb +49 -56
  65. data/lib/backup/storage/s3.rb +33 -44
  66. data/lib/backup/storage/scp.rb +21 -48
  67. data/lib/backup/storage/sftp.rb +26 -40
  68. data/lib/backup/syncer/base.rb +7 -0
  69. data/lib/backup/syncer/rsync/base.rb +78 -0
  70. data/lib/backup/syncer/rsync/local.rb +53 -0
  71. data/lib/backup/syncer/rsync/pull.rb +38 -0
  72. data/lib/backup/syncer/rsync/push.rb +113 -0
  73. data/lib/backup/syncer/s3.rb +42 -32
  74. data/lib/backup/version.rb +1 -1
  75. data/spec/archive_spec.rb +235 -69
  76. data/spec/cleaner_spec.rb +304 -0
  77. data/spec/cli/helpers_spec.rb +142 -1
  78. data/spec/cli/utility_spec.rb +338 -13
  79. data/spec/compressor/base_spec.rb +31 -0
  80. data/spec/compressor/bzip2_spec.rb +60 -35
  81. data/spec/compressor/gzip_spec.rb +60 -35
  82. data/spec/compressor/lzma_spec.rb +60 -35
  83. data/spec/compressor/pbzip2_spec.rb +98 -37
  84. data/spec/config_spec.rb +321 -0
  85. data/spec/configuration/base_spec.rb +4 -4
  86. data/spec/configuration/compressor/bzip2_spec.rb +1 -0
  87. data/spec/configuration/compressor/gzip_spec.rb +1 -0
  88. data/spec/configuration/compressor/lzma_spec.rb +1 -0
  89. data/spec/configuration/compressor/pbzip2_spec.rb +32 -0
  90. data/spec/configuration/database/base_spec.rb +2 -1
  91. data/spec/configuration/database/mongodb_spec.rb +26 -16
  92. data/spec/configuration/database/mysql_spec.rb +4 -0
  93. data/spec/configuration/database/postgresql_spec.rb +4 -0
  94. data/spec/configuration/database/redis_spec.rb +4 -0
  95. data/spec/configuration/database/riak_spec.rb +4 -0
  96. data/spec/configuration/encryptor/gpg_spec.rb +1 -0
  97. data/spec/configuration/encryptor/open_ssl_spec.rb +1 -0
  98. data/spec/configuration/notifier/base_spec.rb +32 -0
  99. data/spec/configuration/notifier/campfire_spec.rb +1 -0
  100. data/spec/configuration/notifier/hipchat_spec.rb +1 -0
  101. data/spec/configuration/notifier/mail_spec.rb +1 -0
  102. data/spec/configuration/notifier/presently_spec.rb +1 -0
  103. data/spec/configuration/notifier/prowl_spec.rb +1 -0
  104. data/spec/configuration/notifier/twitter_spec.rb +1 -0
  105. data/spec/configuration/storage/cloudfiles_spec.rb +1 -0
  106. data/spec/configuration/storage/dropbox_spec.rb +4 -3
  107. data/spec/configuration/storage/ftp_spec.rb +1 -0
  108. data/spec/configuration/storage/local_spec.rb +1 -0
  109. data/spec/configuration/storage/ninefold_spec.rb +1 -0
  110. data/spec/configuration/storage/rsync_spec.rb +3 -1
  111. data/spec/configuration/storage/s3_spec.rb +1 -0
  112. data/spec/configuration/storage/scp_spec.rb +1 -0
  113. data/spec/configuration/storage/sftp_spec.rb +1 -0
  114. data/spec/configuration/syncer/rsync/base_spec.rb +33 -0
  115. data/spec/configuration/syncer/rsync/local_spec.rb +10 -0
  116. data/spec/configuration/syncer/rsync/pull_spec.rb +10 -0
  117. data/spec/configuration/syncer/{rsync_spec.rb → rsync/push_spec.rb} +12 -15
  118. data/spec/configuration/syncer/s3_spec.rb +2 -3
  119. data/spec/database/base_spec.rb +35 -20
  120. data/spec/database/mongodb_spec.rb +298 -119
  121. data/spec/database/mysql_spec.rb +147 -72
  122. data/spec/database/postgresql_spec.rb +155 -100
  123. data/spec/database/redis_spec.rb +200 -97
  124. data/spec/database/riak_spec.rb +82 -24
  125. data/spec/dependency_spec.rb +49 -0
  126. data/spec/encryptor/base_spec.rb +30 -0
  127. data/spec/encryptor/gpg_spec.rb +105 -28
  128. data/spec/encryptor/open_ssl_spec.rb +85 -114
  129. data/spec/logger_spec.rb +74 -8
  130. data/spec/model_spec.rb +528 -220
  131. data/spec/notifier/base_spec.rb +89 -0
  132. data/spec/notifier/campfire_spec.rb +147 -119
  133. data/spec/notifier/hipchat_spec.rb +140 -145
  134. data/spec/notifier/mail_spec.rb +190 -248
  135. data/spec/notifier/presently_spec.rb +147 -282
  136. data/spec/notifier/prowl_spec.rb +79 -111
  137. data/spec/notifier/twitter_spec.rb +87 -106
  138. data/spec/package_spec.rb +61 -0
  139. data/spec/packager_spec.rb +154 -0
  140. data/spec/spec_helper.rb +36 -13
  141. data/spec/splitter_spec.rb +90 -41
  142. data/spec/storage/base_spec.rb +95 -239
  143. data/spec/storage/cloudfiles_spec.rb +185 -75
  144. data/spec/storage/cycler_spec.rb +239 -0
  145. data/spec/storage/dropbox_spec.rb +318 -87
  146. data/spec/storage/ftp_spec.rb +165 -152
  147. data/spec/storage/local_spec.rb +206 -54
  148. data/spec/storage/ninefold_spec.rb +264 -128
  149. data/spec/storage/rsync_spec.rb +244 -163
  150. data/spec/storage/s3_spec.rb +175 -64
  151. data/spec/storage/scp_spec.rb +156 -150
  152. data/spec/storage/sftp_spec.rb +153 -135
  153. data/spec/syncer/base_spec.rb +22 -0
  154. data/spec/syncer/rsync/base_spec.rb +118 -0
  155. data/spec/syncer/rsync/local_spec.rb +121 -0
  156. data/spec/syncer/rsync/pull_spec.rb +90 -0
  157. data/spec/syncer/rsync/push_spec.rb +327 -0
  158. data/spec/syncer/s3_spec.rb +180 -91
  159. data/templates/cli/utility/config +1 -1
  160. data/templates/cli/utility/database/mongodb +4 -0
  161. data/templates/cli/utility/database/mysql +3 -0
  162. data/templates/cli/utility/database/postgresql +3 -0
  163. data/templates/cli/utility/database/redis +3 -0
  164. data/templates/cli/utility/database/riak +3 -0
  165. data/templates/cli/utility/storage/dropbox +4 -1
  166. data/templates/cli/utility/syncer/rsync_local +12 -0
  167. data/templates/cli/utility/syncer/{rsync → rsync_pull} +2 -2
  168. data/templates/cli/utility/syncer/rsync_push +17 -0
  169. data/templates/storage/dropbox/authorization_url.erb +1 -1
  170. metadata +42 -17
  171. data/lib/backup/configuration/syncer/rsync.rb +0 -45
  172. data/lib/backup/finder.rb +0 -87
  173. data/lib/backup/storage/object.rb +0 -47
  174. data/lib/backup/syncer/rsync.rb +0 -152
  175. data/spec/backup_spec.rb +0 -11
  176. data/spec/finder_spec.rb +0 -91
  177. data/spec/storage/object_spec.rb +0 -74
  178. data/spec/syncer/rsync_spec.rb +0 -195
@@ -5,54 +5,79 @@ require File.expand_path('../../spec_helper.rb', __FILE__)
5
5
  describe Backup::Compressor::Gzip do
6
6
  let(:compressor) { Backup::Compressor::Gzip.new }
7
7
 
8
- before do
9
- Backup::Model.extension = 'tar'
10
- end
8
+ describe 'setting configuration defaults' do
9
+ after { Backup::Configuration::Compressor::Gzip.clear_defaults! }
11
10
 
12
- describe 'the options' do
13
- it do
14
- compressor.send(:best).should == []
15
- end
11
+ it 'uses and overrides configuration defaults' do
12
+ Backup::Configuration::Compressor::Gzip.best.should be_false
13
+ Backup::Configuration::Compressor::Gzip.fast.should be_false
14
+
15
+ compressor = Backup::Compressor::Gzip.new
16
+ compressor.best.should be_false
17
+ compressor.fast.should be_false
18
+
19
+ Backup::Configuration::Compressor::Gzip.defaults do |c|
20
+ c.best = true
21
+ c.fast = true
22
+ end
23
+ Backup::Configuration::Compressor::Gzip.best.should be_true
24
+ Backup::Configuration::Compressor::Gzip.fast.should be_true
16
25
 
17
- it do
18
- compressor.send(:fast).should == []
26
+ compressor = Backup::Compressor::Gzip.new
27
+ compressor.best.should be_true
28
+ compressor.fast.should be_true
29
+
30
+ compressor = Backup::Compressor::Gzip.new do |c|
31
+ c.best = false
32
+ end
33
+ compressor.best.should be_false
34
+ compressor.fast.should be_true
35
+
36
+ compressor = Backup::Compressor::Gzip.new do |c|
37
+ c.fast = false
38
+ end
39
+ compressor.best.should be_true
40
+ compressor.fast.should be_false
19
41
  end
20
- end
42
+ end # describe 'setting configuration defaults'
21
43
 
22
- describe '#perform!' do
44
+ describe '#compress_with' do
23
45
  before do
24
- [:run, :utility].each { |method| compressor.stubs(method) }
46
+ compressor.expects(:log!)
47
+ compressor.expects(:utility).with(:gzip).returns('gzip')
25
48
  end
26
49
 
27
- it 'should perform the compression' do
28
- compressor.expects(:utility).with(:gzip).returns(:gzip)
29
- compressor.expects(:run).with("gzip '#{ File.join(Backup::TMP_PATH, "#{ Backup::TIME }.#{ Backup::TRIGGER }.tar") }'")
30
- compressor.perform!
50
+ it 'should yield with the --best option' do
51
+ compressor.best = true
52
+ compressor.compress_with do |cmd, ext|
53
+ cmd.should == 'gzip --best'
54
+ ext.should == '.gz'
55
+ end
31
56
  end
32
57
 
33
- it 'should perform the compression with the --best and --fast options' do
34
- compressor = Backup::Compressor::Gzip.new do |c|
35
- c.best = true
36
- c.fast = true
58
+ it 'should yield with the --fast option' do
59
+ compressor.fast = true
60
+ compressor.compress_with do |cmd, ext|
61
+ cmd.should == 'gzip --fast'
62
+ ext.should == '.gz'
37
63
  end
38
-
39
- compressor.stubs(:utility).returns(:gzip)
40
- compressor.expects(:run).with("gzip --best --fast '#{ File.join(Backup::TMP_PATH, "#{ Backup::TIME }.#{ Backup::TRIGGER }.tar") }'")
41
- compressor.perform!
42
64
  end
43
65
 
44
- it 'should set the class variable @extension (Backup::Model.extension) to .gz' do
45
- compressor.stubs(:utility).returns(:gzip)
46
- compressor.expects(:run)
47
-
48
- Backup::Model.extension.should == 'tar'
49
- compressor.perform!
50
- Backup::Model.extension.should == 'tar.gz'
66
+ it 'should yield with the --best and --fast options' do
67
+ compressor.best = true
68
+ compressor.fast = true
69
+ compressor.compress_with do |cmd, ext|
70
+ cmd.should == 'gzip --best --fast'
71
+ ext.should == '.gz'
72
+ end
51
73
  end
52
74
 
53
- it 'should log' do
54
- Backup::Logger.expects(:message).with("Backup::Compressor::Gzip started compressing the archive.")
55
- compressor.perform!
75
+ it 'should yield with no options' do
76
+ compressor.compress_with do |cmd, ext|
77
+ cmd.should == 'gzip'
78
+ ext.should == '.gz'
79
+ end
56
80
  end
57
- end
81
+ end # describe '#compress_with'
82
+
58
83
  end
@@ -5,54 +5,79 @@ require File.expand_path('../../spec_helper.rb', __FILE__)
5
5
  describe Backup::Compressor::Lzma do
6
6
  let(:compressor) { Backup::Compressor::Lzma.new }
7
7
 
8
- before do
9
- Backup::Model.extension = 'tar'
10
- end
8
+ describe 'setting configuration defaults' do
9
+ after { Backup::Configuration::Compressor::Lzma.clear_defaults! }
11
10
 
12
- describe 'the options' do
13
- it do
14
- compressor.send(:best).should == []
15
- end
11
+ it 'uses and overrides configuration defaults' do
12
+ Backup::Configuration::Compressor::Lzma.best.should be_false
13
+ Backup::Configuration::Compressor::Lzma.fast.should be_false
14
+
15
+ compressor = Backup::Compressor::Lzma.new
16
+ compressor.best.should be_false
17
+ compressor.fast.should be_false
18
+
19
+ Backup::Configuration::Compressor::Lzma.defaults do |c|
20
+ c.best = true
21
+ c.fast = true
22
+ end
23
+ Backup::Configuration::Compressor::Lzma.best.should be_true
24
+ Backup::Configuration::Compressor::Lzma.fast.should be_true
16
25
 
17
- it do
18
- compressor.send(:fast).should == []
26
+ compressor = Backup::Compressor::Lzma.new
27
+ compressor.best.should be_true
28
+ compressor.fast.should be_true
29
+
30
+ compressor = Backup::Compressor::Lzma.new do |c|
31
+ c.best = false
32
+ end
33
+ compressor.best.should be_false
34
+ compressor.fast.should be_true
35
+
36
+ compressor = Backup::Compressor::Lzma.new do |c|
37
+ c.fast = false
38
+ end
39
+ compressor.best.should be_true
40
+ compressor.fast.should be_false
19
41
  end
20
- end
42
+ end # describe 'setting configuration defaults'
21
43
 
22
- describe '#perform!' do
44
+ describe '#compress_with' do
23
45
  before do
24
- [:run, :utility].each { |method| compressor.stubs(method) }
46
+ compressor.expects(:log!)
47
+ compressor.expects(:utility).with(:lzma).returns('lzma')
25
48
  end
26
49
 
27
- it 'should perform the compression' do
28
- compressor.expects(:utility).with(:lzma).returns(:lzma)
29
- compressor.expects(:run).with("lzma '#{ File.join(Backup::TMP_PATH, "#{ Backup::TIME }.#{ Backup::TRIGGER }.tar") }'")
30
- compressor.perform!
50
+ it 'should yield with the --best option' do
51
+ compressor.best = true
52
+ compressor.compress_with do |cmd, ext|
53
+ cmd.should == 'lzma --best'
54
+ ext.should == '.lzma'
55
+ end
31
56
  end
32
57
 
33
- it 'should perform the compression with the --best and --fast options' do
34
- compressor = Backup::Compressor::Lzma.new do |c|
35
- c.best = true
36
- c.fast = true
58
+ it 'should yield with the --fast option' do
59
+ compressor.fast = true
60
+ compressor.compress_with do |cmd, ext|
61
+ cmd.should == 'lzma --fast'
62
+ ext.should == '.lzma'
37
63
  end
38
-
39
- compressor.stubs(:utility).returns(:lzma)
40
- compressor.expects(:run).with("lzma --best --fast '#{ File.join(Backup::TMP_PATH, "#{ Backup::TIME }.#{ Backup::TRIGGER }.tar") }'")
41
- compressor.perform!
42
64
  end
43
65
 
44
- it 'should set the class variable @extension (Backup::Model.extension) to .lzma' do
45
- compressor.stubs(:utility).returns(:lzma)
46
- compressor.expects(:run)
47
-
48
- Backup::Model.extension.should == 'tar'
49
- compressor.perform!
50
- Backup::Model.extension.should == 'tar.lzma'
66
+ it 'should yield with the --best and --fast options' do
67
+ compressor.best = true
68
+ compressor.fast = true
69
+ compressor.compress_with do |cmd, ext|
70
+ cmd.should == 'lzma --best --fast'
71
+ ext.should == '.lzma'
72
+ end
51
73
  end
52
74
 
53
- it 'should log' do
54
- Backup::Logger.expects(:message).with("Backup::Compressor::Lzma started compressing the archive.")
55
- compressor.perform!
75
+ it 'should yield with no options' do
76
+ compressor.compress_with do |cmd, ext|
77
+ cmd.should == 'lzma'
78
+ ext.should == '.lzma'
79
+ end
56
80
  end
57
- end
81
+ end # describe '#compress_with'
82
+
58
83
  end
@@ -5,59 +5,120 @@ require File.expand_path('../../spec_helper.rb', __FILE__)
5
5
  describe Backup::Compressor::Pbzip2 do
6
6
  let(:compressor) { Backup::Compressor::Pbzip2.new }
7
7
 
8
- before do
9
- Backup::Model.extension = 'tar'
10
- end
8
+ describe 'setting configuration defaults' do
9
+ after { Backup::Configuration::Compressor::Pbzip2.clear_defaults! }
11
10
 
12
- describe 'the options' do
13
- it do
14
- compressor.send(:best).should == []
15
- end
11
+ it 'uses and overrides configuration defaults' do
12
+ Backup::Configuration::Compressor::Pbzip2.best.should be_false
13
+ Backup::Configuration::Compressor::Pbzip2.fast.should be_false
14
+ Backup::Configuration::Compressor::Pbzip2.processors.should be_false
16
15
 
17
- it do
18
- compressor.send(:fast).should == []
19
- end
16
+ compressor = Backup::Compressor::Pbzip2.new
17
+ compressor.best.should be_false
18
+ compressor.fast.should be_false
19
+ compressor.processors.should be_false
20
+
21
+ Backup::Configuration::Compressor::Pbzip2.defaults do |c|
22
+ c.best = true
23
+ c.fast = true
24
+ c.processors = 2
25
+ end
26
+ Backup::Configuration::Compressor::Pbzip2.best.should be_true
27
+ Backup::Configuration::Compressor::Pbzip2.fast.should be_true
28
+ Backup::Configuration::Compressor::Pbzip2.processors.should == 2
20
29
 
21
- it do
22
- compressor.send(:processors).should == []
30
+ compressor = Backup::Compressor::Pbzip2.new
31
+ compressor.best.should be_true
32
+ compressor.fast.should be_true
33
+ compressor.processors.should == 2
34
+
35
+ compressor = Backup::Compressor::Pbzip2.new do |c|
36
+ c.best = false
37
+ end
38
+ compressor.best.should be_false
39
+ compressor.fast.should be_true
40
+ compressor.processors.should == 2
41
+
42
+ compressor = Backup::Compressor::Pbzip2.new do |c|
43
+ c.fast = false
44
+ end
45
+ compressor.best.should be_true
46
+ compressor.fast.should be_false
47
+ compressor.processors.should == 2
48
+
49
+ compressor = Backup::Compressor::Pbzip2.new do |c|
50
+ c.processors = false
51
+ end
52
+ compressor.best.should be_true
53
+ compressor.fast.should be_true
54
+ compressor.processors.should be_false
23
55
  end
24
- end
56
+ end # describe 'setting configuration defaults'
25
57
 
26
- describe '#perform!' do
58
+ describe '#compress_with' do
27
59
  before do
28
- [:run, :utility].each { |method| compressor.stubs(method) }
60
+ compressor.expects(:log!)
61
+ compressor.expects(:utility).with(:pbzip2).returns('pbzip2')
29
62
  end
30
63
 
31
- it 'should perform the compression' do
32
- compressor.expects(:utility).with(:pbzip2).returns(:pbzip2)
33
- compressor.expects(:run).with("pbzip2 '#{ File.join(Backup::TMP_PATH, "#{ Backup::TIME }.#{ Backup::TRIGGER }.tar") }'")
34
- compressor.perform!
64
+ it 'should yield with the --best option' do
65
+ compressor.best = true
66
+ compressor.compress_with do |cmd, ext|
67
+ cmd.should == 'pbzip2 --best'
68
+ ext.should == '.bz2'
69
+ end
35
70
  end
36
71
 
37
- it 'should perform the compression with the --best, --fast and -p1 options' do
38
- compressor = Backup::Compressor::Pbzip2.new do |c|
39
- c.best = true
40
- c.fast = true
41
- c.processors = 1
72
+ it 'should yield with the --fast option' do
73
+ compressor.fast = true
74
+ compressor.compress_with do |cmd, ext|
75
+ cmd.should == 'pbzip2 --fast'
76
+ ext.should == '.bz2'
42
77
  end
78
+ end
43
79
 
44
- compressor.stubs(:utility).returns(:pbzip2)
45
- compressor.expects(:run).with("pbzip2 --best --fast -p1 '#{ File.join(Backup::TMP_PATH, "#{ Backup::TIME }.#{ Backup::TRIGGER }.tar") }'")
46
- compressor.perform!
80
+ it 'should yield with the -p option' do
81
+ compressor.processors = 2
82
+ compressor.compress_with do |cmd, ext|
83
+ cmd.should == 'pbzip2 -p2'
84
+ ext.should == '.bz2'
85
+ end
47
86
  end
48
87
 
49
- it 'should set the class variable @extension (Backup::Model.extension) to .bz2' do
50
- compressor.stubs(:utility).returns(:pbzip2)
51
- compressor.expects(:run)
88
+ it 'should yield with the --best and --fast options' do
89
+ compressor.best = true
90
+ compressor.fast = true
91
+ compressor.compress_with do |cmd, ext|
92
+ cmd.should == 'pbzip2 --best --fast'
93
+ ext.should == '.bz2'
94
+ end
95
+ end
52
96
 
53
- Backup::Model.extension.should == 'tar'
54
- compressor.perform!
55
- Backup::Model.extension.should == 'tar.bz2'
97
+ it 'should yield with the --best and -p options' do
98
+ compressor.best = true
99
+ compressor.processors = 2
100
+ compressor.compress_with do |cmd, ext|
101
+ cmd.should == 'pbzip2 --best -p2'
102
+ ext.should == '.bz2'
103
+ end
56
104
  end
57
105
 
58
- it 'should log' do
59
- Backup::Logger.expects(:message).with("Backup::Compressor::Pbzip2 started compressing the archive.")
60
- compressor.perform!
106
+ it 'should yield with the --fast and -p options' do
107
+ compressor.fast = true
108
+ compressor.processors = 2
109
+ compressor.compress_with do |cmd, ext|
110
+ cmd.should == 'pbzip2 --fast -p2'
111
+ ext.should == '.bz2'
112
+ end
61
113
  end
62
- end
114
+
115
+ it 'should yield with no options' do
116
+ compressor.compress_with do |cmd, ext|
117
+ cmd.should == 'pbzip2'
118
+ ext.should == '.bz2'
119
+ end
120
+ end
121
+
122
+ end # describe '#compress_with'
123
+
63
124
  end
@@ -0,0 +1,321 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../spec_helper.rb', __FILE__)
4
+
5
+ describe 'Backup::Config' do
6
+ let(:config) { Backup::Config }
7
+ before(:all) { config.send(:reset!) }
8
+ after(:each) do
9
+ config.unstub(:update)
10
+ config.unstub(:set_root_path)
11
+ config.unstub(:set_path_variable)
12
+ config.send(:reset!)
13
+ end
14
+
15
+ describe '#update' do
16
+ let(:default_root_path) { config.root_path }
17
+
18
+ context 'when a root_path is given' do
19
+ it 'should use #set_root_path to set the new root_path' do
20
+ config.expects(:set_root_path).with('a/path')
21
+
22
+ config.update(:root_path => 'a/path')
23
+ end
24
+
25
+ it 'should set all paths using the new root_path' do
26
+ config.expects(:set_root_path).with('path').returns('/root/path')
27
+ Backup::Config::DEFAULTS.each do |key, val|
28
+ config.expects(:set_path_variable).with(key, nil, val, '/root/path')
29
+ end
30
+
31
+ config.update(:root_path => 'path')
32
+ end
33
+ end # context 'when a root_path is given'
34
+
35
+ context 'when a root_path is not given' do
36
+ it 'should set all paths without using a root_path' do
37
+ Backup::Config::DEFAULTS.each do |key, val|
38
+ config.expects(:set_path_variable).with(key, nil, val, false)
39
+ end
40
+
41
+ config.update
42
+ end
43
+ end # context 'when a root_path is not given'
44
+
45
+ end # describe '#update'
46
+
47
+ describe '#load_config!' do
48
+ context 'when @config_file exists' do
49
+ before do
50
+ File.expects(:exist?).with(config.config_file).returns(true)
51
+ end
52
+
53
+ it 'should load the config file' do
54
+ File.expects(:read).with(config.config_file).returns(:file_contents)
55
+ config.expects(:module_eval).with(:file_contents, config.config_file)
56
+
57
+ expect do
58
+ config.load_config!
59
+ end.not_to raise_error
60
+ end
61
+ end
62
+
63
+ context 'when @config_file does not exist' do
64
+ before do
65
+ File.expects(:exist?).returns(false)
66
+ end
67
+
68
+ it 'should raise an error' do
69
+ File.expects(:read).never
70
+ config.expects(:module_eval).never
71
+
72
+ expect do
73
+ config.load_config!
74
+ end.to raise_error {|err|
75
+ err.should be_an_instance_of Backup::Errors::Config::NotFoundError
76
+ err.message.should match(
77
+ /Could not find configuration file: '#{config.config_file}'/
78
+ )
79
+ }
80
+ end
81
+ end
82
+ end # describe '#load_config!'
83
+
84
+ describe '#set_root_path' do
85
+
86
+ context 'when the given path == @root_path' do
87
+ it 'should return @root_path without requiring the path to exist' do
88
+ File.expects(:directory?).never
89
+ expect do
90
+ config.send(:set_root_path, config.root_path).
91
+ should == config.root_path
92
+ end.not_to raise_error
93
+ end
94
+ end
95
+
96
+ context 'when the given path exists' do
97
+ it 'should set and return the @root_path' do
98
+ expect do
99
+ config.send(:set_root_path, Dir.pwd).should == Dir.pwd
100
+ end.not_to raise_error
101
+ config.root_path.should == Dir.pwd
102
+ end
103
+
104
+ it 'should expand relative paths' do
105
+ expect do
106
+ config.send(:set_root_path, '').should == Dir.pwd
107
+ end.not_to raise_error
108
+ config.root_path.should == Dir.pwd
109
+ end
110
+ end
111
+
112
+ context 'when the given path does not exist' do
113
+ it 'should raise an error' do
114
+ path = File.expand_path('foo')
115
+ expect do
116
+ config.send(:set_root_path, 'foo')
117
+ end.to raise_error {|err|
118
+ err.should be_an_instance_of Backup::Errors::Config::NotFoundError
119
+ err.message.should match(/Root Path Not Found/)
120
+ err.message.should match(/Path was: #{ path }/)
121
+ }
122
+ end
123
+ end
124
+
125
+ end # describe '#set_root_path'
126
+
127
+ describe '#set_path_variable' do
128
+ after do
129
+ if config.instance_variable_defined?(:@var)
130
+ config.send(:remove_instance_variable, :@var)
131
+ end
132
+ end
133
+
134
+ context 'when a path is given' do
135
+ context 'when the given path is an absolute path' do
136
+ it 'should always use the given path' do
137
+ path = File.expand_path('foo')
138
+
139
+ config.send(:set_path_variable, 'var', path, 'none', '/root/path')
140
+ config.instance_variable_get(:@var).should == path
141
+
142
+ config.send(:set_path_variable, 'var', path, 'none', nil)
143
+ config.instance_variable_get(:@var).should == path
144
+ end
145
+ end
146
+
147
+ context 'when the given path is a relative path' do
148
+ context 'when a root_path is given' do
149
+ it 'should append the path to the root_path' do
150
+ config.send(:set_path_variable, 'var', 'foo', 'none', '/root/path')
151
+ config.instance_variable_get(:@var).should == '/root/path/foo'
152
+ end
153
+ end
154
+ context 'when a root_path is not given' do
155
+ it 'should expand the path' do
156
+ path = File.expand_path('foo')
157
+
158
+ config.send(:set_path_variable, 'var', 'foo', 'none', false)
159
+ config.instance_variable_get(:@var).should == path
160
+ end
161
+ end
162
+ end
163
+ end # context 'when a path is given'
164
+
165
+ context 'when no path is given' do
166
+ context 'when a root_path is given' do
167
+ it 'should use the root_path with the given ending' do
168
+ config.send(:set_path_variable, 'var', nil, 'ending', '/root/path')
169
+ config.instance_variable_get(:@var).should == '/root/path/ending'
170
+ end
171
+ end
172
+ context 'when a root_path is not given' do
173
+ it 'should do nothing' do
174
+ config.send(:set_path_variable, 'var', nil, 'ending', false)
175
+ config.instance_variable_defined?(:@var).should be_false
176
+ end
177
+ end
178
+ end # context 'when no path is given'
179
+
180
+ end # describe '#set_path_variable'
181
+
182
+ describe '#reset!' do
183
+ before do
184
+ @env_user = ENV['USER']
185
+ @env_home = ENV['HOME']
186
+ end
187
+
188
+ after do
189
+ ENV['USER'] = @env_user
190
+ ENV['HOME'] = @env_home
191
+ end
192
+
193
+ it 'should be called to set variables when module is loaded' do
194
+ # just to avoid 'already initialized constant' warnings
195
+ config.constants.each {|const| config.send(:remove_const, const) }
196
+
197
+ expected = config.instance_variables.sort.map(&:to_sym) - [:@mocha]
198
+ config.instance_variables.each do |var|
199
+ config.send(:remove_instance_variable, var)
200
+ end
201
+ config.instance_variables.should be_empty
202
+
203
+ load File.expand_path('../../lib/backup/config.rb', __FILE__)
204
+ config.instance_variables.sort.map(&:to_sym).should == expected
205
+ end
206
+
207
+ context 'when setting @user' do
208
+ context 'when ENV["USER"] is set' do
209
+ before { ENV['USER'] = 'test' }
210
+
211
+ it 'should set value for @user to ENV["USER"]' do
212
+ config.send(:reset!)
213
+ config.user.should == 'test'
214
+ end
215
+ end
216
+
217
+ context 'when ENV["USER"] is not set' do
218
+ before { ENV.delete('USER') }
219
+
220
+ it 'should set value using the user login name' do
221
+ config.send(:reset!)
222
+ config.user.should == Etc.getpwuid.name
223
+ end
224
+ end
225
+ end # context 'when setting @user'
226
+
227
+ context 'when setting @root_path' do
228
+ context 'when ENV["HOME"] is set' do
229
+ before { ENV['HOME'] = 'test/home/dir' }
230
+
231
+ it 'should set value using ENV["HOME"]' do
232
+ config.send(:reset!)
233
+ config.root_path.should ==
234
+ File.join(File.expand_path('test/home/dir'),'Backup')
235
+ end
236
+ end
237
+
238
+ context 'when ENV["HOME"] is not set' do
239
+ before { ENV.delete('HOME') }
240
+
241
+ it 'should set value using $PWD' do
242
+ config.send(:reset!)
243
+ config.root_path.should == File.expand_path('Backup')
244
+ end
245
+ end
246
+ end # context 'when setting @root_path'
247
+
248
+ context 'when setting other path variables' do
249
+ before { ENV['HOME'] = 'test/home/dir' }
250
+
251
+ it 'should use #update' do
252
+ config.expects(:update).with(
253
+ :root_path => File.join(File.expand_path('test/home/dir'),'Backup')
254
+ )
255
+ config.send(:reset!)
256
+ end
257
+ end
258
+
259
+ end # describe '#reset!'
260
+
261
+ describe '#add_dsl_constants!' do
262
+ it 'should be called when the module is loaded' do
263
+ config.constants.each {|const| config.send(:remove_const, const) }
264
+ config.constants.should be_empty
265
+
266
+ load File.expand_path('../../lib/backup/config.rb', __FILE__)
267
+
268
+ Backup::Config.const_defined?('MySQL').should be_true
269
+ Backup::Config.const_defined?('RSync').should be_true
270
+ Backup::Config::RSync.const_defined?('Local').should be_true
271
+ end
272
+ end # describe '#add_dsl_constants!'
273
+
274
+ describe '#create_modules' do
275
+ module TestScope; end
276
+
277
+ context 'when given an array of constant names' do
278
+ it 'should create modules for the given scope' do
279
+ config.send(:create_modules, TestScope, ['Foo', 'Bar'])
280
+ TestScope.const_defined?('Foo').should be_true
281
+ TestScope.const_defined?('Bar').should be_true
282
+ TestScope::Foo.class.should == Module
283
+ TestScope::Bar.class.should == Module
284
+ end
285
+ end
286
+
287
+ context 'when the given array contains Hash values' do
288
+ it 'should create deeply nested modules' do
289
+ config.send(
290
+ :create_modules,
291
+ TestScope,
292
+ [ 'FooBar', {
293
+ :LevelA => [ 'NameA', {
294
+ :LevelB => ['NameB']
295
+ } ]
296
+ } ]
297
+ )
298
+ TestScope.const_defined?('FooBar').should be_true
299
+ TestScope.const_defined?('LevelA').should be_true
300
+ TestScope::LevelA.const_defined?('NameA').should be_true
301
+ TestScope::LevelA.const_defined?('LevelB').should be_true
302
+ TestScope::LevelA::LevelB.const_defined?('NameB').should be_true
303
+ end
304
+ end
305
+ end
306
+
307
+ describe 'Backup.const_missing' do
308
+ it 'should warn if Backup::CONFIG_FILE is referenced from an older config.rb' do
309
+ Backup::Logger.expects(:warn)
310
+ expect do
311
+ Backup.const_get('CONFIG_FILE').should == Backup::Config.config_file
312
+ end.not_to raise_error
313
+ end
314
+
315
+ it 'should still handle other missing constants' do
316
+ expect do
317
+ Backup.const_get('FOO')
318
+ end.to raise_error(NameError, 'uninitialized constant Backup::FOO')
319
+ end
320
+ end
321
+ end