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
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../../spec_helper.rb', __FILE__)
4
+
5
+ describe 'Backup::Notifier::Base' do
6
+ let(:model) { Backup::Model.new(:test_trigger, 'test label') }
7
+ let(:notifier) { Backup::Notifier::Base.new(model) }
8
+
9
+ describe '#initialize' do
10
+
11
+ it "sets the correct defaults" do
12
+ notifier.on_success.should == true
13
+ notifier.on_warning.should == true
14
+ notifier.on_failure.should == true
15
+ end
16
+
17
+ context 'when using configuration defaults' do
18
+ after { Backup::Configuration::Notifier::Base.clear_defaults! }
19
+
20
+ it 'uses configuration defaults' do
21
+ Backup::Configuration::Notifier::Base.defaults do |notifier|
22
+ notifier.on_success = false
23
+ notifier.on_warning = false
24
+ notifier.on_failure = false
25
+ end
26
+
27
+ base = Backup::Notifier::Base.new(model)
28
+ base.on_success.should == false
29
+ base.on_warning.should == false
30
+ base.on_failure.should == false
31
+ end
32
+ end
33
+
34
+ end # describe '#initialize'
35
+
36
+ describe '#perform!' do
37
+ before do
38
+ notifier.expects(:log!)
39
+ Backup::Template.expects(:new).with({:model => model})
40
+ end
41
+
42
+ context 'when failure is false' do
43
+ context 'when no warnings were issued' do
44
+ before do
45
+ Backup::Logger.expects(:has_warnings?).returns(false)
46
+ end
47
+
48
+ it 'should call #notify! with :success' do
49
+ notifier.expects(:notify!).with(:success)
50
+ notifier.perform!
51
+ end
52
+ end
53
+
54
+ context 'when warnings were issued' do
55
+ before do
56
+ Backup::Logger.expects(:has_warnings?).returns(true)
57
+ end
58
+
59
+ it 'should call #notify! with :warning' do
60
+ notifier.expects(:notify!).with(:warning)
61
+ notifier.perform!
62
+ end
63
+ end
64
+ end # context 'when failure is false'
65
+
66
+ context 'when failure is true' do
67
+ it 'should call #notify with :failure' do
68
+ notifier.expects(:notify!).with(:failure)
69
+ notifier.perform!(true)
70
+ end
71
+ end
72
+ end # describe '#perform!'
73
+
74
+ describe '#notifier_name' do
75
+ it 'should return class name without Backup:: namespace' do
76
+ notifier.send(:notifier_name).should == 'Notifier::Base'
77
+ end
78
+ end
79
+
80
+ describe '#log!' do
81
+ it 'should log a message' do
82
+ Backup::Logger.expects(:message).with(
83
+ "Notifier::Base started notifying about the process."
84
+ )
85
+ notifier.send(:log!)
86
+ end
87
+ end
88
+
89
+ end
@@ -3,8 +3,9 @@
3
3
  require File.expand_path('../../spec_helper.rb', __FILE__)
4
4
 
5
5
  describe Backup::Notifier::Campfire do
6
+ let(:model) { Backup::Model.new(:test_trigger, 'test label') }
6
7
  let(:notifier) do
7
- Backup::Notifier::Campfire.new do |campfire|
8
+ Backup::Notifier::Campfire.new(model) do |campfire|
8
9
  campfire.api_token = 'token'
9
10
  campfire.subdomain = 'subdomain'
10
11
  campfire.room_id = 'room_id'
@@ -12,7 +13,7 @@ describe Backup::Notifier::Campfire do
12
13
  end
13
14
 
14
15
  describe '#initialize' do
15
- it 'sets the correct defaults' do
16
+ it 'should sets the correct values' do
16
17
  notifier.api_token.should == 'token'
17
18
  notifier.subdomain.should == 'subdomain'
18
19
  notifier.room_id.should == 'room_id'
@@ -22,150 +23,177 @@ describe Backup::Notifier::Campfire do
22
23
  notifier.on_failure.should == true
23
24
  end
24
25
 
