backup 3.0.19 → 3.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +9 -8
  3. data/Gemfile.lock +19 -1
  4. data/Guardfile +13 -9
  5. data/README.md +93 -31
  6. data/backup.gemspec +3 -3
  7. data/bin/backup +6 -283
  8. data/lib/backup.rb +101 -72
  9. data/lib/backup/archive.rb +21 -9
  10. data/lib/backup/binder.rb +22 -0
  11. data/lib/backup/cleaner.rb +36 -0
  12. data/lib/backup/cli/helpers.rb +103 -0
  13. data/lib/backup/cli/utility.rb +308 -0
  14. data/lib/backup/compressor/base.rb +2 -2
  15. data/lib/backup/compressor/pbzip2.rb +76 -0
  16. data/lib/backup/configuration/compressor/pbzip2.rb +28 -0
  17. data/lib/backup/configuration/database/riak.rb +25 -0
  18. data/lib/backup/configuration/encryptor/open_ssl.rb +6 -0
  19. data/lib/backup/configuration/helpers.rb +5 -18
  20. data/lib/backup/configuration/notifier/base.rb +13 -0
  21. data/lib/backup/configuration/notifier/hipchat.rb +41 -0
  22. data/lib/backup/configuration/notifier/mail.rb +38 -0
  23. data/lib/backup/configuration/notifier/prowl.rb +23 -0
  24. data/lib/backup/configuration/storage/cloudfiles.rb +4 -0
  25. data/lib/backup/configuration/storage/dropbox.rb +8 -4
  26. data/lib/backup/database/base.rb +10 -2
  27. data/lib/backup/database/mongodb.rb +16 -19
  28. data/lib/backup/database/mysql.rb +2 -2
  29. data/lib/backup/database/postgresql.rb +2 -2
  30. data/lib/backup/database/redis.rb +15 -7
  31. data/lib/backup/database/riak.rb +45 -0
  32. data/lib/backup/dependency.rb +21 -7
  33. data/lib/backup/encryptor/base.rb +1 -1
  34. data/lib/backup/encryptor/open_ssl.rb +20 -5
  35. data/lib/backup/errors.rb +124 -0
  36. data/lib/backup/finder.rb +11 -3
  37. data/lib/backup/logger.rb +121 -82
  38. data/lib/backup/model.rb +103 -44
  39. data/lib/backup/notifier/base.rb +50 -0
  40. data/lib/backup/notifier/campfire.rb +32 -52
  41. data/lib/backup/notifier/hipchat.rb +99 -0
  42. data/lib/backup/notifier/mail.rb +100 -61
  43. data/lib/backup/notifier/presently.rb +31 -40
  44. data/lib/backup/notifier/prowl.rb +73 -0
  45. data/lib/backup/notifier/twitter.rb +29 -39
  46. data/lib/backup/packager.rb +25 -0
  47. data/lib/backup/splitter.rb +62 -0
  48. data/lib/backup/storage/base.rb +178 -18
  49. data/lib/backup/storage/cloudfiles.rb +34 -28
  50. data/lib/backup/storage/dropbox.rb +64 -67
  51. data/lib/backup/storage/ftp.rb +48 -40
  52. data/lib/backup/storage/local.rb +33 -28
  53. data/lib/backup/storage/ninefold.rb +40 -26
  54. data/lib/backup/storage/object.rb +8 -6
  55. data/lib/backup/storage/rsync.rb +61 -51
  56. data/lib/backup/storage/s3.rb +29 -27
  57. data/lib/backup/storage/scp.rb +56 -36
  58. data/lib/backup/storage/sftp.rb +49 -33
  59. data/lib/backup/syncer/base.rb +1 -1
  60. data/lib/backup/syncer/rsync.rb +1 -1
  61. data/lib/backup/template.rb +46 -0
  62. data/lib/backup/version.rb +1 -1
  63. data/spec/archive_spec.rb +34 -9
  64. data/spec/backup_spec.rb +1 -1
  65. data/spec/cli/helpers_spec.rb +35 -0
  66. data/spec/cli/utility_spec.rb +38 -0
  67. data/spec/compressor/bzip2_spec.rb +1 -1
  68. data/spec/compressor/gzip_spec.rb +1 -1
  69. data/spec/compressor/lzma_spec.rb +1 -1
  70. data/spec/compressor/pbzip2_spec.rb +63 -0
  71. data/spec/configuration/base_spec.rb +1 -1
  72. data/spec/configuration/compressor/bzip2_spec.rb +1 -1
  73. data/spec/configuration/compressor/gzip_spec.rb +1 -1
  74. data/spec/configuration/compressor/lzma_spec.rb +1 -1
  75. data/spec/configuration/database/base_spec.rb +1 -1
  76. data/spec/configuration/database/mongodb_spec.rb +1 -1
  77. data/spec/configuration/database/mysql_spec.rb +1 -1
  78. data/spec/configuration/database/postgresql_spec.rb +1 -1
  79. data/spec/configuration/database/redis_spec.rb +1 -1
  80. data/spec/configuration/database/riak_spec.rb +31 -0
  81. data/spec/configuration/encryptor/gpg_spec.rb +1 -1
  82. data/spec/configuration/encryptor/open_ssl_spec.rb +4 -1
  83. data/spec/configuration/notifier/campfire_spec.rb +1 -1
  84. data/spec/configuration/notifier/hipchat_spec.rb +43 -0
  85. data/spec/configuration/notifier/mail_spec.rb +34 -22
  86. data/spec/configuration/notifier/presently_spec.rb +1 -1
  87. data/spec/configuration/notifier/prowl_spec.rb +28 -0
  88. data/spec/configuration/notifier/twitter_spec.rb +1 -1
  89. data/spec/configuration/storage/cloudfiles_spec.rb +19 -16
  90. data/spec/configuration/storage/dropbox_spec.rb +1 -1
  91. data/spec/configuration/storage/ftp_spec.rb +1 -1
  92. data/spec/configuration/storage/local_spec.rb +1 -1
  93. data/spec/configuration/storage/ninefold_spec.rb +1 -1
  94. data/spec/configuration/storage/rsync_spec.rb +1 -1
  95. data/spec/configuration/storage/s3_spec.rb +1 -1
  96. data/spec/configuration/storage/scp_spec.rb +1 -1
  97. data/spec/configuration/storage/sftp_spec.rb +1 -1
  98. data/spec/configuration/syncer/rsync_spec.rb +1 -1
  99. data/spec/configuration/syncer/s3_spec.rb +1 -1
  100. data/spec/database/base_spec.rb +10 -1
  101. data/spec/database/mongodb_spec.rb +34 -7
  102. data/spec/database/mysql_spec.rb +8 -7
  103. data/spec/database/postgresql_spec.rb +8 -7
  104. data/spec/database/redis_spec.rb +39 -9
  105. data/spec/database/riak_spec.rb +50 -0
  106. data/spec/encryptor/gpg_spec.rb +1 -1
  107. data/spec/encryptor/open_ssl_spec.rb +77 -20
  108. data/spec/errors_spec.rb +306 -0
  109. data/spec/finder_spec.rb +91 -0
  110. data/spec/logger_spec.rb +254 -33
  111. data/spec/model_spec.rb +120 -15
  112. data/spec/notifier/campfire_spec.rb +127 -52
  113. data/spec/notifier/hipchat_spec.rb +193 -0
  114. data/spec/notifier/mail_spec.rb +290 -74
  115. data/spec/notifier/presently_spec.rb +290 -73
  116. data/spec/notifier/prowl_spec.rb +149 -0
  117. data/spec/notifier/twitter_spec.rb +106 -41
  118. data/spec/spec_helper.rb +8 -2
  119. data/spec/splitter_spec.rb +71 -0
  120. data/spec/storage/base_spec.rb +280 -19
  121. data/spec/storage/cloudfiles_spec.rb +38 -22
  122. data/spec/storage/dropbox_spec.rb +17 -13
  123. data/spec/storage/ftp_spec.rb +145 -55
  124. data/spec/storage/local_spec.rb +6 -6
  125. data/spec/storage/ninefold_spec.rb +70 -29
  126. data/spec/storage/object_spec.rb +44 -44
  127. data/spec/storage/rsync_spec.rb +186 -63
  128. data/spec/storage/s3_spec.rb +23 -24
  129. data/spec/storage/scp_spec.rb +116 -41
  130. data/spec/storage/sftp_spec.rb +124 -46
  131. data/spec/syncer/rsync_spec.rb +3 -3
  132. data/spec/syncer/s3_spec.rb +1 -1
  133. data/spec/version_spec.rb +1 -1
  134. data/templates/cli/utility/archive +13 -0
  135. data/{lib/templates → templates/cli/utility}/compressor/bzip2 +1 -1
  136. data/{lib/templates → templates/cli/utility}/compressor/gzip +1 -1
  137. data/{lib/templates → templates/cli/utility}/compressor/lzma +0 -0
  138. data/templates/cli/utility/compressor/pbzip2 +7 -0
  139. data/templates/cli/utility/config +31 -0
  140. data/{lib/templates → templates/cli/utility}/database/mongodb +1 -1
  141. data/{lib/templates → templates/cli/utility}/database/mysql +1 -1
  142. data/{lib/templates → templates/cli/utility}/database/postgresql +1 -1
  143. data/{lib/templates → templates/cli/utility}/database/redis +1 -1
  144. data/templates/cli/utility/database/riak +8 -0
  145. data/{lib/templates → templates/cli/utility}/encryptor/gpg +1 -1
  146. data/templates/cli/utility/encryptor/openssl +9 -0
  147. data/templates/cli/utility/model.erb +23 -0
  148. data/{lib/templates → templates/cli/utility}/notifier/campfire +2 -1
  149. data/templates/cli/utility/notifier/hipchat +15 -0
  150. data/{lib/templates → templates/cli/utility}/notifier/mail +6 -1
  151. data/{lib/templates → templates/cli/utility}/notifier/presently +1 -0
  152. data/templates/cli/utility/notifier/prowl +11 -0
  153. data/{lib/templates → templates/cli/utility}/notifier/twitter +2 -1
  154. data/templates/cli/utility/splitter +7 -0
  155. data/templates/cli/utility/storage/cloudfiles +12 -0
  156. data/{lib/templates → templates/cli/utility}/storage/dropbox +1 -1
  157. data/{lib/templates → templates/cli/utility}/storage/ftp +0 -0
  158. data/templates/cli/utility/storage/local +7 -0
  159. data/{lib/templates → templates/cli/utility}/storage/ninefold +1 -1
  160. data/templates/cli/utility/storage/rsync +11 -0
  161. data/{lib/templates → templates/cli/utility}/storage/s3 +0 -2
  162. data/templates/cli/utility/storage/scp +11 -0
  163. data/templates/cli/utility/storage/sftp +11 -0
  164. data/{lib/templates → templates/cli/utility}/syncer/rsync +1 -1
  165. data/{lib/templates → templates/cli/utility}/syncer/s3 +1 -1
  166. data/templates/general/links +11 -0
  167. data/templates/general/version.erb +2 -0
  168. data/templates/notifier/mail/failure.erb +9 -0
  169. data/templates/notifier/mail/success.erb +7 -0
  170. data/templates/notifier/mail/warning.erb +9 -0
  171. data/templates/storage/dropbox/authorization_url.erb +6 -0
  172. data/templates/storage/dropbox/authorized.erb +4 -0
  173. data/templates/storage/dropbox/cache_file_written.erb +10 -0
  174. metadata +81 -45
  175. data/lib/backup/cli.rb +0 -110
  176. data/lib/backup/exception/command_failed.rb +0 -8
  177. data/lib/backup/exception/command_not_found.rb +0 -8
  178. data/lib/backup/notifier/binder.rb +0 -32
  179. data/lib/backup/notifier/templates/notify_failure.erb +0 -33
  180. data/lib/backup/notifier/templates/notify_success.erb +0 -16
  181. data/lib/templates/archive +0 -7
  182. data/lib/templates/encryptor/openssl +0 -8
  183. data/lib/templates/readme +0 -15
  184. data/lib/templates/storage/cloudfiles +0 -11
  185. data/lib/templates/storage/local +0 -7
  186. data/lib/templates/storage/rsync +0 -11
  187. data/lib/templates/storage/scp +0 -11
  188. data/lib/templates/storage/sftp +0 -11
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- require File.dirname(__FILE__) + '/../spec_helper'
3
+ require File.expand_path('../../spec_helper.rb', __FILE__)
4
4
 
