forked from flapjack/flapjack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifier_application_spec.rb
222 lines (185 loc) · 9.11 KB
/
notifier_application_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require File.join(File.dirname(__FILE__), '..', 'lib', 'flapjack', 'applications', 'notifier')
require File.join(File.dirname(__FILE__), 'helpers')
describe "notifier application" do
#
# booting interface
#
it "should have a simple interface to start the notifier" do
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
end
it "should log when loading a notifier" do
options = { :notifiers => {:testmailer => {}},
:filters => [],
:log => MockLogger.new,
:notifier_directories => [File.join(File.dirname(__FILE__),'notifier-directories', 'spoons')],
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
app.log.messages.find {|msg| msg =~ /loading the testmailer notifier/i}.should_not be_nil
end
it "should warn if a specified notifier doesn't exist" do
options = { :notifiers => {:nonexistant => {}},
:filters => [],
:log => MockLogger.new,
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
app.log.messages.find {|msg| msg =~ /Flapjack::Notifiers::Nonexistant doesn't exist/}.should_not be_nil
end
it "should give precedence to notifiers in user-specified notifier directories" do
options = { :notifiers => {:testmailer => {}},
:filters => [],
:log => MockLogger.new,
:notifier_directories => [File.join(File.dirname(__FILE__),'notifier-directories', 'spoons')],
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
app.log.messages.find {|msg| msg =~ /spoons/i}.should_not be_nil
# this will raise an exception if the notifier directory isn't specified,
# as the mailer notifier requires several arguments anyhow
end
it "should pass global notifier config options to each notifier"
it "should setup recipients from a list" do
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:recipients => [{:name => "Spoons McDoom"}],
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
# 1 passed recipient should be here
app.recipients.size.should == 1
end
#
# transports
#
it "should use beanstalkd as the default transport" do
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
app.log.messages.find {|msg| msg =~ /loading.+beanstalkd.+transport/i}.should_not be_nil
end
it "should use a transport as specified in options" do
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:transport => {:backend => :beanstalkd},
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
app.log.messages.find {|msg| msg =~ /loading.+beanstalkd.+transport/i}.should_not be_nil
end
it "should error if the specified transport doesn't exist" do
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:transport => {:backend => :nonexistant} }
lambda {
app = Flapjack::Notifier::Application.run(options)
#app.log.messages.find {|msg| msg =~ /attempted to load nonexistant/i}.should_not be_nil
}.should raise_error
end
it "should use a limited interface for dealing with the results queue" do
# Interface for a Flapjack::Transport::<transport> is as follows:
#
# methods: next, delete
#
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:transport => {:backend => :mock_transport,
:basedir => File.join(File.dirname(__FILE__), 'transports')},
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
# processes a single result
app.process_result
# check that allowed methods were called
allowed_methods = %w(next delete)
allowed_methods.each do |method|
app.log.messages.find {|msg| msg =~ /#{method.gsub(/\?/,'\?')} was called on MockTransport/i}.should_not be_nil
end
# check that no other methods were
called_methods = app.log.messages.find_all {|msg| msg =~ /^method .+ was called on MockTransport$/i }.map {|msg| msg.split(' ')[1]}
(allowed_methods - called_methods).size.should == 0
end
it "should use a limited interface for dealing with results" do
# Interface for a Flapjack::Transport::Result is as follows:
#
# methods: id, warning?, critical?
#
options = { :notifiers => {},
:filters => ['ok'],
:log => MockLogger.new,
:transport => {:backend => :mock_transport,
:basedir => File.join(File.dirname(__FILE__), 'transports')},
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
# processes a single result
app.process_result
# check that allowed methods were called
allowed_methods = %w(check_id warning?)
allowed_methods.each do |method|
app.log.messages.find {|msg| msg =~ /#{method.gsub(/\?/,'\?')} was called/i}.should_not be_nil
end
# check that no other methods were called
called_methods = app.log.messages.find_all {|msg| msg =~ /^method .+ was called on MockResult$/i }.map {|msg| msg.split(' ')[1]}.uniq
(allowed_methods - called_methods).size.should == 0
end
it "should use a limited interface for dealing with the persistence backend" do
# Interface for a Flapjack::Persistence::<backend> is as follows:
#
# methods: any_parents_failed?, save
#
options = { :notifiers => {},
:filters => ['any_parents_failed'],
:log => MockLogger.new,
:transport => {:backend => :mock_transport,
:basedir => File.join(File.dirname(__FILE__), 'transports')},
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
# processes a single result
app.process_result
# check that allowed methods were called
allowed_methods = %w(any_parents_failed? save)
allowed_methods.each do |method|
app.log.messages.find {|msg| msg =~ /#{method.gsub(/\?/,'\?')} was called/i}.should_not be_nil
end
# check that no other methods were called
called_methods = app.log.messages.find_all {|msg| msg =~ /^method .+ was called on MockPersistenceBackend$/i }.map {|msg| msg.split(' ')[1]}
(allowed_methods - called_methods).size.should == 0
end
#
# persistence backend
#
it "should load couchdb as the default persistence backend"
it "should load a persistence backend as specified in options" do
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:persistence => {:backend => :mock_persistence_backend,
:basedir => File.join(File.dirname(__FILE__), 'persistence')} }
app = Flapjack::Notifier::Application.run(options)
app.log.messages.find {|msg| msg =~ /loading.+MockPersistenceBackend.+backend/i}.should_not be_nil
end
it "should raise if the specified persistence backend doesn't exist" do
options = { :notifiers => {},
:filters => [],
:log => MockLogger.new,
:persistence => {:backend => :nonexistant} }
lambda {
app = Flapjack::Notifier::Application.run(options)
}.should raise_error(LoadError)
end
end
def puts(*args) ; end