25
- it 'uses and overrides configuration defaults' do
26
- Backup::Configuration::Notifier::Campfire.defaults do |campfire|
27
- campfire.api_token = 'old_token'
28
- campfire.on_success = false
29
- end
30
- notifier = Backup::Notifier::Campfire.new do |campfire|
31
- campfire.api_token = 'new_token'
32
- end
26
+ context 'when using configuration defaults' do
27
+ after { Backup::Configuration::Notifier::Campfire.clear_defaults! }
33
28
 
34
- notifier.api_token.should == 'new_token'
35
- notifier.on_success.should == false
36
- notifier.on_warning.should == true
37
- notifier.on_failure.should == true
38
- end
39
- end
29
+ it 'should use the configuration defaults' do
30
+ Backup::Configuration::Notifier::Campfire.defaults do |campfire|
31
+ campfire.api_token = 'some_token'
32
+ campfire.subdomain = 'some_subdomain'
33
+ campfire.room_id = 'some_room_id'
40
34
 
41
- describe '#perform!' do
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
-
50
- before do
51
- notifier.on_success = false
52
- notifier.on_warning = false
53
- notifier.on_failure = false
54
- end
35
+ campfire.on_success = false
36
+ campfire.on_warning = false
37
+ campfire.on_failure = false
38
+ end
39
+ notifier = Backup::Notifier::Campfire.new(model)
40
+ notifier.api_token.should == 'some_token'
41
+ notifier.subdomain.should == 'some_subdomain'
42
+ notifier.room_id.should == 'some_room_id'
43
+
44
+ notifier.on_success.should == false
45
+ notifier.on_warning.should == false
46
+ notifier.on_failure.should == false
47
+ end
55
48
 
56
- context 'success' do
49
+ it 'should override the configuration defaults' do
50
+ Backup::Configuration::Notifier::Campfire.defaults do |campfire|
51
+ campfire.api_token = 'old_token'
52
+ campfire.subdomain = 'old_subdomain'
53
+ campfire.room_id = 'old_room_id'
57
54
 
58
- context 'when on_success is true' do
59
- before { notifier.on_success = true }
55
+ campfire.on_success = true
56
+ campfire.on_warning = false
57
+ campfire.on_failure = false
58
+ end
59
+ notifier = Backup::Notifier::Campfire.new(model) do |campfire|
60
+ campfire.api_token = 'new_token'
61
+ campfire.subdomain = 'new_subdomain'
62
+ campfire.room_id = 'new_room_id'
63
+
64
+ campfire.on_success = false
65
+ campfire.on_warning = true
66
+ campfire.on_failure = true
67
+ end
60
68
 
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)
69
+ notifier.api_token.should == 'new_token'
70
+ notifier.subdomain.should == 'new_subdomain'
71
+ notifier.room_id.should == 'new_room_id'
70
72
 
71
- notifier.perform!(model)
72
- end
73
+ notifier.on_success.should == false
74
+ notifier.on_warning.should == true
75
+ notifier.on_failure.should == true
73
76
  end
77
+ end # context 'when using configuration defaults'
78
+ end
74
79
 
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
80
+ describe '#notify!' do
81
+ context 'when status is :success' do
82
+ it 'should send Success message' do
83
+ notifier.expects(:send_message).with(
84
+ '[Backup::Success] test label (test_trigger)'
85
+ )
86
+ notifier.send(:notify!, :success)
87
+ end
88
+ end
81
89
 
82
- notifier.perform!(model)
83
- end
90
+ context 'when status is :warning' do
91
+ it 'should send Warning message' do
92
+ notifier.expects(:send_message).with(
93
+ '[Backup::Warning] test label (test_trigger)'
94
+ )
95
+ notifier.send(:notify!, :warning)
84
96
  end
97
+ end
85
98
 
86
- end # context 'success'
99
+ context 'when status is :failure' do
100
+ it 'should send Failure message' do
101
+ notifier.expects(:send_message).with(
102
+ '[Backup::Failure] test label (test_trigger)'
103
+ )
104
+ notifier.send(:notify!, :failure)
105
+ end
106
+ end
107
+ end # describe '#notify!'
87
108
 