5
5
  describe Backup::Notifier::Campfire do
6
6
  let(:notifier) do
@@ -11,21 +11,21 @@ describe Backup::Notifier::Campfire do
11
11
  end
12
12
  end
13
13
 
14
- it do
15
- notifier.api_token.should == 'token'
16
- notifier.subdomain.should == 'subdomain'
17
- notifier.room_id.should == 'room_id'
14
+ describe '#initialize' do
15
+ it 'sets the correct defaults' do
16
+ notifier.api_token.should == 'token'
17
+ notifier.subdomain.should == 'subdomain'
18
+ notifier.room_id.should == 'room_id'
18
19
 
19
- notifier.on_success.should == true
20
- notifier.on_failure.should == true
21
- end
20
+ notifier.on_success.should == true
21
+ notifier.on_warning.should == true
22
+ notifier.on_failure.should == true
23
+ end
22
24
 
23
- describe 'defaults' do
24
- it do
25
+ it 'uses and overrides configuration defaults' do
25
26
  Backup::Configuration::Notifier::Campfire.defaults do |campfire|
26
27
  campfire.api_token = 'old_token'
27
28
  campfire.on_success = false
28
- campfire.on_failure = true
29
29
  end
30
30
  notifier = Backup::Notifier::Campfire.new do |campfire|
31
31
  campfire.api_token = 'new_token'
@@ -33,64 +33,139 @@ describe Backup::Notifier::Campfire do
33
33
 
34
34
  notifier.api_token.should == 'new_token'
35
35
  notifier.on_success.should == false
36
+ notifier.on_warning.should == true
36
37
  notifier.on_failure.should == true
37
38
  end
38
39
  end
39
40
 
40
- describe '#initialize' do
41
- it do
42
- Backup::Notifier::Campfire.any_instance.expects(:set_defaults!)
43
- Backup::Notifier::Campfire.new
44
- end
45
- end
46
-
47
41
  describe '#perform!' do
48
- let(:model) { Backup::Model.new('blah', 'blah') {} }
42
+ let(:model) { Backup::Model.new('trigger', 'label') {} }
43
+ let(:interface) { Backup::Notifier::Campfire::Interface }
44
+ let(:attrs) { [notifier.room_id, notifier.subdomain, notifier.api_token] }
45
+ let(:room) { Backup::Notifier::Campfire::Room.new(*attrs) }
46
+ let(:base_uri) { "https://#{notifier.subdomain}.campfirenow.com" }
47
+ let(:room_url) { "/room/#{notifier.room_id}/speak.json" }
48
+ let(:message) { '[Backup::%s] label (trigger)' }
49
+
49
50
  before do
50
51
  notifier.on_success = false
52
+ notifier.on_warning = false
51
53
  notifier.on_failure = false
52
54
  end
53
55
 
54
- context "when successful" do
55
- it do
56
- Backup::Logger.expects(:message).with("Backup::Notifier::Campfire started notifying about the process.")
57
- notifier.expects("notify_success!")
58
- notifier.on_success = true
59
- notifier.perform!(model)
56
+ context 'success' do
57
+
58
+ context 'when on_success is true' do
59
+ before { notifier.on_success = true }
60
+
61
+ it 'sends success message' do
62
+ notifier.expects(:log!)
63
+ interface.expects(:room).with(*attrs).returns(room)
64
+ interface.expects(:base_uri).with(base_uri)
65
+ interface.expects(:basic_auth).with(notifier.api_token, 'x')
66
+ message_body = message % 'Success'
67
+ interface.expects(:post).with(room_url, :body => {
68
+ :message => { :body => message_body, :type => 'Textmessage' }
69
+ }.to_json)
70
+
71
+ notifier.perform!(model)
72
+ end
60
73
  end
61
74
 