88
- context 'warning' do
89
- before { Backup::Logger.stubs(:has_warnings?).returns(true) }
109
+ describe '#send_message' do
110
+ it 'should send a message' do
111
+ room = mock
112
+ Backup::Notifier::Campfire::Interface.expects(:room).
113
+ with('room_id', 'subdomain', 'token').returns(room)
114
+ room.expects(:message).with('a message')
90
115
 
91
- context 'when on_success is true' do
92
- before { notifier.on_success = true }
116
+ notifier.send(:send_message, 'a message')
117
+ end
118
+ end
93
119
 
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)
120
+ end
103
121
 
104
- notifier.perform!(model)
105
- end
106
- end
122
+ describe 'Backup::Notifier::Campfire::Interface' do
123
+ let(:interface) { Backup::Notifier::Campfire::Interface }
107
124
 
108
- context 'when on_warning is true' do
109
- before { notifier.on_warning = true }
125
+ it 'should include HTTParty' do
126
+ interface.included_modules.should include(HTTParty)
127
+ end
110
128
 
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)
129
+ it 'should set the proper headers' do
130
+ interface.headers['Content-Type'].should == 'application/json'
131
+ end
120
132
 
121
- notifier.perform!(model)
122
- end
123
- end
133
+ describe '.room' do
134
+ let(:room) { mock }
124
135
 
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
136
+ it 'should create and return a new Room' do
137
+ Backup::Notifier::Campfire::Room.expects(:new).
138
+ with('room_id', 'subdomain', 'api_token').returns(room)
131
139
 
132
- notifier.perform!(model)
133
- end
134
- end
140
+ interface.room('room_id', 'subdomain', 'api_token').should == room
141
+ end
142
+ end
143
+ end
135
144
 
136
- end # context 'warning'
145
+ describe 'Backup::Notifier::Campfire::Room' do
146
+ let(:room) do
147
+ Backup::Notifier::Campfire::Room.new('room_id', 'subdomain', 'api_token')
148
+ end
137
149
 
138
- context 'failure' do
150
+ it 'should set the given values for the room' do
151
+ room.room_id.should == 'room_id'
152
+ room.subdomain.should == 'subdomain'
153
+ room.api_token.should == 'api_token'
154
+ end
139
155
 
140
- context 'when on_failure is true' do
141
- before { notifier.on_failure = true }
156
+ describe '#message' do
157
+ it 'should wrap #send_message' do
158
+ room.expects(:send_message).with('a message')
142
159
 
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)
160
+ room.message('a message')
161
+ end
162
+ end
152
163
 
153
- notifier.perform!(model, Exception.new)
154
- end
155
- end
164
+ describe '#send_message' do
165
+ it 'should pass a JSON formatted HTTParty.post to #post' do
166
+ room.expects(:post).with('speak',
167
+ {
168
+ :body => {
169
+ :message => {
170
+ :body => 'a message',
171
+ :type => 'Textmessage'
172
+ }
173
+ }.to_json
174
+ }
175
+ )
176
+ room.send(:send_message, 'a message')
177
+ end
178
+ end
156
179
 
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
180
+ describe '#post' do
181
+ let(:interface) { Backup::Notifier::Campfire::Interface }
163
182
 
164
- notifier.perform!(model, Exception.new)
165
- end
166
- end
183
+ it 'should send an HTTParty.post with the given options through Interface' do
184
+ room.expects(:room_url_for).with('an_action').returns('a_room_url')
185
+ interface.expects(:base_uri).with('https://subdomain.campfirenow.com')
186
+ interface.expects(:basic_auth).with('api_token', 'x')
187
+ interface.expects(:post).with('a_room_url', { :hash => 'of options' })
167
188
 
168
- end # context 'failure'
189
+ room.send(:post, 'an_action', :hash => 'of options')
190
+ end
191
+ end
169
192
 
170
- end # describe '#perform!'
193
+ describe '#room_url_for' do
194
+ it 'should return a properly formated url for the given action' do
195
+ room.send(:room_url_for, 'my_action').should ==
196
+ '/room/room_id/my_action.json'
197
+ end
198
+ end
171
199
  end