62
- it do
63
- notifier.expects("notify_success!").never
64
- notifier.on_success = false
65
- notifier.perform!(model)
75
+ context 'when on_success is false' do
76
+ it 'sends no message' do
77
+ notifier.expects(:log!).never
78
+ notifier.expects(:notify!).never
79
+ room.expects(:send_message).never
80
+ interface.expects(:post).never
81
+
82
+ notifier.perform!(model)
83
+ end
66
84
  end
67
- end
68
85
 
69
- context "when failed" do
70
- it do
71
- Backup::Logger.expects(:message).with("Backup::Notifier::Campfire started notifying about the process.")
72
- notifier.expects("notify_failure!")
73
- notifier.on_failure = true
74
- notifier.perform!(model, Exception.new)
86
+ end # context 'success'
87
+
88
+ context 'warning' do
89
+ before { Backup::Logger.stubs(:has_warnings?).returns(true) }
90
+
91
+ context 'when on_success is true' do
92
+ before { notifier.on_success = true }
93
+
94
+ it 'sends warning message' do
95
+ notifier.expects(:log!)
96
+ interface.expects(:room).with(*attrs).returns(room)
97
+ interface.expects(:base_uri).with(base_uri)
98
+ interface.expects(:basic_auth).with(notifier.api_token, 'x')
99
+ message_body = message % 'Warning'
100
+ interface.expects(:post).with(room_url, :body => {
101
+ :message => { :body => message_body, :type => 'Textmessage' }
102
+ }.to_json)
103
+
104
+ notifier.perform!(model)
105
+ end
75
106
  end
76
107
 
77
- it do
78
- notifier.expects("notify_failure!").never
79
- notifier.on_failure = false
80
- notifier.perform!(model, Exception.new)
108
+ context 'when on_warning is true' do
109
+ before { notifier.on_warning = true }
110
+
111
+ it 'sends warning message' do
112
+ notifier.expects(:log!)
113
+ interface.expects(:room).with(*attrs).returns(room)
114
+ interface.expects(:base_uri).with(base_uri)
115
+ interface.expects(:basic_auth).with(notifier.api_token, 'x')
116
+ message_body = message % 'Warning'
117
+ interface.expects(:post).with(room_url, :body => {
118
+ :message => { :body => message_body, :type => 'Textmessage' }
119
+ }.to_json)
120
+
121
+ notifier.perform!(model)
122
+ end
81
123
  end
82
- end
83
- end
84
124
 
85
- describe Backup::Notifier::Campfire::Interface do
86
- let(:room) do
87
- Backup::Notifier::Campfire::Room.new('room_id', 'subdomain', 'token')
88
- end
125
+ context 'when on_success and on_warning are false' do
126
+ it 'sends no message' do
127
+ notifier.expects(:log!).never
128
+ notifier.expects(:notify!).never
129
+ room.expects(:send_message).never
130
+ interface.expects(:post).never
89
131
 
90
- it do
91
- room.api_token.should == 'token'
92
- room.subdomain.should == 'subdomain'
93
- room.room_id.should == 'room_id'
94
- end
95
- end
132
+ notifier.perform!(model)
133
+ end
134
+ end
135
+
136
+ end # context 'warning'
137
+
138
+ context 'failure' do
139
+
140
+ context 'when on_failure is true' do
141
+ before { notifier.on_failure = true }
142
+
143
+ it 'sends failure message' do
144
+ notifier.expects(:log!)
145
+ interface.expects(:room).with(*attrs).returns(room)
146
+ interface.expects(:base_uri).with(base_uri)
147
+ interface.expects(:basic_auth).with(notifier.api_token, 'x')
148
+ message_body = message % 'Failure'
149
+ interface.expects(:post).with(room_url, :body => {
150
+ :message => { :body => message_body, :type => 'Textmessage' }
151
+ }.to_json)
152
+
153
+ notifier.perform!(model, Exception.new)
154
+ end
155
+ end
156
+
157
+ context 'when on_failure is false' do
158
+ it 'sends no message' do
159
+ notifier.expects(:log!).never
160
+ notifier.expects(:notify!).never
161
+ room.expects(:send_message).never
162
+ interface.expects(:post).never
163
+
164
+ notifier.perform!(model, Exception.new)
165
+ end
166
+ end
167
+
168
+ end # context 'failure'
169
+
170
+ end # describe '#perform!'
96
171
  end