@@ -3,191 +3,186 @@
3
3
  require File.expand_path('../../spec_helper.rb', __FILE__)
4
4
 
5
5
  describe Backup::Notifier::Hipchat do
6
+ let(:model) { Backup::Model.new(:test_trigger, 'test label') }
6
7
  let(:notifier) do
7
- Backup::Notifier::Hipchat.new do |notifier|
8
- notifier.from = 'application'
8
+ Backup::Notifier::Hipchat.new(model) do |notifier|
9
9
  notifier.token = 'token'
10
+ notifier.from = 'application'
10
11
  notifier.rooms_notified = ['room1', 'room2']
11
- notifier.notify_users = true
12
12
  end
13
13
  end
14
14
 
15
15
  describe '#initialize' do
16
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'
17
+ it 'should set the correct values and defaults' do
18
+ notifier.token.should == 'token'
19
+ notifier.from.should == 'application'
20
+ notifier.rooms_notified.should == ['room1', 'room2']
21
+ notifier.notify_users.should == false
22
+ notifier.success_color.should == 'yellow'
23
+ notifier.warning_color.should == 'yellow'
24
+ notifier.failure_color.should == 'yellow'
25
+
23
26
  notifier.on_success.should == true
24
27
  notifier.on_warning.should == true
25
28
  notifier.on_failure.should == true
26
29
  end
27
30
 
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
31
+ context 'when setting configuration defaults' do
32
+ after { Backup::Configuration::Notifier::Hipchat.clear_defaults! }
68
33
 
69
- context 'when on_success is true' do
70
- before { notifier.on_success = true }
34
+ it 'should use the configuration defaults' do
35
+ Backup::Configuration::Notifier::Hipchat.defaults do |notifier|
36
+ notifier.token = 'old'
37
+ notifier.from = 'before'
38
+ notifier.success_color = 'green'
71
39
 
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)
40
+ notifier.on_failure = false
84
41
  end
42
+ hipchat = Backup::Notifier::Hipchat.new(model)
43
+
44
+ hipchat.token.should == 'old'
45
+ hipchat.from.should == 'before'
46
+ hipchat.rooms_notified.should == []
47
+ hipchat.notify_users.should == false
48
+ hipchat.success_color.should == 'green'
49
+ hipchat.warning_color.should == 'yellow'
50
+ hipchat.failure_color.should == 'yellow'
51
+
52
+ hipchat.on_success.should == true
53
+ hipchat.on_warning.should == true
54
+ hipchat.on_failure.should == false
85
55
  end
86
56
 
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
57
+ it 'should override the configuration defaults' do
58
+ Backup::Configuration::Notifier::Hipchat.defaults do |notifier|
59
+ notifier.token = 'old'
60
+ notifier.from = 'before'
61
+ notifier.success_color = 'green'
92
62
 
93
- notifier.perform!(model)
63
+ notifier.on_failure = false
94
64
  end
95
- end
65
+ hipchat = Backup::Notifier::Hipchat.new(model) do |notifier|
66
+ notifier.token = 'new'
67
+ notifier.from = 'after'
68
+ notifier.failure_color = 'red'
96
69
 
97
- end # context 'success'
70
+ notifier.on_success = false
71
+ notifier.on_failure = true
72
+ end
98
73
 
99
- context 'warning' do
100
- before { Backup::Logger.stubs(:has_warnings?).returns(true) }
74
+ hipchat.token.should == 'new'
75
+ hipchat.from.should == 'after'
76
+ hipchat.success_color.should == 'green'
77
+ hipchat.warning_color.should == 'yellow'
78
+ hipchat.failure_color.should == 'red'
79
+
80
+ hipchat.on_success.should == false
81
+ hipchat.on_warning.should == true
82
+ hipchat.on_failure.should == true
83
+ end
84
+ end # context 'when setting configuration defaults'
101
85
 
102
- context 'when on_success is true' do
103
- before { notifier.on_success = true }
86
+ end # describe '#initialize'
104
87
 
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
- }
88
+ describe '#notify!' do
89
+ before do
90
+ notifier.success_color = 'green'
91
+ notifier.warning_color = 'yellow'
92
+ notifier.failure_color = 'red'
93
+ end
115
94
 