@@ -0,0 +1,193 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../../spec_helper.rb', __FILE__)
4
+
5
+ describe Backup::Notifier::Hipchat do
6
+ let(:notifier) do
7
+ Backup::Notifier::Hipchat.new do |notifier|
8
+ notifier.from = 'application'
9
+ notifier.token = 'token'
10
+ notifier.rooms_notified = ['room1', 'room2']
11
+ notifier.notify_users = true
12
+ end
13
+ end
14
+
15
+ describe '#initialize' do
16
+
17
+ it "sets the correct defaults" do
18
+ notifier.from.should == 'application'
19
+ notifier.token.should == 'token'
20
+ notifier.success_color.should == 'yellow'
21
+ notifier.warning_color.should == 'yellow'
22
+ notifier.failure_color.should == 'yellow'
23
+ notifier.on_success.should == true
24
+ notifier.on_warning.should == true
25
+ notifier.on_failure.should == true
26
+ end
27
+
28
+ it 'uses and overrides configuration defaults' do
29
+ Backup::Configuration::Notifier::Hipchat.defaults do |notifier|
30
+ notifier.token = 'old'
31
+ notifier.from = 'before'
32
+ notifier.success_color = 'green'
33
+ end
34
+ hipchat = Backup::Notifier::Hipchat.new do |notifier|
35
+ notifier.token = 'new'
36
+ notifier.from = 'after'
37
+ notifier.failure_color = 'red'
38
+ end
39
+
40
+ hipchat.token.should == 'new'
41
+ hipchat.from.should == 'after'
42
+ hipchat.success_color.should == 'green'
43
+ hipchat.warning_color.should == 'yellow'
44
+ hipchat.failure_color.should == 'red'
45
+ hipchat.on_success.should == true
46
+ hipchat.on_warning.should == true
47
+ hipchat.on_failure.should == true
48
+ end
49
+
50
+ end # describe '#initialize'
51
+
52
+ describe '#perform!' do
53
+ let(:model) { Backup::Model.new('trigger', 'label') {} }
54
+ let(:hipchat_mock) { mock }
55
+ let(:hipchat_client) { HipChat::Client.any_instance }
56
+ let(:message) { '[Backup::%s] label (trigger)' }
57
+
58
+ before do
59
+ notifier.on_success = false
60
+ notifier.on_warning = false
61
+ notifier.on_failure = false
62
+ notifier.success_color = 'green'
63
+ notifier.warning_color = 'yellow'
64
+ notifier.failure_color = 'red'
65
+ end
66
+
67
+ context 'success' do
68
+
69
+ context 'when on_success is true' do
70
+ before { notifier.on_success = true }
71
+
72
+ it 'sends success message' do
73
+ notifier.expects(:log!)
74
+ hipchat_client.expects(:[]).with('room1').returns(hipchat_mock)
75
+ hipchat_client.expects(:[]).with('room2').returns(hipchat_mock)
76
+ hipchat_mock.expects(:send).twice.with {|user, msg, hash|
77
+ (user.should == notifier.from) &&
78
+ (msg.should == message % 'Success') &&
79
+ (hash[:color].should == notifier.success_color) &&
80
+ (hash[:notify].should == notifier.notify_users)
81
+ }
82
+
83
+ notifier.perform!(model)
84
+ end
85
+ end
86
+
87
+ context 'when on_success is false' do
88
+ it 'sends no message' do
89
+ notifier.expects(:log!).never
90
+ notifier.expects(:notify!).never
91
+ hipchat_client.expects(:[]).never
92
+
93
+ notifier.perform!(model)
94
+ end
95
+ end
96
+
97
+ end # context 'success'
98
+
99
+ context 'warning' do
100
+ before { Backup::Logger.stubs(:has_warnings?).returns(true) }
101
+
102
+ context 'when on_success is true' do
103
+ before { notifier.on_success = true }
104
+
105
+ it 'sends warning message' do
106
+ notifier.expects(:log!)
107
+ hipchat_client.expects(:[]).with('room1').returns(hipchat_mock)
108
+ hipchat_client.expects(:[]).with('room2').returns(hipchat_mock)
109
+ hipchat_mock.expects(:send).twice.with {|user, msg, hash|
110
+ (user.should == notifier.from) &&
111
+ (msg.should == message % 'Warning') &&
112
+ (hash[:color].should == notifier.warning_color) &&
113
+ (hash[:notify].should == notifier.notify_users)
114
+ }
115
+
116
+ notifier.perform!(model)
117
+ end
118
+ end
119
+
120
+ context 'when on_warning is true' do
121
+ before { notifier.on_warning = true }
122
+
123
+ it 'sends warning message' do
124
+ notifier.expects(:log!)
125
+ hipchat_client.expects(:[]).with('room1').returns(hipchat_mock)
126
+ hipchat_client.expects(:[]).with('room2').returns(hipchat_mock)
127
+ hipchat_mock.expects(:send).twice.with {|user, msg, hash|
128
+ (user.should == notifier.from) &&
129
+ (msg.should == message % 'Warning') &&
130
+ (hash[:color].should == notifier.warning_color) &&
131
+ (hash[:notify].should == notifier.notify_users)
132
+ }
133
+
134
+ notifier.perform!(model)
135
+ end
136
+ end
137
+
138
+ context 'when on_success and on_warning are false' do
139
+ it 'sends no message' do
140
+ notifier.expects(:log!).never
141
+ notifier.expects(:notify!).never
142
+ hipchat_client.expects(:[]).never
143
+
144
+ notifier.perform!(model)
145
+ end
146
+ end
147
+
148
+ end # context 'warning'
149
+
150
+ context 'failure' do
151
+
152
+ context 'when on_failure is true' do
153
+ before { notifier.on_failure = true }
154
+
155
+ it 'sends failure message' do
156
+ notifier.expects(:log!)
157
+ hipchat_client.expects(:[]).with('room1').returns(hipchat_mock)
158
+ hipchat_client.expects(:[]).with('room2').returns(hipchat_mock)
159
+ hipchat_mock.expects(:send).twice.with {|user, msg, hash|
160
+ (user.should == notifier.from) &&
161
+ (msg.should == message % 'Failure') &&
162
+ (hash[:color].should == notifier.failure_color) &&
163
+ (hash[:notify].should == notifier.notify_users)
164
+ }
165
+
166
+ notifier.perform!(model, Exception.new)
167
+ end
168
+ end
169
+
170
+ context 'when on_failure is false' do
171
+ it 'sends no message' do
172
+ notifier.expects(:log!).never
173
+ notifier.expects(:notify!).never
174
+ hipchat_client.expects(:[]).never
175
+
176
+ notifier.perform!(model, Exception.new)
177
+ end
178
+ end
179
+
180
+ end # context 'failure'
181
+
182
+ it 'will convert a single room param to an array' do
183
+ notifier.on_success = true
184
+ notifier.rooms_notified = 'one_room'
185
+
186
+ hipchat_client.expects(:[]).with('one_room').returns(stub(:send))
187
+
188
+ notifier.perform!(model)
189
+ notifier.rooms_notified.should == ['one_room']
190
+ end
191
+
192
+ end # describe '#perform!'
193
+ end
@@ -1,97 +1,313 @@
1
1
  # encoding: utf-8
2
2
 
3
- require File.dirname(__FILE__) + '/../spec_helper'
3
+ require File.expand_path('../../spec_helper.rb', __FILE__)
4
4
 
5
5
  describe Backup::Notifier::Mail do
6
- let(:notifier) do
7
- Backup::Notifier::Mail.new do |mail|
8
- mail.from = 'my.sender.email@gmail.com'
9
- mail.to = 'my.receiver.email@gmail.com'
10
- mail.address = 'smtp.gmail.com'
11
- mail.port = 587
12
- mail.domain = 'your.host.name'
13
- mail.user_name = 'user'
14
- mail.password = 'secret'
15
- mail.authentication = 'plain'
16
- mail.enable_starttls_auto = true
17
- end
18
- end
19
-
20
- it do
21
- notifier.from.should == 'my.sender.email@gmail.com'
22
- notifier.to.should == 'my.receiver.email@gmail.com'
23
- notifier.address.should == 'smtp.gmail.com'
24
- notifier.port.should == 587
25
- notifier.domain.should == 'your.host.name'
26
- notifier.user_name.should == 'user'
27
- notifier.password.should == 'secret'
28
- notifier.authentication.should == 'plain'
29
- notifier.enable_starttls_auto.should == true
30
-
31
- notifier.on_success.should == true
32
- notifier.on_failure.should == true
33
- end
34
-
35
- describe 'defaults' do
36
- it do
37
- Backup::Configuration::Notifier::Mail.defaults do |mail|
38
- mail.to = 'some.receiver.email@gmail.com'
39
- mail.on_success = false
40
- mail.on_failure = true
6
+
7
+ describe '#initialize' do
8
+
9
+ context 'specifying the delivery_method' do
10
+
11
+ it 'creates a new notifier using Mail::SMTP' do
12
+ notifier = Backup::Notifier::Mail.new do |mail|
13
+ mail.delivery_method = :smtp
14
+ mail.from = 'my.sender.email@gmail.com'
15
+ mail.to = 'my.receiver.email@gmail.com'
16
+ mail.address = 'smtp.gmail.com'
17
+ mail.port = 587
18
+ mail.domain = 'your.host.name'
19
+ mail.user_name = 'user'
20
+ mail.password = 'secret'
21
+ mail.authentication = 'plain'
22
+ mail.enable_starttls_auto = true
23
+ end
24
+
25
+ notifier.mail.delivery_method.should
26
+ be_an_instance_of ::Mail::SMTP
27
+
28
+ notifier.delivery_method.should == 'smtp'
29
+ notifier.from.should == 'my.sender.email@gmail.com'
30
+ notifier.to.should == 'my.receiver.email@gmail.com'
31
+ notifier.address.should == 'smtp.gmail.com'
32
+ notifier.port.should == 587
33
+ notifier.domain.should == 'your.host.name'
34
+ notifier.user_name.should == 'user'
35
+ notifier.password.should == 'secret'
36
+ notifier.authentication.should == 'plain'
37
+ notifier.enable_starttls_auto.should == true
38
+ notifier.openssl_verify_mode.should be_nil
39
+ notifier.sendmail.should be_nil
40
+ notifier.sendmail_args.should be_nil
41
+ notifier.mail_folder.should be_nil
42
+
43
+ notifier.on_success.should == true
44
+ notifier.on_warning.should == true
45
+ notifier.on_failure.should == true
41
46
  end
42
- notifier = Backup::Notifier::Mail.new do |mail|
43
- mail.from = 'my.sender.email@gmail.com'
47
+
48
+ it 'creates a new notifier using Mail::Sendmail' do
49
+ notifier = Backup::Notifier::Mail.new do |mail|
50
+ mail.delivery_method = :sendmail
51
+ mail.from = 'my.sender.email@gmail.com'
52
+ mail.to = 'my.receiver.email@gmail.com'
53
+ mail.sendmail = '/path/to/sendmail'
54
+ mail.sendmail_args = '-i -t -X/tmp/traffic.log'
55
+ end
56
+
57
+ notifier.mail.delivery_method.should
58
+ be_an_instance_of ::Mail::Sendmail
59
+
60
+ notifier.delivery_method.should == 'sendmail'
61
+ notifier.from.should == 'my.sender.email@gmail.com'
62
+ notifier.to.should == 'my.receiver.email@gmail.com'
63
+ notifier.address.should be_nil
64
+ notifier.port.should be_nil
65
+ notifier.domain.should be_nil
66
+ notifier.user_name.should be_nil
67
+ notifier.password.should be_nil
68
+ notifier.authentication.should be_nil
69
+ notifier.enable_starttls_auto.should be_nil
70
+ notifier.openssl_verify_mode.should be_nil
71
+ notifier.sendmail.should == '/path/to/sendmail'
72
+ notifier.sendmail_args.should == '-i -t -X/tmp/traffic.log'
73
+ notifier.mail_folder.should be_nil
74
+
75
+ notifier.on_success.should == true
76
+ notifier.on_warning.should == true
77
+ notifier.on_failure.should == true
44
78
  end