116
- notifier.perform!(model)
117
- end
95
+ context 'when status is :success' do
96
+ it 'should send Success message' do
97
+ notifier.expects(:send_message).with(
98
+ '[Backup::Success] test label (test_trigger)', 'green'
99
+ )
100
+ notifier.send(:notify!, :success)
118
101
  end
102
+ end
119
103
 
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
104
+ context 'when status is :warning' do
105
+ it 'should send Warning message' do
106
+ notifier.expects(:send_message).with(
107
+ '[Backup::Warning] test label (test_trigger)', 'yellow'
108
+ )
109
+ notifier.send(:notify!, :warning)
136
110
  end
111
+ end
137
112
 
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
113
+ context 'when status is :failure' do
114
+ it 'should send Failure message' do
115
+ notifier.expects(:send_message).with(
116
+ '[Backup::Failure] test label (test_trigger)', 'red'
117
+ )
118
+ notifier.send(:notify!, :failure)
146
119
  end
120
+ end
121
+ end # describe '#notify!'
122
+
123
+ describe '#send_message' do
124
+ let(:client) { mock }
125
+ let(:room) { mock }
126
+
127
+ it 'should handle rooms_notified being set as a single room string' do
128
+ notifier.rooms_notified = 'a_room'
129
+ HipChat::Client.expects(:new).with('token').returns(client)
130
+ client.expects(:[]).with('a_room').returns(room)
131
+ room.expects(:send).with(
132
+ 'application',
133
+ 'a message',
134
+ {:color => 'a color', :notify => false}
135
+ )
136
+
137
+ notifier.send(:send_message, 'a message', 'a color')
138
+ end
147
139
 
148
- end # context 'warning'
140
+ context 'when notify_users is set to true' do
141
+ before { notifier.notify_users = true }
149
142
 
150
- context 'failure' do
143
+ it 'should notify rooms with :notify => true' do
144
+ HipChat::Client.expects(:new).with('token').returns(client)
151
145
 
152
- context 'when on_failure is true' do
153
- before { notifier.on_failure = true }
146
+ client.expects(:[]).with('room1').returns(room)
147
+ room.expects(:send).with(
148
+ 'application',
149
+ 'a message',
150
+ {:color => 'a color', :notify => true}
151
+ )
154
152
 
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
- }
153
+ client.expects(:[]).with('room2').returns(room)
154
+ room.expects(:send).with(
155
+ 'application',
156
+ 'a message',
157
+ {:color => 'a color', :notify => true}
158
+ )
165
159
 
166
- notifier.perform!(model, Exception.new)
167
- end
160
+ notifier.send(:send_message, 'a message', 'a color')
168
161
  end
162
+ end
169
163
 
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
164
+ context 'when notify_users is set to false' do
165
+ before { notifier.notify_users = false }
175
166
 
176
- notifier.perform!(model, Exception.new)
177
- end
178
- end
179
-
180
- end # context 'failure'
167
+ it 'should notify rooms with :notify => false' do
168
+ HipChat::Client.expects(:new).with('token').returns(client)
181
169
 
182
- it 'will convert a single room param to an array' do
183
- notifier.on_success = true
184
- notifier.rooms_notified = 'one_room'
170
+ client.expects(:[]).with('room1').returns(room)
171
+ room.expects(:send).with(
172
+ 'application',
173
+ 'a message',
174
+ {:color => 'a color', :notify => false}
175
+ )
185
176
 
186
- hipchat_client.expects(:[]).with('one_room').returns(stub(:send))
177
+ client.expects(:[]).with('room2').returns(room)
178
+ room.expects(:send).with(
179
+ 'application',
180
+ 'a message',
181
+ {:color => 'a color', :notify => false}
182
+ )
187
183
 
188
- notifier.perform!(model)
189
- notifier.rooms_notified.should == ['one_room']
184
+ notifier.send(:send_message, 'a message', 'a color')
185
+ end
190
186
  end
191
-
192
- end # describe '#perform!'
187
+ end
193
188
  end