45
79
 
46
- notifier.to.should == 'some.receiver.email@gmail.com'
47
- notifier.from.should == 'my.sender.email@gmail.com'
48
- notifier.on_success.should == false
49
- notifier.on_failure.should == true
50
- end
51
- end
80
+ it 'creates a new notifier using Mail::FileDelivery' do
81
+ notifier = Backup::Notifier::Mail.new do |mail|
82
+ mail.delivery_method = :file
83
+ mail.from = 'my.sender.email@gmail.com'
84
+ mail.to = 'my.receiver.email@gmail.com'
85
+ mail.mail_folder = '/path/to/backup/mails'
86
+ end
52
87
 
53
- describe '#initialize' do
54
- it do
55
- Backup::Notifier::Mail.any_instance.expects(:set_defaults!)
56
- Backup::Notifier::Mail.new
57
- end
58
- end
88
+ notifier.mail.delivery_method.should
89
+ be_an_instance_of ::Mail::FileDelivery
90
+
91
+ notifier.delivery_method.should == 'file'
92
+ notifier.from.should == 'my.sender.email@gmail.com'
93
+ notifier.to.should == 'my.receiver.email@gmail.com'
94
+ notifier.address.should be_nil
95
+ notifier.port.should be_nil
96
+ notifier.domain.should be_nil
97
+ notifier.user_name.should be_nil
98
+ notifier.password.should be_nil
99
+ notifier.authentication.should be_nil
100
+ notifier.enable_starttls_auto.should be_nil
101
+ notifier.openssl_verify_mode.should be_nil
102
+ notifier.sendmail.should be_nil
103
+ notifier.sendmail_args.should be_nil
104
+ notifier.mail_folder.should == '/path/to/backup/mails'
105
+
106
+ notifier.on_success.should == true
107
+ notifier.on_warning.should == true
108
+ notifier.on_failure.should == true
109
+ end
110
+
111
+ end # context 'specifying the delivery_method'
112
+
113
+ context 'without specifying the delivery_method' do
114
+
115
+ it 'uses Mail::SMTP by default' do
116
+ [nil, :foo, 'foo'].each do |val|
117
+ notifier = Backup::Notifier::Mail.new do |mail|
118
+ mail.delivery_method = val
119
+ mail.from = 'my.sender.email@gmail.com'
120
+ mail.to = 'my.receiver.email@gmail.com'
121
+ end
122
+
123
+ notifier.delivery_method.should == 'smtp'
124
+ notifier.from.should == 'my.sender.email@gmail.com'
125
+ notifier.to.should == 'my.receiver.email@gmail.com'
126
+ end
127
+ end
128
+
129
+ end # context 'without specifying the delivery_method'
130
+
131
+ describe 'setting configuration defaults' do
132
+ let(:config) { Backup::Configuration::Notifier::Mail }
133
+ after { config.clear_defaults! }
134
+
135
+ it 'uses and overrides configuration defaults' do
136
+ config.defaults do |mail|
137
+ mail.delivery_method = :file
138
+ mail.to = 'some.receiver.email@gmail.com'
139
+ mail.from = 'default.sender.email@gmail.com'
140
+ mail.on_success = false
141
+ end
142
+
143
+ config.delivery_method.should == :file
144
+ config.to.should == 'some.receiver.email@gmail.com'
145
+ config.from.should == 'default.sender.email@gmail.com'
146
+ config.on_success.should == false
147
+
148
+ notifier = Backup::Notifier::Mail.new do |mail|
149
+ mail.mail_folder = '/my/backup/mails'
150
+ mail.from = 'my.sender.email@gmail.com'
151
+ mail.on_warning = false
152
+ end
153
+
154
+ notifier.delivery_method.should == 'file'
155
+ notifier.mail_folder = '/my/backup/mails'
156
+ notifier.to.should == 'some.receiver.email@gmail.com'
157
+ notifier.from.should == 'my.sender.email@gmail.com'
158
+
159
+ notifier.on_success.should == false
160
+ notifier.on_warning.should == false
161
+ notifier.on_failure.should == true
162
+ end
163
+
164
+ end # describe 'setting configuration defaults'
165
+
166
+ end # describe '#initialize'
59
167
 
60
168
  describe '#perform!' do
61
- let(:model) { Backup::Model.new('blah', 'blah') {} }
169
+ let(:model) { Backup::Model.new('trigger', 'label') {} }
170
+ let(:message) { '[Backup::%s] label (trigger)' }
171
+ let(:notifier) do
172
+ Backup::Notifier::Mail.new do |mail|
173
+ mail.delivery_method = :test
174
+ end
175
+ end
176
+
62
177
  before do
63
178
  notifier.on_success = false
179
+ notifier.on_warning = false
64
180
  notifier.on_failure = false
181
+ ::Mail::TestMailer.deliveries.clear
65
182
  end
66
183
 
67
- context "when successful" do
68
- it do
69
- Backup::Logger.expects(:message).with("Backup::Notifier::Mail started notifying about the process.")
70
- notifier.expects("notify_success!")
71
- notifier.on_success = true
72
- notifier.perform!(model)
184
+ context 'success' do
185
+
186
+ context 'when Notifier#on_success is true' do
187
+ before { notifier.on_success = true }
188
+
189
+ it 'sends the notification without an attached backup log' do
190
+ notifier.expects(:log!)
191
+ Backup::Template.any_instance.expects(:result).
192
+ with('notifier/mail/success.erb').
193
+ returns('message body')
194
+
195
+ notifier.perform!(model)
196
+ sent_message = ::Mail::TestMailer.deliveries.first
197
+ sent_message.subject.should == message % 'Success'
198
+ sent_message.multipart?.should be_false
199
+ sent_message.body.should == 'message body'
200
+ sent_message.has_attachments?.should be_false
201
+ end
73
202
  end
74
203
 
75
- it do
76
- notifier.expects("notify_success!").never
77
- notifier.on_success = false
78
- notifier.perform!(model)
204
+ context 'when Notifier#on_success is false' do
205
+ it 'does not send the notification' do
206
+ notifier.expects(:log!).never
207
+ notifier.expects(:notify!).never
208
+
209
+ notifier.perform!(model)
210
+ ::Mail::TestMailer.deliveries.should be_empty
211
+ end
79
212
  end
80
- end
81
213
 
82
- context "when failed" do
83
- it do
84
- Backup::Logger.expects(:message).with("Backup::Notifier::Mail started notifying about the process.")
85
- notifier.expects("notify_failure!")
86
- notifier.on_failure = true
87
- notifier.perform!(model, Exception.new)
214
+ end # context 'success'
215
+
216
+ context 'warning' do
217
+ let(:log_messages){ ['line 1', 'line 2', 'line 3'] }
218
+
219
+ before do
220
+ Backup::Logger.stubs(:has_warnings?).returns(true)
221
+ Backup::Logger.stubs(:messages).returns(log_messages)
88
222
  end
89
223
 
90
- it do
91
- notifier.expects("notify_failure!").never
92
- notifier.on_failure = false
93
- notifier.perform!(model, Exception.new)
224
+ context 'when Notifier#on_warning is true' do
225
+ before { notifier.on_warning = true }
226
+
227
+ it 'sends the notification with an attached backup log' do
228
+ notifier.expects(:log!)
229
+ Backup::Template.any_instance.expects(:result).
230
+ with('notifier/mail/warning.erb').
231
+ returns('message body')
232
+
233
+ notifier.perform!(model)
234
+ sent_message = ::Mail::TestMailer.deliveries.first
235
+ sent_message.subject.should == message % 'Warning'
236
+ sent_message.body.multipart?.should be_true
237
+ sent_message.text_part.decoded.should == 'message body'
238
+ sent_message.attachments["#{model.time}.#{model.trigger}.log"].
239
+ read.should == "line 1\nline 2\nline 3"
240
+ end
94
241
  end
95
- end
96
- end
242
+
243
+ context 'when Notifier#on_success is true' do
244
+ before { notifier.on_success = true }
245
+
246
+ it 'sends the notification with an attched backup log' do
247
+ notifier.expects(:log!)
248
+ Backup::Template.any_instance.expects(:result).
249
+ with('notifier/mail/warning.erb').
250
+ returns('message body')
251
+
252
+ notifier.perform!(model)
253
+ sent_message = ::Mail::TestMailer.deliveries.first
254
+ sent_message.subject.should == message % 'Warning'
255
+ sent_message.body.multipart?.should be_true
256
+ sent_message.text_part.decoded.should == 'message body'
257
+ sent_message.attachments["#{model.time}.#{model.trigger}.log"].
258
+ read.should == "line 1\nline 2\nline 3"
259
+ end
260
+ end
261
+
262
+ context 'when Notifier#on_warning and Notifier#on_success are false' do
263
+ it 'does not send the notification' do
264
+ notifier.expects(:log!).never
265
+ notifier.expects(:notify!).never
266
+
267
+ notifier.perform!(model)
268
+ ::Mail::TestMailer.deliveries.should be_empty
269
+ end
270
+ end
271
+
272
+ end # context 'warning'
273
+
274
+ context 'failure' do
275
+ let(:log_messages){ ['line 1', 'line 2', 'line 3'] }
276
+
277
+ before do
278
+ Backup::Logger.stubs(:messages).returns(log_messages)
279
+ end
280
+
281
+ context 'when Notifier#on_failure is true' do
282
+ before { notifier.on_failure = true }
283
+
284
+ it 'sends the notification with an attached backup log' do
285
+ notifier.expects(:log!)
286
+ Backup::Template.any_instance.expects(:result).
287
+ with('notifier/mail/failure.erb').
288
+ returns('message body')
289
+
290
+ notifier.perform!(model, Exception.new)
291
+ sent_message = ::Mail::TestMailer.deliveries.first
292
+ sent_message.subject.should == message % 'Failure'
293
+ sent_message.body.multipart?.should be_true
294
+ sent_message.text_part.decoded.should == 'message body'
295
+ sent_message.attachments["#{model.time}.#{model.trigger}.log"].
296
+ read.should == "line 1\nline 2\nline 3"
297
+ end
298
+ end
299
+
300
+ context 'when Notifier#on_failure is false' do
301
+ it 'does not send the notification' do
302
+ notifier.expects(:log!).never
303
+ notifier.expects(:notify!).never
304
+
305
+ notifier.perform!(model, Exception.new)
306
+ ::Mail::TestMailer.deliveries.should be_empty
307
+ end
308
+ end
309
+
310
+ end # context 'failure'
311
+
312
+ end # describe '#perform!'
97
313
  end